@@ -20,7 +20,7 @@ import class Foundation.Bundle
20
20
21
21
22
22
/// Test-helper function that runs a block of code on a copy of a test fixture package. The copy is made into a temporary directory, and the block is given a path to that directory. The block is permitted to modify the copy. The temporary copy is deleted after the block returns. The fixture name may contain `/` characters, which are treated as path separators, exactly as if the name were a relative path.
23
- func fixture( name: String , tags: [ String ] = [ ] , file: StaticString = #file, line: UInt = #line, body: @noescape ( AbsolutePath) throws -> Void ) {
23
+ public func fixture( name: String , tags: [ String ] = [ ] , file: StaticString = #file, line: UInt = #line, body: @noescape ( AbsolutePath) throws -> Void ) {
24
24
do {
25
25
// Make a suitable test directory name from the fixture subpath.
26
26
let fixtureSubpath = RelativePath ( name)
@@ -83,7 +83,7 @@ func fixture(name: String, tags: [String] = [], file: StaticString = #file, line
83
83
}
84
84
85
85
/// Test-helper function that creates a new Git repository in a directory. The new repository will contain exactly one empty file, and if a tag name is provided, a tag with that name will be created.
86
- func initGitRepo( _ dir: AbsolutePath , tag: String ? = nil , file: StaticString = #file, line: UInt = #line) {
86
+ public func initGitRepo( _ dir: AbsolutePath , tag: String ? = nil , file: StaticString = #file, line: UInt = #line) {
87
87
do {
88
88
let file = dir. appending ( component: " file.swift " )
89
89
try systemQuietly ( [ " touch " , file. asString] )
@@ -101,89 +101,19 @@ func initGitRepo(_ dir: AbsolutePath, tag: String? = nil, file: StaticString = #
101
101
}
102
102
}
103
103
104
- func tagGitRepo( _ dir: AbsolutePath , tag: String ) throws {
104
+ public func tagGitRepo( _ dir: AbsolutePath , tag: String ) throws {
105
105
try systemQuietly ( [ Git . tool, " -C " , dir. asString, " tag " , tag] )
106
106
}
107
107
108
- enum Configuration {
108
+ public enum Configuration {
109
109
case Debug
110
110
case Release
111
111
}
112
112
113
113
private var globalSymbolInMainBinary = 0
114
114
115
- /// Defines the executables used by SwiftPM.
116
- /// Contains path to the currently built executable and
117
- /// helper method to execute them.
118
- enum SwiftPMProduct {
119
- case SwiftBuild
120
- case SwiftPackage
121
- case SwiftTest
122
- case XCTestHelper
123
-
124
- /// Path to currently built binary.
125
- var path : AbsolutePath {
126
- #if os(macOS)
127
- for bundle in Bundle . allBundles where bundle. bundlePath. hasSuffix ( " .xctest " ) {
128
- return AbsolutePath ( bundle. bundlePath) . parentDirectory. appending ( self . exec)
129
- }
130
- fatalError ( )
131
- #else
132
- return AbsolutePath ( CommandLine . arguments. first!, relativeTo: currentWorkingDirectory) . parentDirectory. appending ( self . exec)
133
- #endif
134
- }
135
-
136
- /// Executable name.
137
- var exec : RelativePath {
138
- switch self {
139
- case . SwiftBuild:
140
- return RelativePath ( " swift-build " )
141
- case . SwiftPackage:
142
- return RelativePath ( " swift-package " )
143
- case . SwiftTest:
144
- return RelativePath ( " swift-test " )
145
- case . XCTestHelper:
146
- return RelativePath ( " swiftpm-xctest-helper " )
147
- }
148
- }
149
- }
150
-
151
- extension SwiftPMProduct {
152
- /// Executes the product with specified arguments.
153
- ///
154
- /// - Parameters:
155
- /// - args: The arguments to pass.
156
- /// - env: Enviroment variables to pass. Enviroment will never be inherited.
157
- /// - chdir: Adds argument `--chdir <path>` if not nil.
158
- /// - printIfError: Print the output on non-zero exit.
159
- ///
160
- /// - Returns: The output of the process.
161
- func execute( _ args: [ String ] , chdir: AbsolutePath ? = nil , env: [ String : String ] = [ : ] , printIfError: Bool = false ) throws -> String {
162
- var out = " "
163
- var completeArgs = [ path. asString]
164
- if let chdir = chdir {
165
- completeArgs += [ " --chdir " , chdir. asString]
166
- }
167
- completeArgs += args
168
- do {
169
- try POSIX . popen ( completeArgs, redirectStandardError: true , environment: env) {
170
- out += $0
171
- }
172
- return out
173
- } catch {
174
- if printIfError {
175
- print ( " **** FAILURE EXECUTING SUBPROCESS **** " )
176
- print ( " command: " + completeArgs. map { $0. shellEscaped ( ) } . joined ( separator: " " ) )
177
- print ( " SWIFT_EXEC: " , env [ " SWIFT_EXEC " ] ?? " nil " )
178
- print ( " output: " , out)
179
- }
180
- throw error
181
- }
182
- }
183
- }
184
-
185
115
@discardableResult
186
- func executeSwiftBuild( _ chdir: AbsolutePath , configuration: Configuration = . Debug, printIfError: Bool = false , Xcc: [ String ] = [ ] , Xld: [ String ] = [ ] , Xswiftc: [ String ] = [ ] , env: [ String : String ] = [ : ] ) throws -> String {
116
+ public func executeSwiftBuild( _ chdir: AbsolutePath , configuration: Configuration = . Debug, printIfError: Bool = false , Xcc: [ String ] = [ ] , Xld: [ String ] = [ ] , Xswiftc: [ String ] = [ ] , env: [ String : String ] = [ : ] ) throws -> String {
187
117
var args = [ " --configuration " ]
188
118
switch configuration {
189
119
case . Debug:
@@ -205,7 +135,7 @@ func executeSwiftBuild(_ chdir: AbsolutePath, configuration: Configuration = .De
205
135
}
206
136
207
137
/// Test helper utility for executing a block with a temporary directory.
208
- func mktmpdir( function: StaticString = #function, file: StaticString = #file, line: UInt = #line, body: @noescape ( AbsolutePath) throws -> Void ) {
138
+ public func mktmpdir( function: StaticString = #function, file: StaticString = #file, line: UInt = #line, body: @noescape ( AbsolutePath) throws -> Void ) {
209
139
do {
210
140
let tmpDir = try TemporaryDirectory ( prefix: " spm-tests- \( function) " , removeTreeOnDeinit: true )
211
141
try body ( tmpDir. path)
@@ -214,63 +144,13 @@ func mktmpdir(function: StaticString = #function, file: StaticString = #file, li
214
144
}
215
145
}
216
146
217
- func XCTAssertBuilds( _ path: AbsolutePath , configurations: Set < Configuration > = [ . Debug, . Release] , file: StaticString = #file, line: UInt = #line, Xcc: [ String ] = [ ] , Xld: [ String ] = [ ] , Xswiftc: [ String ] = [ ] , env: [ String : String ] = [ : ] ) {
218
- for conf in configurations {
219
- do {
220
- print ( " Building \( conf) " )
221
- _ = try executeSwiftBuild ( path, configuration: conf, printIfError: true , Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc, env: env)
222
- } catch {
223
- XCTFail ( " `swift build -c \( conf) ' failed: \n \n \( error) \n " , file: file, line: line)
224
- }
225
- }
226
- }
227
-
228
- func XCTAssertSwiftTest( _ path: AbsolutePath , file: StaticString = #file, line: UInt = #line, env: [ String : String ] = [ : ] ) {
229
- do {
230
- _ = try SwiftPMProduct . SwiftTest. execute ( [ ] , chdir: path, env: env, printIfError: true )
231
- } catch {
232
- XCTFail ( " `swift test' failed: \n \n \( error) \n " , file: file, line: line)
233
- }
234
- }
235
-
236
- func XCTAssertBuildFails( _ path: AbsolutePath , file: StaticString = #file, line: UInt = #line, Xcc: [ String ] = [ ] , Xld: [ String ] = [ ] , Xswiftc: [ String ] = [ ] , env: [ String : String ] = [ : ] ) {
237
- do {
238
- _ = try executeSwiftBuild ( path, Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc)
239
-
240
- XCTFail ( " `swift build' succeeded but should have failed " , file: file, line: line)
241
-
242
- } catch POSIX . Error . exitStatus( let status, _) where status == 1 {
243
- // noop
244
- } catch {
245
- XCTFail ( " `swift build' failed in an unexpected manner " )
246
- }
247
- }
248
-
249
- func XCTAssertFileExists( _ path: AbsolutePath , file: StaticString = #file, line: UInt = #line) {
250
- if !isFile( path) {
251
- XCTFail ( " Expected file doesn’t exist: \( path. asString) " , file: file, line: line)
252
- }
253
- }
254
-
255
- func XCTAssertDirectoryExists( _ path: AbsolutePath , file: StaticString = #file, line: UInt = #line) {
256
- if !isDirectory( path) {
257
- XCTFail ( " Expected directory doesn’t exist: \( path. asString) " , file: file, line: line)
258
- }
259
- }
260
-
261
- func XCTAssertNoSuchPath( _ path: AbsolutePath , file: StaticString = #file, line: UInt = #line) {
262
- if exists ( path) {
263
- XCTFail ( " path exists but should not: \( path. asString) " , file: file, line: line)
264
- }
265
- }
266
-
267
- func systemQuietly( _ args: [ String ] ) throws {
147
+ public func systemQuietly( _ args: [ String ] ) throws {
268
148
// Discard the output, by default.
269
149
//
270
150
// FIXME: Find a better default behavior here.
271
151
let _ = try POSIX . popen ( args, redirectStandardError: true )
272
152
}
273
153
274
- func systemQuietly( _ args: String ... ) throws {
154
+ public func systemQuietly( _ args: String ... ) throws {
275
155
try systemQuietly ( args)
276
156
}
0 commit comments