Skip to content

snapshot.py

append_packages_dir_to_sys_path(packages_dir)

Append the packages directory to sys.path.

This allows us to import the example packages at test time. Cleanup is performed after use to ensure a clean state for each test.

Parameters:

Name Type Description Default
packages_dir Path

The path to the packages directory.

required

capture_module(file_path, caplog, capsys)

Capture the output, logging, errors, and typing feedback from running a Python module.

Parameters:

Name Type Description Default
file_path Path

The path to the Python file to run.

required
caplog LogCaptureFixture

The pytest caplog fixture for capturing logs.

required
capsys CaptureFixture

The pytest capsys fixture for capturing stdout/stderr.

required

Returns:

Type Description
str

The normalized captured output as a string.

compare(captured, expected)

Return a unified diff between captured and expected strings.

Parameters:

Name Type Description Default
captured str

The actual captured output.

required
expected str

The expected output.

required

Returns:

Type Description
str

A unified diff string showing the differences.

compare_resources_against_snapshots(file_path, snapshot_path, caplog, capsys)

Compare the output of a resource file against its snapshot, updating the snapshot if different.

Parameters:

Name Type Description Default
file_path Path

The path to the resource file to test.

required
snapshot_path Path

The path to the snapshot file to compare against.

required
caplog LogCaptureFixture

The pytest caplog fixture for capturing logs.

required
capsys CaptureFixture

The pytest capsys fixture for capturing stdout/stderr.

required

Returns:

Type Description
str | None

A unified diff string if the outputs differ, otherwise None.

make_output_invariant(output)

Normalize output to be invariant to UUIDs, object hashes, and OS-specific paths.

Parameters:

Name Type Description Default
output str

The captured output string to normalize.

required

Returns:

Type Description
str

The normalized output string.

replace_object_hashes(text)

Replace object hashes (e.g., 0x103308890) in the text with sequential placeholders.

Parameters:

Name Type Description Default
text str

The input string to process.

required

Returns:

Type Description
str

The text with object hashes replaced by HASH1, HASH2, etc.

replace_uuid4(text)

Replace UUID4 strings in the text with sequential placeholders.

Parameters:

Name Type Description Default
text str

The input string to process.

required

Returns:

Type Description
str

The text with UUID4 strings replaced by ID1, ID2, etc.

run_module(file_path)

Dynamically import and run a Python module from a file path.

Parameters:

Name Type Description Default
file_path Path

The path to the Python file to import and run.

required

Returns:

Type Description
str | None

None if the module runs successfully, otherwise a string describing

str | None

the exception.