@@ -28,6 +28,7 @@ class TestProcess : XCTestCase {
28
28
( " test_pipe_stdin " , test_pipe_stdin) ,
29
29
( " test_pipe_stdout " , test_pipe_stdout) ,
30
30
( " test_pipe_stderr " , test_pipe_stderr) ,
31
+ ( " test_current_working_directory " , test_current_working_directory) ,
31
32
// disabled for now
32
33
// ("test_pipe_stdout_and_stderr_same_pipe", test_pipe_stdout_and_stderr_same_pipe),
33
34
( " test_file_stdout " , test_file_stdout) ,
@@ -262,6 +263,19 @@ class TestProcess : XCTestCase {
262
263
XCTFail ( " Test failed: \( error) " )
263
264
}
264
265
}
266
+
267
+ func test_current_working_directory( ) {
268
+ do {
269
+ let previousWorkingDirectory = FileManager . default. currentDirectoryPath
270
+
271
+ // `bash` will not be found if the current working directory is not set correctly.
272
+ let _ = try runTask ( [ " bash " , " -c " , " exit 0 " ] , currentDirectoryPath: " /bin " )
273
+
274
+ XCTAssertEqual ( previousWorkingDirectory, FileManager . default. currentDirectoryPath)
275
+ } catch let error {
276
+ XCTFail ( " Test failed: \( error) " )
277
+ }
278
+ }
265
279
}
266
280
267
281
private func mkstemp( template: String , body: ( FileHandle ) throws -> Void ) rethrows {
@@ -284,14 +298,18 @@ private enum Error: Swift.Error {
284
298
case InvalidEnvironmentVariable( String )
285
299
}
286
300
287
- private func runTask( _ arguments: [ String ] , environment: [ String : String ] ? = nil ) throws -> String {
301
+ private func runTask( _ arguments: [ String ] , environment: [ String : String ] ? = nil , currentDirectoryPath : String ? = nil ) throws -> String {
288
302
let process = Process ( )
289
303
290
304
var arguments = arguments
291
305
process. launchPath = arguments. removeFirst ( )
292
306
process. arguments = arguments
293
307
process. environment = environment
294
308
309
+ if let directoryPath = currentDirectoryPath {
310
+ process. currentDirectoryPath = directoryPath
311
+ }
312
+
295
313
let pipe = Pipe ( )
296
314
process. standardOutput = pipe
297
315
process. standardError = pipe
0 commit comments