Skip to content

UI test target support #975

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ jobs:
strategy:
matrix:
xcode:
- 15.4
- '16.1'
- '16.2'

name: macOS
runs-on: macos-14
runs-on: macos-15
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Select Xcode ${{ matrix.xcode }}
run: sudo xcode-select -s /Applications/Xcode_${{ matrix.xcode }}.app
- name: Run tests
Expand All @@ -29,7 +28,7 @@ jobs:
strategy:
matrix:
swift:
- '5.9'
- '6.1'

container: swift:${{ matrix.swift }}
name: Linux (Swift ${{ matrix.swift }})
Expand All @@ -42,7 +41,7 @@ jobs:
# strategy:
# matrix:
# swift:
# - "5.9.1"
# - "6.1"

# name: Windows (Swift ${{ matrix.swift }})
# runs-on: windows-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ playground.xcworkspace
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
Package.resolved
.build/
.swiftpm

Expand Down
2 changes: 2 additions & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ builder:
configs:
- documentation_targets:
# First item in the list is the "landing" (default) target
- SnapshotTestingCore
- SnapshotTesting
- InlineSnapshotTesting
#- SnapshotUITesting
- platform: watchos
scheme: InlineSnapshotTesting
32 changes: 0 additions & 32 deletions Package.resolved

This file was deleted.

