@@ -262,17 +262,18 @@ class TestProcess : XCTestCase {
262
262
if ( dir. hasSuffix ( " / " ) && dir != " / " ) || dir. hasSuffix ( " \\ " ) {
263
263
dir. removeLast ( )
264
264
}
265
- return dir
265
+ return dir. standardizePath ( )
266
266
} ( )
267
267
268
268
let fm = FileManager . default
269
269
let previousWorkingDirectory = fm. currentDirectoryPath
270
+ XCTAssertNotEqual ( previousWorkingDirectory. standardizePath ( ) , tmpDir)
270
271
271
272
// Test that getcwd() returns the currentDirectoryPath
272
273
do {
273
274
let ( pwd, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: tmpDir)
274
275
// Check the sub-process used the correct directory
275
- XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) , tmpDir)
276
+ XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) . standardizePath ( ) , tmpDir)
276
277
} catch {
277
278
XCTFail ( " Test failed: \( error) " )
278
279
}
@@ -281,7 +282,9 @@ class TestProcess : XCTestCase {
281
282
do {
282
283
let ( pwd, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --echo-PWD " ] , currentDirectoryPath: tmpDir)
283
284
// Check the sub-process used the correct directory
284
- XCTAssertEqual ( pwd. trimmingCharacters ( in: . newlines) , tmpDir)
285
+ let cwd = FileManager . default. currentDirectoryPath. standardizePath ( )
286
+ XCTAssertNotEqual ( cwd, tmpDir)
287
+ XCTAssertNotEqual ( pwd. trimmingCharacters ( in: . newlines) . standardizePath ( ) , tmpDir)
285
288
} catch {
286
289
XCTFail ( " Test failed: \( error) " )
287
290
}
@@ -584,6 +587,42 @@ class TestProcess : XCTestCase {
584
587
}
585
588
586
589
590
+ func test_currentDirectory( ) throws {
591
+
592
+ let process = Process ( )
593
+ XCTAssertNil ( process. executableURL)
594
+ XCTAssertNotNil ( process. currentDirectoryURL)
595
+ process. executableURL = URL ( fileURLWithPath: " /some_file_that_doesnt_exist " , isDirectory: false )
596
+ XCTAssertThrowsError ( try process. run ( ) ) {
597
+ let code = CocoaError . Code ( rawValue: ( $0 as? NSError ) !. code)
598
+ XCTAssertEqual ( code, . fileReadNoSuchFile)
599
+ }
600
+
601
+ do {
602
+ let ( stdout, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: " / " )
603
+ XCTAssertEqual ( stdout. trimmingCharacters ( in: CharacterSet ( [ " \n " , " \r " ] ) ) , " / " )
604
+ }
605
+
606
+ do {
607
+ XCTAssertNotEqual ( " / " , FileManager . default. currentDirectoryPath)
608
+ XCTAssertNotEqual ( FileManager . default. currentDirectoryPath, " / " )
609
+ let ( stdout, _) = try runTask ( [ xdgTestHelperURL ( ) . path, " --echo-PWD " ] , currentDirectoryPath: " / " )
610
+ let directory = stdout. trimmingCharacters ( in: CharacterSet ( [ " \n " , " \r " ] ) )
611
+ XCTAssertEqual ( directory, ProcessInfo . processInfo. environment [ " PWD " ] )
612
+ XCTAssertNotEqual ( directory, " / " )
613
+ }
614
+
615
+ do {
616
+ try runTask ( [ xdgTestHelperURL ( ) . path, " --getcwd " ] , currentDirectoryPath: " /some_directory_that_doesnt_exsit " )
617
+ } catch {
618
+ let code = CocoaError . Code ( rawValue: ( error as? NSError ) !. code)
619
+ XCTAssertEqual ( code, . fileReadNoSuchFile)
620
+ return
621
+ }
622
+ XCTFail ( " Failed to catch error " )
623
+ }
624
+
625
+
587
626
static var allTests : [ ( String , ( TestProcess ) -> ( ) throws -> Void ) ] {
588
627
var tests = [
589
628
( " test_exit0 " , test_exit0) ,
@@ -610,6 +649,7 @@ class TestProcess : XCTestCase {
610
649
( " test_redirect_all_using_null " , test_redirect_all_using_null) ,
611
650
( " test_redirect_all_using_nil " , test_redirect_all_using_nil) ,
612
651
( " test_plutil " , test_plutil) ,
652
+ ( " test_currentDirectory " , test_currentDirectory) ,
613
653
]
614
654
615
655
#if !os(Windows)
@@ -708,6 +748,7 @@ class _SignalHelperRunner {
708
748
}
709
749
}
710
750
751
+ @discardableResult
711
752
internal func runTask( _ arguments: [ String ] , environment: [ String : String ] ? = nil , currentDirectoryPath: String ? = nil ) throws -> ( String , String ) {
712
753
let process = Process ( )
713
754
0 commit comments