From 9aa0a58d184679e48f96c1ea7d87d831d53df1e6 Mon Sep 17 00:00:00 2001 From: Chris Burgess Date: Tue, 26 Dec 2023 20:30:04 -0500 Subject: [PATCH] Update CI workflow and add run-tests.py script --- .github/workflows/ci.yml | 25 +++++++++++++----- run-tests.py | 56 ++++++++++++++++++++++++++++++++++++++++ tests/basic-1 | 3 +++ tests/basic-2 | 8 ++++++ tests/basic-3 | 6 +++++ tests/basic-4 | 7 +++++ tests/csv | 7 +++++ tests/delims | 4 +++ tests/echo | 3 +++ tests/fixed-1 | 4 +++ tests/fixed-2 | 4 +++ tests/fixed-3 | 4 +++ tests/json | 2 ++ tests/raw-1 | 8 ++++++ tests/skips-bad-input | 13 ++++++++++ 15 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 run-tests.py create mode 100644 tests/basic-1 create mode 100644 tests/basic-2 create mode 100644 tests/basic-3 create mode 100644 tests/basic-4 create mode 100644 tests/csv create mode 100644 tests/delims create mode 100644 tests/echo create mode 100644 tests/fixed-1 create mode 100644 tests/fixed-2 create mode 100644 tests/fixed-3 create mode 100644 tests/json create mode 100644 tests/raw-1 create mode 100644 tests/skips-bad-input diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 22ec253..a5adcde 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,10 +1,9 @@ - name: CI on: push: tags: - - '*' + - "*" branches: - main pull_request: @@ -24,13 +23,28 @@ jobs: - uses: goto-bus-stop/setup-zig@v2 - run: zig fmt --check src/*.zig + cli-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: goto-bus-stop/setup-zig@v2 + - run: zig build + - run: PATH=$PATH:zig-out/bin python3 run-tests.py + build: - needs: [test, lint] + needs: [test, lint, cli-test] if: startsWith(github.ref, 'refs/tags/v') strategy: fail-fast: false matrix: - target: [ x86_64-linux, aarch64-linux, riscv64-linux, x86_64-windows, aarch64-macos ] + target: + [ + x86_64-linux, + aarch64-linux, + riscv64-linux, + x86_64-windows, + aarch64-macos, + ] runs-on: ubuntu-latest steps: - name: Checkout @@ -68,10 +82,9 @@ jobs: - name: Display structure of downloaded files run: ls -R - working-directory: artifacts + working-directory: artifacts - name: Create Release uses: softprops/action-gh-release@v1 with: files: artifacts/**/* - diff --git a/run-tests.py b/run-tests.py new file mode 100644 index 0000000..2c7f906 --- /dev/null +++ b/run-tests.py @@ -0,0 +1,56 @@ +import os +import subprocess +import difflib + + +def run_test(test_file): + with open(test_file, "r") as f: + lines = f.readlines() + cmd = lines[0].strip() + expected_output = "".join(lines[1:]) + actual_output = subprocess.check_output(cmd, shell=True).decode("utf-8").strip() + + return expected_output == actual_output, expected_output, actual_output + + +def print_diff(expected_output, actual_output): + diff = difflib.unified_diff( + expected_output.splitlines(keepends=True), + actual_output.splitlines(keepends=True), + fromfile="Expected", + tofile="Actual", + ) + print("\nDifference (Expected vs Actual):") + print("".join(diff), end="") + + +def main(): + test_dir = "tests" + passed = 0 + failed = 0 + files = sorted(os.listdir(test_dir)) + for test_file in files: + success, expected_output, actual_output = run_test( + os.path.join(test_dir, test_file) + ) + if success: + passed += 1 + print(f"\033[92m✔ {test_file}: Test passed\033[0m") + else: + failed += 1 + print(f"\033[91m✖ {test_file}: Test failed\033[0m") + print_diff(expected_output, actual_output) + print() + print() + + # Summary + print("\nTest Summary") + print(f"Passed: \033[92m{passed}\033[0m") + print(f"Failed: \033[91m{failed}\033[0m") + + if failed > 0: + exit(1) + + +if __name__ == "__main__": + main() diff --git a/tests/basic-1 b/tests/basic-1 new file mode 100644 index 0000000..c005bcc --- /dev/null +++ b/tests/basic-1 @@ -0,0 +1,3 @@ +pc 1 23 40000 +↑ 2200% 23x [ 1 → 23 ] +↑ 173813% 1739x [ 23B → 39KiB ] \ No newline at end of file diff --git a/tests/basic-2 b/tests/basic-2 new file mode 100644 index 0000000..5c1ba49 --- /dev/null +++ b/tests/basic-2 @@ -0,0 +1,8 @@ +pc 400000 123 12312412312312312 0 0 0 1 0 +↓ -100.0% 0.00x [ 390.6KiB → 123.0B ] +↑ 10010091298226176% 100100915331072x [ 123B → 11PiB ] +↓ -100% 0x [ 11PiB → 0B ] +→ 0% 0x [ 0 → 0 ] +→ 0% 0x [ 0 → 0 ] +↑ inf% infx [ 0 → 1 ] +↓ -100% 0x [ 1 → 0 ] \ No newline at end of file diff --git a/tests/basic-3 b/tests/basic-3 new file mode 100644 index 0000000..b808e66 --- /dev/null +++ b/tests/basic-3 @@ -0,0 +1,6 @@ +pc 10 11 200 2000 2000000 1999999 +↑ 10% 1.10x [ 10 → 11 ] +↑ 1718% 18.2x [ 11 → 200 ] +↑ 900% 10x [ 200B → 2KiB ] +↑ 99900% 1000x [ 2KiB → 2MiB ] +↓ -0.00% 1.00x [ 1.91MiB → 1.91MiB ] \ No newline at end of file diff --git a/tests/basic-4 b/tests/basic-4 new file mode 100644 index 0000000..dafa826 --- /dev/null +++ b/tests/basic-4 @@ -0,0 +1,7 @@ +pc 18024 19503 11124 12321 340200 424212 1000000000 +↑ 8.21% 1.08x [ 17.60KiB → 19.05KiB ] +↓ -43.0% 0.57x [ 19.0KiB → 10.9KiB ] +↑ 10.8% 1.11x [ 10.9KiB → 12.0KiB ] +↑ 2661% 27.6x [ 12KiB → 332KiB ] +↑ 24.7% 1.25x [ 332.2KiB → 414.3KiB ] +↑ 235631% 2357x [ 414KiB → 954MiB ] \ No newline at end of file diff --git a/tests/csv b/tests/csv new file mode 100644 index 0000000..5e7b657 --- /dev/null +++ b/tests/csv @@ -0,0 +1,7 @@ +pc 50 1200 3432 23 0 0 --format csv +percent,times,prev,cur +2300,24,50,1200 +186,2.859999895095825,1200,3432 +-99.329833984375,0.006701631471514702,3432,23 +-100,0,23,0 +0,0,0,0 \ No newline at end of file diff --git a/tests/delims b/tests/delims new file mode 100644 index 0000000..e173deb --- /dev/null +++ b/tests/delims @@ -0,0 +1,4 @@ +echo "15@20@3 6" | pc -d "@" +↑ 33.3% 1.33x [ 15 → 20 ] +↓ -85% 0.15x [ 20 → 3 ] +↑ 100% 2x [ 3 → 6 ] \ No newline at end of file diff --git a/tests/echo b/tests/echo new file mode 100644 index 0000000..aaf1d84 --- /dev/null +++ b/tests/echo @@ -0,0 +1,3 @@ +echo 100 2000 20 | pc +↑ 1900% 20x [ 100B → 2KiB ] +↓ -99% 0.01x [ 2KiB → 20B ] \ No newline at end of file diff --git a/tests/fixed-1 b/tests/fixed-1 new file mode 100644 index 0000000..3b852b4 --- /dev/null +++ b/tests/fixed-1 @@ -0,0 +1,4 @@ +pc 1 2 3 4 -f +↑ 100% 2x [ 1 → 2 ] +↑ 200% 3x [ 1 → 3 ] +↑ 300% 4x [ 1 → 4 ] \ No newline at end of file diff --git a/tests/fixed-2 b/tests/fixed-2 new file mode 100644 index 0000000..406269c --- /dev/null +++ b/tests/fixed-2 @@ -0,0 +1,4 @@ +pc 1 2 3 4 -f 2 +↓ -50% 0.50x [ 2 → 1 ] +↑ 50% 1.50x [ 2 → 3 ] +↑ 100% 2x [ 2 → 4 ] \ No newline at end of file diff --git a/tests/fixed-3 b/tests/fixed-3 new file mode 100644 index 0000000..3ac8650 --- /dev/null +++ b/tests/fixed-3 @@ -0,0 +1,4 @@ +pc 1 2 3 4 -f -1 +↓ -75% 0.25x [ 4 → 1 ] +↓ -50% 0.50x [ 4 → 2 ] +↓ -25% 0.75x [ 4 → 3 ] \ No newline at end of file diff --git a/tests/json b/tests/json new file mode 100644 index 0000000..f4dc852 --- /dev/null +++ b/tests/json @@ -0,0 +1,2 @@ +pc 50 1200 3432 23 0 0 --format json +[{"percent":2.3e+03,"times":2.4e+01,"prev":5.0e+01,"cur":1.2e+03},{"percent":1.86e+02,"times":2.859999895095825e+00,"prev":1.2e+03,"cur":3.432e+03},{"percent":-9.9329833984375e+01,"times":6.701631471514702e-03,"prev":3.432e+03,"cur":2.3e+01},{"percent":-1.0e+02,"times":0.0e+00,"prev":2.3e+01,"cur":0.0e+00},{"percent":0.0e+00,"times":0.0e+00,"prev":0.0e+00,"cur":0.0e+00}] \ No newline at end of file diff --git a/tests/raw-1 b/tests/raw-1 new file mode 100644 index 0000000..fd68e8b --- /dev/null +++ b/tests/raw-1 @@ -0,0 +1,8 @@ +pc 400000 123 12312412312312312 0 0 0 1 0 --raw +↓ -100.0% 0.00x [ 400000 → 123 ] +↑ 10010091298226176% 100100915331072x [ 123 → 12312412812214272 ] +↓ -100% 0x [ 12312412812214272 → 0 ] +→ 0% 0x [ 0 → 0 ] +→ 0% 0x [ 0 → 0 ] +↑ inf% infx [ 0 → 1 ] +↓ -100% 0x [ 1 → 0 ] \ No newline at end of file diff --git a/tests/skips-bad-input b/tests/skips-bad-input new file mode 100644 index 0000000..685562b --- /dev/null +++ b/tests/skips-bad-input @@ -0,0 +1,13 @@ +pc --help | pc +→ 0% 1x [ 2 → 2 ] +→ 0% 1x [ 2 → 2 ] +→ 0% 1x [ 2 → 2 ] +↓ -150% -0.50x [ 2 → -1 ] +↑ 100000096% -1000000x [ -1 → 1000000 ] +↓ -100.0% 0.00x [ 976.6KiB → 2.0B ] +↑ 400% 5x [ 2 → 10 ] +↑ 100% 2x [ 10 → 20 ] +↑ 50% 1.50x [ 20 → 30 ] +↓ -33.3% 0.67x [ 30 → 20 ] +↑ 1005% 11.1x [ 20 → 221 ] +→ 0% 1x [ 221 → 221 ] \ No newline at end of file