diff options
-rw-r--r-- | src/gistatic.c | 55 | ||||
-rw-r--r-- | tests/resources/assets/index.html | 1 | ||||
-rw-r--r-- | tests/resources/assets/refs.html | 1 |
3 files changed, 55 insertions, 2 deletions
diff --git a/src/gistatic.c b/src/gistatic.c index 217f3dd..cfd5c55 100644 --- a/src/gistatic.c +++ b/src/gistatic.c @@ -816,6 +816,56 @@ static void test_escape_html() { } #endif +static void trim_newline(char *const s) { + if (s == NULL) { + return; + } + + if (s[0] == '\0') { + return; + } + + const size_t len = strlen(s); + if (s[len - 1] == '\n') { + s[len - 1] = '\0'; + } +} + +#ifdef TEST +static void test_trim_newline() { + test_start("test_trim_newline"); + { + testing("empty string"); + char *const s = ""; + trim_newline(s); + assert(strcmp(s, "") == 0); + test_ok(); + } + { + testing("NULL string"); + char *const s = NULL; + trim_newline(s); + assert(s == NULL); + test_ok(); + } + { + testing("string without a newline"); + char *const s = "a string without a newline"; + trim_newline(s); + assert(strcmp(s, "a string without a newline") == 0); + test_ok(); + } + return; + { + testing("string with a newline"); + char *const s = "a string with an ending newline\n"; + trim_newline(s); + assert(strcmp(s, "a string with an ending newline") == 0); + test_ok(); + } +} +#endif + static int index_write_header(FILE *const fd, const char *const idx_title) { const int e = fprintf( fd, @@ -1042,6 +1092,8 @@ static int index_write_row(FILE *const fd, char *const repopath) { if (!fgets(description, sizeof(description), f)) { logerrs("fgets(\"", description_path, "\")", strerror(errno), __LINE__); + } else { + trim_newline(description); } if (fclose(f)) { logerrs("fclose(\"", description_path, "\")", @@ -1737,6 +1789,8 @@ static int repo_write( if (!fgets(description, sizeof(description), f)) { logerrs("fget(\"", description_path, "\")", strerror(errno), __LINE__); + } else { + trim_newline(description); } if (fclose(f)) { logerrs("fclose(\"", description_path, "\")", @@ -1797,6 +1851,7 @@ static void unit_tests(){ test_strjoin(); test_formatted_date(); test_escape_html(); + test_trim_newline(); test_last_commit_date(); git_libgit2_shutdown(); } diff --git a/tests/resources/assets/index.html b/tests/resources/assets/index.html index 04aa8a2..6719eec 100644 --- a/tests/resources/assets/index.html +++ b/tests/resources/assets/index.html @@ -41,7 +41,6 @@ </td> <td> Submodule repository for gistatic project tests - </td> <td> 2021-07-31 19:29 diff --git a/tests/resources/assets/refs.html b/tests/resources/assets/refs.html index b24bd52..be93bda 100644 --- a/tests/resources/assets/refs.html +++ b/tests/resources/assets/refs.html @@ -19,7 +19,6 @@ </h1> <h2> Submodule repository for gistatic project tests - </h2> <code> git clone https://example.com/ |