Skip to content

Commit 930ee2e

Browse files
committed
Tests: Convert more tests to Swift Testing
Convert additional test from XCTest to Swift Testing to make use of parallel execution and, in some cases, test parameterization. Not all Test Suite in the respective folders have been converted as some use helpers in swift-tools-core-support, which don't have a matching swift testing helper.
1 parent afa76d9 commit 930ee2e

7 files changed

+577
-484
lines changed

Tests/PackageSigningTests/FilePackageSigningEntityStorageTests.swift

+157-91
Large diffs are not rendered by default.

Tests/PackageSigningTests/SigningEntityTests.swift

+35-50
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
13-
import XCTest
12+
import Foundation
13+
import Testing
1414

1515
import Basics
1616
@testable import PackageSigning
1717
import _InternalTestSupport
1818
import X509
1919

20-
final class SigningEntityTests: XCTestCase {
21-
func testTwoADPSigningEntitiesAreEqualIfTeamIDEqual() {
20+
struct SigningEntityTests {
21+
@Test
22+
func twoADPSigningEntitiesAreEqualIfTeamIDEqual() {
2223
let adp1 = SigningEntity.recognized(
2324
type: .adp,
2425
name: "A. Appleseed",
@@ -37,74 +38,58 @@ final class SigningEntityTests: XCTestCase {
3738
organizationalUnit: "SwiftPM Test Unit Y",
3839
organization: "C"
3940
)
40-
XCTAssertEqual(adp1, adp2) // Only team ID (org unit) needs to match
41-
XCTAssertNotEqual(adp1, adp3)
41+
#expect(adp1 == adp2) // Only team ID (org unit) needs to match
42+
#expect(adp1 != adp3)
4243
}
4344

44-
func testFromECKeyCertificate() throws {
45+
@Test(
46+
"From certificate key",
47+
arguments: [
48+
(certificateFilename: "Test_ec.cer", id: "EC Key"),
49+
(certificateFilename: "Test_rsa.cer", id: "RSA Key")
50+
]
51+
)
52+
func fromCertificate(certificateFilename: String, id: String) throws {
4553
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
4654
let certificateBytes = try readFileContents(
4755
in: fixturePath,
4856
pathComponents: "Certificates",
49-
"Test_ec.cer"
57+
certificateFilename
5058
)
5159
let certificate = try Certificate(certificateBytes)
5260

5361
let signingEntity = SigningEntity.from(certificate: certificate)
5462
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
55-
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
63+
Issue.record("Expected SigningEntity.unrecognized but got \(signingEntity)")
64+
return
5665
}
57-
XCTAssertEqual(name, certificate.subject.commonName)
58-
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
59-
XCTAssertEqual(organization, certificate.subject.organizationName)
66+
#expect(name == certificate.subject.commonName)
67+
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
68+
#expect(organization == certificate.subject.organizationName)
6069
}
6170
}
6271

63-
func testFromRSAKeyCertificate() throws {
64-
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
65-
let certificateBytes = try readFileContents(
66-
in: fixturePath,
67-
pathComponents: "Certificates",
68-
"Test_rsa.cer"
69-
)
70-
let certificate = try Certificate(certificateBytes)
72+
@Test(
73+
.enabled(if: isMacOS() && isRealSigningIdentityTestEnabled() && isEnvironmentVariableSet("REAL_SIGNING_IDENTITY_LABEL"))
74+
)
75+
func fromKeychainCertificate() async throws {
76+
let label = try #require(Environment.current["REAL_SIGNING_IDENTITY_LABEL"])
7177

72-
let signingEntity = SigningEntity.from(certificate: certificate)
73-
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
74-
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
75-
}
76-
XCTAssertEqual(name, certificate.subject.commonName)
77-
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
78-
XCTAssertEqual(organization, certificate.subject.organizationName)
79-
}
80-
}
81-
82-
#if os(macOS)
83-
func testFromKeychainCertificate() async throws {
84-
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
85-
#else
86-
try XCTSkipIf(true)
87-
#endif
88-
89-
guard let label = Environment.current["REAL_SIGNING_IDENTITY_LABEL"] else {
90-
throw XCTSkip("Skipping because 'REAL_SIGNING_IDENTITY_LABEL' env var is not set")
91-
}
9278
let identityStore = SigningIdentityStore(observabilityScope: ObservabilitySystem.NOOP)
9379
let matches = identityStore.find(by: label)
94-
XCTAssertTrue(!matches.isEmpty)
80+
#expect(!matches.isEmpty)
9581

9682
let certificate = try Certificate(secIdentity: matches[0] as! SecIdentity)
9783
let signingEntity = SigningEntity.from(certificate: certificate)
9884
switch signingEntity {
99-
case .recognized(_, let name, let organizationalUnit, let organization):
100-
XCTAssertEqual(name, certificate.subject.commonName)
101-
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
102-
XCTAssertEqual(organization, certificate.subject.organizationName)
103-
case .unrecognized(let name, let organizationalUnit, let organization):
104-
XCTAssertEqual(name, certificate.subject.commonName)
105-
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
106-
XCTAssertEqual(organization, certificate.subject.organizationName)
85+
case .recognized(_, let name, let organizationalUnit, let organization):
86+
#expect(name == certificate.subject.commonName)
87+
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
88+
#expect(organization == certificate.subject.organizationName)
89+
case .unrecognized(let name, let organizationalUnit, let organization):
90+
#expect(name == certificate.subject.commonName)
91+
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
92+
#expect(organization == certificate.subject.organizationName)
10793
}
10894
}
109-
#endif
11095
}

Tests/PackageSigningTests/SigningIdentityTests.swift

+24-26
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import Foundation
1213

13-
import XCTest
14+
import Testing
1415

1516
import _CryptoExtras // For RSA
1617
import Basics
@@ -19,8 +20,9 @@ import Crypto
1920
import _InternalTestSupport
2021
import X509
2122

22-
final class SigningIdentityTests: XCTestCase {
23-
func testSwiftSigningIdentityWithECKey() throws {
23+
struct SigningIdentityTests {
24+
@Test
25+
func swiftSigningIdentityWithECKey() throws {
2426
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
2527
let certificateBytes = try readFileContents(
2628
in: fixturePath,
@@ -30,9 +32,9 @@ final class SigningIdentityTests: XCTestCase {
3032
let certificate = try Certificate(certificateBytes)
3133

3234
let subject = certificate.subject
33-
XCTAssertEqual("Test (EC) leaf", subject.commonName)
34-
XCTAssertEqual("Test (EC) org unit", subject.organizationalUnitName)
35-
XCTAssertEqual("Test (EC) org", subject.organizationName)
35+
#expect("Test (EC) leaf" == subject.commonName)
36+
#expect("Test (EC) org unit" == subject.organizationalUnitName)
37+
#expect("Test (EC) org" == subject.organizationName)
3638

3739
let privateKeyBytes = try readFileContents(
3840
in: fixturePath,
@@ -43,17 +45,19 @@ final class SigningIdentityTests: XCTestCase {
4345
_ = SwiftSigningIdentity(certificate: certificate, privateKey: Certificate.PrivateKey(privateKey))
4446

4547
// Test public API
46-
XCTAssertNoThrow(
48+
#expect(throws: Never.self) {
49+
4750
try SwiftSigningIdentity(
4851
derEncodedCertificate: certificateBytes,
4952
derEncodedPrivateKey: privateKeyBytes,
5053
privateKeyType: .p256
5154
)
52-
)
55+
}
5356
}
5457
}
5558

56-
func testSwiftSigningIdentityWithRSAKey() throws {
59+
@Test
60+
func swiftSigningIdentityWithRSAKey() throws {
5761
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
5862
let certificateBytes = try readFileContents(
5963
in: fixturePath,
@@ -63,9 +67,9 @@ final class SigningIdentityTests: XCTestCase {
6367
let certificate = try Certificate(certificateBytes)
6468

6569
let subject = certificate.subject
66-
XCTAssertEqual("Test (RSA) leaf", subject.commonName)
67-
XCTAssertEqual("Test (RSA) org unit", subject.organizationalUnitName)
68-
XCTAssertEqual("Test (RSA) org", subject.organizationName)
70+
#expect("Test (RSA) leaf" == subject.commonName)
71+
#expect("Test (RSA) org unit" == subject.organizationalUnitName)
72+
#expect("Test (RSA) org" == subject.organizationName)
6973

7074
let privateKeyBytes = try readFileContents(
7175
in: fixturePath,
@@ -77,24 +81,18 @@ final class SigningIdentityTests: XCTestCase {
7781
}
7882
}
7983

80-
#if os(macOS)
84+
@Test(
85+
.enabled(if: isMacOS() && isRealSigningIdentityTestEnabled() && isEnvironmentVariableSet("REAL_SIGNING_IDENTITY_LABEL"))
86+
)
8187
func testSigningIdentityFromKeychain() async throws {
82-
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
83-
#else
84-
try XCTSkipIf(true)
85-
#endif
86-
87-
guard let label = Environment.current["REAL_SIGNING_IDENTITY_LABEL"] else {
88-
throw XCTSkip("Skipping because 'REAL_SIGNING_IDENTITY_LABEL' env var is not set")
89-
}
88+
let label = try #require(Environment.current["REAL_SIGNING_IDENTITY_LABEL"])
9089
let identityStore = SigningIdentityStore(observabilityScope: ObservabilitySystem.NOOP)
9190
let matches = identityStore.find(by: label)
92-
XCTAssertTrue(!matches.isEmpty)
91+
#expect(!matches.isEmpty)
9392

9493
let subject = try Certificate(secIdentity: matches[0] as! SecIdentity).subject
95-
XCTAssertNotNil(subject.commonName)
96-
XCTAssertNotNil(subject.organizationalUnitName)
97-
XCTAssertNotNil(subject.organizationName)
94+
#expect(subject.commonName != nil)
95+
#expect(subject.organizationalUnitName != nil)
96+
#expect(subject.organizationName != nil)
9897
}
99-
#endif
10098
}

0 commit comments

Comments
 (0)