Skip to content

Commit

Permalink
Add update-e2e make target to update E2E baselines (#124)
Browse files Browse the repository at this point in the history
This makes it easy to update the expected outputs for E2E tests.
  • Loading branch information
robertknight authored Oct 2, 2024
1 parent 1317bb9 commit b8877c4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions tools/test-e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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")
Expand All @@ -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)

0 comments on commit b8877c4

Please sign in to comment.