Skip to content

Tests: Convert some WorkspaceTests module tests to Swift Testing #8092

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
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
84 changes: 48 additions & 36 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 {
fileprivate struct AuthorizationProviderTests {
@Test
func netrcAuthorizationProviders() throws {
let observability = ObservabilitySystem.makeForTesting()

// custom .netrc file
Expand All @@ -32,19 +34,20 @@ final class AuthorizationProviderTests: XCTestCase {

let configuration = Workspace.Configuration.Authorization(netrc: .custom(customPath), keychain: .disabled)
let authorizationProvider = try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? CompositeAuthorizationProvider
let netrcProviders = authorizationProvider?.providers.compactMap { $0 as? NetrcAuthorizationProvider }
let netrcProviders = try #require(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, "[email protected]")
XCTAssertEqual(auth?.password, "custom")
let auth = try #require(authorizationProvider?.authentication(for: "https://mymachine.labkey.org"))
#expect(auth.user == "[email protected]")
#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(throws: StringError("Did not find netrc file at \(customPath).")) {
try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope)
}
}

Expand All @@ -61,25 +64,27 @@ final class AuthorizationProviderTests: XCTestCase {

let configuration = Workspace.Configuration.Authorization(netrc: .user, keychain: .disabled)
let authorizationProvider = try configuration.makeAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? CompositeAuthorizationProvider
let netrcProviders = authorizationProvider?.providers.compactMap { $0 as? NetrcAuthorizationProvider }
let netrcProviders = try #require(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, "[email protected]")
XCTAssertEqual(auth?.password, "user")
let auth = try #require(authorizationProvider?.authentication(for: "https://mymachine.labkey.org"))
#expect(auth.user == "[email protected]")
#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 +102,18 @@ 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, "[email protected]")
XCTAssertEqual(auth?.password, "custom")
let auth = try #require(netrcProvider?.authentication(for: "https://mymachine.labkey.org"))
#expect(auth.user == "[email protected]")
#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(throws: StringError("did not find netrc file at \(customPath)")) {
try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope)
}
}

Expand All @@ -126,22 +132,28 @@ 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, "[email protected]")
XCTAssertEqual(auth?.password, "user")
let auth = try #require(netrcProvider?.authentication(for: "https://mymachine.labkey.org"))
#expect(auth.user == "[email protected]")
#expect(auth.password == "user")

// delete it
do {
try fileSystem.removeFileTree(userPath)
let authorizationProvider = try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope) as? NetrcAuthorizationProvider
let authorizationProviderOpt =
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))

XCTAssertTrue(authorizationProvider!.machines.isEmpty)
let expectedAuthorizationProvider = try resolveSymlinks(userPath)
let authorizationProvider: NetrcAuthorizationProvider = try #require(
authorizationProviderOpt)
#expect(authorizationProvider.path == expectedAuthorizationProvider)
#expect(authorizationProvider.machines.isEmpty)
}
}
}
Expand Down
58 changes: 32 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 {
fileprivate struct MirrorsConfigurationTests {
@Test
func loadingSchema1() throws {
let fs = InMemoryFileSystem()
let configFile = AbsolutePath("/config/mirrors.json")

Expand All @@ -42,70 +43,75 @@ 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(throws: StringError("Mirror not found for '\(gitUrl)'")) {
try mirrors.unset(originalOrMirror: 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 +131,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 +144,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)
}
}
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 {
fileprivate 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