Skip to content

Commit

Permalink
Add test dependency on CwlPreconditionTesting
Browse files Browse the repository at this point in the history
CwlPreconditionTesting allows us to unit test traps (i.e. fatalError,
assert, precondition). We currently don't have any way to test these in
the repo, so engineers simply don't test our assertions. This will
improve the overall state of our unit tests by encouraging testing of
more code paths.

Further reading on how CwlPreconditionTesting works: https://www.cocoawithlove.com/blog/2016/02/02/partial-functions-part-two-catching-precondition-failures.html
  • Loading branch information
sfriedman-stripe committed Apr 27, 2023
1 parent d74365e commit 17e4252
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions Tuist/ProjectDescriptionHelpers/Tuist+Stripe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extension Project {
public var includesSnapshots: Bool = false
public var usesMocks: Bool = false
public var usesStubs: Bool = false
public var usesPreconditionTesting: Bool = false

/// Creates a `TestOptions` instance.
///
Expand All @@ -28,6 +29,8 @@ extension Project {
/// If `true`, the OCMock package will be linked.
/// - usesStubs: Whether the tests in this target use stubs.
/// If `true`, the OHHTTPStubs package will be linked.
/// - usesPreconditionTesting: Whether the tests in this target uses precondition, assert, and/or fatal error testing.
/// If `true`, the CwlPreconditionTesting package will be linked.
/// - Returns: A `TestOptions` instance.
public static func testOptions(
resources: ResourceFileElements? = nil,
Expand All @@ -37,15 +40,17 @@ extension Project {
),
includesSnapshots: Bool = false,
usesMocks: Bool = false,
usesStubs: Bool = false
usesStubs: Bool = false,
usesPreconditionTesting: Bool = false
) -> TestOptions {
return TestOptions(
resources: resources,
dependencies: dependencies,
settings: settings,
includesSnapshots: includesSnapshots,
usesMocks: usesMocks,
usesStubs: usesStubs
usesStubs: usesStubs,
usesPreconditionTesting: usesPreconditionTesting
)
}
}
Expand Down Expand Up @@ -160,6 +165,7 @@ extension Project {
)
)
}

if testUtilsOptions?.usesMocks ?? false
|| unitTestOptions?.usesMocks ?? false
|| uiTestOptions?.usesMocks ?? false
Expand All @@ -168,6 +174,7 @@ extension Project {
.remote(url: "https://github.com/erikdoe/ocmock", requirement: .branch("master"))
)
}

if testUtilsOptions?.usesStubs ?? false
|| unitTestOptions?.usesStubs ?? false
|| uiTestOptions?.usesStubs ?? false
Expand All @@ -179,6 +186,19 @@ extension Project {
)
)
}

if testUtilsOptions?.usesPreconditionTesting ?? false
|| unitTestOptions?.usesPreconditionTesting ?? false
|| uiTestOptions?.usesPreconditionTesting ?? false
{
packages.append(
.remote(
url: "https://github.com/mattgallagher/CwlPreconditionTesting",
requirement: .upToNextMajor(from: "2.1.2")
)
)
}

return packages
}

Expand Down Expand Up @@ -301,6 +321,10 @@ extension Project {
.package(product: "OHHTTPStubsSwift"),
]
}
if testOptions.usesPreconditionTesting {
dependencies.append(.package(product: "CwlPreconditionTesting"))
}

return dependencies + testOptions.dependencies
}

Expand Down

0 comments on commit 17e4252

Please sign in to comment.