Skip to content

Commit da19586

Browse files
authored
Merge pull request #2558 from spevans/pr_darwin_compatibility2
DarwinCompatibilty: Add shims for missing FileHandle methods
2 parents 817bb47 + 815377c commit da19586

File tree

9 files changed

+35
-58
lines changed

9 files changed

+35
-58
lines changed

DarwinCompatibilityTests.xcodeproj/project.pbxproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
B9C89FCC1F6DCAEB00087AF4 /* NSKeyedUnarchiver-NotificationTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = B9C89FB71F6DCAEB00087AF4 /* NSKeyedUnarchiver-NotificationTest.plist */; };
127127
B9C89FCD1F6DCAEB00087AF4 /* PropertyList-1.0.dtd in Resources */ = {isa = PBXBuildFile; fileRef = B9C89FB81F6DCAEB00087AF4 /* PropertyList-1.0.dtd */; };
128128
B9C89FCE1F6DCAEB00087AF4 /* Test.plist in Resources */ = {isa = PBXBuildFile; fileRef = B9C89FB91F6DCAEB00087AF4 /* Test.plist */; };
129+
B9ED84FD23641F7000A58AF2 /* DarwinShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9F3269E1FC714DD003C3599 /* DarwinShims.swift */; };
129130
B9F137A120B998D0000B7577 /* xdgTestHelper in CopyFiles */ = {isa = PBXBuildFile; fileRef = B917D31C20B0DB8B00728EE0 /* xdgTestHelper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
130131
B9F326A01FC714DD003C3599 /* DarwinShims.swift in Sources */ = {isa = PBXBuildFile; fileRef = B9F3269E1FC714DD003C3599 /* DarwinShims.swift */; };
131132
DAE7D0F320D8224200DC6C54 /* TestURLProtectionSpace.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAE7D0F220D8224200DC6C54 /* TestURLProtectionSpace.swift */; };
@@ -605,6 +606,7 @@
605606
buildActionMask = 2147483647;
606607
files = (
607608
B917D32620B0DE2000728EE0 /* main.swift in Sources */,
609+
B9ED84FD23641F7000A58AF2 /* DarwinShims.swift in Sources */,
608610
);
609611
runOnlyForDeploymentPostprocessing = 0;
610612
};
@@ -727,7 +729,7 @@
727729
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
728730
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
729731
CODE_SIGN_STYLE = Automatic;
730-
MACOSX_DEPLOYMENT_TARGET = 10.14;
732+
MACOSX_DEPLOYMENT_TARGET = 10.15;
731733
OTHER_SWIFT_FLAGS = "-DDEPLOYMENT_RUNTIME_OBJC -DDARWIN_COMPATIBILITY_TESTS";
732734
PRODUCT_NAME = "$(TARGET_NAME)";
733735
SWIFT_VERSION = 5.0;
@@ -741,7 +743,7 @@
741743
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
742744
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
743745
CODE_SIGN_STYLE = Automatic;
744-
MACOSX_DEPLOYMENT_TARGET = 10.14;
746+
MACOSX_DEPLOYMENT_TARGET = 10.15;
745747
OTHER_SWIFT_FLAGS = "-DDEPLOYMENT_RUNTIME_OBJC -DDARWIN_COMPATIBILITY_TESTS";
746748
PRODUCT_NAME = "$(TARGET_NAME)";
747749
SWIFT_VERSION = 5.0;
@@ -796,7 +798,7 @@
796798
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
797799
GCC_WARN_UNUSED_FUNCTION = YES;
798800
GCC_WARN_UNUSED_VARIABLE = YES;
799-
MACOSX_DEPLOYMENT_TARGET = 10.14;
801+
MACOSX_DEPLOYMENT_TARGET = 10.15;
800802
MTL_ENABLE_DEBUG_INFO = YES;
801803
ONLY_ACTIVE_ARCH = YES;
802804
SDKROOT = macosx;
@@ -847,7 +849,7 @@
847849
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
848850
GCC_WARN_UNUSED_FUNCTION = YES;
849851
GCC_WARN_UNUSED_VARIABLE = YES;
850-
MACOSX_DEPLOYMENT_TARGET = 10.14;
852+
MACOSX_DEPLOYMENT_TARGET = 10.15;
851853
MTL_ENABLE_DEBUG_INFO = NO;
852854
SDKROOT = macosx;
853855
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
@@ -861,7 +863,7 @@
861863
COMBINE_HIDPI_IMAGES = YES;
862864
INFOPLIST_FILE = DarwinCompatibilityTests/Info.plist;
863865
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
864-
MACOSX_DEPLOYMENT_TARGET = 10.14;
866+
MACOSX_DEPLOYMENT_TARGET = 10.15;
865867
"OTHER_SWIFT_FLAGS[arch=*]" = "-DDEPLOYMENT_RUNTIME_OBJC -DDARWIN_COMPATIBILITY_TESTS";
866868
PRODUCT_BUNDLE_IDENTIFIER = org.swift.DarwinCompatibilityTests;
867869
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -876,7 +878,7 @@
876878
COMBINE_HIDPI_IMAGES = YES;
877879
INFOPLIST_FILE = DarwinCompatibilityTests/Info.plist;
878880
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
879-
MACOSX_DEPLOYMENT_TARGET = 10.14;
881+
MACOSX_DEPLOYMENT_TARGET = 10.15;
880882
"OTHER_SWIFT_FLAGS[arch=*]" = "-DDEPLOYMENT_RUNTIME_OBJC -DDARWIN_COMPATIBILITY_TESTS";
881883
PRODUCT_BUNDLE_IDENTIFIER = org.swift.DarwinCompatibilityTests;
882884
PRODUCT_NAME = "$(TARGET_NAME)";

