|
8 | 8 | See http://swift.org/CONTRIBUTORS.txt for Swift project authors
|
9 | 9 | */
|
10 | 10 |
|
11 |
| -import Basics |
| 11 | +@testable import Basics |
12 | 12 | @testable import Commands
|
13 | 13 | import SPMTestSupport
|
14 | 14 | import TSCBasic
|
15 | 15 | import XCTest
|
16 | 16 |
|
17 | 17 | final class SwiftToolTests: XCTestCase {
|
18 |
| - func testNetrcLocations() throws { |
| 18 | + func testNetrcAuthorizationProviders() throws { |
19 | 19 | fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in
|
20 | 20 | let fs = localFileSystem
|
| 21 | + |
| 22 | + let localPath = packageRoot.appending(component: ".netrc") |
| 23 | + let userHomePath = fs.homeDirectory.appending(component: ".netrc") |
21 | 24 |
|
22 | 25 | // custom .netrc file
|
23 |
| - |
24 | 26 | do {
|
25 | 27 | let customPath = fs.homeDirectory.appending(component: UUID().uuidString)
|
26 | 28 | try fs.writeFileContents(customPath) {
|
27 | 29 | "machine mymachine.labkey.org login [email protected] password custom"
|
28 | 30 | }
|
29 | 31 |
|
30 |
| - |
31 | 32 | let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString, "--netrc-file", customPath.pathString])
|
32 | 33 | let tool = try SwiftTool(options: options)
|
33 |
| - XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(customPath)) |
| 34 | + |
| 35 | + let netrcProviders = try tool.getNetrcAuthorizationProviders() |
| 36 | + XCTAssertEqual(netrcProviders.count, 1) |
| 37 | + XCTAssertEqual(netrcProviders.first.map { resolveSymlinks($0.path) }, resolveSymlinks(customPath)) |
| 38 | + |
34 | 39 | let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!)
|
35 | 40 | XCTAssertEqual(auth ?.user , "[email protected]")
|
36 | 41 | XCTAssertEqual(auth?.password, "custom")
|
37 | 42 |
|
38 | 43 | // delete it
|
39 | 44 | try localFileSystem.removeFileTree(customPath)
|
40 |
| - XCTAssertThrowsError(try tool.getNetrcConfigFile(), "error expected") { error in |
| 45 | + XCTAssertThrowsError(try tool.getNetrcAuthorizationProviders(), "error expected") { error in |
41 | 46 | XCTAssertEqual(error as? StringError, StringError("Did not find .netrc file at \(customPath)."))
|
42 | 47 | }
|
43 | 48 | }
|
44 | 49 |
|
45 | 50 | // local .netrc file
|
46 |
| - |
47 | 51 | do {
|
48 |
| - let localPath = packageRoot.appending(component: ".netrc") |
| 52 | + // make sure there isn't a user home one |
| 53 | + try localFileSystem.removeFileTree(userHomePath) |
| 54 | + |
49 | 55 | try fs.writeFileContents(localPath) {
|
50 | 56 | return "machine mymachine.labkey.org login [email protected] password local"
|
51 | 57 | }
|
52 | 58 |
|
53 | 59 | let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString])
|
54 | 60 | let tool = try SwiftTool(options: options)
|
| 61 | + |
| 62 | + let netrcProviders = try tool.getNetrcAuthorizationProviders() |
| 63 | + XCTAssertEqual(netrcProviders.count, 1) |
| 64 | + XCTAssertEqual(netrcProviders.first.map { resolveSymlinks($0.path) }, resolveSymlinks(localPath)) |
55 | 65 |
|
56 |
| - XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(localPath)) |
57 | 66 | let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!)
|
58 | 67 | XCTAssertEqual(auth ?.user , "[email protected]")
|
59 | 68 | XCTAssertEqual(auth?.password, "local")
|
60 | 69 | }
|
61 | 70 |
|
62 | 71 | // user .netrc file
|
63 |
| - |
64 | 72 | do {
|
65 | 73 | // make sure there isn't a local one
|
66 |
| - try localFileSystem.removeFileTree(packageRoot.appending(component: ".netrc")) |
| 74 | + try localFileSystem.removeFileTree(localPath) |
67 | 75 |
|
68 |
| - let userHomePath = fs.homeDirectory.appending(component: ".netrc") |
69 | 76 | try fs.writeFileContents(userHomePath) {
|
70 | 77 | return "machine mymachine.labkey.org login [email protected] password user"
|
71 | 78 | }
|
72 | 79 |
|
73 | 80 | let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString])
|
74 | 81 | let tool = try SwiftTool(options: options)
|
75 | 82 |
|
76 |
| - XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(userHomePath)) |
| 83 | + let netrcProviders = try tool.getNetrcAuthorizationProviders() |
| 84 | + XCTAssertEqual(netrcProviders.count, 1) |
| 85 | + XCTAssertEqual(netrcProviders.first.map { resolveSymlinks($0.path) }, resolveSymlinks(userHomePath)) |
| 86 | + |
77 | 87 | let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!)
|
78 | 88 | XCTAssertEqual(auth ?.user , "[email protected]")
|
79 | 89 | XCTAssertEqual(auth?.password, "user")
|
80 | 90 | }
|
| 91 | + |
| 92 | + // both local and user .netrc file |
| 93 | + do { |
| 94 | + try fs.writeFileContents(localPath) { |
| 95 | + return "machine mymachine.labkey.org login [email protected] password local" |
| 96 | + } |
| 97 | + try fs.writeFileContents(userHomePath) { |
| 98 | + return "machine mymachine.labkey.org login [email protected] password user" |
| 99 | + } |
| 100 | + |
| 101 | + let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString]) |
| 102 | + let tool = try SwiftTool(options: options) |
| 103 | + |
| 104 | + let netrcProviders = try tool.getNetrcAuthorizationProviders() |
| 105 | + XCTAssertEqual(netrcProviders.count, 2) |
| 106 | + XCTAssertEqual(netrcProviders.map { resolveSymlinks($0.path) }, [localPath, userHomePath].map(resolveSymlinks)) |
| 107 | + |
| 108 | + // local before user .netrc file |
| 109 | + let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) |
| 110 | + XCTAssertEqual(auth ?.user , "[email protected]") |
| 111 | + XCTAssertEqual(auth?.password, "local") |
| 112 | + } |
81 | 113 | }
|
82 | 114 | }
|
83 | 115 | }
|
0 commit comments