Skip to content

[GenerateDocCReference] Snapshot recording for snapshot comparison is not working properly #755

Closed
@Chamepp

Description

@Chamepp

The snapshot comparison feature allows us to record and update the expected output of the generated documentation, making it easier to track changes and verify modifications over time. This can be done by setting the record parameter to true when it comes to testing.

The assertSnapshot method do the following steps:

  1. Saves a snapshot of the actual output for future comparisons if record is true.
  2. Reads the existing snapshot file and compares it with the actual output.
  3. Allows associating snapshots with specific file paths, test functions and extensions (e.g. .md and .json).
  4. Handles directory and file creation if missing.

Checklist

  • If possible, I've reproduced the issue using the main branch of this package
  • I've searched for existing GitHub issues

Steps to Reproduce

  1. Modify the way current documentation structure is generated
  2. Add and set the record parameter to true
  3. Run swift test
  4. Remove the record parameter
  5. Run swift test again
  6. Tests fail due to the fact that there is an additional line in the snapshot comparison diff.

Expected behavior

  1. The documentation generation output is modified by the developer
  2. The snapshot recording is set to true in the testing stage
  3. We run swift test and tests fail for the first time
  4. Now the snapshot output is recorded
  5. We run swift test again, the outputs match and the tests pass

Actual behavior

The feature follows the steps mentioned earlier, expect tests fail this time due to the fact that recorded snapshot and the generated document output are different. After checking the diff result provided by the snapshot when we run swift test, we can see that the only difference is the addition of an extra line at the end of the file.

The reason behind this extra line added at the end of the file could be these:

1. Formatting Consistency & Standards (POSIX Compliance)
According to the POSIX standard a line is defined as a sequence of characters ending with a newline (\n). Without it the final line isnt considered a proper line. This may cause subtle issues in file parsing and formatting tools.

2. Improved Readability in Editors & Diffs
Text editors like VSCode, Sublime Text and diff tools often show warnings such as No newline at end of file if a newline is missing. Adding it ensures the snapshot files render cleanly and predictably, avoiding those warnings and improving visual clarity when reviewing changes.

3. Avoiding Version Control Confusion
Tools like Git rely on exact byte comparisons and a missing final newline can cause unnecessary diffs. Including a newline avoids misleading diffs. This reduces noise in version control history and improves collaboration.

4. Prevents Trimming & Merge Conflicts
As noted in this Medium article, a missing newline can lead to unexpected merge conflicts, line joining or trimming behavior in shell utilities, compilers and file processing tools. Including it ensures compatibility and clean merges.

This feature was first introduced here. When I check the commit history, I see that the snapshot files were recorded before this PR and do not include a trailing newline. The snapshot recordings haven’t been updated after this PR.

Possible solutions and scenarios

Option 1: Triming trailing newlines during the comparison and document generation step to avoid mismatches.
Option 2: Normalizing the formatting by ensuring all snapshot files and generate docs output include a trailing newline.
Option 3: Removing the newline addition (actual + "\n") when recording or comparing snapshots.

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