DarwinCompatibilityTests.xcodeproj/xcshareddata/xcschemes/xdgTestHelper.xcscheme

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@
5050
<Test
5151
Identifier = "TestDateComponents/test_hash()">
5252
</Test>
53-
<Test
54-
Identifier = "TestFileHandle">
55-
</Test>
56-
<Test
57-
Identifier = "TestPipe">
58-
</Test>
5953
<Test
6054
Identifier = "TestURLComponents/test_hash()">
6155
</Test>

DarwinCompatibilityTests/DarwinShims.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,27 @@ extension NSCharacterSet {
8383
return self as CharacterSet
8484
}
8585
}
86+
87+
88+
extension FileHandle {
89+
public func offset() throws -> UInt64 {
90+
return self.offsetInFile
91+
}
92+
93+
public func read(upToCount count: Int) throws -> Data? {
94+
guard count > 0 else { return nil }
95+
let data = readData(ofLength: count)
96+
if data.count == 0 { return nil }
97+
return data
98+
}
99+
100+
public func readToEnd() throws -> Data? {
101+
try read(upToCount: Int.max)
102+
}
103+
104+
public func write<T: DataProtocol>(contentsOf data: T) throws {
105+
if let d = data as? Data {
106+
self.write(d)
107+
}
108+
}
109+
}

TestFoundation/TestFileHandle.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
#if !DARWIN_COMPATIBILITY_TESTS // Disable until Foundation has the new FileHandle API
1110

1211
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
1312
#if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC
@@ -565,5 +564,3 @@ class TestFileHandle : XCTestCase {
565564
return tests
566565
}
567566
}
568-
569-
#endif

TestFoundation/TestPipe.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
#if !DARWIN_COMPATIBILITY_TESTS // Disable until Foundation has the new FileHandle API
1110

1211
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
1312
#if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC
@@ -79,4 +78,3 @@ class TestPipe: XCTestCase {
7978
XCTAssertEqual(text, convertedData)
8079
}
8180
}
82-
#endif

