-
Notifications
You must be signed in to change notification settings - Fork 134
Cross-backend identifier normalizer and baseline render capture (#391) #437
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // | ||
| // BaselineCaptureTests.swift | ||
| // | ||
| // Writes renders of every fixture to $XCHR_BASELINE_DIR. | ||
| // Fixtures are regenerated on each `prepareTestResults.sh` run, so a golden | ||
| // file cannot be checked in; capture before a refactor and again after, | ||
| // against one fixture generation, then diff the two directories. | ||
| // | ||
| // Renders are captured verbatim. Identifiers are deterministic per structural | ||
| // path (#430), so an identifier that moved is a real finding about the | ||
| // refactor, not noise to normalize away. | ||
| // | ||
|
|
||
| import XCTest | ||
| @testable import XCTestHTMLReportCore | ||
|
|
||
| final class BaselineCaptureTests: XCTestCase { | ||
| func testCaptureRawRenders() throws { | ||
| guard let dir = ProcessInfo.processInfo.environment["XCHR_BASELINE_DIR"] else { | ||
| throw XCTSkip("Set XCHR_BASELINE_DIR to capture baseline renders") | ||
| } | ||
| try FileManager.default.createDirectory( | ||
| atPath: dir, withIntermediateDirectories: true | ||
| ) | ||
|
|
||
| let resources = ["TestResults", "SanityResults", "RetryResults"] | ||
| for resource in resources { | ||
| // Deliberately not `continue`: a skipped fixture would produce a | ||
| // partial baseline, and Task 5a's `diff -r` reports two partial | ||
| // directories as identical. Fail here instead. | ||
| let url = try XCTUnwrap( | ||
| Bundle.testBundle.url(forResource: resource, withExtension: "xcresult"), | ||
| "Fixture \(resource).xcresult is missing — run ./prepareTestResults.sh" | ||
| ) | ||
| let html = Summary( | ||
| resultPaths: [url.path], | ||
| renderingMode: .linking, | ||
| downsizeImagesEnabled: false, | ||
| downsizeScaleFactor: 0.5 | ||
| ).generatedHtmlReport() | ||
| let path = "\(dir)/\(resource).html" | ||
| try html.write( | ||
| toFile: path, atomically: true, encoding: .utf8 | ||
| ) | ||
| let written = try XCTUnwrap( | ||
| FileManager.default.contents(atPath: path) | ||
| ) | ||
| XCTAssertFalse(written.isEmpty, "\(resource) captured an empty baseline") | ||
| } | ||
|
|
||
| let captured = try FileManager.default | ||
| .contentsOfDirectory(atPath: dir) | ||
| .filter { $0.hasSuffix(".html") } | ||
| XCTAssertEqual( | ||
| Set(captured), Set(resources.map { "\($0).html" }), | ||
| "Baseline must contain exactly one file per fixture" | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // | ||
| // ReportNormalizer.swift | ||
| // | ||
| // Report element identifiers are a digest of each element's structural path | ||
| // through the report (see `IdentifierPath`, #411/#430). Two renders on one | ||
| // backend therefore agree byte-for-byte and need no normalization. | ||
| // | ||
| // Two *backends* do not: the modern reader's tree omits the "All tests" and | ||
| // "<bundle>.xctest" wrapper levels, so the same test case sits at a different | ||
| // path under each backend and digests differently. The cross-backend | ||
| // differential normalizes those digests away; nothing else does. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// `IdentifierPath.identifier` is the first 128 bits of a SHA-256, lowercase hex. | ||
| /// Anchored with word boundaries so it cannot bite into a longer hex run. | ||
| private let identifierPattern: NSRegularExpression = { | ||
| guard let pattern = try? NSRegularExpression(pattern: "\\b[0-9a-f]{32}\\b") else { | ||
| preconditionFailure("The identifier pattern is a constant and must compile") | ||
| } | ||
| return pattern | ||
| }() | ||
|
|
||
| /// Replaces every `IdentifierPath` digest with the literal `ID`. | ||
| /// | ||
| /// Only safe on reports rendered in `.linking` mode. Inline rendering embeds | ||
| /// base64 payloads, where a 32-character run drawn from `[0-9a-f]` is possible; | ||
| /// the differential renders `.linking` for this reason. | ||
| func normalizeIdentifiers(_ html: String) -> String { | ||
| identifierPattern.stringByReplacingMatches( | ||
| in: html, | ||
| range: NSRange(html.startIndex..., in: html), | ||
| withTemplate: "ID" | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: XCTestHTMLReport/XCTestHTMLReport
Length of output: 13266
🏁 Script executed:
Repository: XCTestHTMLReport/XCTestHTMLReport
Length of output: 21795
Use hexadecimal boundaries for
IdentifierPathdigests —\bdoes not match the digest indevice_<digest>, so cross-backend normalization leaves rendered identifiers unchanged. Use(?<![0-9a-f])[0-9a-f]{32}(?![0-9a-f])in both the implementation and the copied plan example.📍 Affects 2 files
Tests/XCTestHTMLReportTests/ReportNormalizer.swift#L18-L23(this comment)docs/superpowers/plans/2026-08-10-xcresulttool-legacy-migration.md#L198-L208🤖 Prompt for AI Agents