Skip to content

Commit a9e09ef

Browse files
authored
Test: Convert more tests to Swift Testing (#8100)
1 parent e8aff4b commit a9e09ef

File tree

8 files changed

+559
-461
lines changed

8 files changed

+559
-461
lines changed

Sources/_InternalTestSupport/misc.swift

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ import enum TSCUtility.Git
4242
public let isInCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil
4343
public let isSelfHostedCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_IS_SELF_HOSTED"] != nil
4444

45+
public let isRealSigningIdentyEcLabelEnvVarSet =
46+
ProcessInfo.processInfo.environment["REAL_SIGNING_IDENTITY_EC_LABEL"] != nil
47+
48+
public let isRealSigningIdentitTestDefined = {
49+
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
50+
return true
51+
#else
52+
return false
53+
#endif
54+
}()
55+
4556
/// Test helper utility for executing a block with a temporary directory.
4657
public func testWithTemporaryDirectory(
4758
function: StaticString = #function,

Tests/PackageSigningTests/FilePackageSigningEntityStorageTests.swift

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

Tests/PackageSigningTests/SigningEntityTests.swift

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

1516
import Basics
1617
@testable import PackageSigning
1718
import _InternalTestSupport
1819
import X509
1920

20-
final class SigningEntityTests: XCTestCase {
21-
func testTwoADPSigningEntitiesAreEqualIfTeamIDEqual() {
21+
struct SigningEntityTests {
22+
@Test
23+
func twoADPSigningEntitiesAreEqualIfTeamIDEqual() {
2224
let adp1 = SigningEntity.recognized(
2325
type: .adp,
2426
name: "A. Appleseed",
@@ -37,48 +39,39 @@ final class SigningEntityTests: XCTestCase {
3739
organizationalUnit: "SwiftPM Test Unit Y",
3840
organization: "C"
3941
)
40-
XCTAssertEqual(adp1, adp2) // Only team ID (org unit) needs to match
41-
XCTAssertNotEqual(adp1, adp3)
42+
#expect(adp1 == adp2) // Only team ID (org unit) needs to match
43+
#expect(adp1 != adp3)
4244
}
4345

44-
func testFromECKeyCertificate() throws {
46+
@Test(
47+
"From certificate key",
48+
arguments: [
49+
(certificateFilename: "Test_ec.cer", id: "EC Key"),
50+
(certificateFilename: "Test_rsa.cer", id: "RSA Key")
51+
]
52+
)
53+
func fromCertificate(certificateFilename: String, id: String) throws {
4554
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
4655
let certificateBytes = try readFileContents(
4756
in: fixturePath,
4857
pathComponents: "Certificates",
49-
"Test_ec.cer"
58+
certificateFilename
5059
)
5160
let certificate = try Certificate(certificateBytes)
5261

5362
let signingEntity = SigningEntity.from(certificate: certificate)
5463
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
55-
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
64+
Issue.record("Expected SigningEntity.unrecognized but got \(signingEntity)")
65+
return
5666
}
57-
XCTAssertEqual(name, certificate.subject.commonName)
58-
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
59-
XCTAssertEqual(organization, certificate.subject.organizationName)
60-
}
61-
}
62-
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)
71-
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)
67+
#expect(name == certificate.subject.commonName)
68+
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
69+
#expect(organization == certificate.subject.organizationName)
7970
}
8071
}
72+
}
8173

74+
final class SigningEntityXCTests: XCTestCase {
8275
#if os(macOS)
8376
func testFromKeychainCertificate() async throws {
8477
#if ENABLE_REAL_SIGNING_IDENTITY_TEST

Tests/PackageSigningTests/SigningIdentityTests.swift

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

14+
import Testing
1315
import XCTest
1416

1517
import _CryptoExtras // For RSA
@@ -19,8 +21,9 @@ import Crypto
1921
import _InternalTestSupport
2022
import X509
2123

22-
final class SigningIdentityTests: XCTestCase {
23-
func testSwiftSigningIdentityWithECKey() throws {
24+
struct SigningIdentityTests {
25+
@Test
26+
func swiftSigningIdentityWithECKey() throws {
2427
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
2528
let certificateBytes = try readFileContents(
2629
in: fixturePath,
@@ -30,9 +33,9 @@ final class SigningIdentityTests: XCTestCase {
3033
let certificate = try Certificate(certificateBytes)
3134

3235
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)
36+
#expect("Test (EC) leaf" == subject.commonName)
37+
#expect("Test (EC) org unit" == subject.organizationalUnitName)
38+
#expect("Test (EC) org" == subject.organizationName)
3639

3740
let privateKeyBytes = try readFileContents(
3841
in: fixturePath,
@@ -43,17 +46,19 @@ final class SigningIdentityTests: XCTestCase {
4346
_ = SwiftSigningIdentity(certificate: certificate, privateKey: Certificate.PrivateKey(privateKey))
4447

4548
// Test public API
46-
XCTAssertNoThrow(
49+
#expect(throws: Never.self) {
50+
4751
try SwiftSigningIdentity(
4852
derEncodedCertificate: certificateBytes,
4953
derEncodedPrivateKey: privateKeyBytes,
5054
privateKeyType: .p256
5155
)
52-
)
56+
}
5357
}
5458
}
5559

56-
func testSwiftSigningIdentityWithRSAKey() throws {
60+
@Test
61+
func swiftSigningIdentityWithRSAKey() throws {
5762
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
5863
let certificateBytes = try readFileContents(
5964
in: fixturePath,
@@ -63,9 +68,9 @@ final class SigningIdentityTests: XCTestCase {
6368
let certificate = try Certificate(certificateBytes)
6469

6570
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)
71+
#expect("Test (RSA) leaf" == subject.commonName)
72+
#expect("Test (RSA) org unit" == subject.organizationalUnitName)
73+
#expect("Test (RSA) org" == subject.organizationName)
6974

7075
let privateKeyBytes = try readFileContents(
7176
in: fixturePath,
@@ -76,6 +81,8 @@ final class SigningIdentityTests: XCTestCase {
7681
_ = SwiftSigningIdentity(certificate: certificate, privateKey: Certificate.PrivateKey(privateKey))
7782
}
7883
}
84+
}
85+
final class SigningIdentityXCTests: XCTestCase {
7986

8087
#if os(macOS)
8188
func testSigningIdentityFromKeychain() async throws {

0 commit comments

Comments
 (0)