/
BuildToolTests.swift
209 lines (178 loc) · 9.76 KB
/
BuildToolTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import XCTest
import SPMTestSupport
import TSCBasic
import Commands
import Workspace
struct BuildResult {
let binPath: AbsolutePath
let output: String
let binContents: [String]
}
final class BuildToolTests: XCTestCase {
@discardableResult
private func execute(
_ args: [String],
packagePath: AbsolutePath? = nil
) throws -> (stdout: String, stderr: String) {
return try SwiftPMProduct.SwiftBuild.execute(args, packagePath: packagePath)
}
func build(_ args: [String], packagePath: AbsolutePath? = nil) throws -> BuildResult {
let (output, _) = try execute(args, packagePath: packagePath)
defer { try! SwiftPMProduct.SwiftPackage.execute(["clean"], packagePath: packagePath) }
let (binPathOutput, _) = try execute(["--show-bin-path"], packagePath: packagePath)
let binPath = AbsolutePath(binPathOutput.trimmingCharacters(in: .whitespacesAndNewlines))
let binContents = try localFileSystem.getDirectoryContents(binPath)
return BuildResult(binPath: binPath, output: output, binContents: binContents)
}
func testUsage() throws {
XCTAssert(try execute(["-help"]).stdout.contains("USAGE: swift build"))
}
func testSeeAlso() throws {
XCTAssert(try execute(["--help"]).stdout.contains("SEE ALSO: swift run, swift package, swift test"))
}
func testVersion() throws {
XCTAssert(try execute(["--version"]).stdout.contains("Swift Package Manager"))
}
func testBinPathAndSymlink() throws {
fixture(name: "ValidLayouts/SingleModule/ExecutableNew") { path in
let fullPath = resolveSymlinks(path)
let targetPath = fullPath.appending(components: ".build", Resources.default.toolchain.triple.tripleString)
let xcbuildTargetPath = fullPath.appending(components: ".build", "apple")
XCTAssertEqual(try execute(["--show-bin-path"], packagePath: fullPath).stdout,
"\(targetPath.appending(component: "debug").pathString)\n")
XCTAssertEqual(try execute(["-c", "release", "--show-bin-path"], packagePath: fullPath).stdout,
"\(targetPath.appending(component: "release").pathString)\n")
// Print correct path when building with XCBuild.
let xcodeDebugOutput = try execute(["--build-system", "xcode", "--show-bin-path"], packagePath: fullPath).stdout
let xcodeReleaseOutput = try execute(["--build-system", "xcode", "-c", "release", "--show-bin-path"], packagePath: fullPath).stdout
#if os(macOS)
XCTAssertEqual(xcodeDebugOutput, "\(xcbuildTargetPath.appending(components: "Products", "Debug").pathString)\n")
XCTAssertEqual(xcodeReleaseOutput, "\(xcbuildTargetPath.appending(components: "Products", "Release").pathString)\n")
#else
XCTAssertEqual(xcodeDebugOutput, "\(targetPath.appending(component: "debug").pathString)\n")
XCTAssertEqual(xcodeReleaseOutput, "\(targetPath.appending(component: "release").pathString)\n")
#endif
// Test symlink.
_ = try execute([], packagePath: fullPath)
XCTAssertEqual(resolveSymlinks(fullPath.appending(components: ".build", "debug")),
targetPath.appending(component: "debug"))
_ = try execute(["-c", "release"], packagePath: fullPath)
XCTAssertEqual(resolveSymlinks(fullPath.appending(components: ".build", "release")),
targetPath.appending(component: "release"))
}
}
func testProductAndTarget() throws {
fixture(name: "Miscellaneous/MultipleExecutables") { path in
let fullPath = resolveSymlinks(path)
do {
let result = try build(["--product", "exec1"], packagePath: fullPath)
XCTAssert(result.binContents.contains("exec1"))
XCTAssert(!result.binContents.contains("exec2.build"))
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTFail(stderr)
}
do {
let (_, stderr) = try execute(["--product", "lib1"], packagePath: fullPath)
try SwiftPMProduct.SwiftPackage.execute(["clean"], packagePath: fullPath)
XCTAssertMatch(stderr, .contains("'--product' cannot be used with the automatic product 'lib1'; building the default target instead"))
}
do {
let result = try build(["--target", "exec2"], packagePath: fullPath)
XCTAssert(result.binContents.contains("exec2.build"))
XCTAssert(!result.binContents.contains("exec1"))
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTFail(stderr)
}
do {
_ = try execute(["--product", "exec1", "--target", "exec2"], packagePath: path)
XCTFail("Expected to fail")
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTAssertEqual(stderr, "error: '--product' and '--target' are mutually exclusive\n")
}
do {
_ = try execute(["--product", "exec1", "--build-tests"], packagePath: path)
XCTFail("Expected to fail")
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTAssertEqual(stderr, "error: '--product' and '--build-tests' are mutually exclusive\n")
}
do {
_ = try execute(["--build-tests", "--target", "exec2"], packagePath: path)
XCTFail("Expected to fail")
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTAssertEqual(stderr, "error: '--target' and '--build-tests' are mutually exclusive\n")
}
do {
_ = try execute(["--build-tests", "--target", "exec2", "--product", "exec1"], packagePath: path)
XCTFail("Expected to fail")
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTAssertEqual(stderr, "error: '--product', '--target', and '--build-tests' are mutually exclusive\n")
}
do {
_ = try execute(["--product", "UnkownProduct"], packagePath: path)
XCTFail("Expected to fail")
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTAssertEqual(stderr, "error: no product named 'UnkownProduct'\n")
}
do {
_ = try execute(["--target", "UnkownTarget"], packagePath: path)
XCTFail("Expected to fail")
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTAssertEqual(stderr, "error: no target named 'UnkownTarget'\n")
}
}
}
func testNonReachableProductsAndTargetsFunctional() {
fixture(name: "Miscellaneous/UnreachableTargets") { path in
let aPath = path.appending(component: "A")
do {
let result = try build([], packagePath: aPath)
XCTAssert(!result.binContents.contains("bexec"))
XCTAssert(!result.binContents.contains("BTarget2.build"))
XCTAssert(!result.binContents.contains("cexec"))
XCTAssert(!result.binContents.contains("CTarget.build"))
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTFail(stderr)
}
// Dependency contains a dependent product
do {
let result = try build(["--product", "bexec"], packagePath: aPath)
XCTAssert(result.binContents.contains("BTarget2.build"))
XCTAssert(result.binContents.contains("bexec"))
XCTAssert(!result.binContents.contains("aexec"))
XCTAssert(!result.binContents.contains("ATarget.build"))
XCTAssert(!result.binContents.contains("BLibrary.a"))
// FIXME: We create the modulemap during build planning, hence this uglyness.
let bTargetBuildDir = ((try? localFileSystem.getDirectoryContents(result.binPath.appending(component: "BTarget1.build"))) ?? []).filter{ $0 != moduleMapFilename }
XCTAssertEqual(bTargetBuildDir, [])
XCTAssert(!result.binContents.contains("cexec"))
XCTAssert(!result.binContents.contains("CTarget.build"))
// Also make sure we didn't emit parseable module interfaces
// (do this here to avoid doing a second build in
// testParseableInterfaces().
XCTAssert(!result.binContents.contains("ATarget.swiftinterface"))
XCTAssert(!result.binContents.contains("BTarget.swiftinterface"))
XCTAssert(!result.binContents.contains("CTarget.swiftinterface"))
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTFail(stderr)
}
}
}
func testParseableInterfaces() {
fixture(name: "Miscellaneous/ParseableInterfaces") { path in
do {
let result = try build(["--enable-parseable-module-interfaces"], packagePath: path)
XCTAssert(result.binContents.contains("A.swiftinterface"))
XCTAssert(result.binContents.contains("B.swiftinterface"))
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
XCTFail(stderr)
}
}
}
}