|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basics |
| 14 | +import Commands |
| 15 | +import SPMTestSupport |
| 16 | +import XCTest |
| 17 | + |
| 18 | +import class TSCBasic.Process |
| 19 | +import enum TSCBasic.ProcessEnv |
| 20 | + |
| 21 | +private let deprecationWarning = "warning: `swift experimental-sdk` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk` instead." |
| 22 | + |
| 23 | +final class SDKCommandTests: CommandsTestCase { |
| 24 | + func testUsage() throws { |
| 25 | + for command in [SwiftPM.sdk, SwiftPM.experimentalSDK] { |
| 26 | + let stdout = try command.execute(["-help"]).stdout |
| 27 | + XCTAssert(stdout.contains("USAGE: swift sdk <subcommand>") || stdout.contains("USAGE: swift sdk [<subcommand>]"), "got stdout:\n" + stdout) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + func testVersion() throws { |
| 32 | + for command in [SwiftPM.sdk, SwiftPM.experimentalSDK] { |
| 33 | + let stdout = try command.execute(["--version"]).stdout |
| 34 | + XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + func testInstallSDK() throws { |
| 39 | + for command in [SwiftPM.sdk, SwiftPM.experimentalSDK] { |
| 40 | + try fixture(name: "SwiftSDKs") { fixturePath in |
| 41 | + for bundle in ["test-sdk.artifactbundle.tar.gz", "test-sdk.artifactbundle.zip"] { |
| 42 | + var (stdout, stderr) = try command.execute( |
| 43 | + [ |
| 44 | + "install", |
| 45 | + "--swift-sdks-path", fixturePath.pathString, |
| 46 | + fixturePath.appending(bundle).pathString |
| 47 | + ] |
| 48 | + ) |
| 49 | + |
| 50 | + if command == .experimentalSDK { |
| 51 | + XCTAssertMatch(stdout, .contains(deprecationWarning)) |
| 52 | + } |
| 53 | + |
| 54 | + // We only expect tool's output on the stdout stream. |
| 55 | + XCTAssertMatch( |
| 56 | + stdout, |
| 57 | + .contains("\(bundle)` successfully installed as test-sdk.artifactbundle.") |
| 58 | + ) |
| 59 | + |
| 60 | + XCTAssertEqual(stderr.count, 0) |
| 61 | + |
| 62 | + (stdout, stderr) = try command.execute( |
| 63 | + ["list", "--swift-sdks-path", fixturePath.pathString]) |
| 64 | + |
| 65 | + if command == .experimentalSDK { |
| 66 | + XCTAssertMatch(stdout, .contains(deprecationWarning)) |
| 67 | + } |
| 68 | + |
| 69 | + // We only expect tool's output on the stdout stream. |
| 70 | + XCTAssertMatch(stdout, .contains("test-artifact")) |
| 71 | + XCTAssertEqual(stderr.count, 0) |
| 72 | + |
| 73 | + XCTAssertThrowsError(try command.execute( |
| 74 | + [ |
| 75 | + "install", |
| 76 | + "--swift-sdks-path", fixturePath.pathString, |
| 77 | + fixturePath.appending(bundle).pathString |
| 78 | + ] |
| 79 | + )) { error in |
| 80 | + guard case SwiftPMError.executionFailure(_, _, let stderr) = error else { |
| 81 | + XCTFail() |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + XCTAssertTrue( |
| 86 | + stderr.contains( |
| 87 | + "Error: Swift SDK bundle with name `test-sdk.artifactbundle` is already installed. Can't install a new bundle with the same name." |
| 88 | + ), |
| 89 | + "got stderr: \(stderr)" |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + if command == .experimentalSDK { |
| 94 | + XCTAssertMatch(stdout, .contains(deprecationWarning)) |
| 95 | + } |
| 96 | + |
| 97 | + (stdout, stderr) = try command.execute( |
| 98 | + ["remove", "--swift-sdks-path", fixturePath.pathString, "test-artifact"]) |
| 99 | + |
| 100 | + if command == .experimentalSDK { |
| 101 | + XCTAssertMatch(stdout, .contains(deprecationWarning)) |
| 102 | + } |
| 103 | + |
| 104 | + // We only expect tool's output on the stdout stream. |
| 105 | + XCTAssertMatch(stdout, .contains("test-sdk.artifactbundle` was successfully removed from the file system.")) |
| 106 | + XCTAssertEqual(stderr.count, 0) |
| 107 | + |
| 108 | + (stdout, stderr) = try command.execute( |
| 109 | + ["list", "--swift-sdks-path", fixturePath.pathString]) |
| 110 | + |
| 111 | + if command == .experimentalSDK { |
| 112 | + XCTAssertMatch(stdout, .contains(deprecationWarning)) |
| 113 | + } |
| 114 | + |
| 115 | + // We only expect tool's output on the stdout stream. |
| 116 | + XCTAssertNoMatch(stdout, .contains("test-artifact")) |
| 117 | + XCTAssertEqual(stderr.count, 0) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments