From 8cb0207e09c301a02e21f5e8a395c7c958781f1b Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Thu, 5 Aug 2021 18:07:21 -0300 Subject: tests/: Add integration tests, also test with Valgrind --- Makefile | 15 +--- src/gistatic.c | 4 +- tests/dev-integration.sh | 16 +++++ tests/integration.sh | 58 ++++++++++++++++ tests/lib.sh | 143 ++++++++++++++++++++++++++++++++++++++ tests/resources/assets/index.html | 73 +++++++++++++++++++ tests/resources/assets/logo.svg | 62 +++++++++++++++++ tests/resources/assets/refs.html | 120 ++++++++++++++++++++++++++++++++ tests/resources/assets/style.css | 76 ++++++++++++++++++++ 9 files changed, 553 insertions(+), 14 deletions(-) create mode 100755 tests/dev-integration.sh create mode 100755 tests/integration.sh create mode 100755 tests/lib.sh create mode 100644 tests/resources/assets/index.html create mode 100644 tests/resources/assets/logo.svg create mode 100644 tests/resources/assets/refs.html create mode 100644 tests/resources/assets/style.css diff --git a/Makefile b/Makefile index f12fa70..03c006b 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ check: all src/gistatic.t ln -s .gitdir tests/resources/repositories/repo-1/.git ln -s .gitdir tests/resources/repositories/repo-2/.git ./src/gistatic.t + sh tests/integration.sh sh aux/assert-catgets.sh src/gistatic.c clean: @@ -64,18 +65,8 @@ uninstall: # Personal workflow targets # - -VALGRIND_FLAGS = \ - --show-error-list=yes \ - --show-leak-kinds=all \ - --leak-check=full \ - --track-origins=yes \ - --error-exitcode=1 - -c-dev-check: check - valgrind $(VALGRIND_FLAGS) ./src/gistatic.t - -dev-check: c-dev-check public +dev-check: check public + sh tests/dev-integration.sh sh aux/assert-shellcheck.sh sh aux/workflow/assert-todos.sh sh aux/workflow/assert-changelog.sh -n $(NAME) diff --git a/src/gistatic.c b/src/gistatic.c index d07cf17..217f3dd 100644 --- a/src/gistatic.c +++ b/src/gistatic.c @@ -260,7 +260,7 @@ static const char *const STYLE_STR = "" " padding-right: 1em;\n" "}\n" "\n" - "footer { \n" + "footer {\n" " text-align: center;\n" "}\n" ""; @@ -1641,7 +1641,7 @@ static int repo_write_snapshots( goto loop_cleanup; } - printf("reference name: %s\n", git_reference_shorthand(ref)); + // printf("reference name: %s\n", git_reference_shorthand(ref)); // https://github.com/ionescu007/minlzma // https://git.tukaani.org/?p=xz.git;a=tree;f=src;h=665b39e6439f1bb5afd9181ec0890c2ed26d047e;hb=HEAD diff --git a/tests/dev-integration.sh b/tests/dev-integration.sh new file mode 100755 index 0000000..a82ce14 --- /dev/null +++ b/tests/dev-integration.sh @@ -0,0 +1,16 @@ +#!/bin/sh +set -eu + +. tests/lib.sh + +VALGRIND_FLAGS=' +--show-error-list=yes +--show-leak-kinds=all +--leak-check=full +--track-origins=yes +--error-exitcode=1 +' + +valgrind $VALGRIND_FLAGS ./src/gistatic.t +valgrind $VALGRIND_FLAGS ./src/gistatic -o `mkdtemp` -u 'https://example.com' tests/resources/repositories/repo-1 +valgrind $VALGRIND_FLAGS ./src/gistatic -o `mkdtemp` -i tests/resources/repositories/* diff --git a/tests/integration.sh b/tests/integration.sh new file mode 100755 index 0000000..27a2dfc --- /dev/null +++ b/tests/integration.sh @@ -0,0 +1,58 @@ +#!/bin/sh +set -u + +. tests/lib.sh + +assert_file() { + if ! diff "$2" "$1"; then + printf '\n%s: File content differs.\n' \ + "$(ERROR)" >&2 + printf '\nexpected: %s\ngot: %s\n\n' "$1" "$2" >&2 + print_debug_info + exit 1 + fi +} + +test_index_generation() { + testing 'index generation' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + DIR="$(mkdtemp)" + ./src/gistatic -i -o "$DIR" tests/resources/repositories/* \ + 1>"$OUT" 2>"$ERR" + STATUS=$? + assert_empty_stdout + assert_empty_stderr + assert_status 0 + assert_file tests/resources/assets/style.css "$DIR/style.css" + assert_file tests/resources/assets/logo.svg "$DIR/logo.svg" + assert_file tests/resources/assets/index.html "$DIR/index.html" + + test_ok +} + +test_repo_generation() { + testing 'repo generation' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + DIR="$(mkdtemp)" + ./src/gistatic -o "$DIR" -u https://example.com/ \ + tests/resources/repositories/repo-1 1>"$OUT" 2>"$ERR" + STATUS=$? + assert_empty_stdout + assert_empty_stderr + assert_status 0 + + assert_file tests/resources/assets/style.css "$DIR/style.css" + assert_file tests/resources/assets/logo.svg "$DIR/logo.svg" + assert_file tests/resources/assets/refs.html "$DIR/refs.html" + + test_ok +} + +test_index_generation +test_repo_generation diff --git a/tests/lib.sh b/tests/lib.sh new file mode 100755 index 0000000..06a5b54 --- /dev/null +++ b/tests/lib.sh @@ -0,0 +1,143 @@ +#!/bin/sh + +end="\033[0m" +red="\033[0;31m" +green="\033[0;32m" +yellow="\033[0;33m" + +N= +OUT= +ERR= +STATUS= + +ERROR() { + # shellcheck disable=2059 + printf "${red}ERROR${end}" +} + +print_debug_info() { + printf 'LINENO: %s\nOUT: %s\nERR: %s\n' \ + "$N" "$OUT" "$ERR" >&2 +} + +assert_status() { + if [ "$STATUS" != "$1" ]; then + printf '\n%s: Bad status.\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$STATUS" >&2 + print_debug_info + exit 1 + fi +} + +assert_usage() { + if ! grep -Fq 'Usage' "$1"; then + echo 'Expected to find "Usage" text, it was missing:' >&2 + cat "$1" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stream() { + if [ -s "$2" ]; then + FMT='\n%s: Expected %s (%s) to be empty, but has content:\n%s\n' + # shellcheck disable=2059 + printf "$FMT" \ + "$(ERROR)" "$1" "$2" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stdout() { + assert_empty_stream STDOUT "$OUT" +} + +assert_empty_stderr() { + assert_empty_stream STDERR "$ERR" +} + +assert_stream() { + if [ "$(cat "$2")" != "$3" ]; then + printf '\nERR: Bad %s (%s)\n\nexpected: %s\ngot: %s\n' \ + "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_stdout() { + assert_stream STDOUT "$OUT" "$1" +} + +assert_stderr() { + assert_stream STDERR "$ERR" "$1" +} + +assert_grep_stream() { + if ! grep -qE "$3" "$2"; then + printf '\nERR: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_grep_stdout() { + assert_grep_stream STDOUT "$OUT" "$1" +} + +assert_grep_stderr() { + assert_grep_stream STDERR "$ERR" "$1" +} + +assert_fgrep_stream() { + if ! grep -Fq -- "$3" "$2"; then + printf '\nERR: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$1" "$2" "$3" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_fgrep_stdout() { + assert_fgrep_stream STDOUT "$OUT" "$1" +} + +assert_fgrep_stderr() { + assert_fgrep_stream STDERR "$ERR" "$1" +} + +testing() { + printf "${yellow}testing${end}: %s..." "$1" >&2 +} + +test_ok() { + # shellcheck disable=2059 + printf " ${green}OK${end}.\n" >&2 +} + +uuid() { + # Taken from: + # https://serverfault.com/a/799198 + od -xN20 /dev/urandom | + head -1 | + awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' +} + +tmpname() { + echo 'mkstemp(template)' | m4 -D template="${TMPDIR:-/tmp}/m4-tmpname." +} + +mkstemp() { + name="$(tmpname)" + touch "$name" + echo "$name" +} + +mkdtemp() { + name="$(tmpname)" + rm -f "$name" + mkdir "$name" + echo "$name" +} diff --git a/tests/resources/assets/index.html b/tests/resources/assets/index.html new file mode 100644 index 0000000..04aa8a2 --- /dev/null +++ b/tests/resources/assets/index.html @@ -0,0 +1,73 @@ + + + + + + + + Repositories + + +
+
+ +

