| 
3 | 3 | #define NOB_STRIP_PREFIX  | 
4 | 4 | #include "nob.h"  | 
5 | 5 | 
 
  | 
 | 6 | +bool mktestdir() {  | 
 | 7 | +    // build/tests/read_entire_dir.cwd  | 
 | 8 | +    // ├── external  | 
 | 9 | +    // │   ├── foobar  | 
 | 10 | +    // │   │   ├── foobar.h  | 
 | 11 | +    // │   │   ├── libfoobar.a  | 
 | 12 | +    // │   │   └── libfoobar.so  | 
 | 13 | +    // │   └── foobarbaz  | 
 | 14 | +    // │       ├── foobarbaz.h  | 
 | 15 | +    // │       ├── libfoobarbaz.a  | 
 | 16 | +    // │       └── libfoobarbaz.so  | 
 | 17 | +    // ├── include  | 
 | 18 | +    // │   ├── bar  | 
 | 19 | +    // │   │   └── bar.h  | 
 | 20 | +    // │   ├── baz.h  | 
 | 21 | +    // │   └── foo  | 
 | 22 | +    // │       └── foo.h  | 
 | 23 | +    // └── src  | 
 | 24 | +    //     ├── bar  | 
 | 25 | +    //     │   └── bar.c  | 
 | 26 | +    //     ├── baz.c  | 
 | 27 | +    //     └── foo  | 
 | 28 | +    //         └── foo.c  | 
 | 29 | +    return mkdir_if_not_exists("src")  | 
 | 30 | +        && mkdir_if_not_exists("src/foo")  | 
 | 31 | +        && mkdir_if_not_exists("src/bar")  | 
 | 32 | +        && mkdir_if_not_exists("include")  | 
 | 33 | +        && mkdir_if_not_exists("include/foo")  | 
 | 34 | +        && mkdir_if_not_exists("include/bar")  | 
 | 35 | +        && mkdir_if_not_exists("external")  | 
 | 36 | +        && mkdir_if_not_exists("external/foobar")  | 
 | 37 | +        && mkdir_if_not_exists("external/foobarbaz")  | 
 | 38 | +        && write_entire_file("src/foo/foo.c", NULL, 0)  | 
 | 39 | +        && write_entire_file("src/bar/bar.c", NULL, 0)  | 
 | 40 | +        && write_entire_file("src/baz.c", NULL, 0)  | 
 | 41 | +        && write_entire_file("include/foo/foo.h", NULL, 0)  | 
 | 42 | +        && write_entire_file("include/bar/bar.h", NULL, 0)  | 
 | 43 | +        && write_entire_file("include/baz.h", NULL, 0)  | 
 | 44 | +        && write_entire_file("external/foobar/foobar.h", NULL, 0)  | 
 | 45 | +        && write_entire_file("external/foobar/libfoobar.a", NULL, 0)  | 
 | 46 | +        && write_entire_file("external/foobar/libfoobar.so", NULL, 0)  | 
 | 47 | +        && write_entire_file("external/foobarbaz/foobarbaz.h", NULL, 0)  | 
 | 48 | +        && write_entire_file("external/foobarbaz/libfoobarbaz.a", NULL, 0)  | 
 | 49 | +        && write_entire_file("external/foobarbaz/libfoobarbaz.so", NULL, 0);  | 
 | 50 | +}  | 
 | 51 | + | 
6 | 52 | int main(void)  | 
7 | 53 | {  | 
8 |  | -    if (!write_entire_file("foo.txt", NULL, 0)) return 1;  | 
9 |  | -    if (!write_entire_file("bar.txt", NULL, 0)) return 1;  | 
10 |  | -    if (!write_entire_file("baz.txt", NULL, 0)) return 1;  | 
11 |  | - | 
 | 54 | +    // Test nob_read_entire_dir()  | 
 | 55 | +    if (!mktestdir()) return 1;  | 
12 | 56 |     Nob_File_Paths children = {0};  | 
13 |  | -    if (!nob_read_entire_dir(".", &children)) return 1;  | 
14 |  | -    nob_log(INFO, "Tests:");  | 
15 |  | -    for (size_t i = 0; i < children.count; ++i) {  | 
16 |  | -        if (*children.items[i] != '.') {  | 
17 |  | -            nob_log(INFO, "    %s", children.items[i]);  | 
 | 57 | +    if (!read_entire_dir(".", &children)) return 1;  | 
 | 58 | +    nob_log(INFO, "read_entire_dir():");  | 
 | 59 | +    da_foreach(const char *, child, &children) {  | 
 | 60 | +        if (**child != '.') {  | 
 | 61 | +            nob_log(INFO, "    %s", *child);  | 
18 | 62 |         }  | 
19 | 63 |     }  | 
 | 64 | + | 
 | 65 | +    // Test nob_read_entire_dir() with recursively option  | 
 | 66 | +    nob_da_free(children);  | 
 | 67 | +    children = (Nob_File_Paths){0};  | 
 | 68 | +    if (!read_entire_dir(".", &children, .recursively = true)) return 1;  | 
 | 69 | +    nob_log(INFO, "read_entire_dir() recursively:");  | 
 | 70 | +    da_foreach(const char *, child, &children) {  | 
 | 71 | +        nob_log(INFO, "    %s", *child);  | 
 | 72 | +    }  | 
 | 73 | + | 
 | 74 | +    // Test nob_read_entire_dir() with wildcard option  | 
 | 75 | +    File_Paths srcs = {0}, headers = {0}, libs = {0}, dlls = {0};  | 
 | 76 | +    if (!read_entire_dir(".", &srcs, .wildcard = "src/*.c")) return 1;  | 
 | 77 | +    if (!read_entire_dir(".", &srcs, .wildcard = "src/**/*.c")) return 1;  | 
 | 78 | +    if (!read_entire_dir(".", &headers, .wildcard = "include/*.h")) return 1;  | 
 | 79 | +    if (!read_entire_dir(".", &headers, .wildcard = "include/**/*.h")) return 1;  | 
 | 80 | +    if (!read_entire_dir(".", &headers, .wildcard = "external/**/*.h")) return 1;  | 
 | 81 | +    if (!read_entire_dir(".", &libs, .wildcard = "external/**/*.a")) return 1;  | 
 | 82 | +    if (!read_entire_dir(".", &dlls, .wildcard = "external/**/*.so")) return 1;  | 
 | 83 | +    nob_log(INFO, "read_entire_dir() wildcard:");  | 
 | 84 | +    nob_log(INFO, "srcs:");  | 
 | 85 | +    da_foreach(const char *, src, &srcs) {  | 
 | 86 | +        nob_log(INFO, "    %s", *src);  | 
 | 87 | +    }  | 
 | 88 | +    nob_log(INFO, "headers:");  | 
 | 89 | +    da_foreach(const char *, header, &headers) {  | 
 | 90 | +        nob_log(INFO, "    %s", *header);  | 
 | 91 | +    }  | 
 | 92 | +    nob_log(INFO, "libs:");  | 
 | 93 | +    da_foreach(const char *, lib, &libs) {  | 
 | 94 | +        nob_log(INFO, "    %s", *lib);  | 
 | 95 | +    }  | 
 | 96 | +    nob_log(INFO, "dlls:");  | 
 | 97 | +    da_foreach(const char *, dll, &dlls) {  | 
 | 98 | +        nob_log(INFO, "    %s", *dll);  | 
 | 99 | +    }  | 
20 | 100 |     return 0;  | 
21 | 101 | }  | 
0 commit comments