Skip to content

Test: Convert more tests to Swift Testing #8100

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

Merged
Merged
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
11 changes: 11 additions & 0 deletions Sources/_InternalTestSupport/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ import enum TSCUtility.Git
public let isInCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil
public let isSelfHostedCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_IS_SELF_HOSTED"] != nil

public let isRealSigningIdentyEcLabelEnvVarSet =
ProcessInfo.processInfo.environment["REAL_SIGNING_IDENTITY_EC_LABEL"] != nil

public let isRealSigningIdentitTestDefined = {
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
return true
#else
return false
#endif
}()

/// Test helper utility for executing a block with a temporary directory.
public func testWithTemporaryDirectory(
function: StaticString = #function,
Expand Down
204 changes: 113 additions & 91 deletions Tests/PackageSigningTests/FilePackageSigningEntityStorageTests.swift

Large diffs are not rendered by default.

53 changes: 23 additions & 30 deletions Tests/PackageSigningTests/SigningEntityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import Testing
import XCTest

import Basics
@testable import PackageSigning
import _InternalTestSupport
import X509

final class SigningEntityTests: XCTestCase {
func testTwoADPSigningEntitiesAreEqualIfTeamIDEqual() {
struct SigningEntityTests {
@Test
func twoADPSigningEntitiesAreEqualIfTeamIDEqual() {
let adp1 = SigningEntity.recognized(
type: .adp,
name: "A. Appleseed",
Expand All @@ -37,48 +39,39 @@ final class SigningEntityTests: XCTestCase {
organizationalUnit: "SwiftPM Test Unit Y",
organization: "C"
)
XCTAssertEqual(adp1, adp2) // Only team ID (org unit) needs to match
XCTAssertNotEqual(adp1, adp3)
#expect(adp1 == adp2) // Only team ID (org unit) needs to match
#expect(adp1 != adp3)
}

func testFromECKeyCertificate() throws {
@Test(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: I'm liking the parameterized test cases and how they make the matrix visible and explicit.

"From certificate key",
arguments: [
(certificateFilename: "Test_ec.cer", id: "EC Key"),
(certificateFilename: "Test_rsa.cer", id: "RSA Key")
]
)
func fromCertificate(certificateFilename: String, id: String) throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
pathComponents: "Certificates",
"Test_ec.cer"
certificateFilename
)
let certificate = try Certificate(certificateBytes)

let signingEntity = SigningEntity.from(certificate: certificate)
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
Issue.record("Expected SigningEntity.unrecognized but got \(signingEntity)")
return
}
XCTAssertEqual(name, certificate.subject.commonName)
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
XCTAssertEqual(organization, certificate.subject.organizationName)
}
}

func testFromRSAKeyCertificate() throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
pathComponents: "Certificates",
"Test_rsa.cer"
)
let certificate = try Certificate(certificateBytes)

let signingEntity = SigningEntity.from(certificate: certificate)
guard case .unrecognized(let name, let organizationalUnit, let organization) = signingEntity else {
return XCTFail("Expected SigningEntity.unrecognized but got \(signingEntity)")
}
XCTAssertEqual(name, certificate.subject.commonName)
XCTAssertEqual(organizationalUnit, certificate.subject.organizationalUnitName)
XCTAssertEqual(organization, certificate.subject.organizationName)
#expect(name == certificate.subject.commonName)
#expect(organizationalUnit == certificate.subject.organizationalUnitName)
#expect(organization == certificate.subject.organizationName)
}
}
}

final class SigningEntityXCTests: XCTestCase {
#if os(macOS)
func testFromKeychainCertificate() async throws {
#if ENABLE_REAL_SIGNING_IDENTITY_TEST
Expand Down
29 changes: 18 additions & 11 deletions Tests/PackageSigningTests/SigningIdentityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation

import Testing
import XCTest

import _CryptoExtras // For RSA
Expand All @@ -19,8 +21,9 @@ import Crypto
import _InternalTestSupport
import X509

final class SigningIdentityTests: XCTestCase {
func testSwiftSigningIdentityWithECKey() throws {
struct SigningIdentityTests {
@Test
func swiftSigningIdentityWithECKey() throws {
try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
Expand All @@ -30,9 +33,9 @@ final class SigningIdentityTests: XCTestCase {
let certificate = try Certificate(certificateBytes)

let subject = certificate.subject
XCTAssertEqual("Test (EC) leaf", subject.commonName)
XCTAssertEqual("Test (EC) org unit", subject.organizationalUnitName)
XCTAssertEqual("Test (EC) org", subject.organizationName)
#expect("Test (EC) leaf" == subject.commonName)
#expect("Test (EC) org unit" == subject.organizationalUnitName)
#expect("Test (EC) org" == subject.organizationName)

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

// Test public API
XCTAssertNoThrow(
#expect(throws: Never.self) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just throw.


try SwiftSigningIdentity(
derEncodedCertificate: certificateBytes,
derEncodedPrivateKey: privateKeyBytes,
privateKeyType: .p256
)
)
}
}
}

func testSwiftSigningIdentityWithRSAKey() throws {
@Test
func swiftSigningIdentityWithRSAKey() throws {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: This is a nice way to cut back on the test method name sizes.

try fixture(name: "Signing", createGitRepo: false) { fixturePath in
let certificateBytes = try readFileContents(
in: fixturePath,
Expand All @@ -63,9 +68,9 @@ final class SigningIdentityTests: XCTestCase {
let certificate = try Certificate(certificateBytes)

let subject = certificate.subject
XCTAssertEqual("Test (RSA) leaf", subject.commonName)
XCTAssertEqual("Test (RSA) org unit", subject.organizationalUnitName)
XCTAssertEqual("Test (RSA) org", subject.organizationName)
#expect("Test (RSA) leaf" == subject.commonName)
#expect("Test (RSA) org unit" == subject.organizationalUnitName)
#expect("Test (RSA) org" == subject.organizationName)

let privateKeyBytes = try readFileContents(
in: fixturePath,
Expand All @@ -76,6 +81,8 @@ final class SigningIdentityTests: XCTestCase {
_ = SwiftSigningIdentity(certificate: certificate, privateKey: Certificate.PrivateKey(privateKey))
}
}
}
final class SigningIdentityXCTests: XCTestCase {

#if os(macOS)
func testSigningIdentityFromKeychain() async throws {
Expand Down
Loading