Skip to content

Commit

Permalink
Convert most WorkspaceTests to Swift Testing
Browse files Browse the repository at this point in the history
Convert most of the WorkspaceTests from XCTests to Swift Testing to make
use of parallelism and, in some cases, test parameterization.

Not all Test Suites in WorkspaceTests have been converted as some use
helpers in swift-tools-core-support, which don't have a matching Swift
Testing helper.
  • Loading branch information
bkhouri committed Nov 1, 2024
1 parent 0b569a1 commit 6d94d63
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 243 deletions.
70 changes: 42 additions & 28 deletions Tests/WorkspaceTests/AuthorizationProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation

@testable import Basics
import _InternalTestSupport
import Workspace
import XCTest
import Testing

final class AuthorizationProviderTests: XCTestCase {
func testNetrcAuthorizationProviders() throws {
struct AuthorizationProviderTests {
@Test
func netrcAuthorizationProviders() throws {
let observability = ObservabilitySystem.makeForTesting()

// custom .netrc file
Expand All @@ -34,17 +36,21 @@ final class AuthorizationProviderTests: XCTestCase {
let authorizationProvider = try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? CompositeAuthorizationProvider
let netrcProviders = authorizationProvider?.providers.compactMap { $0 as? NetrcAuthorizationProvider }

XCTAssertEqual(netrcProviders?.count, 1)
XCTAssertEqual(try netrcProviders?.first.map { try resolveSymlinks($0.path) }, try resolveSymlinks(customPath))
let expectedNetrcProvider = try resolveSymlinks(customPath)
#expect(netrcProviders?.count == 1)
#expect(try netrcProviders?.first.map { try resolveSymlinks($0.path) } == expectedNetrcProvider)

let auth = authorizationProvider?.authentication(for: "https://mymachine.labkey.org")
XCTAssertEqual(auth?.user, "custom@labkey.org")
XCTAssertEqual(auth?.password, "custom")
#expect(auth?.user == "custom@labkey.org")
#expect(auth?.password == "custom")

// delete it
try fileSystem.removeFileTree(customPath)
XCTAssertThrowsError(try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope), "error expected") { error in
XCTAssertEqual(error as? StringError, StringError("Did not find netrc file at \(customPath)."))
#expect {
try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope)
} throws: { error in
let error = try #require(error as? StringError, "Error is wrong type")
return error == StringError("Did not find netrc file at \(customPath).")
}
}

Expand All @@ -63,23 +69,25 @@ final class AuthorizationProviderTests: XCTestCase {
let authorizationProvider = try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? CompositeAuthorizationProvider
let netrcProviders = authorizationProvider?.providers.compactMap { $0 as? NetrcAuthorizationProvider }

XCTAssertEqual(netrcProviders?.count, 1)
XCTAssertEqual(try netrcProviders?.first.map { try resolveSymlinks($0.path) }, try resolveSymlinks(userPath))
let expectedNetrcProvider = try resolveSymlinks(userPath)
#expect(netrcProviders?.count == 1)
#expect(try netrcProviders?.first.map { try resolveSymlinks($0.path) } == expectedNetrcProvider)

let auth = authorizationProvider?.authentication(for: "https://mymachine.labkey.org")
XCTAssertEqual(auth?.user, "user@labkey.org")
XCTAssertEqual(auth?.password, "user")
#expect(auth?.user == "user@labkey.org")
#expect(auth?.password == "user")

// delete it
do {
try fileSystem.removeFileTree(userPath)
let authorizationProvider = try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? CompositeAuthorizationProvider
XCTAssertNil(authorizationProvider)
#expect(authorizationProvider == nil)
}
}
}

func testRegistryNetrcAuthorizationProviders() throws {
@Test
func registryNetrcAuthorizationProviders() throws {
let observability = ObservabilitySystem.makeForTesting()

// custom .netrc file
Expand All @@ -97,17 +105,21 @@ final class AuthorizationProviderTests: XCTestCase {
let configuration = Workspace.Configuration.Authorization(netrc: .custom(customPath), keychain: .disabled)
let netrcProvider = try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? NetrcAuthorizationProvider

XCTAssertNotNil(netrcProvider)
XCTAssertEqual(try netrcProvider.map { try resolveSymlinks($0.path) }, try resolveSymlinks(customPath))
let expectedNetrcProvider = try resolveSymlinks(customPath)
#expect(netrcProvider != nil)
#expect(try netrcProvider.map { try resolveSymlinks($0.path) } == expectedNetrcProvider)

let auth = netrcProvider?.authentication(for: "https://mymachine.labkey.org")
XCTAssertEqual(auth?.user, "custom@labkey.org")
XCTAssertEqual(auth?.password, "custom")
#expect(auth?.user == "custom@labkey.org")
#expect(auth?.password == "custom")

// delete it
try fileSystem.removeFileTree(customPath)
XCTAssertThrowsError(try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope), "error expected") { error in
XCTAssertEqual(error as? StringError, StringError("did not find netrc file at \(customPath)"))
#expect {
try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope)
} throws: { error in
let error = try #require(error as? StringError, "Error is wrong type")
return error == StringError("did not find netrc file at \(customPath)")
}
}

Expand All @@ -126,22 +138,24 @@ final class AuthorizationProviderTests: XCTestCase {
let configuration = Workspace.Configuration.Authorization(netrc: .user, keychain: .disabled)
let netrcProvider = try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? NetrcAuthorizationProvider

XCTAssertNotNil(netrcProvider)
XCTAssertEqual(try netrcProvider.map { try resolveSymlinks($0.path) }, try resolveSymlinks(userPath))
let expectedNetrcProvider = try resolveSymlinks(userPath)
#expect(netrcProvider != nil)
#expect(try netrcProvider.map { try resolveSymlinks($0.path) } == expectedNetrcProvider)

let auth = netrcProvider?.authentication(for: "https://mymachine.labkey.org")
XCTAssertEqual(auth?.user, "user@labkey.org")
XCTAssertEqual(auth?.password, "user")
#expect(auth?.user == "user@labkey.org")
#expect(auth?.password == "user")

// delete it
do {
try fileSystem.removeFileTree(userPath)
let authorizationProvider = try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? NetrcAuthorizationProvider
// Even if user .netrc file doesn't exist, the provider will be non-nil but contain no data.
XCTAssertNotNil(authorizationProvider)
XCTAssertEqual(try authorizationProvider.map { try resolveSymlinks($0.path) }, try resolveSymlinks(userPath))
let expectedAuthorizationProvider = try resolveSymlinks(userPath)
#expect(authorizationProvider != nil)
#expect(try authorizationProvider.map { try resolveSymlinks($0.path) } == expectedAuthorizationProvider)

XCTAssertTrue(authorizationProvider!.machines.isEmpty)
#expect(authorizationProvider!.machines.isEmpty)
}
}
}
Expand Down
62 changes: 36 additions & 26 deletions Tests/WorkspaceTests/MirrorsConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import Basics
import _InternalTestSupport
import Workspace
import XCTest
import Testing

