From 1653f569c09d0208b8e34695ddbe411be62ca05c Mon Sep 17 00:00:00 2001 From: Oscar Costoya Vidal Date: Fri, 20 Feb 2026 19:05:44 +0100 Subject: [PATCH] Fix bundled default HTML template resolution --- .../Reporters/HTML/HTMLSnapshotReporter.swift | 33 ++++++++++++------- .../SnapshotReportCoreTests.swift | 8 +++-- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Sources/SnapshotReportCore/Reporters/HTML/HTMLSnapshotReporter.swift b/Sources/SnapshotReportCore/Reporters/HTML/HTMLSnapshotReporter.swift index 4e73288..ca147d3 100644 --- a/Sources/SnapshotReportCore/Reporters/HTML/HTMLSnapshotReporter.swift +++ b/Sources/SnapshotReportCore/Reporters/HTML/HTMLSnapshotReporter.swift @@ -108,6 +108,10 @@ struct HTMLRenderer { return try String(contentsOfFile: customTemplatePath, encoding: .utf8) } + if let moduleTemplateURL = Bundle.module.url(forResource: "default-report", withExtension: "stencil") { + return try String(contentsOf: moduleTemplateURL, encoding: .utf8) + } + for candidate in Self.defaultTemplateCandidateURLs() { guard fileManager.fileExists(atPath: candidate.path) else { continue } return try String(contentsOf: candidate, encoding: .utf8) @@ -128,7 +132,10 @@ struct HTMLRenderer { executablePath: String = CommandLine.arguments.first ?? "" ) -> [URL] { let bundleName = "SnapshotReportKit_SnapshotReportCore.bundle" - let templateRelativePath = "Contents/Resources/default-report.stencil" + let templateRelativePaths = [ + "default-report.stencil", + "Contents/Resources/default-report.stencil", + ] var candidateDirectories: [URL] = [] if executablePath.isEmpty == false { @@ -150,17 +157,19 @@ struct HTMLRenderer { var candidates: [URL] = [] for directory in uniqueDirectories { - let bundleInExecutableDirectory = directory - .appendingPathComponent(bundleName, isDirectory: true) - .appendingPathComponent(templateRelativePath) - candidates.append(bundleInExecutableDirectory) - - let bundleInLibexecDirectory = directory - .appendingPathComponent("..", isDirectory: true) - .appendingPathComponent("libexec", isDirectory: true) - .appendingPathComponent(bundleName, isDirectory: true) - .appendingPathComponent(templateRelativePath) - candidates.append(bundleInLibexecDirectory) + for templateRelativePath in templateRelativePaths { + let bundleInExecutableDirectory = directory + .appendingPathComponent(bundleName, isDirectory: true) + .appendingPathComponent(templateRelativePath) + candidates.append(bundleInExecutableDirectory) + + let bundleInLibexecDirectory = directory + .appendingPathComponent("..", isDirectory: true) + .appendingPathComponent("libexec", isDirectory: true) + .appendingPathComponent(bundleName, isDirectory: true) + .appendingPathComponent(templateRelativePath) + candidates.append(bundleInLibexecDirectory) + } } let sourceTemplate = URL(fileURLWithPath: #filePath) diff --git a/Tests/SnapshotReportCoreTests/SnapshotReportCoreTests.swift b/Tests/SnapshotReportCoreTests/SnapshotReportCoreTests.swift index e577166..afdf68e 100644 --- a/Tests/SnapshotReportCoreTests/SnapshotReportCoreTests.swift +++ b/Tests/SnapshotReportCoreTests/SnapshotReportCoreTests.swift @@ -374,11 +374,15 @@ func htmlRendererTemplateCandidatesIncludeResolvedSymlinkPath() throws { try FileManager.default.createSymbolicLink(atPath: linkedExecutable.path, withDestinationPath: cellarExecutable.path) let candidates = HTMLRenderer.defaultTemplateCandidateURLs(executablePath: linkedExecutable.path).map(\.path) - let expected = cellarBin + let expectedSwiftPMLayout = cellarBin + .appendingPathComponent("SnapshotReportKit_SnapshotReportCore.bundle/default-report.stencil") + .path + let expectedCFBundleLayout = cellarBin .appendingPathComponent("SnapshotReportKit_SnapshotReportCore.bundle/Contents/Resources/default-report.stencil") .path - #expect(candidates.contains(expected)) + #expect(candidates.contains(expectedSwiftPMLayout)) + #expect(candidates.contains(expectedCFBundleLayout)) } @Test