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