Skip to content

Commit

Permalink
added unit tests for rgpgfs_fs_mkdirs; docker ignore binaries in src/…
Browse files Browse the repository at this point in the history
… & tests/
  • Loading branch information
fphammerle committed Mar 31, 2019
1 parent d6fe4ab commit a31c36f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
*
!Makefile
!docker/
!src/
!tests/
!src/*.c
!src/*.h
!tests/*.c

# https://docs.docker.com/engine/reference/builder/#dockerignore-file
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 $@

Expand Down
50 changes: 50 additions & 0 deletions tests/fs.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit a31c36f

Please sign in to comment.