Skip to content

Commit 232ca57

Browse files
committed
feat: Ghetto-pytest refactor (aka compilation-station)
1 parent a13a800 commit 232ca57

16 files changed

+105
-443
lines changed

run-tests.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import contextlib
2+
import glob
3+
import io
4+
import os
5+
import runpy
6+
import sys
7+
8+
# Check if a specific test is provided
9+
if len(sys.argv) > 1:
10+
TEST_FILE = sys.argv[1]
11+
print(f"Checking file: {TEST_FILE}")
12+
if not os.path.isfile(TEST_FILE):
13+
print("Error: Test file '{}' not found.".format(TEST_FILE))
14+
sys.exit(1)
15+
TEST_FILES = [TEST_FILE]
16+
else:
17+
test_dir = os.getenv("TEST_DIR", "./tests/")
18+
g = f"{test_dir}*.py"
19+
print(f"CHecking glob: {g}")
20+
# Find all Python test files in ./tests directory
21+
TEST_FILES = [f for f in glob.glob(g) if ".skip" not in f and ".broken" not in f]
22+
23+
# Colors for output
24+
GREEN = "\033[0;32m"
25+
RED = "\033[0;31m"
26+
YELLOW = "\033[1;33m"
27+
NC = "\033[0m" # No Color
28+
29+
30+
# Function to run a test and display result
31+
def run_test(test_file):
32+
print("{}Running test: {}{}".format(YELLOW, test_file, NC))
33+
34+
buf = io.StringIO()
35+
try:
36+
with contextlib.redirect_stdout(buf):
37+
runpy.run_path(test_file, run_name="__main__")
38+
print(buf.getvalue())
39+
print("{}✓ PASS: {}{}".format(GREEN, test_file, NC))
40+
return True
41+
except SystemExit as e:
42+
output = buf.getvalue()
43+
print(output)
44+
if e.code == 0:
45+
print("{}✓ PASS: {}{}".format(GREEN, test_file, NC))
46+
return True
47+
else:
48+
print("{}✗ FAIL: {}{}".format(RED, test_file, NC))
49+
return False
50+
except Exception:
51+
output = buf.getvalue()
52+
print(output)
53+
print("{}✗ FAIL: {}{}".format(RED, test_file, NC))
54+
import traceback
55+
56+
traceback.print_exc()
57+
return False
58+
59+
60+
# Initialize counters and failed list
61+
TOTAL_TESTS = 0
62+
PASSED_TESTS = 0
63+
FAILED_TESTS = 0
64+
FAILED_LIST = []
65+
66+
# Run each test
67+
print("Found {} tests".format(len(TEST_FILES)))
68+
for test in TEST_FILES:
69+
TOTAL_TESTS += 1
70+
if run_test(test):
71+
PASSED_TESTS += 1
72+
else:
73+
FAILED_TESTS += 1
74+
FAILED_LIST.append(test)
75+
print("")
76+
77+
# Summary
78+
print("{}Test Summary:{}".format(YELLOW, NC))
79+
print("Total tests: {}".format(TOTAL_TESTS))
80+
print("{}Passed: {}{}".format(GREEN, PASSED_TESTS, NC))
81+
print("{}Failed: {}{}".format(RED, FAILED_TESTS, NC))
82+
83+
# List failed tests if any
84+
if FAILED_TESTS > 0:
85+
print("{}Failed tests:{}".format(RED, NC))
86+
for failed in FAILED_LIST:
87+
print("{} - {}{}".format(RED, failed, NC))
88+
89+
# Exit with failure if any test failed
90+
if FAILED_TESTS > 0:
91+
sys.exit(1)
92+
else:
93+
print("{}All tests passed! Package works.{}".format(GREEN, NC))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/protobuf-test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# test_protobuf_basic.py
22

3+
import os
4+
import sys
35
import unittest
6+
7+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
48
from person_pb2 import Person
59

610

@@ -34,7 +38,9 @@ def test_clear_field(self):
3438
def test_unknown_field_ignored(self):
3539
# Protocol Buffers ignore unknown fields during deserialization.
3640
serialized = self.person.SerializeToString()
37-
corrupted = serialized + b"\x20\x01" # Append unknown field (field number 4, varint 1)
41+
corrupted = (
42+
serialized + b"\x20\x01"
43+
) # Append unknown field (field number 4, varint 1)
3844
new_person = Person()
3945
new_person.ParseFromString(corrupted)
4046
self.assertEqual(new_person.name, "Alice")
@@ -51,5 +57,5 @@ def test_repr_str(self):
5157
self.assertIn("id: 123", repr_str)
5258

5359

54-
if __name__ == '__main__':
55-
unittest.main()
60+
if __name__ == "__main__":
61+
unittest.main()
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)