+ Repositories +

+
+
+
+
+ + + + + + + + + + + + + + + + + + + + +
+ Name + + Description + + Last commit +
+ + repo-1 + + + Submodule repository for gistatic project tests + + + 2021-07-31 19:29 +
+ + repo-2 + + + + + 2021-07-31 19:27 +
+
+ + + diff --git a/tests/resources/assets/logo.svg b/tests/resources/assets/logo.svg new file mode 100644 index 0000000..ce566b2 --- /dev/null +++ b/tests/resources/assets/logo.svg @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/resources/assets/refs.html b/tests/resources/assets/refs.html new file mode 100644 index 0000000..b24bd52 --- /dev/null +++ b/tests/resources/assets/refs.html @@ -0,0 +1,120 @@ + + + + + + + + + + repo-1 + + +
+
+ +
+

+ repo-1 +

+

+ Submodule repository for gistatic project tests + +

+ + git clone https://example.com/ + +
+
+ +
+
+
+ + + + + + + + + + + + + + + + + +
+ Branch + + Commit message + + Author + + Date +
+ + main + + + + description: Add + + + EuAndreh + + 2021-07-31 19:29 +
+ + + + + + + + + + + + +
+ Tag + + Commit message + + Download + + Author + + Date +
+
+ + + diff --git a/tests/resources/assets/style.css b/tests/resources/assets/style.css new file mode 100644 index 0000000..9972738 --- /dev/null +++ b/tests/resources/assets/style.css @@ -0,0 +1,76 @@ +body { + font-family: monospace; + max-width: 1100px; + margin: 0 auto 0 auto; +} + +.logo { + height: 6em; + width: 6em; +} + +.header-horizontal-grouping { + display: flex; + align-items: center; + margin-top: 1em; + margin-bottom: 1em; +} + +.header-description { + margin-left: 2em; +} + +nav { + margin-top: 2em; +} + +nav ul { + display: flex; + list-style-type: none; + margin-bottom: 0; +} + +nav li { + margin-left: 10px; +} + +nav a { + padding: 2px 8px 0px 8px; + text-decoration: none; + color: black; +} + +nav a:hover { + text-decoration: underline; +} + +.selected-nav-item { + background-color: hsl(0, 0%, 87%); +} + +hr { + margin-top: 0; + border: 0; + border-top: 3px solid hsl(0, 0%, 87%); +} + +table { + margin: 2em auto; +} + +th { + padding-bottom: 1em; +} + +tbody tr:hover { + background-color: hsl(0, 0%, 93%); +} + +td { + padding-left: 1em; + padding-right: 1em; +} + +footer { + text-align: center; +} -- cgit v1.2.3