Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI testing to CI #9

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

name: CI

on:
push:
tags:
- '*'
- "*"
branches:
- main
pull_request:
Expand All @@ -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
Expand Down Expand Up @@ -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/**/*

56 changes: 56 additions & 0 deletions run-tests.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 5 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ fn percentDiff(a: f32, b: f32) f32 {
if (a == 0) {
return if (b == 0) 0.0 else std.math.inf(f32);
}

return (b - a) / @fabs(a) * 100.0;
return (b - a) / @abs(a) * 100.0;
}

test "calculates percent differences correctly" {
Expand Down Expand Up @@ -93,7 +92,7 @@ test "calculates times differences correctly" {
fn numberPrecision(num: f32) u8 {
// if its a whole number, don't show decimal places otherwise, show up to 2
// decimal places
if (@fabs(num - std.math.round(num)) < 0.001) {
if (@abs(num - std.math.round(num)) < 0.001) {
return 0;
} else {
return 2;
Expand All @@ -112,7 +111,7 @@ fn sizeFormatPrecision(num: f32) u8 {
if (@floor(num) == num) {
return 0;
}
const rounded = @round(@fabs(num));
const rounded = @round(@abs(num));
if (rounded > std.math.maxInt(u64)) {
diff = std.math.maxInt(u64);
} else {
Expand Down Expand Up @@ -425,7 +424,7 @@ pub fn main() !void {

// if no nums, read from stdin
if (nums.items.len == 0) {
var input = std.io.getStdIn().reader().readAllAlloc(allocator, 10 * 1024 * 1024) catch |e| {
const input = std.io.getStdIn().reader().readAllAlloc(allocator, 10 * 1024 * 1024) catch |e| {
std.debug.print("pc: error reading stdin: {s}\n", .{@errorName(e)});
return std.process.exit(1);
};
Expand All @@ -448,7 +447,7 @@ pub fn main() !void {
defer rows.deinit();

// calculate the current index based on the target
var cur_idx = switch (target) {
const cur_idx = switch (target) {
.Moving => 0, // start at the first number
.Fixed => |index| blk: {
// account for negative indices and clamp
Expand Down
3 changes: 3 additions & 0 deletions tests/basic-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pc 1 23 40000
↑ 2200% 23x [ 1 → 23 ]
↑ 173813% 1739x [ 23B → 39KiB ]
8 changes: 8 additions & 0 deletions tests/basic-2
Original file line number Diff line number Diff line change
@@ -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 ]
6 changes: 6 additions & 0 deletions tests/basic-3
Original file line number Diff line number Diff line change
@@ -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 ]
7 changes: 7 additions & 0 deletions tests/basic-4
Original file line number Diff line number Diff line change
@@ -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 ]
7 changes: 7 additions & 0 deletions tests/csv
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions tests/delims
Original file line number Diff line number Diff line change
@@ -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 ]
3 changes: 3 additions & 0 deletions tests/echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
echo 100 2000 20 | pc
↑ 1900% 20x [ 100B → 2KiB ]
↓ -99% 0.01x [ 2KiB → 20B ]
4 changes: 4 additions & 0 deletions tests/fixed-1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pc 1 2 3 4 -f
↑ 100% 2x [ 1 → 2 ]
↑ 200% 3x [ 1 → 3 ]
↑ 300% 4x [ 1 → 4 ]
4 changes: 4 additions & 0 deletions tests/fixed-2
Original file line number Diff line number Diff line change
@@ -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 ]
4 changes: 4 additions & 0 deletions tests/fixed-3
Original file line number Diff line number Diff line change
@@ -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 ]
2 changes: 2 additions & 0 deletions tests/json
Original file line number Diff line number Diff line change
@@ -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}]
8 changes: 8 additions & 0 deletions tests/raw-1
Original file line number Diff line number Diff line change
@@ -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 ]
13 changes: 13 additions & 0 deletions tests/skips-bad-input
Original file line number Diff line number Diff line change
@@ -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 ]