final class MirrorsConfigurationTests: XCTestCase {
func testLoadingSchema1() throws {
struct MirrorsConfigurationTests {
@Test
func loadingSchema1() throws {
let fs = InMemoryFileSystem()
let configFile = AbsolutePath("/config/mirrors.json")

Expand All @@ -42,70 +43,79 @@ final class MirrorsConfigurationTests: XCTestCase {
let config = Workspace.Configuration.MirrorsStorage(path: configFile, fileSystem: fs, deleteWhenEmpty: true)
let mirrors = try config.get()

XCTAssertEqual(mirrors.mirror(for: originalURL),mirrorURL)
XCTAssertEqual(mirrors.original(for: mirrorURL), originalURL)
#expect(mirrors.mirror(for: originalURL) == mirrorURL)
#expect(mirrors.original(for: mirrorURL) == originalURL)
}

func testThrowsWhenNotFound() throws {
@Test
func throwsWhenNotFound() throws {
let gitUrl = "https://github.com/apple/swift-argument-parser.git"
let fs = InMemoryFileSystem()
let configFile = AbsolutePath("/config/mirrors.json")

let config = Workspace.Configuration.MirrorsStorage(path: configFile, fileSystem: fs, deleteWhenEmpty: true)
let mirrors = try config.get()

XCTAssertThrows(StringError("Mirror not found for 'https://github.com/apple/swift-argument-parser.git'")) {
try mirrors.unset(originalOrMirror: "https://github.com/apple/swift-argument-parser.git")
#expect {
try mirrors.unset(originalOrMirror: gitUrl)
} throws: { error in
let error = try #require(error as? StringError)
return error == StringError("Mirror not found for '\(gitUrl)'")
}

}

func testDeleteWhenEmpty() throws {
@Test
func deleteWhenEmpty() throws {
let fs = InMemoryFileSystem()
let configFile = AbsolutePath("/config/mirrors.json")

let config = Workspace.Configuration.MirrorsStorage(path: configFile, fileSystem: fs, deleteWhenEmpty: true)

try config.apply{ _ in }
XCTAssertFalse(fs.exists(configFile))
#expect(!fs.exists(configFile))

let originalURL = "https://github.com/apple/swift-argument-parser.git"
let mirrorURL = "https://github.com/mona/swift-argument-parser.git"

try config.apply{ mirrors in
try mirrors.set(mirror: mirrorURL, for: originalURL)
}
XCTAssertTrue(fs.exists(configFile))
#expect(fs.exists(configFile))

try config.apply{ mirrors in
try mirrors.unset(originalOrMirror: originalURL)
}
XCTAssertFalse(fs.exists(configFile))
#expect(!fs.exists(configFile))
}

func testDontDeleteWhenEmpty() throws {
@Test
func dontDeleteWhenEmpty() throws {
let fs = InMemoryFileSystem()
let configFile = AbsolutePath("/config/mirrors.json")

let config = Workspace.Configuration.MirrorsStorage(path: configFile, fileSystem: fs, deleteWhenEmpty: false)

try config.apply{ _ in }
XCTAssertFalse(fs.exists(configFile))
#expect(!fs.exists(configFile))

let originalURL = "https://github.com/apple/swift-argument-parser.git"
let mirrorURL = "https://github.com/mona/swift-argument-parser.git"

try config.apply{ mirrors in
try mirrors.set(mirror: mirrorURL, for: originalURL)
}
XCTAssertTrue(fs.exists(configFile))
#expect(fs.exists(configFile))

try config.apply{ mirrors in
try mirrors.unset(originalOrMirror: originalURL)
}
XCTAssertTrue(fs.exists(configFile))
XCTAssertTrue(try config.get().isEmpty)
#expect(fs.exists(configFile))
#expect(try config.get().isEmpty)
}

func testLocalAndShared() throws {
@Test
func localAndShared() throws {
let fs = InMemoryFileSystem()
let localConfigFile = AbsolutePath("/config/local-mirrors.json")
let sharedConfigFile = AbsolutePath("/config/shared-mirrors.json")
Expand All @@ -125,9 +135,9 @@ final class MirrorsConfigurationTests: XCTestCase {
try mirrors.set(mirror: mirror1URL, for: original1URL)
}

XCTAssertEqual(config.mirrors.count, 1)
XCTAssertEqual(config.mirrors.mirror(for: original1URL), mirror1URL)
XCTAssertEqual(config.mirrors.original(for: mirror1URL), original1URL)
#expect(config.mirrors.count == 1)
#expect(config.mirrors.mirror(for: original1URL) == mirror1URL)
#expect(config.mirrors.original(for: mirror1URL) == original1URL)

// now write to local location

Expand All @@ -138,12 +148,12 @@ final class MirrorsConfigurationTests: XCTestCase {
try mirrors.set(mirror: mirror2URL, for: original2URL)
}

XCTAssertEqual(config.mirrors.count, 1)
XCTAssertEqual(config.mirrors.mirror(for: original2URL), mirror2URL)
XCTAssertEqual(config.mirrors.original(for: mirror2URL), original2URL)
#expect(config.mirrors.count == 1)
#expect(config.mirrors.mirror(for: original2URL) == mirror2URL)
#expect(config.mirrors.original(for: mirror2URL) == original2URL)

// should not see the shared any longer
XCTAssertEqual(config.mirrors.mirror(for: original1URL), nil)
XCTAssertEqual(config.mirrors.original(for: mirror1URL), nil)
#expect(config.mirrors.mirror(for: original1URL) == nil)
#expect(config.mirrors.original(for: mirror1URL) == nil)
}
}
43 changes: 26 additions & 17 deletions Tests/WorkspaceTests/ToolsVersionSpecificationGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,45 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation

///
/// This file tests the generation of a Swift tools version specification from a known version.
///

import XCTest
import Testing
import PackageModel

import struct TSCUtility.Version

/// Test cases for the generation of Swift tools version specifications.
class ToolsVersionSpecificationGenerationTests: XCTestCase {
struct ToolsVersionSpecificationGenerationTests {
/// Tests the generation of Swift tools version specifications.
func testToolsVersionSpecificationGeneration() throws {
@Test
func toolsVersionSpecificationGeneration() throws {
let versionWithNonZeroPatch = ToolsVersion(version: Version(4, 3, 2))
XCTAssertEqual(versionWithNonZeroPatch.specification(), "// swift-tools-version:4.3.2")
XCTAssertEqual(versionWithNonZeroPatch.specification(roundedTo: .automatic), "// swift-tools-version:4.3.2")
XCTAssertEqual(versionWithNonZeroPatch.specification(roundedTo: .minor), "// swift-tools-version:4.3")
XCTAssertEqual(versionWithNonZeroPatch.specification(roundedTo: .patch), "// swift-tools-version:4.3.2")
#expect(versionWithNonZeroPatch.specification() == "// swift-tools-version:4.3.2")
#expect(versionWithNonZeroPatch.specification(roundedTo: .automatic) == "// swift-tools-version:4.3.2")
#expect(versionWithNonZeroPatch.specification(roundedTo: .minor) == "// swift-tools-version:4.3")
#expect(versionWithNonZeroPatch.specification(roundedTo: .patch) == "// swift-tools-version:4.3.2")

let versionWithZeroPatch = ToolsVersion.v5_3 // 5.3.0
XCTAssertEqual(versionWithZeroPatch.specification(), "// swift-tools-version:5.3")
XCTAssertEqual(versionWithZeroPatch.specification(roundedTo: .automatic), "// swift-tools-version:5.3")
XCTAssertEqual(versionWithZeroPatch.specification(roundedTo: .minor), "// swift-tools-version:5.3")
XCTAssertEqual(versionWithZeroPatch.specification(roundedTo: .patch), "// swift-tools-version:5.3.0")
#expect(versionWithZeroPatch.specification() == "// swift-tools-version:5.3")
#expect(versionWithZeroPatch.specification(roundedTo: .automatic) == "// swift-tools-version:5.3")
#expect(versionWithZeroPatch.specification(roundedTo: .minor) == "// swift-tools-version:5.3")
#expect(versionWithZeroPatch.specification(roundedTo: .patch) == "// swift-tools-version:5.3.0")

let newMajorVersion = ToolsVersion.v5 // 5.0.0
XCTAssertEqual(newMajorVersion.specification(), "// swift-tools-version:5.0")
XCTAssertEqual(newMajorVersion.specification(roundedTo: .automatic), "// swift-tools-version:5.0")
XCTAssertEqual(newMajorVersion.specification(roundedTo: .minor), "// swift-tools-version:5.0")
XCTAssertEqual(newMajorVersion.specification(roundedTo: .patch), "// swift-tools-version:5.0.0")
#expect(newMajorVersion.specification() == "// swift-tools-version:5.0")
#expect(newMajorVersion.specification(roundedTo: .automatic) == "// swift-tools-version:5.0")
#expect(newMajorVersion.specification(roundedTo: .minor) == "// swift-tools-version:5.0")
#expect(newMajorVersion.specification(roundedTo: .patch) == "// swift-tools-version:5.0.0")

let allZeroVersion = ToolsVersion(version: Version(0, 0, 0))
#expect(allZeroVersion.specification() == "// swift-tools-version:0.0")
#expect(allZeroVersion.specification(roundedTo: .automatic) == "// swift-tools-version:0.0")
#expect(allZeroVersion.specification(roundedTo: .minor) == "// swift-tools-version:0.0")
#expect(allZeroVersion.specification(roundedTo: .patch) == "// swift-tools-version:0.0.0")
}

}
Loading

0 comments on commit 6d94d63

Please sign in to comment.