From 25ce73c10c148cdb726425209492d4b53fb7d6f7 Mon Sep 17 00:00:00 2001 From: Oskari Pirhonen Date: Sun, 28 Aug 2022 17:13:36 -0500 Subject: Create test runner for ANSI stripping --- .github/workflows/run_test.yml | 12 +++ test/red.txt | 1 + test/red_no_ansi.txt | 1 + test/test_ansi.sh | 186 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 test/red.txt create mode 100644 test/red_no_ansi.txt create mode 100755 test/test_ansi.sh diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml index 6a1f85e..85dbd29 100644 --- a/.github/workflows/run_test.yml +++ b/.github/workflows/run_test.yml @@ -8,3 +8,15 @@ jobs: - uses: actions/checkout@v2 - run: test/test.sh shell: bash + run-test-ansi: + name: 'Run test/test_ansi.sh' + # 22.04 is the earliest version that has ansifilter in the repos + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v2 + - name: 'Install ansifilter' + run: | + sudo apt-get update + sudo apt-get install ansifilter + - run: test/test_ansi.sh + shell: bash diff --git a/test/red.txt b/test/red.txt new file mode 100644 index 0000000..4de08cf --- /dev/null +++ b/test/red.txt @@ -0,0 +1 @@ +red text diff --git a/test/red_no_ansi.txt b/test/red_no_ansi.txt new file mode 100644 index 0000000..f5f7d45 --- /dev/null +++ b/test/red_no_ansi.txt @@ -0,0 +1 @@ +red text diff --git a/test/test_ansi.sh b/test/test_ansi.sh new file mode 100755 index 0000000..ce01c0c --- /dev/null +++ b/test/test_ansi.sh @@ -0,0 +1,186 @@ +#! /bin/bash + +# wgetpaste test script (stripping ANSI codes) +# Based on test/test.sh +# Exit code: number of mismatched downloads or 1 for general failure +# Copyright (C) 2022 Oskari Pirhonen + +# Don't assume the test is being run from the same directory as the script +TEST_DIR="$(dirname "$0")" +ANSI_FILE="$TEST_DIR/red.txt" +NOANSI_FILE="$TEST_DIR/red_no_ansi.txt" +DL_DIR="$(mktemp -q -d /tmp/wgetpaste_test_ansi.XXXXX)" +# Services to hard skip +# Pre-declare as map to maintain type even if empty +# key -> value := service -> reason +declare -A HARD_SKIPS +HARD_SKIPS=(['codepad']='always times out') +# Services expected to require an authorization token +AUTH_SKIPS=('gists' 'snippets') +# Used to save the first working service +WORKING= +FAILED_PASTE=0 +DL_MISMATCH=0 + +# Test that temp directory was created +if [ ! -d "$DL_DIR" ]; then + echo "Failed to create temporary download directory: $DL_DIR" + exit 1 +fi +echo "Using download directory: $DL_DIR" + +# Post test file into each service until one succeeds +for serv in $("$TEST_DIR"/../wgetpaste -S --completions); do + # Hard skips + for hs in "${!HARD_SKIPS[@]}"; do + if [ "$serv" == "$hs" ]; then + echo "HARD SKIP on $serv -- reason: ${HARD_SKIPS[$serv]}" + continue 2 + fi + done + + # Log errors to analyze the reason + # Use verbose output to get more meaningful errors + # Log deleted at the end of each loop unless error other than 401 + echo -n "Posting to $serv: " + ERROR_LOG="$DL_DIR/$serv-error.log" + URL="$("$TEST_DIR"/../wgetpaste -r -s "$serv" -v "$ANSI_FILE" 2>"$ERROR_LOG")" + STATUS="$?" + + # Skip failed posts (eg, not authorized for GitHub/GitLab, service error) + if [ "$STATUS" -ne 0 ]; then + if (grep -iq "HTTP.*401.*Unauthorized" "$ERROR_LOG"); then + # Check if a 401 is expected behavior. If it isn't, mark as fail + for as in "${AUTH_SKIPS[@]}"; do + if [ "$serv" == "$as" ]; then + echo "SKIPPING, needs authorization..." + rm "$ERROR_LOG" + continue 2 + fi + done + echo "UNEXPECTED 401, skipping..." + else + echo "SKIPPING, failed to post..." + fi + + continue + fi + echo "SUCCESS!" + + echo -n "Downloading from $serv: " + if ! (wget -q "$URL" -O "/dev/null" 2>>"$ERROR_LOG"); then + echo "FAILED, skipping..." + continue + fi + echo "SUCCESS!" + rm "$ERROR_LOG" + + # This is the service we want to use + echo "Using service $serv" + WORKING="$serv" + break +done + +# Test if we have a working service +if [ -z "$WORKING" ]; then + echo "No working service found!" + for log in "$DL_DIR"/*.log; do + echo "$(basename "$log"):" + cat "$log" + done + rm -rf "$DL_DIR" + exit 1 +fi + +# Paste stuff. Use a short timeout between requests (we're friendly after all!) +sleep 1 +echo -n "Pasting command output (cat): " +ERROR_LOG="$DL_DIR/command-error.log" +URL="$("$TEST_DIR"/../wgetpaste -N -r -s "$WORKING" -v -c "cat $ANSI_FILE" 2>"$ERROR_LOG")" +if [ $? -ne 0 ]; then + echo "FAILED!" + FAILED_PASTE=$((FAILED_PASTE + 1)) +else + echo "SUCCESS!" + + echo -n "Downloading: " + if ! (wget -q "$URL" -O "$DL_DIR/command.txt" 2>>"$ERROR_LOG"); then + echo "FAILED!" + FAILED_PASTE=$((FAILED_PASTE + 1)) + else + echo "SUCCESS" + rm "$ERROR_LOG" + + echo "Removing 'command run' header" + sed -i -e '1d' "$DL_DIR/command.txt" + fi +fi + +sleep 1 +echo -n "Pasting stdin (cat | wgetpaste): " +ERROR_LOG="$DL_DIR/stdin-error.log" +URL="$(cat "$ANSI_FILE" | "$TEST_DIR"/../wgetpaste -N -r -s "$WORKING" -v 2>"$ERROR_LOG")" +if [ $? -ne 0 ]; then + echo "FAILED!" + FAILED_PASTE=$((FAILED_PASTE + 1)) +else + echo "SUCCESS!" + + echo -n "Downloading: " + if ! (wget -q "$URL" -O "$DL_DIR/stdin.txt" 2>>"$ERROR_LOG"); then + echo "FAILED!" + FAILED_PASTE=$((FAILED_PASTE + 1)) + else + echo "SUCCESS!" + rm "$ERROR_LOG" + fi +fi + +sleep 1 +echo -n "Pasting a file: " +ERROR_LOG="$DL_DIR/file-error.log" +URL="$("$TEST_DIR"/../wgetpaste -N -r -s "$WORKING" -v "$ANSI_FILE" 2>"$ERROR_LOG")" +if [ $? -ne 0 ]; then + echo "FAILED!" + FAILED_PASTE=$((FAILED_PASTE + 1)) +else + echo "SUCCESS!" + + echo -n "Downloading: " + if ! (wget -q "$URL" -O "$DL_DIR/file.txt" 2>>"$ERROR_LOG"); then + echo "FAILED!" + FAILED_PASTE=$((FAILED_PASTE + 1)) + else + echo "SUCCESS!" + rm "$ERROR_LOG" + fi +fi + +# Compare downloaded files +for dl_file in "$DL_DIR"/*.txt; do + echo -n "Testing file $dl_file: " + # Ignore missing trailing newline and extra empty lines in downloaded file + if (diff -q -Z -B "$NOANSI_FILE" "$dl_file" &>/dev/null); then + echo "SUCCESS!" + else + echo "FAILED!" + DL_MISMATCH=$((DL_MISMATCH + 1)) + fi +done + +echo "Total failed pastes: $FAILED_PASTE" +echo "Total mismatches: $DL_MISMATCH" + +# Print failure logs +if [ $FAILED_PASTE -ne 0 ]; then + for log in "$DL_DIR"/*.log; do + echo "$(basename "$log"):" + cat "$log" + done +# Delete download directory if all tests succeeded +elif [ $DL_MISMATCH -eq 0 ]; then + echo "Deleting download directory" + rm -rf "$DL_DIR" +fi + +exit "$DL_MISMATCH" -- cgit v1.2.3