@@ -260,12 +260,13 @@ class TestProcess : XCTestCase {
260
260
261
261
let fm = FileManager . default
262
262
let previousWorkingDirectory = fm. currentDirectoryPath
263
+ XCTAssertNotEqual ( previousWorkingDirectory. standardizePath ( ) , tmpDir. standardizePath ( ) )
263
264
264
265
// Test that getcwd() returns the currentDirectoryPath
265
266
do {
266
267
let ( pwd, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: tmpDir)
267
268
// Check the sub-process used the correct directory
268
- XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) , tmpDir)
269
+ XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) . standardizePath ( ) , tmpDir. standardizePath ( ) )
269
270
} catch {
270
271
XCTFail ( " Test failed: \( error) " )
271
272
}
@@ -274,7 +275,9 @@ class TestProcess : XCTestCase {
274
275
do {
275
276
let ( pwd, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --echo-PWD " ] , currentDirectoryPath: tmpDir)
276
277
// Check the sub-process used the correct directory
277
- XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) , tmpDir)
278
+ let cwd = FileManager . default. currentDirectoryPath
279
+ XCTAssertNotEqual ( tmpDir, cwd)
280
+ XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) , cwd)
278
281
} catch {
279
282
XCTFail ( " Test failed: \( error) " )
280
283
}
@@ -566,6 +569,42 @@ class TestProcess : XCTestCase {
566
569
}
567
570
568
571
572
+ func test_currentDirectory( ) throws {
573
+
574
+ let process = Process ( )
575
+ XCTAssertNil ( process. executableURL)
576
+ XCTAssertNotNil ( process. currentDirectoryURL)
577
+ process. executableURL = URL ( fileURLWithPath: " /some_file_that_doesnt_exist " , isDirectory: false )
578
+ XCTAssertThrowsError ( try process. run ( ) ) {
579
+ let code = CocoaError . Code ( rawValue: ( $0 as? NSError ) !. code)
580
+ XCTAssertEqual ( code, . fileReadNoSuchFile)
581
+ }
582
+
583
+ do {
584
+ let ( stdout, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: " / " )
585
+ XCTAssertEqual ( stdout. trimmingCharacters ( in: CharacterSet ( [ " \n " , " \r " ] ) ) , " / " )
586
+ }
587
+
588
+ do {
589
+ XCTAssertNotEqual ( " / " , FileManager . default. currentDirectoryPath)
590
+ XCTAssertNotEqual ( FileManager . default. currentDirectoryPath, " / " )
591
+ let ( stdout, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --echo-PWD " ] , currentDirectoryPath: " / " )
592
+ let directory = stdout. trimmingCharacters ( in: CharacterSet ( [ " \n " , " \r " ] ) )
593
+ XCTAssertEqual ( directory, ProcessInfo . processInfo. environment [ " PWD " ] )
594
+ XCTAssertNotEqual ( directory, " / " )
595
+ }
596
+
597
+ do {
598
+ try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: " /some_directory_that_doesnt_exsit " )
599
+ } catch {
600
+ let code = CocoaError . Code ( rawValue: ( error as? NSError ) !. code)
601
+ XCTAssertEqual ( code, . fileReadNoSuchFile)
602
+ return
603
+ }
604
+ XCTFail ( " Failed to catch error " )
605
+ }
606
+
607
+
569
608
static var allTests : [ ( String , ( TestProcess ) -> ( ) throws -> Void ) ] {
570
609
var tests = [
571
610
( " test_exit0 " , test_exit0) ,
@@ -592,6 +631,7 @@ class TestProcess : XCTestCase {
592
631
( " test_redirect_all_using_null " , test_redirect_all_using_null) ,
593
632
( " test_redirect_all_using_nil " , test_redirect_all_using_nil) ,
594
633
( " test_plutil " , test_plutil) ,
634
+ ( " test_currentDirectory " , test_currentDirectory) ,
595
635
]
596
636
597
637
#if !os(Windows)
@@ -690,6 +730,7 @@ class _SignalHelperRunner {
690
730
}
691
731
}
692
732
733
+ @discardableResult
693
734
internal func runTask( _ arguments: [ String ] , environment: [ String : String ] ? = nil , currentDirectoryPath: String ? = nil ) throws -> ( String , String ) {
694
735
let process = Process ( )
695
736
0 commit comments