]>
git.scottworley.com Git - tl-append/blob - tl-append-test.c
ea42dc947fcf7fb94619fdacb74e029ed2d54398
1 #define _POSIX_C_SOURCE 2
8 typedef struct expectation
{
13 const ex_t END
= {((time_t)-1), ((time_t)-1), NULL
};
14 static int is_end(ex_t exp
) {
15 return exp
.a
== END
.a
&& exp
.b
== END
.b
&& exp
.message
== END
.message
;
17 static ex_t
expectation(time_t a
, time_t b
, const char *message
) {
21 exp
.message
= message
;
25 static void die(const char *message
) {
26 fputs(message
, stderr
);
31 static void die_err(const char *message
) {
36 static void remove_logfile() {
37 if (remove("tl.log") != 0) {
38 if (errno
!= ENOENT
) {
39 die_err("Error removing log file");
44 static ex_t
write_to_tl_append(const char *content
) {
45 FILE *p
= popen("./tl-append", "w");
47 die_err("Couldn't run tl-append");
48 time_t start
= time(NULL
);
49 if (fputs(content
, p
) == EOF
)
50 die("Couldn't write to pipe");
51 int status
= pclose(p
);
52 time_t end
= time(NULL
);
54 die_err("Error closing pipe");
56 die("tl-append exited abnormally");
57 return expectation(start
, end
, content
);
60 static void verify_log_contents(ex_t exps
[]) {
62 FILE *f
= fopen("tl.log", "r");
64 die_err("Error opening log file");
65 for (size_t i
= 0; !is_end(exps
[i
]); i
++) {
66 size_t len
= strlen(exps
[i
].message
);
67 char *buf
= (char *)malloc(len
+ 2);
68 if (fgets(buf
, len
+ 1, f
) == NULL
)
69 die("Error reading log file");
71 die("Error reading log file");
72 if (strncmp(exps
[i
].message
, buf
, len
+ 1) != 0)
73 die("Wrong contents in log file");
77 die_err("Error closing log file");
80 static void write_and_read_line() {
82 ex_t e
= write_to_tl_append("foo\n");
83 verify_log_contents((ex_t
[]){e
, END
});
86 static void write_and_read_two_lines() {
88 ex_t e1
= write_to_tl_append("foo\n");
89 ex_t e2
= write_to_tl_append("bar\n");
90 verify_log_contents((ex_t
[]){e1
, e2
, END
});
94 write_and_read_line();
95 write_and_read_two_lines();