From: Scott Worley Date: Thu, 31 Aug 2023 18:01:08 +0000 (-0700) Subject: Test writing twice X-Git-Tag: v1.0.0~23 X-Git-Url: http://git.scottworley.com/tl-append/commitdiff_plain/27dfbfeb85b47cf506ebae0a760895dd43e2b62d?ds=sidebyside Test writing twice --- diff --git a/tl-append-test.c b/tl-append-test.c index 0867280..6761b02 100644 --- a/tl-append-test.c +++ b/tl-append-test.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 2 +#include #include #include #include @@ -14,6 +15,14 @@ static void die_err(const char *message) { exit(1); } +static void remove_logfile() { + if (remove("tl.log") != 0) { + if (errno != ENOENT) { + die_err("Error removing log file"); + } + } +} + static void write_to_tl_append(const char *content) { FILE *p = popen("./tl-append", "w"); if (p == NULL) @@ -33,7 +42,8 @@ static void verify_log_contents(const char *contents) { 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, sizeof(buf), f)] = '\0'; + if (ferror(f)) die("Error reading log file"); if (strncmp(contents, buf, sizeof(buf)) != 0) die("Wrong contents in log file"); @@ -42,8 +52,19 @@ static void verify_log_contents(const char *contents) { } static void write_and_read_line() { + remove_logfile(); write_to_tl_append("foo\n"); verify_log_contents("foo\n"); } -int main() { write_and_read_line(); } +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(); +}