Skip to content

Golden master pattern broken in several tests due to incorrect order and teardown reset #779

Closed

Description

@opt12

Description

The golden master pattern is a testing technique where the output of a
function is written to a reference file ("golden master") on the first run.
On subsequent runs the output is compared to this reference file. If the
output changes, the test fails and the reference file is updated, making
the change visible in git diff for deliberate review and approval.

This pattern was not working correctly in doorstop due to two separate
issues.

Issue 1: Assert before write

In some tests the assertion was performed before the reference file was
updated, so the golden master file was never updated on a failing test:

# Broken: reference file is never updated when assertion fails
self.assertEqual(expected, text)
common.write_text(text, path)  # ← never reached on failure

The correct order is to write the reference file first, then assert:

# Correct: reference file is always updated, then compared
if text != expected:
    common.log.error(f"Published content changed: {path}")
    common.write_text(text, path)  # ← always executed
self.assertEqual(expected, text)

Issue 2: Teardown reset of golden master files

The test teardown fixture cleanup_test_yml_files and reset_fixture_files
reset all Git-tracked YAML files after each test, including golden master
reference files such as exported.yml. This undid any intentional updates
made by the golden master pattern, so changes to the reference file were
lost before they could be reviewed in git diff.

Expected behavior

  • The golden master reference files should always be updated with the
    current output before the assertion is performed.
  • Golden master files should not be reset by the teardown fixtures, so
    that intentional changes are visible in git diff and can be reviewed
    and committed deliberately.

Fix

  • Correct the order of write_text and assertEqual in affected tests.
  • Introduce a GOLDEN_MASTER_FILES set in doorstop/core/tests/__init__.py
    and exclude these files from the teardown reset in both
    cleanup_test_yml_files and reset_fixture_files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions