Browse Source

added unit tests for rgpgfs_fs_mkdirs; docker ignore binaries in src/ & tests/

Fabian Peter Hammerle 5 years ago
parent
commit
a31c36f687
4 changed files with 61 additions and 3 deletions
  1. 3 2
      .dockerignore
  2. 2 1
      Dockerfile
  3. 6 0
      Makefile
  4. 50 0
      tests/fs.c

+ 3 - 2
.dockerignore

@@ -1,7 +1,8 @@
 *
 !Makefile
 !docker/
-!src/
-!tests/
+!src/*.c
+!src/*.h
+!tests/*.c
 
 # https://docs.docker.com/engine/reference/builder/#dockerignore-file

+ 2 - 1
Dockerfile

@@ -20,7 +20,8 @@ RUN make
 FROM build as test
 
 COPY --chown=build:nogroup tests /rgpgfs/tests
-RUN make tests/str && tests/str
+RUN make tests/str && tests/str \
+    && make tests/fs && tests/fs
 
 
 FROM alpine:3.9 as runtime

+ 6 - 0
Makefile

@@ -27,6 +27,12 @@ src/str.o : src/str.c src/str.h
 rgpgfs : src/fs.o src/gpgme.o src/main.o src/str.o
 	$(LD) $^ -o $@ $(LIBS)
 
+tests/fs.o : tests/fs.c src/fs.h
+	$(CC) $(CFLAGS) -c $< -o $@
+
+tests/fs : tests/fs.o src/fs.o
+	$(LD) $^ -o $@
+
 tests/str.o : tests/str.c src/str.h
 	$(CC) $(CFLAGS) -c $< -o $@
 

+ 50 - 0
tests/fs.c

@@ -0,0 +1,50 @@
+#include "src/fs.h"
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <ftw.h>
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define PATH_BUF_LEN 256
+
+void test_mkdirs(const char *temp_dir, const char *rel_path) {
+  char abs_path[PATH_BUF_LEN];
+  strcpy(abs_path, temp_dir);
+  strcat(abs_path, "/");
+  strcat(abs_path, rel_path);
+  if (rgpgfs_fs_mkdirs(abs_path)) {
+    fprintf(stderr, "rel_path = '%s'\n", rel_path);
+    perror("rgpgfs_fs_mkdirs failed");
+    exit(1);
+  }
+
+  FILE *f = fopen(abs_path, "w");
+  assert(f);
+  fclose(f);
+};
+
+int cleanup_rm_cb(const char *fpath, const struct stat *sb, int typeflag,
+                  struct FTW *ftwbuf) {
+  if (remove(fpath)) {
+    perror(fpath);
+  }
+  return 0;
+}
+
+int main() {
+  char temp_dir[] = "/tmp/rgpgfs-tests-fs-XXXXXX";
+  assert(mkdtemp(temp_dir));
+  // printf("temp dir: %s\n", temp_dir);
+
+  test_mkdirs(temp_dir, "file");
+  test_mkdirs(temp_dir, "a/file");
+  test_mkdirs(temp_dir, "a/b/file");
+  test_mkdirs(temp_dir, "c/d/file");
+
+  nftw(temp_dir, cleanup_rm_cb, 8, FTW_DEPTH);
+  return 0;
+}