@@ -596,6 +596,38 @@ class TestProcess : XCTestCase {
596
596
let process = Process ( )
597
597
XCTAssertNil ( process. executableURL)
598
598
XCTAssertNotNil ( process. currentDirectoryURL)
599
+
600
+ // Test currentDirectoryURL cannot be set to nil even though it is a URL?
601
+ let cwd = URL ( fileURLWithPath: FileManager . default. currentDirectoryPath, isDirectory: true )
602
+ process. currentDirectoryURL = nil
603
+ XCTAssertNotNil ( process. currentDirectoryURL)
604
+ XCTAssertEqual ( process. currentDirectoryURL, cwd)
605
+
606
+ let aFileURL = URL ( fileURLWithPath: " /a_file " , isDirectory: false )
607
+ XCTAssertFalse ( aFileURL. hasDirectoryPath)
608
+ XCTAssertEqual ( aFileURL. path, " /a_file " )
609
+ process. currentDirectoryURL = aFileURL
610
+ XCTAssertNotEqual ( process. currentDirectoryURL, aFileURL)
611
+ XCTAssertEqual ( process. currentDirectoryPath, " /a_file " )
612
+ XCTAssertTrue ( try XCTUnwrap ( process. currentDirectoryURL) . hasDirectoryPath)
613
+ XCTAssertEqual ( try XCTUnwrap ( process. currentDirectoryURL) . absoluteString, " file:///a_file/ " )
614
+
615
+ let aDirURL = URL ( fileURLWithPath: " /a_dir " , isDirectory: true )
616
+ XCTAssertTrue ( aDirURL. hasDirectoryPath)
617
+ XCTAssertEqual ( aDirURL. path, " /a_dir " )
618
+ process. currentDirectoryURL = aDirURL
619
+ XCTAssertEqual ( process. currentDirectoryURL, aDirURL)
620
+ XCTAssertEqual ( process. currentDirectoryPath, " /a_dir " )
621
+ XCTAssertTrue ( try XCTUnwrap ( process. currentDirectoryURL) . hasDirectoryPath)
622
+ XCTAssertEqual ( try XCTUnwrap ( process. currentDirectoryURL) . absoluteString, " file:///a_dir/ " )
623
+
624
+ process. currentDirectoryPath = " "
625
+ XCTAssertEqual ( process. currentDirectoryPath, " " )
626
+ XCTAssertNil ( process. currentDirectoryURL)
627
+ process. currentDirectoryURL = nil
628
+ XCTAssertEqual ( process. currentDirectoryPath, cwd. path)
629
+
630
+
599
631
process. executableURL = URL ( fileURLWithPath: " /some_file_that_doesnt_exist " , isDirectory: false )
600
632
XCTAssertThrowsError ( try process. run ( ) ) {
601
633
let code = CocoaError . Code ( rawValue: ( $0 as? NSError ) !. code)
@@ -617,13 +649,44 @@ class TestProcess : XCTestCase {
617
649
}
618
650
619
651
do {
620
- try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: " /some_directory_that_doesnt_exsit " )
652
+ let process = Process ( )
653
+ process. executableURL = xdgTestHelperURL ( )
654
+ process. arguments = [ " --getcwd " ]
655
+ process. currentDirectoryPath = " "
656
+
657
+ let stdoutPipe = Pipe ( )
658
+ process. standardOutput = stdoutPipe
659
+
660
+ try process. run ( )
661
+ process. waitUntilExit ( )
662
+
663
+ guard process. terminationStatus == 0 else {
664
+ throw Error . TerminationStatus ( process. terminationStatus)
665
+ }
666
+
667
+ var stdoutData = Data ( )
668
+ #if DARWIN_COMPATIBILITY_TESTS
669
+ // Use old API for now
670
+ stdoutData. append ( stdoutPipe. fileHandleForReading. availableData)
671
+ #else
672
+ if let d = try stdoutPipe. fileHandleForReading. readToEnd ( ) {
673
+ stdoutData. append ( d)
674
+ }
675
+ #endif
676
+
677
+ guard let stdout = String ( data: stdoutData, encoding: . utf8) else {
678
+ throw Error . UnicodeDecodingError ( stdoutData)
679
+ }
680
+ let directory = stdout. trimmingCharacters ( in: CharacterSet ( [ " \n " , " \r " ] ) )
681
+ XCTAssertEqual ( directory, FileManager . default. currentDirectoryPath)
621
682
} catch {
683
+ XCTFail ( String ( describing: error) )
684
+ }
685
+
686
+ XCTAssertThrowsError ( try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: " /some_directory_that_doesnt_exsit " ) ) { error in
622
687
let code = CocoaError . Code ( rawValue: ( error as? NSError ) !. code)
623
688
XCTAssertEqual ( code, . fileReadNoSuchFile)
624
- return
625
689
}
626
- XCTFail ( " Failed to catch error " )
627
690
}
628
691
629
692
0 commit comments