]>
git.scottworley.com Git - tl-append/blob - tl-append-test.c
2a5d233d6b6ebaeb22bc358ea6fc30611a1a8f93
1 #define _POSIX_C_SOURCE 2
7 typedef struct expectation
{
11 const ex_t END
= {NULL
};
12 static int is_end(ex_t exp
) { return exp
.message
== END
.message
; }
13 static ex_t
expectation(const char *message
) {
15 exp
.message
= message
;
19 static void die(const char *message
) {
20 fputs(message
, stderr
);
25 static void die_err(const char *message
) {
30 static void remove_logfile() {
31 if (remove("tl.log") != 0) {
32 if (errno
!= ENOENT
) {
33 die_err("Error removing log file");
38 static ex_t
write_to_tl_append(const char *content
) {
39 FILE *p
= popen("./tl-append", "w");
41 die_err("Couldn't run tl-append");
42 if (fputs(content
, p
) == EOF
)
43 die("Couldn't write to pipe");
44 int status
= pclose(p
);
46 die_err("Error closing pipe");
48 die("tl-append exited abnormally");
49 return expectation(content
);
52 static void verify_log_contents(ex_t exps
[]) {
54 FILE *f
= fopen("tl.log", "r");
56 die_err("Error opening log file");
57 for (size_t i
= 0; !is_end(exps
[i
]); i
++) {
58 size_t len
= strlen(exps
[i
].message
);
59 char *buf
= (char *)malloc(len
+ 2);
60 if (fgets(buf
, len
+ 1, f
) == NULL
)
61 die("Error reading log file");
63 die("Error reading log file");
64 if (strncmp(exps
[i
].message
, buf
, len
+ 1) != 0)
65 die("Wrong contents in log file");
69 die_err("Error closing log file");
72 static void write_and_read_line() {
74 ex_t e
= write_to_tl_append("foo\n");
75 verify_log_contents((ex_t
[]){e
, END
});
78 static void write_and_read_two_lines() {
80 ex_t e1
= write_to_tl_append("foo\n");
81 ex_t e2
= write_to_tl_append("bar\n");
82 verify_log_contents((ex_t
[]){e1
, e2
, END
});
86 write_and_read_line();
87 write_and_read_two_lines();