@@ -750,9 +750,10 @@ class TestProcess : XCTestCase {
750
750
}
751
751
}
752
752
753
+ #if !os(Windows)
753
754
func test_fileDescriptorsAreNotInherited( ) throws {
754
755
let task = Process ( )
755
- let clonedFD = dup ( 1 )
756
+ let someExtraFDs = [ dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) , dup ( 1 ) ]
756
757
task. executableURL = xdgTestHelperURL ( )
757
758
task. arguments = [ " --print-open-file-descriptors " ]
758
759
task. standardInput = FileHandle . nullDevice
@@ -761,14 +762,32 @@ class TestProcess : XCTestCase {
761
762
task. standardError = FileHandle . nullDevice
762
763
XCTAssertNoThrow ( try task. run ( ) )
763
764
764
- try task. run ( )
765
765
try stdoutPipe. fileHandleForWriting. close ( )
766
766
let stdoutData = try stdoutPipe. fileHandleForReading. readToEnd ( )
767
767
task. waitUntilExit ( )
768
- print ( String ( decoding: stdoutData ?? Data ( ) , as: Unicode . UTF8. self) )
769
- XCTAssertEqual ( " 0 \n 1 \n 2 \n " , String ( decoding: stdoutData ?? Data ( ) , as: Unicode . UTF8. self) )
770
- close ( clonedFD)
771
- }
768
+ let stdoutString = String ( decoding: stdoutData ?? Data ( ) , as: Unicode . UTF8. self)
769
+ #if os(macOS)
770
+ XCTAssertEqual ( " 0 \n 1 \n 2 \n " , stdoutString)
771
+ #else
772
+ // on Linux we may also have a /dev/urandom open as well as some socket that Process uses for something.
773
+
774
+ // we should definitely have stdin (0), stdout (1), and stderr (2) open
775
+ XCTAssert ( stdoutString. utf8. starts ( with: " 0 \n 1 \n 2 \n " . utf8) )
776
+
777
+ // in total we should have 6 or fewer lines:
778
+ // 1. stdin
779
+ // 2. stdout
780
+ // 3. stderr
781
+ // 4. /dev/urandom (optional)
782
+ // 5. communication socket (optional)
783
+ // 6. trailing new line
784
+ XCTAssertLessThanOrEqual ( stdoutString. components ( separatedBy: " \n " ) . count, 6 , " \( stdoutString) " )
785
+ #endif
786
+ for fd in someExtraFDs {
787
+ close ( fd)
788
+ }
789
+ }
790
+ #endif
772
791
773
792
static var allTests : [ ( String , ( TestProcess ) -> ( ) throws -> Void ) ] {
774
793
var tests = [
@@ -807,6 +826,7 @@ class TestProcess : XCTestCase {
807
826
tests += [
808
827
( " test_interrupt " , test_interrupt) ,
809
828
( " test_suspend_resume " , test_suspend_resume) ,
829
+ ( " test_fileDescriptorsAreNotInherited " , test_fileDescriptorsAreNotInherited) ,
810
830
]
811
831
#endif
812
832
return tests
0 commit comments