#define _POSIX_C_SOURCE 2
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+struct expectation {
+ const char *message;
+};
+
+const struct expectation END = {NULL};
+static struct expectation expectation(const char *message) {
+ struct expectation exp;
+ exp.message = message;
+ return exp;
+}
+
static void die(const char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
-static void write_and_read_line() {
- char buf[10];
+static void remove_logfile() {
+ if (remove("tl.log") != 0) {
+ if (errno != ENOENT) {
+ die_err("Error removing log file");
+ }
+ }
+}
+static struct expectation write_to_tl_append(const char *content) {
FILE *p = popen("./tl-append", "w");
if (p == NULL)
die_err("Couldn't run tl-append");
- if (fputs("foo\n", p) == EOF)
+ if (fputs(content, p) == EOF)
die("Couldn't write to pipe");
int status = pclose(p);
if (status < 0)
die_err("Error closing pipe");
if (status != 0)
die("tl-append exited abnormally");
+ return expectation(content);
+}
+
+static void verify_log_contents(const char *contents) {
+ size_t len = strlen(contents);
+ char *buf = (char *)malloc(len + 2);
FILE *f = fopen("tl.log", "r");
if (f == NULL)
die_err("Error opening log file");
- if (fgets(buf, sizeof(buf), f) == NULL)
+ buf[fread(buf, 1, len + 1, f)] = '\0';
+ if (ferror(f))
die("Error reading log file");
- if (strncmp("foo\n", buf, sizeof(buf)) != 0)
+ if (strncmp(contents, buf, len + 1) != 0)
die("Wrong contents in log file");
if (fclose(f) != 0)
die_err("Error closing log file");
+ free(buf);
}
-int main() { write_and_read_line(); }
+static void write_and_read_line() {
+ remove_logfile();
+ write_to_tl_append("foo\n");
+ verify_log_contents("foo\n");
+}
+
+static void write_and_read_two_lines() {
+ remove_logfile();
+ write_to_tl_append("foo\n");
+ write_to_tl_append("bar\n");
+ verify_log_contents("foo\nbar\n");
+}
+
+int main() {
+ write_and_read_line();
+ write_and_read_two_lines();
+}