|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 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 Basics |
| 12 | +@testable import Commands |
| 13 | +import SPMTestSupport |
| 14 | +import TSCBasic |
| 15 | +import XCTest |
| 16 | + |
| 17 | +final class SwiftToolTests: XCTestCase { |
| 18 | + func testNetrcLocations() throws { |
| 19 | + fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in |
| 20 | + let fs = localFileSystem |
| 21 | + |
| 22 | + // custom .netrc file |
| 23 | + |
| 24 | + do { |
| 25 | + let customPath = fs.homeDirectory.appending(component: UUID().uuidString) |
| 26 | + try fs.writeFileContents(customPath) { |
| 27 | + "machine mymachine.labkey.org login [email protected] password custom" |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString, "--netrc-file", customPath.pathString]) |
| 32 | + let tool = try SwiftTool(options: options) |
| 33 | + XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(customPath)) |
| 34 | + let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) |
| 35 | + XCTAssertEqual(auth ?.user , "[email protected]") |
| 36 | + XCTAssertEqual(auth?.password, "custom") |
| 37 | + |
| 38 | + // delete it |
| 39 | + try localFileSystem.removeFileTree(customPath) |
| 40 | + XCTAssertThrowsError(try tool.getNetrcConfigFile(), "error expected") { error in |
| 41 | + XCTAssertEqual(error as? StringError, StringError("Did not find .netrc file at \(customPath).")) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // local .netrc file |
| 46 | + |
| 47 | + do { |
| 48 | + let localPath = packageRoot.appending(component: ".netrc") |
| 49 | + try fs.writeFileContents(localPath) { |
| 50 | + return "machine mymachine.labkey.org login [email protected] password local" |
| 51 | + } |
| 52 | + |
| 53 | + let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString]) |
| 54 | + let tool = try SwiftTool(options: options) |
| 55 | + |
| 56 | + XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(localPath)) |
| 57 | + let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) |
| 58 | + XCTAssertEqual(auth ?.user , "[email protected]") |
| 59 | + XCTAssertEqual(auth?.password, "local") |
| 60 | + } |
| 61 | + |
| 62 | + // user .netrc file |
| 63 | + |
| 64 | + do { |
| 65 | + // make sure there isn't a local one |
| 66 | + try localFileSystem.removeFileTree(packageRoot.appending(component: ".netrc")) |
| 67 | + |
| 68 | + let userHomePath = fs.homeDirectory.appending(component: ".netrc") |
| 69 | + try fs.writeFileContents(userHomePath) { |
| 70 | + return "machine mymachine.labkey.org login [email protected] password user" |
| 71 | + } |
| 72 | + |
| 73 | + let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString]) |
| 74 | + let tool = try SwiftTool(options: options) |
| 75 | + |
| 76 | + XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(userHomePath)) |
| 77 | + let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) |
| 78 | + XCTAssertEqual(auth ?.user , "[email protected]") |
| 79 | + XCTAssertEqual(auth?.password, "user") |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments