diff --git a/Makefile b/Makefile index cc3aafd..bc5ab42 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,10 @@ test: test-e2e: python tools/test-e2e.py ocrs-cli/test-data/ +.PHONY: update-e2e +update-e2e: + python tools/test-e2e.py --update ocrs-cli/test-data/ + .PHONY: wasm wasm: RUSTFLAGS="-C target-feature=+simd128" cargo build --release --target wasm32-unknown-unknown --package ocrs diff --git a/tools/test-e2e.py b/tools/test-e2e.py index ca1becf..ca00d75 100755 --- a/tools/test-e2e.py +++ b/tools/test-e2e.py @@ -29,13 +29,17 @@ def extract_text(image_path: str) -> str: IMAGE_PAT = "\\.(jpeg|jpg|png|webp)$" -def run_tests(test_case_dir: str, *, verbose=False) -> bool: +def run_tests(test_case_dir: str, *, verbose=False, update_baselines=False) -> bool: """ Compare extracted text for image files against expectations. Each image file in `test_case_dir` is expected to have an accompanying "{image_name}.expected.txt" file. + If `update_baselines` is true, mismatches between the actual and expected + results will result in the expected results being updated. When this flag + is set, the tests will still succeed if there is a mismatch. + Returns True if all test cases passed. """ image_filenames = [ @@ -61,15 +65,20 @@ def run_tests(test_case_dir: str, *, verbose=False) -> bool: text = text.strip() if text != expected_text: - errors += 1 + if update_baselines: + with open(expected_path, 'w') as fp: + fp.write(text) + print(f"Updated baseline for {fname}") + else: + errors += 1 - print(f"Actual vs expected mismatch for {fname}") + print(f"Actual vs expected mismatch for {fname}") - if verbose: - print("Actual:") - print(textwrap.indent(text, " ")) - print("Expected:") - print(textwrap.indent(expected_text, " ")) + if verbose: + print("Actual:") + print(textwrap.indent(text, " ")) + print("Expected:") + print(textwrap.indent(expected_text, " ")) if errors != 0: print(f"{errors} tests failed") @@ -89,11 +98,14 @@ def run_tests(test_case_dir: str, *, verbose=False) -> bool: parser.add_argument( "-v", "--verbose", action=BooleanOptionalAction, help="Enable verbose logging" ) +parser.add_argument( + "-u", "--update", action=BooleanOptionalAction, help="Update baselines" +) args = parser.parse_args() print("Building ocrs...") build_ocrs() -passed = run_tests(args.dir, verbose=args.verbose) +passed = run_tests(args.dir, verbose=args.verbose, update_baselines=args.update) if not passed: sys.exit(1)