Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
strategy:
matrix:
swift:
- '6.0'
- '6.2'
name: Ubuntu (Swift ${{ matrix.swift }})
runs-on: ubuntu-latest
container: swift:${{ matrix.swift }}
Expand Down
22 changes: 20 additions & 2 deletions Sources/DependenciesTestSupport/TestTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@_documentation(visibility: private)
public struct _DependenciesTrait: TestScoping, TestTrait, SuiteTrait {
let updateValues: @Sendable (inout DependencyValues) async throws -> Void
var tearDown: @Sendable (DependencyValues) async throws -> Void = { _ in }

@TaskLocal static var isRoot = true

Expand All @@ -24,6 +25,7 @@
} operation: {
try await Self.$isRoot.withValue(false) {
try await function()
try await tearDown(DependencyValues._current)
}
}
}
Expand Down Expand Up @@ -122,10 +124,26 @@
/// }
/// ```
///
/// An additional trailing closure can be provided that is executed after the test finishes
/// allow cleanup, such as closing resources:
///
/// ```swift
/// @Test(
/// .dependencies {
/// $0.defaultDatabase = Database()
/// } tearDown: {
/// $0.defaultDatabase.close()
/// }
/// )
/// func feature() {
/// // ...
/// }
/// ```
public static func dependencies(
_ updateValues: @escaping @Sendable (inout DependencyValues) async throws -> Void
_ updateValues: @escaping @Sendable (inout DependencyValues) async throws -> Void,
tearDown: @escaping @Sendable (DependencyValues) async throws -> Void = { _ in }
) -> Self {
Self(updateValues: updateValues)
Self(updateValues: updateValues, tearDown: tearDown)
}
}
#else
Expand Down
22 changes: 22 additions & 0 deletions Tests/DependenciesTests/TestTraitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,27 @@
try await Task.sleep(for: .milliseconds(1))
}
}

@Test(
.dependencies {
$0[TearDownDependency.self] = TearDownDependency()
} tearDown: {
$0[TearDownDependency.self].tearDown()
}
) func tearDown() {
}
}

final class TearDownDependency: @unchecked Sendable, TestDependencyKey {
var didExplicitlyTearDown = false
func tearDown() {
didExplicitlyTearDown = true
}
deinit {
#expect(didExplicitlyTearDown, "Did not explicitly tear down.")
}
static var testValue: TearDownDependency {
TearDownDependency()
}
}
#endif
Loading