Debian Codegraph @

passthrough/scripts/extract_test_cases.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 Extract test case markdown from passthrough_test.go for manual testing.
4
5 This script extracts the 'input' variable from each test function and generates
6 a markdown file containing all test cases. Each test case is shown twice:
7 once in a code block and once rendered, for easy manual verification in
8 markdown previewers.
9
10 Usage:
11 python3 extract_test_cases.py [input_file] [output_file]
12
13 Default:
14 python3 extract_test_cases.py
15 # Reads: passthrough/passthrough_test.go
16 # Writes: passthrough/test-cases.md
17 """
18
19 import re
20 import sys
21 from pathlib import Path
22
23
24 def extract_test_cases(input_file):
25 """Extract test cases from Go test file."""
26 with open(input_file, 'r', encoding='utf-8') as f:
27 content = f.read()
28
29 # Pattern to match test functions and their input variables
30 # Captures: test name, delimiter type, and content
31 # Group 1: Test name, Group 2: delimiter (` or "), Group 3: content
32 pattern = r'func (Test\w+)\(t \*testing\.T\) \{.*?input := ([`"])([^`"]*)\2'
33 matches = re.findall(pattern, content, re.DOTALL)
34
35 return matches
36
37
38 def generate_markdown(test_cases, output_file):
39 """Generate markdown file with all test cases."""
40 with open(output_file, 'w', encoding='utf-8') as f:
41 f.write("# Passthrough Extension Test Cases\n\n")
42 f.write("This file contains all test inputs from the passthrough test suite for manual testing.\n\n")
43
44 for test_name, delimiter, markdown_input in test_cases:
45 # Only unescape interpreted string literals (double-quoted)
46 # Raw string literals (backticked) should preserve backslashes verbatim
47 if delimiter == '"':
48 markdown_input = markdown_input.replace('\\n', '\n').replace('\\"', '"').replace('\\\\', '\\')
49
50 # Write the test section
51 f.write(f"## {test_name}\n\n")
52 f.write("```markdown\n")
53 f.write(markdown_input)
54 f.write("\n```\n\n")
55 f.write(markdown_input)
56 f.write("\n\n")
57
58 return len(test_cases)
59
60
61 def main():
62 # Parse command line arguments with defaults relative to script location
63 script_dir = Path(__file__).parent
64 passthrough_dir = script_dir.parent
65 default_input = passthrough_dir / 'passthrough_test.go'
66 default_output = passthrough_dir / 'test-cases.md'
67
68 input_file = sys.argv[1] if len(sys.argv) > 1 else str(default_input)
69 output_file = sys.argv[2] if len(sys.argv) > 2 else str(default_output)
70
71 # Verify input file exists
72 if not Path(input_file).exists():
73 print(f"Error: Input file '{input_file}' not found", file=sys.stderr)
74 sys.exit(1)
75
76 # Extract test cases
77 print(f"Extracting test cases from {input_file}...", file=sys.stderr)
78 test_cases = extract_test_cases(input_file)
79
80 if not test_cases:
81 print("Warning: No test cases found", file=sys.stderr)
82 sys.exit(1)
83
84 # Generate output
85 count = generate_markdown(test_cases, output_file)
86 print(f"Extracted {count} test cases to {output_file}", file=sys.stderr)
87
88 # Report file size
89 size = Path(output_file).stat().st_size
90 print(f"Output file size: {size:,} bytes", file=sys.stderr)
91
92
93 if __name__ == '__main__':
94 main()