]> git.scottworley.com Git - tl-append/blob - tl-append-test.c
make clean
[tl-append] / tl-append-test.c
1 #define _POSIX_C_SOURCE 2
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 static void die(const char *message) {
7 fputs(message, stderr);
8 fputc('\n', stderr);
9 exit(1);
10 }
11
12 static void die_err(const char *message) {
13 perror(message);
14 exit(1);
15 }
16
17 static void write_to_tl_append(const char *content) {
18 FILE *p = popen("./tl-append", "w");
19 if (p == NULL)
20 die_err("Couldn't run tl-append");
21 if (fputs(content, p) == EOF)
22 die("Couldn't write to pipe");
23 int status = pclose(p);
24 if (status < 0)
25 die_err("Error closing pipe");
26 if (status != 0)
27 die("tl-append exited abnormally");
28 }
29
30 static void verify_log_contents(const char *contents) {
31 char buf[10];
32
33 FILE *f = fopen("tl.log", "r");
34 if (f == NULL)
35 die_err("Error opening log file");
36 if (fgets(buf, sizeof(buf), f) == NULL)
37 die("Error reading log file");
38 if (strncmp(contents, buf, sizeof(buf)) != 0)
39 die("Wrong contents in log file");
40 if (fclose(f) != 0)
41 die_err("Error closing log file");
42 }
43
44 static void write_and_read_line() {
45 write_to_tl_append("foo\n");
46 verify_log_contents("foo\n");
47 }
48
49 int main() { write_and_read_line(); }