X-Git-Url: http://git.scottworley.com/tl-append/blobdiff_plain/aacfb261e9aa8c28961adf78b86f293165a71b69..7a92d9bfaad4ee868571e9d71b4d557a6d5ad7ef:/tl-append-test.c diff --git a/tl-append-test.c b/tl-append-test.c index 0867280..22589da 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) @@ -28,22 +37,36 @@ static void write_to_tl_append(const char *content) { } static void verify_log_contents(const char *contents) { - char buf[10]; + 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(contents, 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); } 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(); +}