TestFoundation/TestProcess.swift

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ class TestProcess : XCTestCase {
8484
try process.run()
8585
let msg = try XCTUnwrap("Hello, 🐶.\n".data(using: .utf8))
8686
do {
87-
#if DARWIN_COMPATIBILITY_TESTS
88-
inputPipe.fileHandleForWriting.write(msg)
89-
#else
9087
try inputPipe.fileHandleForWriting.write(contentsOf: msg)
91-
#endif
9288
} catch {
9389
XCTFail("Cant write to pipe: \(error)")
9490
return
@@ -585,14 +581,9 @@ class TestProcess : XCTestCase {
585581
stdoutPipe.fileHandleForReading.readabilityHandler = nil
586582

587583
try dataLock.synchronized {
588-
#if DARWIN_COMPATIBILITY_TESTS
589-
// Use old API for now
590-
stdoutData.append(stdoutPipe.fileHandleForReading.availableData)
591-
#else
592584
if let d = try stdoutPipe.fileHandleForReading.readToEnd() {
593585
stdoutData.append(d)
594586
}
595-
#endif
596587
XCTAssertEqual(String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines), "No files specified.")
597588
}
598589
}
@@ -672,14 +663,9 @@ class TestProcess : XCTestCase {
672663
}
673664

674665
var stdoutData = Data()
675-
#if DARWIN_COMPATIBILITY_TESTS
676-
// Use old API for now
677-
stdoutData.append(stdoutPipe.fileHandleForReading.availableData)
678-
#else
679-
if let d = try stdoutPipe.fileHandleForReading.readToEnd() {
680-
stdoutData.append(d)
681-
}
682-
#endif
666+
if let d = try stdoutPipe.fileHandleForReading.readToEnd() {
667+
stdoutData.append(d)
668+
}
683669

684670
guard let stdout = String(data: stdoutData, encoding: .utf8) else {
685671
throw Error.UnicodeDecodingError(stdoutData)
@@ -948,19 +934,13 @@ internal func runTask(_ arguments: [String], environment: [String: String]? = ni
948934

949935
return try dataLock.synchronized {
950936
// Drain any data remaining in the pipes
951-
#if DARWIN_COMPATIBILITY_TESTS
952-
// Use old API for now
953-
stdoutData.append(stdoutPipe.fileHandleForReading.availableData)
954-
stderrData.append(stderrPipe.fileHandleForReading.availableData)
955-
#else
956937
if let d = try stdoutPipe.fileHandleForReading.readToEnd() {
957938
stdoutData.append(d)
958939
}
959940

960941
if let d = try stderrPipe.fileHandleForReading.readToEnd() {
961942
stderrData.append(d)
962943
}
963-
#endif
964944

965945
guard let stdout = String(data: stdoutData, encoding: .utf8) else {
966946
throw Error.UnicodeDecodingError(stdoutData)

TestFoundation/TestScanner.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10-
#if !DARWIN_COMPATIBILITY_TESTS // Disable until Foundation has the new Scanner API
11-
1210
fileprivate func withScanner(for string: String, invoking block: ((Scanner) throws -> Void)? = nil) rethrows {
1311
let scanner = Scanner(string: string)
1412
scanner.locale = Locale(identifier: "en_US_POSIX")
@@ -528,5 +526,3 @@ class TestScanner : XCTestCase {
528526
]
529527
}
530528
}
531-
532-
#endif

TestFoundation/Utilities.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,7 @@ private var shouldRunXFailTests: Bool {
528528

529529

530530
private func printStderr(_ msg: String) {
531-
#if DARWIN_COMPATIBILITY_TESTS
532-
FileHandle.standardError.write(Data(msg.utf8))
533-
#else
534531
try? FileHandle.standardError.write(contentsOf: Data(msg.utf8))
535-
#endif
536532
}
537533

538534
func shouldAttemptXFailTests(_ reason: String) -> Bool {

TestFoundation/xdgTestHelper/main.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,9 @@ func cat(_ args: ArraySlice<String>.Iterator) {
180180
exitCode = 1
181181
return
182182
}
183-
#if DARWIN_COMPATIBILITY_TESTS
184-
var data: Data
185-
repeat {
186-
data = fh.readData(ofLength: Int.max)
187-
if data.count > 0 {
188-
FileHandle.standardOutput.write(data)
189-
}
190-
} while data.count > 0
191-
#else
192183
while let data = try fh.readToEnd() {
193184
try FileHandle.standardOutput.write(contentsOf: data)
194185
}
195-
#endif
196186
}
197187
catch { print(error) }
198188
}

0 commit comments

Comments
 (0)