Skip to content

Test traits #157

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 9 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
40 changes: 33 additions & 7 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
objects = {

/* Begin PBXBuildFile section */
CA1972D72DB1836900351823 /* TraitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA1972D62DB1836900351823 /* TraitTests.swift */; };
CA1972D92DB1847200351823 /* ExampleTrait.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA1972D82DB1847200351823 /* ExampleTrait.swift */; };
CADE79AE2C4A9D2C005A85F7 /* ExamplesApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = CADE79AD2C4A9D2C005A85F7 /* ExamplesApp.swift */; };
CADE79B22C4A9D2D005A85F7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CADE79B12C4A9D2D005A85F7 /* Assets.xcassets */; };
CADE79DA2C4A9D3A005A85F7 /* IssueReporting in Frameworks */ = {isa = PBXBuildFile; productRef = CADE79D92C4A9D3A005A85F7 /* IssueReporting */; };
CADE79DD2C4A9E59005A85F7 /* SwiftTestingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CADE79DB2C4A9E59005A85F7 /* SwiftTestingTests.swift */; };
CADE79DE2C4A9E59005A85F7 /* XCTestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = CADE79DC2C4A9E59005A85F7 /* XCTestTests.swift */; };
CADE79E02C4ABD94005A85F7 /* Examples.xctestplan in Resources */ = {isa = PBXBuildFile; fileRef = CADE79DF2C4ABD94005A85F7 /* Examples.xctestplan */; };
CADE79E22C4ABE90005A85F7 /* IssueReporting in Frameworks */ = {isa = PBXBuildFile; productRef = CADE79E12C4ABE90005A85F7 /* IssueReporting */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -26,7 +27,32 @@
};
/* End PBXContainerItemProxy section */

