|  | 
|  | 1 | +/* | 
|  | 2 | + This source file is part of the Swift.org open source project | 
|  | 3 | + | 
|  | 4 | + Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors | 
|  | 5 | + Licensed under Apache License v2.0 with Runtime Library Exception | 
|  | 6 | + | 
|  | 7 | + See http://swift.org/LICENSE.txt for license information | 
|  | 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors | 
|  | 9 | + */ | 
|  | 10 | + | 
|  | 11 | +import XCTest | 
|  | 12 | + | 
|  | 13 | +import SPMTestSupport | 
|  | 14 | +import Commands | 
|  | 15 | +import TSCBasic | 
|  | 16 | + | 
|  | 17 | +final class ScriptToolTests: XCTestCase { | 
|  | 18 | +    private func execute(_ args: [String]) throws -> (stdout: String, stderr: String) { | 
|  | 19 | +        return try SwiftPMProduct.SwiftScript.execute(args) | 
|  | 20 | +    } | 
|  | 21 | + | 
|  | 22 | +    private func build(_ args: String...) throws -> (stdout: String, stderr: String) { | 
|  | 23 | +        return try execute(["build"] + args) | 
|  | 24 | +    } | 
|  | 25 | + | 
|  | 26 | +    private func run(_ args: String...) throws -> (stdout: String, stderr: String) { | 
|  | 27 | +        return try execute(["run"] + args) | 
|  | 28 | +    } | 
|  | 29 | + | 
|  | 30 | +    func testUsage() throws { | 
|  | 31 | +        let stdout = try execute(["-help"]).stdout | 
|  | 32 | +        XCTAssert(stdout.contains("USAGE: swift script <subcommand>"), "got stdout:\n" + stdout) | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    func testSeeAlso() throws { | 
|  | 36 | +        let stdout = try execute(["--help"]).stdout | 
|  | 37 | +        XCTAssert(stdout.contains("SEE ALSO: swift build, swift run, swift package, swift test"), "got stdout:\n" + stdout) | 
|  | 38 | +    } | 
|  | 39 | + | 
|  | 40 | +    func testVersion() throws { | 
|  | 41 | +        let stdout = try execute(["--version"]).stdout | 
|  | 42 | +        XCTAssert(stdout.contains("Swift Package Manager"), "got stdout:\n" + stdout) | 
|  | 43 | +    } | 
|  | 44 | + | 
|  | 45 | +    func testWrongScriptPath() throws { | 
|  | 46 | +        try withTemporaryDirectory { path in | 
|  | 47 | +            let pathString = path.appending(component: "EchoArguments").pathString | 
|  | 48 | +            do { | 
|  | 49 | +                _ = try run(pathString) | 
|  | 50 | +                XCTFail("Unexpected success") | 
|  | 51 | +            } catch SwiftPMProductError.executionFailure(_, _, let stderr) { | 
|  | 52 | +                XCTExpectFailure() // FIXME: Seems an implementation bug of TSC? | 
|  | 53 | +                XCTAssertEqual(stderr, "error: \(pathString) doesn't exist\n") | 
|  | 54 | +            } | 
|  | 55 | +        } | 
|  | 56 | +    } | 
|  | 57 | + | 
|  | 58 | +    func testArgumentPassing() throws { | 
|  | 59 | +        fixture(name: "Scripts/EchoArguments") { path in | 
|  | 60 | +            let result = try run(path.appending(component: "EchoArguments.swift").pathString, | 
|  | 61 | +                                 "1", "--hello", "world") | 
|  | 62 | + | 
|  | 63 | +            // We only expect tool's output on the stdout stream. | 
|  | 64 | +            XCTAssertEqual(result.stdout.trimmingCharacters(in: .whitespacesAndNewlines), | 
|  | 65 | +                           #""1" "--hello" "world""#) | 
|  | 66 | + | 
|  | 67 | +            // swift-build-tool output should go to stderr. | 
|  | 68 | +            XCTAssertMatch(result.stderr, .contains("Using cache: EchoArguments-")) | 
|  | 69 | +            XCTAssertMatch(result.stderr, .contains("Compiling")) | 
|  | 70 | +            XCTAssertMatch(result.stderr, .contains("Linking")) | 
|  | 71 | +        } | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    func testCurrentWorkingDirectoryWithLocalDependency() throws { | 
|  | 75 | +        fixture(name: "Scripts/EchoCWD") { path in | 
|  | 76 | +            let result = try run(path.appending(component: "EchoCWD.swift").pathString) | 
|  | 77 | + | 
|  | 78 | +            XCTAssertEqual(result.stdout.trimmingCharacters(in: .whitespacesAndNewlines), | 
|  | 79 | +                           localFileSystem.currentWorkingDirectory?.pathString) | 
|  | 80 | +        } | 
|  | 81 | +    } | 
|  | 82 | + | 
|  | 83 | +    func testPrebuild() throws { | 
|  | 84 | +        fixture(name: "Scripts/EchoArguments") { path in | 
|  | 85 | +            let result = try build(path.appending(component: "EchoArguments.swift").pathString) | 
|  | 86 | + | 
|  | 87 | +            // swift-build-tool output should go to stderr. | 
|  | 88 | +            XCTAssertMatch(result.stderr, .contains("Using cache: EchoArguments-")) | 
|  | 89 | +            XCTAssertMatch(result.stderr, .contains("Compiling")) | 
|  | 90 | +            XCTAssertMatch(result.stderr, .contains("Linking")) | 
|  | 91 | +        } | 
|  | 92 | +    } | 
|  | 93 | + | 
|  | 94 | +    func testQuietMode() throws { | 
|  | 95 | +        fixture(name: "Scripts/EchoArguments") { path in | 
|  | 96 | +            let result = try run(path.appending(component: "EchoArguments.swift").pathString, "--quiet", | 
|  | 97 | +                                 "1", "--hello", "world") | 
|  | 98 | + | 
|  | 99 | +            // We only expect tool's output on the stdout stream. | 
|  | 100 | +            XCTAssertEqual(result.stdout.trimmingCharacters(in: .whitespacesAndNewlines), | 
|  | 101 | +                           #""1" "--hello" "world""#) | 
|  | 102 | + | 
|  | 103 | +            // swift-build-tool output should be muted. | 
|  | 104 | +            XCTAssertNoMatch(result.stderr, .contains("Using cache: EchoArguments-")) | 
|  | 105 | +            XCTAssertNoMatch(result.stderr, .contains("Compiling")) | 
|  | 106 | +            XCTAssertNoMatch(result.stderr, .contains("Linking")) | 
|  | 107 | +        } | 
|  | 108 | +    } | 
|  | 109 | + | 
|  | 110 | +    // TODO: Tests for other swift-script tools | 
|  | 111 | +} | 
0 commit comments