29 changes: 28 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,41 @@ let package = Package(
name: "SnapshotTesting",
targets: ["SnapshotTesting"]
),
.library(
name: "SnapshotTestingCore",
targets: ["SnapshotTestingCore"]
),
.library(
name: "InlineSnapshotTesting",
targets: ["InlineSnapshotTesting"]
),
.library(
name: "SnapshotUITesting",
targets: ["SnapshotUITesting"]
),
.library(
name: "SnapshotTestingCustomDump",
targets: ["SnapshotTestingCustomDump"]
),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", branch: "test-traits"),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"602.0.0"),
],
targets: [
.target(
name: "SnapshotTesting"
name: "SnapshotTestingCore",
dependencies: [
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
.product(name: "IssueReportingTestSupport", package: "xctest-dynamic-overlay"),
]
),
.target(
name: "SnapshotTesting",
dependencies: [
"SnapshotTestingCore",
]
),
.testTarget(
name: "SnapshotTestingTests",
Expand All @@ -47,6 +66,8 @@ let package = Package(
dependencies: [
"SnapshotTesting",
"SnapshotTestingCustomDump",
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
.product(name: "IssueReportingTestSupport", package: "xctest-dynamic-overlay"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
Expand All @@ -65,5 +86,11 @@ let package = Package(
.product(name: "CustomDump", package: "swift-custom-dump"),
]
),
.target(
name: "SnapshotUITesting",
dependencies: [
"SnapshotTestingCore",
]
),
]
)
29 changes: 28 additions & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,41 @@ let package = Package(
name: "SnapshotTesting",
targets: ["SnapshotTesting"]
),
.library(
name: "SnapshotTestingCore",
targets: ["SnapshotTestingCore"]
),
.library(
name: "InlineSnapshotTesting",
targets: ["InlineSnapshotTesting"]
),
.library(
name: "SnapshotUITesting",
targets: ["SnapshotUITesting"]
),
.library(
name: "SnapshotTestingCustomDump",
targets: ["SnapshotTestingCustomDump"]
),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.3.3"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", branch: "test-traits"),
.package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"602.0.0"),
],
targets: [
.target(
name: "SnapshotTesting"
name: "SnapshotTestingCore",
dependencies: [
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
.product(name: "IssueReportingTestSupport", package: "xctest-dynamic-overlay"),
]
),
.target(
name: "SnapshotTesting",
dependencies: [
"SnapshotTestingCore",
]
),
.testTarget(
name: "SnapshotTestingTests",
Expand All @@ -47,6 +66,8 @@ let package = Package(
dependencies: [
"SnapshotTesting",
"SnapshotTestingCustomDump",
.product(name: "IssueReporting", package: "xctest-dynamic-overlay"),
.product(name: "IssueReportingTestSupport", package: "xctest-dynamic-overlay"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
Expand All @@ -65,6 +86,12 @@ let package = Package(
.product(name: "CustomDump", package: "swift-custom-dump"),
]
),
.target(
name: "SnapshotUITesting",
dependencies: [
"SnapshotTestingCore",
]
),
],
swiftLanguageModes: [.v5]
)
54 changes: 19 additions & 35 deletions Sources/InlineSnapshotTesting/AssertInlineSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation

#if canImport(SwiftSyntax509)
@_spi(Internals) import SnapshotTesting
import IssueReporting
import SwiftParser
import SwiftSyntax
import SwiftSyntaxBuilder
Expand Down Expand Up @@ -49,7 +50,7 @@ import Foundation
) {
let record = record ?? SnapshotTestingConfiguration.current?.record ?? _record
withSnapshotTesting(record: record) {
let _: Void = installTestObserver
let _: Void = installAtExitHook
do {
var actual: String?
let expectation = XCTestExpectation()
Expand All @@ -62,7 +63,7 @@ import Foundation
case .completed:
break
case .timedOut:
recordIssue(
reportIssue(
"""
Exceeded timeout of \(timeout) seconds waiting for snapshot.

Expand All @@ -77,7 +78,7 @@ import Foundation
)
return
case .incorrectOrder, .interrupted, .invertedFulfillment:
recordIssue(
reportIssue(
"Couldn't snapshot value",
fileID: fileID,
filePath: filePath,
Expand All @@ -86,7 +87,7 @@ import Foundation
)
return
@unknown default:
recordIssue(
reportIssue(
"Couldn't snapshot value",
fileID: fileID,
filePath: filePath,
Expand Down Expand Up @@ -130,7 +131,7 @@ import Foundation
if let difference = snapshotting.diffing.diff(expected ?? "", actual ?? "")?.0 {
failure += " Difference: …\n\n\(difference.indenting(by: 2))"
}
recordIssue(
reportIssue(
"""
\(failure)

Expand All @@ -146,7 +147,7 @@ import Foundation

guard let expected
else {
recordIssue(
reportIssue(
"""
No expected value to assert against.
""",
Expand Down Expand Up @@ -181,7 +182,7 @@ import Foundation
column: column
)
} catch {
recordIssue(
reportIssue(
"Threw error: \(error)",
fileID: fileID,
filePath: filePath,
Expand Down Expand Up @@ -305,7 +306,7 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
visitor.walk(testSource.sourceFile)
trailingClosureLine = visitor.trailingClosureLine
}
recordIssue(
reportIssue(
message(),
fileID: fileID,
filePath: filePath,
Expand Down Expand Up @@ -334,31 +335,12 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
// MARK: - Private

#if canImport(SwiftSyntax509)
private let installTestObserver: Void = {
final class InlineSnapshotObserver: NSObject, XCTestObservation {
func testBundleDidFinish(_ testBundle: Bundle) {
writeInlineSnapshots()
}
}
DispatchQueue.mainSync {
XCTestObservationCenter.shared.addTestObserver(InlineSnapshotObserver())
private let installAtExitHook: Void = {
atexit {
writeInlineSnapshots()
}
}()

extension DispatchQueue {
private static let key = DispatchSpecificKey<UInt8>()
private static let value: UInt8 = 0

fileprivate static func mainSync<R>(execute block: () -> R) -> R {
Self.main.setSpecific(key: key, value: value)
if getSpecific(key: key) == value {
return block()
} else {
return main.sync(execute: block)
}
}
}

@_spi(Internals) public struct File: Hashable {
public let path: StaticString
public static func == (lhs: Self, rhs: Self) -> Bool {
Expand Down Expand Up @@ -488,7 +470,8 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
.prefix(while: { $0 == " " || $0 == "\t" })
)
let delimiter = String(
repeating: "#", count: (snapshot.actual ?? "").hashCount(isMultiline: true)
repeating: "#",
count: (snapshot.actual ?? "").hashCount(isMultiline: true)
)
let leadingIndent = leadingTrivia + self.indent
let snapshotLabel = TokenSyntax(
Expand Down Expand Up @@ -566,7 +549,7 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
functionCallExpr.trailingClosure = nil
}
} else {
fatalError()
fatalError("Inline snapshot visitor visited unexpected node")
}

case 1...:
Expand All @@ -575,7 +558,8 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
MultipleTrailingClosureElementSyntax(
label: snapshotLabel,
closure: snapshotClosure.with(
\.leadingTrivia, snapshotClosure.leadingTrivia + .space
\.leadingTrivia,
snapshotClosure.leadingTrivia + .space
)
)
}
Expand Down Expand Up @@ -624,11 +608,11 @@ public struct InlineSnapshotSyntaxDescriptor: Hashable {
}
functionCallExpr.additionalTrailingClosures.append(newElement)
} else {
fatalError()
fatalError("Inline snapshot visitor visited unexpected node")
}

default:
fatalError()
fatalError("Inline snapshot visitor visited unexpected node")
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/SnapshotTesting/Exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@_exported import SnapshotTestingCore

Loading