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 update-e2e make target to update E2E baselines #124

Merged
merged 1 commit into from
Oct 2, 2024
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
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)
Loading