aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2021-06-27 12:48:08 -0300
committerEuAndreh <eu@euandre.org>2021-06-27 13:57:30 -0300
commit5d1401fb191309386a258c32d72e432239dcfe31 (patch)
tree09cbb08c82c6d97111e21e2d19289e9278163048
parent2db008e01ee8c4f0438cd3b729b039bca7aac9c1 (diff)
downloadremembering-5d1401fb191309386a258c32d72e432239dcfe31.tar.gz
src/remembering-c.c: Add tests for mkdir_p()
Notes
Notes: See CI logs with: git notes --ref=refs/notes/ci-logs show 5d1401fb191309386a258c32d72e432239dcfe31 git notes --ref=refs/notes/ci-data show 5d1401fb191309386a258c32d72e432239dcfe31
-rw-r--r--src/remembering-c.c52
1 files changed, 49 insertions, 3 deletions
diff --git a/src/remembering-c.c b/src/remembering-c.c
index f120441..494fffc 100644
--- a/src/remembering-c.c
+++ b/src/remembering-c.c
@@ -13,6 +13,7 @@
#ifdef TEST
#include "unit-test.h"
#include <stdbool.h>
+#include <errno.h>
#endif
@@ -240,7 +241,6 @@ static int mkdir_p(const char *const path, mode_t mode) {
int ret;
if (stat(path, &s) == 0 && S_ISDIR(s.st_mode)) {
- printf("ISDIR: %s\n", path);
return 0;
}
@@ -260,9 +260,55 @@ static int mkdir_p(const char *const path, mode_t mode) {
return mkdir(path, mode);
}
-int main(int argc, char *argv[]) {
#ifdef TEST
+static void test_mkdir_p() {
+ {
+ testing("mkdir_p() with an existing directory is a noop");
+ struct stat s;
+ assert(stat("/tmp", &s) == 0 && S_ISDIR(s.st_mode));
+ assert(mkdir_p("/tmp", S_IRWXU | S_IRWXG | S_IRWXO) == 0);
+ assert(stat("/tmp", &s) == 0 && S_ISDIR(s.st_mode));
+ test_ok();
+ }
+ {
+ testing("mkdir_p() with a new directory creates it");
+ struct stat s;
+ const char *const SUBDIRECTORY_SUFFIX = "/a/b/c/d/e";
+ char template[] = "/tmp/remembering.XXXXXX";
+ const char *const prefix = mkdtemp(template);
+ assert(prefix);
+
+ assert(stat(prefix, &s) == 0 && S_ISDIR(s.st_mode));
+
+ const size_t subdirectory_size =
+ strlen(prefix) +
+ strlen(SUBDIRECTORY_SUFFIX) +
+ sizeof(char);
+ char *const subdirectory = malloc(subdirectory_size);
+ assert(subdirectory);
+ strcat(subdirectory, prefix);
+ strcat(subdirectory, SUBDIRECTORY_SUFFIX);
+
+ assert(stat(subdirectory, &s) == -1 && errno == ENOENT);
+ assert(mkdir_p(subdirectory, S_IRWXU | S_IRWXG | S_IRWXO) == 0);
+ assert(stat(subdirectory, &s) == 0 && S_ISDIR(s.st_mode));
+
+ free(subdirectory);
+ test_ok();
+ }
+}
+#endif
+
+#ifdef TEST
+static void unit_tests() {
test_get_profile_file();
+ test_mkdir_p();
+}
+#endif
+
+int main(int argc, char *argv[]) {
+#ifdef TEST
+ unit_tests();
return EXIT_SUCCESS;
#endif
@@ -315,7 +361,7 @@ int main(int argc, char *argv[]) {
return EXIT_USAGE;
}
- // end getopts
+ /* End getopt() */
int ret = EXIT_SUCCESS;