]> git.scottworley.com Git - tl-append/blame - tl-append-test.c
Refactor test
[tl-append] / tl-append-test.c
CommitLineData
b3d5ed96
SW
1#define _POSIX_C_SOURCE 2
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6static void die(const char *message) {
7 fputs(message, stderr);
8 fputc('\n', stderr);
9 exit(1);
10}
11
12static void die_err(const char *message) {
13 perror(message);
14 exit(1);
15}
16
aacfb261 17static void write_to_tl_append(const char *content) {
b3d5ed96
SW
18 FILE *p = popen("./tl-append", "w");
19 if (p == NULL)
20 die_err("Couldn't run tl-append");
aacfb261 21 if (fputs(content, p) == EOF)
b3d5ed96
SW
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");
aacfb261
SW
28}
29
30static void verify_log_contents(const char *contents) {
31 char buf[10];
b3d5ed96
SW
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");
aacfb261 38 if (strncmp(contents, buf, sizeof(buf)) != 0)
b3d5ed96
SW
39 die("Wrong contents in log file");
40 if (fclose(f) != 0)
41 die_err("Error closing log file");
42}
43
aacfb261
SW
44static void write_and_read_line() {
45 write_to_tl_append("foo\n");
46 verify_log_contents("foo\n");
47}
48
b3d5ed96 49int main() { write_and_read_line(); }