/* Begin PBXCopyFilesBuildPhase section */
CA1972DD2DB1854600351823 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
CA1972E12DB186BC00351823 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
CA1972D62DB1836900351823 /* TraitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TraitTests.swift; sourceTree = "<group>"; };
CA1972D82DB1847200351823 /* ExampleTrait.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleTrait.swift; sourceTree = "<group>"; };
CADE79AA2C4A9D2C005A85F7 /* Examples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Examples.app; sourceTree = BUILT_PRODUCTS_DIR; };
CADE79AD2C4A9D2C005A85F7 /* ExamplesApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExamplesApp.swift; sourceTree = "<group>"; };
CADE79B12C4A9D2D005A85F7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -50,7 +76,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
CADE79E22C4ABE90005A85F7 /* IssueReporting in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -80,6 +105,7 @@
isa = PBXGroup;
children = (
CADE79AD2C4A9D2C005A85F7 /* ExamplesApp.swift */,
CA1972D82DB1847200351823 /* ExampleTrait.swift */,
CADE79B12C4A9D2D005A85F7 /* Assets.xcassets */,
CADE79B32C4A9D2D005A85F7 /* Examples.entitlements */,
);
Expand All @@ -92,6 +118,7 @@
CADE79DF2C4ABD94005A85F7 /* Examples.xctestplan */,
CADE79DB2C4A9E59005A85F7 /* SwiftTestingTests.swift */,
CADE79DC2C4A9E59005A85F7 /* XCTestTests.swift */,
CA1972D62DB1836900351823 /* TraitTests.swift */,
);
path = ExamplesTests;
sourceTree = "<group>";
Expand All @@ -113,6 +140,7 @@
CADE79A62C4A9D2C005A85F7 /* Sources */,
CADE79A72C4A9D2C005A85F7 /* Frameworks */,
CADE79A82C4A9D2C005A85F7 /* Resources */,
CA1972E12DB186BC00351823 /* Embed Frameworks */,
);
buildRules = (
);
Expand All @@ -133,6 +161,7 @@
CADE79B72C4A9D2D005A85F7 /* Sources */,
CADE79B82C4A9D2D005A85F7 /* Frameworks */,
CADE79B92C4A9D2D005A85F7 /* Resources */,
CA1972DD2DB1854600351823 /* Embed Frameworks */,
);
buildRules = (
);
Expand All @@ -141,7 +170,6 @@
);
name = ExamplesTests;
packageProductDependencies = (
CADE79E12C4ABE90005A85F7 /* IssueReporting */,
);
productName = ExamplesTests;
productReference = CADE79BB2C4A9D2D005A85F7 /* ExamplesTests.xctest */;
Expand Down Expand Up @@ -210,6 +238,7 @@
buildActionMask = 2147483647;
files = (
CADE79AE2C4A9D2C005A85F7 /* ExamplesApp.swift in Sources */,
CA1972D92DB1847200351823 /* ExampleTrait.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -219,6 +248,7 @@
files = (
CADE79DD2C4A9E59005A85F7 /* SwiftTestingTests.swift in Sources */,
CADE79DE2C4A9E59005A85F7 /* XCTestTests.swift in Sources */,
CA1972D72DB1836900351823 /* TraitTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -504,10 +534,6 @@
isa = XCSwiftPackageProductDependency;
productName = IssueReporting;
};
CADE79E12C4ABE90005A85F7 /* IssueReporting */ = {
isa = XCSwiftPackageProductDependency;
productName = IssueReporting;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = CADE79A22C4A9D2C005A85F7 /* Project object */;
Expand Down
14 changes: 14 additions & 0 deletions Examples/Examples/ExampleTrait.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import IssueReporting

public struct ExampleTrait: Sendable {
public init() {}

public static func hasTrait() -> Bool {
switch TestContext.current {
case .none, .some(.xcTest):
return false
case .some(.swiftTesting(let testing)):
return testing?.test.traits.contains { $0 is Self } ?? false
}
}
}
21 changes: 21 additions & 0 deletions Examples/ExamplesTests/TraitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import IssueReporting
import Testing
import Examples

extension ExampleTrait: @retroactive SuiteTrait, @retroactive TestTrait {
}

@Suite
struct TraitTests {
@Test(ExampleTrait()) func hasTrait() {
#if DEBUG
#expect(ExampleTrait.hasTrait())
#else
#expect(ExampleTrait.hasTrait() == false)
#endif
}

@Test func doesNotHaveTrait() {
#expect(ExampleTrait.hasTrait() == false)
}
}
13 changes: 11 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ let package = Package(
],
targets: [
.target(
name: "IssueReporting"
name: "IssueReportingPackageSupport"
),
.target(
name: "IssueReporting",
dependencies: [
"IssueReportingPackageSupport"
]
),
.testTarget(
name: "IssueReportingTests",
Expand All @@ -32,7 +38,10 @@ let package = Package(
]
),
.target(
name: "IssueReportingTestSupport"
name: "IssueReportingTestSupport",
dependencies: [
"IssueReportingPackageSupport"
]
),
.target(
name: "XCTestDynamicOverlay",
Expand Down
13 changes: 11 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ let package = Package(
],
targets: [
.target(
name: "IssueReporting"
name: "IssueReportingPackageSupport"
),
.target(
name: "IssueReporting",
dependencies: [
"IssueReportingPackageSupport",
]
),
.testTarget(
name: "IssueReportingTests",
Expand All @@ -31,7 +37,10 @@ let package = Package(
]
),
.target(
name: "IssueReportingTestSupport"
name: "IssueReportingTestSupport",
dependencies: [
"IssueReportingPackageSupport",
]
),
.target(
name: "XCTestDynamicOverlay",
Expand Down
15 changes: 10 additions & 5 deletions Sources/IssueReporting/Internal/SwiftTesting.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import IssueReportingPackageSupport

#if canImport(WinSDK)
import WinSDK
Expand Down Expand Up @@ -313,17 +314,21 @@ func _withKnownIssue(
#endif

@usableFromInline
func _currentTestID() -> AnyHashable? {
guard let function = function(for: "$s25IssueReportingTestSupport08_currentC2IDypyF")
func _currentTest() -> _Test? {
guard let function = function(for: "$s25IssueReportingTestSupport08_currentC0ypyF")
else {
#if DEBUG
return Test.current?.id
return Test.current.map { _Test(id: $0.id, traits: $0.traits) }
#else
return nil
#endif
}

return (function as! @Sendable () -> AnyHashable?)()
return withUnsafePointer(to: function) {
$0.withMemoryRebound(to: (@Sendable () -> _Test?).self, capacity: 1) {
$0.pointee()
}
}
}

#if DEBUG
Expand Down Expand Up @@ -446,7 +451,7 @@ func _currentTestID() -> AnyHashable? {
struct Case {}
private var name: String
private var displayName: String?
private var traits: [any Trait]
fileprivate var traits: [any Trait]
private var sourceLocation: SourceLocation
private var containingTypeInfo: TypeInfo?
private var xcTestCompatibleSelector: __XCTestCompatibleSelector?
Expand Down
17 changes: 13 additions & 4 deletions Sources/IssueReporting/TestContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public enum TestContext: Equatable, Sendable {
/// If executed outside of a test process, this will return `nil`.
public static var current: Self? {
guard isTesting else { return nil }
if let currentTestID = _currentTestID() {
return .swiftTesting(Testing(id: currentTestID))
if let currentTest = _currentTest() {
return .swiftTesting(Testing(id: currentTest.id, traits: currentTest.traits))
} else {
return .xcTest
}
Expand All @@ -42,10 +42,19 @@ public enum TestContext: Equatable, Sendable {

public struct Test: Equatable, Hashable, Identifiable, Sendable {
public let id: ID
public let traits: [any Sendable]

public struct ID: Equatable, Hashable, @unchecked Sendable {
public let rawValue: AnyHashable
}

public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id
}

public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
}

Expand Down Expand Up @@ -73,7 +82,7 @@ public enum TestContext: Equatable, Sendable {
}

extension TestContext.Testing {
fileprivate init(id: AnyHashable) {
self.init(test: Test(id: Test.ID(rawValue: id)))
fileprivate init(id: AnyHashable, traits: [any Sendable]) {
self.init(test: Test(id: Test.ID(rawValue: id), traits: traits))
}
}
14 changes: 14 additions & 0 deletions Sources/IssueReportingPackageSupport/_Test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@usableFromInline
package struct _Test {
@usableFromInline
package let id: AnyHashable

@usableFromInline
package let traits: [any Sendable]

@usableFromInline
package init(id: AnyHashable, traits: [any Sendable]) {
self.id = id
self.traits = traits
}
}
8 changes: 5 additions & 3 deletions Sources/IssueReportingTestSupport/SwiftTesting.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import IssueReportingPackageSupport

#if canImport(Testing)
import Testing
#endif
Expand Down Expand Up @@ -134,11 +136,11 @@ private func __withKnownIssueAsync(
}
#endif

public func _currentTestID() -> Any { __currentTestID }
public func _currentTest() -> Any { __currentTest }
@Sendable
private func __currentTestID() -> AnyHashable? {
private func __currentTest() -> _Test? {
#if canImport(Testing)
return Test.current?.id
return Test.current.map { _Test(id: $0.id, traits: $0.traits) }
#else
return nil
#endif
Expand Down
Loading