@@ -626,9 +626,10 @@ class TestProcess : XCTestCase {
626
626
XCTFail ( " Failed to catch error " )
627
627
}
628
628
629
+ #if !os(Windows)
629
630
func test_fileDescriptorsAreNotInherited( ) throws {
630
631
let task = Process ( )
631
- let clonedFD = dup ( 1 )
632
+ let someExtraFDs = [ dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) ]
632
633
task. executableURL = xdgTestHelperURL ( )
633
634
task. arguments = [ " --print-open-file-descriptors " ]
634
635
task. standardInput = FileHandle . nullDevice
@@ -637,14 +638,32 @@ class TestProcess : XCTestCase {
637
638
task. standardError = FileHandle . nullDevice
638
639
XCTAssertNoThrow ( try task. run ( ) )
639
640
640
- try task. run ( )
641
641
try stdoutPipe. fileHandleForWriting. close ( )
642
642
let stdoutData = try stdoutPipe. fileHandleForReading. readToEnd ( )
643
643
task. waitUntilExit ( )
644
- print ( String ( decoding: stdoutData ?? Data ( ) , as: Unicode . UTF8. self) )
645
- XCTAssertEqual ( " 0 \n 1 \n 2 \n " , String ( decoding: stdoutData ?? Data ( ) , as: Unicode . UTF8. self) )
646
- close ( clonedFD)
647
- }
644
+ let stdoutString = String ( decoding: stdoutData ?? Data ( ) , as: Unicode . UTF8. self)
645
+ #if os(macOS)
646
+ XCTAssertEqual ( " 0 \n 1 \n 2 \n " , stdoutString)
647
+ #else
648
+ // on Linux we may also have a /dev/urandom open as well as some socket that Process uses for something.
649
+
650
+ // we should definitely have stdin (0), stdout (1), and stderr (2) open
651
+ XCTAssert ( stdoutString. utf8. starts ( with: " 0 \n 1 \n 2 \n " . utf8) )
652
+
653
+ // in total we should have 6 or fewer lines:
654
+ // 1. stdin
655
+ // 2. stdout
656
+ // 3. stderr
657
+ // 4. /dev/urandom (optional)
658
+ // 5. communication socket (optional)
659
+ // 6. trailing new line
660
+ XCTAssertLessThanOrEqual ( stdoutString. components ( separatedBy: " \n " ) . count, 6 , " \( stdoutString) " )
661
+ #endif
662
+ for fd in someExtraFDs {
663
+ close ( fd)
664
+ }
665
+ }
666
+ #endif
648
667
649
668
static var allTests : [ ( String , ( TestProcess ) -> ( ) throws -> Void ) ] {
650
669
var tests = [
@@ -681,6 +700,7 @@ class TestProcess : XCTestCase {
681
700
tests += [
682
701
( " test_interrupt " , test_interrupt) ,
683
702
( " test_suspend_resume " , test_suspend_resume) ,
703
+ ( " test_fileDescriptorsAreNotInherited " , test_fileDescriptorsAreNotInherited) ,
684
704
]
685
705
#endif
686
706
return tests
0 commit comments