]>
git.scottworley.com Git - tl-append/blob - tl-append-test.c
1 #define _POSIX_C_SOURCE 2
10 typedef struct expectation
{
15 const ex_t END
= {((time_t)-1), ((time_t)-1), NULL
};
16 static int is_end(ex_t exp
) {
17 return exp
.a
== END
.a
&& exp
.b
== END
.b
&& exp
.message
== END
.message
;
19 static ex_t
expectation(time_t a
, time_t b
, const char *message
) {
23 exp
.message
= message
;
27 static void remove_logfile() {
28 if (remove(FILENAME
) != 0) {
29 if (errno
!= ENOENT
) {
30 die_err("Error removing log file");
35 static ex_t
write_to_tl_append(const char *content
) {
36 FILE *p
= popen("./tl-append", "w");
38 die_err("Couldn't run tl-append");
39 time_t start
= time(NULL
);
40 if (fputs(content
, p
) == EOF
)
41 die("Couldn't write to pipe");
42 int status
= pclose(p
);
43 time_t end
= time(NULL
);
45 die_err("Error closing pipe");
47 die("tl-append exited abnormally");
48 return expectation(start
, end
, content
);
51 static void verify_log_contents(ex_t exps
[]) {
53 FILE *f
= fopen(FILENAME
, "r");
55 die_err("Error opening log file");
56 for (size_t i
= 0; !is_end(exps
[i
]); i
++) {
57 size_t len
= strlen(exps
[i
].message
);
58 char *buf
= (char *)malloc(len
+ 2);
59 if (fgets(buf
, len
+ 1, f
) == NULL
)
60 die("Error reading log file");
62 die("Error reading log file");
63 if (strncmp(exps
[i
].message
, buf
, len
+ 1) != 0)
64 die("Wrong contents in log file");
68 die_err("Error closing log file");
71 static void write_and_read_line() {
73 ex_t e
= write_to_tl_append("foo\n");
74 verify_log_contents((ex_t
[]){e
, END
});
77 static void write_and_read_two_lines() {
79 ex_t e1
= write_to_tl_append("foo\n");
80 ex_t e2
= write_to_tl_append("bar\n");
81 verify_log_contents((ex_t
[]){e1
, e2
, END
});
85 write_and_read_line();
86 write_and_read_two_lines();