Skip to content

Instantly share code, notes, and snippets.

@watura
Created August 16, 2022 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watura/a0f75805343f814bbad6c30f8a9d06d1 to your computer and use it in GitHub Desktop.
Save watura/a0f75805343f814bbad6c30f8a9d06d1 to your computer and use it in GitHub Desktop.
SnapshotTesting を Xcode Cloud でつかうと, snapshot が見つからない問題がでてくるので、対応する ref: https://github.com/pointfreeco/swift-snapshot-testing/discussions/553 ref: https://gist.github.com/qmoya/dc007b682b47e6d59f2c65705c067d81
// SnapshotTesting を Xcode Cloud でつかうと,
// snapshot が見つからない問題がでてくるので、対応する
// ref: https://github.com/pointfreeco/swift-snapshot-testing/discussions/553
// ref: https://gist.github.com/qmoya/dc007b682b47e6d59f2c65705c067d81
import UIKit
import SnapshotTesting
import XCTest
private let ArtifactsPath = "SnapshotTestArtifacts"
private let CIPath = "ci_scripts"
private func snapshotDirectory(file: StaticString = #file) -> String? {
// remove TestHelper/SnapshotHelper.swift from #file
let snapshotHelperPath = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.deletingLastPathComponent()
.path
let filePath: String
if let ciWorkspace = ProcessInfo.processInfo.environment["CI_WORKSPACE"], !ciWorkspace.isEmpty {
filePath = ciWorkspace
} else {
filePath = snapshotHelperPath
}
let sourceRoot = URL(fileURLWithPath: filePath,
isDirectory: true)
let fileUrl = URL(fileURLWithPath: "\(file)", isDirectory: false)
let fileName = fileUrl.deletingPathExtension().lastPathComponent
// remove FILENAME.swift and add __Snapshots__/FILENAME
let absoluteSourceTestPath = fileUrl
.deletingLastPathComponent()
.appendingPathComponent("__Snapshots__")
.appendingPathComponent(fileName)
var components = absoluteSourceTestPath.pathComponents
let sourceRootComponents = sourceRoot.pathComponents
for component in sourceRootComponents {
if components.first == component {
components = Array(components.dropFirst())
} else {
return nil
}
}
var snapshotDirectoryUrl = sourceRoot.appendingPathComponent(ArtifactsPath)
if ProcessInfo.processInfo.environment["CI"] == "TRUE" {
snapshotDirectoryUrl = sourceRoot
.appendingPathComponent(CIPath)
.appendingPathComponent(ArtifactsPath)
}
for component in components {
snapshotDirectoryUrl = snapshotDirectoryUrl.appendingPathComponent(component)
}
return snapshotDirectoryUrl.path
}
func assertSnapshot<Value, Format>(
matching value: @autoclosure () throws -> Value,
as snapshotting: Snapshotting<Value, Format>,
named name: String? = nil,
record recording: Bool = false,
timeout: TimeInterval = 5,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line
) {
let failure = verifySnapshot(
matching: try value(),
as: snapshotting,
named: name,
record: recording,
snapshotDirectory: snapshotDirectory(file: file),
timeout: timeout,
file: file,
testName: testName
)
guard let message = failure else { return }
XCTFail(message, file: file, line: line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment