Skip to content

Commit c4aad6e

Browse files
committed
DarwinComptibilityTests: Add Xcode Project
- Add shims and minor test fixes. - Disable XDG tests as the xdgTestHelper does not currently work. - Add handling for some tests requiring macOS 10.13 that run on 10.12 when run against the sclf version of Foundation. This requires some of the tests to be duplicated and wrapped inside of a DARWIN_COMPATIBILITY_TEST conditional compile.
1 parent 4a35a90 commit c4aad6e

12 files changed

+996
-40
lines changed

DarwinCompatibilityTests.xcodeproj/project.pbxproj

+796
Large diffs are not rendered by default.

DarwinCompatibilityTests/Info.plist

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

DarwinShims.swift

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
// These shims are used solely by the DarwinCompatibilityTests Xcode project to
11+
// allow the TestFoundation tests to compile and run against Darwin's native
12+
// Foundation.
13+
// It contains wrappers for methods (some experimental) added in
14+
// swift-corelibs-foundation which do not exist in Foundation, and other small
15+
// differences.
16+
17+
import Foundation
18+
19+
20+
public typealias unichar = UInt16
21+
22+
extension unichar : ExpressibleByUnicodeScalarLiteral {
23+
public typealias UnicodeScalarLiteralType = UnicodeScalar
24+
25+
public init(unicodeScalarLiteral scalar: UnicodeScalar) {
26+
self.init(scalar.value)
27+
}
28+
}
29+
30+
extension NSURL {
31+
func checkResourceIsReachable() throws -> Bool {
32+
var error: NSError?
33+
if checkResourceIsReachableAndReturnError(&error) {
34+
return true
35+
} else {
36+
if let e = error {
37+
throw e
38+
}
39+
}
40+
return false
41+
}
42+
}
43+
44+
extension Thread {
45+
class var mainThread: Thread {
46+
return Thread.main
47+
}
48+
}
49+
50+
extension Scanner {
51+
public func scanString(_ searchString: String) -> String? {
52+
var result: NSString? = nil
53+
if scanString(string, into: &result), let str = result {
54+
return str as String
55+
}
56+
return nil
57+
}
58+
}
59+
60+
extension JSONSerialization {
61+
class func writeJSONObject(_ obj: Any, toStream stream: OutputStream, options opt: WritingOptions) throws -> Int {
62+
var error: NSError?
63+
let ret = writeJSONObject(obj, to: stream, options: opt, error: &error)
64+
guard ret != 0 else {
65+
throw error!
66+
}
67+
return ret
68+
}
69+
}
70+
71+
extension NSIndexSet {
72+
func _bridgeToSwift() -> NSIndexSet {
73+
return self
74+
}
75+
}
76+
77+
extension CharacterSet {
78+
func _bridgeToSwift() -> CharacterSet {
79+
return self
80+
}
81+
}
82+
83+
extension NSCharacterSet {
84+
func _bridgeToSwift() -> CharacterSet {
85+
return self as CharacterSet
86+
}
87+
}

TestFoundation/TestBundle.swift

+22-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
1818

1919

2020
internal func testBundle() -> Bundle {
21+
#if DARWIN_COMPATIBILITY_TESTS
22+
for bundle in Bundle.allBundles {
23+
if let bundleId = bundle.bundleIdentifier, bundleId == "org.swift.DarwinCompatibilityTests", bundle.resourcePath != nil {
24+
return bundle
25+
}
26+
}
27+
fatalError("Cant find test bundle")
28+
#else
2129
return Bundle.main
30+
#endif
2231
}
2332

2433
class TestBundle : XCTestCase {
@@ -88,14 +97,24 @@ class TestBundle : XCTestCase {
8897
let bundle = testBundle()
8998

9099
// bundleIdentifier
100+
#if DARWIN_COMPATIBILITY_TESTS
101+
XCTAssertEqual("org.swift.DarwinCompatibilityTests", bundle.bundleIdentifier)
102+
#else
91103
XCTAssertEqual("org.swift.TestFoundation", bundle.bundleIdentifier)
92-
104+
#endif
105+
93106
// infoDictionary
94107
let info = bundle.infoDictionary
95108
XCTAssertNotNil(info)
96-
XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)
109+
110+
#if DARWIN_COMPATIBILITY_TESTS
111+
XCTAssert("DarwinCompatibilityTests" == info!["CFBundleName"] as! String)
112+
XCTAssert("org.swift.DarwinCompatibilityTests" == info!["CFBundleIdentifier"] as! String)
113+
#else
97114
XCTAssert("TestFoundation" == info!["CFBundleName"] as! String)
98-
115+
XCTAssert("org.swift.TestFoundation" == info!["CFBundleIdentifier"] as! String)
116+
#endif
117+
99118
// localizedInfoDictionary
100119
XCTAssertNil(bundle.localizedInfoDictionary) // FIXME: Add a localized Info.plist for testing
101120
}
@@ -204,6 +223,4 @@ class TestBundle : XCTestCase {
204223
XCTAssertThrowsError(try bundle!.preflight())
205224
_cleanupPlayground(playground)
206225
}
207-
208-
209226
}

TestFoundation/TestHTTPCookieStorage.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class TestHTTPCookieStorage: XCTestCase {
244244
}
245245

246246
func test_cookieInXDGSpecPath() {
247-
#if !os(Android)
247+
#if !os(Android) && !DARWIN_COMPATIBILITY_TESTS
248248
//Test without setting the environment variable
249249
let testCookie = HTTPCookie(properties: [
250250
.name: "TestCookie0",

TestFoundation/TestJSONEncoder.swift

+24-17
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,27 @@ class TestJSONEncoder : XCTestCase {
105105
}
106106

107107
func test_encodingOutputFormattingSortedKeys() {
108+
let expectedJSON = "{\"email\":\"appleseed@apple.com\",\"name\":\"Johnny Appleseed\"}".data(using: .utf8)!
109+
let person = Person.testValue
110+
#if os(OSX) || DARWIN_COMPATIBILITY_TESTS
108111
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
109-
let expectedJSON = "{\"email\":\"appleseed@apple.com\",\"name\":\"Johnny Appleseed\"}".data(using: .utf8)!
110-
let person = Person.testValue
111112
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
112113
}
114+
#else
115+
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
116+
#endif
113117
}
114118

115119
func test_encodingOutputFormattingPrettyPrintedSortedKeys() {
120+
let expectedJSON = "{\n \"email\" : \"appleseed@apple.com\",\n \"name\" : \"Johnny Appleseed\"\n}".data(using: .utf8)!
121+
let person = Person.testValue
122+
#if os(OSX) || DARWIN_COMPATIBILITY_TESTS
116123
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
117-
let expectedJSON = "{\n \"email\" : \"appleseed@apple.com\",\n \"name\" : \"Johnny Appleseed\"\n}".data(using: .utf8)!
118-
let person = Person.testValue
119124
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted, .sortedKeys])
120125
}
126+
#else
127+
_testRoundTrip(of: person, expectedJSON: expectedJSON, outputFormatting: [.prettyPrinted, .sortedKeys])
128+
#endif
121129
}
122130

123131
// MARK: - Date Strategy Tests
@@ -151,19 +159,18 @@ class TestJSONEncoder : XCTestCase {
151159
}
152160

153161
func test_encodingDateISO8601() {
154-
if #available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
155-
let formatter = ISO8601DateFormatter()
156-
formatter.formatOptions = .withInternetDateTime
157-
158-
let timestamp = Date(timeIntervalSince1970: 1000)
159-
let expectedJSON = "[\"\(formatter.string(from: timestamp))\"]".data(using: .utf8)!
160-
161-
// We can't encode a top-level Date, so it'll be wrapped in an array.
162-
_testRoundTrip(of: TopLevelArrayWrapper(timestamp),
163-
expectedJSON: expectedJSON,
164-
dateEncodingStrategy: .iso8601,
165-
dateDecodingStrategy: .iso8601)
166-
}
162+
let formatter = ISO8601DateFormatter()
163+
formatter.formatOptions = .withInternetDateTime
164+
165+
let timestamp = Date(timeIntervalSince1970: 1000)
166+
let expectedJSON = "[\"\(formatter.string(from: timestamp))\"]".data(using: .utf8)!
167+
168+
// We can't encode a top-level Date, so it'll be wrapped in an array.
169+
_testRoundTrip(of: TopLevelArrayWrapper(timestamp),
170+
expectedJSON: expectedJSON,
171+
dateEncodingStrategy: .iso8601,
172+
dateDecodingStrategy: .iso8601)
173+
167174
}
168175

169176
func test_encodingDateFormatted() {

TestFoundation/TestJSONSerialization.swift

+18-11
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ extension TestJSONSerialization {
14151415
}
14161416

14171417
func test_jsonObjectToOutputStreamInsufficientBuffer() {
1418+
#if !DARWIN_COMPATIBILITY_TESTS // Hangs
14181419
let dict = ["a":["b":1]]
14191420
let buffer = Array<UInt8>(repeating: 0, count: 10)
14201421
let outputStream = OutputStream(toBuffer: UnsafeMutablePointer(mutating: buffer), capacity: buffer.count)
@@ -1428,6 +1429,7 @@ extension TestJSONSerialization {
14281429
} catch {
14291430
XCTFail("Error occurred while writing to stream")
14301431
}
1432+
#endif
14311433
}
14321434

14331435
func test_booleanJSONObject() {
@@ -1469,18 +1471,23 @@ extension TestJSONSerialization {
14691471
XCTFail("Failed during serialization")
14701472
}
14711473
}
1472-
1473-
func test_serializeSortedKeys() {
1474-
var dict: [String: Any]
1475-
1476-
dict = ["z": 1, "y": 1, "x": 1, "w": 1, "v": 1, "u": 1, "t": 1, "s": 1, "r": 1, "q": 1, ]
1477-
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"q\":1,\"r\":1,\"s\":1,\"t\":1,\"u\":1,\"v\":1,\"w\":1,\"x\":1,\"y\":1,\"z\":1}")
1478-
1479-
dict = ["aaaa": 1, "aaa": 1, "aa": 1, "a": 1]
1480-
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"a\":1,\"aa\":1,\"aaa\":1,\"aaaa\":1}")
14811474

1482-
dict = ["c": ["c":1,"b":1,"a":1],"b":["c":1,"b":1,"a":1],"a":["c":1,"b":1,"a":1]]
1483-
XCTAssertEqual(try trySerialize(dict, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
1475+
func test_serializeSortedKeys() {
1476+
let dict1 = ["z": 1, "y": 1, "x": 1, "w": 1, "v": 1, "u": 1, "t": 1, "s": 1, "r": 1, "q": 1, ]
1477+
let dict2 = ["aaaa": 1, "aaa": 1, "aa": 1, "a": 1]
1478+
let dict3 = ["c": ["c":1,"b":1,"a":1],"b":["c":1,"b":1,"a":1],"a":["c":1,"b":1,"a":1]]
1479+
1480+
#if DARWIN_COMPATIBILITY_TESTS
1481+
if #available(OSX 10.13, *) {
1482+
XCTAssertEqual(try trySerialize(dict1, options: .sortedKeys), "{\"q\":1,\"r\":1,\"s\":1,\"t\":1,\"u\":1,\"v\":1,\"w\":1,\"x\":1,\"y\":1,\"z\":1}")
1483+
XCTAssertEqual(try trySerialize(dict2, options: .sortedKeys), "{\"a\":1,\"aa\":1,\"aaa\":1,\"aaaa\":1}")
1484+
XCTAssertEqual(try trySerialize(dict3, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
1485+
}
1486+
#else
1487+
XCTAssertEqual(try trySerialize(dict1, options: .sortedKeys), "{\"q\":1,\"r\":1,\"s\":1,\"t\":1,\"u\":1,\"v\":1,\"w\":1,\"x\":1,\"y\":1,\"z\":1}")
1488+
XCTAssertEqual(try trySerialize(dict2, options: .sortedKeys), "{\"a\":1,\"aa\":1,\"aaa\":1,\"aaaa\":1}")
1489+
XCTAssertEqual(try trySerialize(dict3, options: .sortedKeys), "{\"a\":{\"a\":1,\"b\":1,\"c\":1},\"b\":{\"a\":1,\"b\":1,\"c\":1},\"c\":{\"a\":1,\"b\":1,\"c\":1}}")
1490+
#endif
14841491
}
14851492

14861493
func test_serializePrettyPrinted() {

TestFoundation/TestNSArray.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,21 @@ class TestNSArray : XCTestCase {
559559
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
560560
let testFile = tempDir + "/readWriteURL.txt"
561561
let url = URL(fileURLWithPath: testFile)
562+
let data2: NSArray
563+
#if DARWIN_COMPATIBILITY_TESTS
564+
if #available(OSX 10.13, *) {
565+
try data.write(to: url)
566+
data2 = try NSArray(contentsOf: url, error: ())
567+
} else {
568+
guard data.write(toFile: testFile, atomically: true) else {
569+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue)
570+
}
571+
data2 = NSArray(contentsOfFile: testFile)!
572+
}
573+
#else
562574
try data.write(to: url)
563-
let data2 = try NSArray(contentsOf: url, error: ())
575+
data2 = try NSArray(contentsOf: url, error: ())
576+
#endif
564577
XCTAssertEqual(data, data2)
565578
removeTestFile(testFile)
566579
} catch let e {

TestFoundation/TestNSKeyedArchiver.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,21 @@ class TestNSKeyedArchiver : XCTestCase {
285285

286286
let s1 = String(cString: charPtr)
287287
let s2 = String(cString: expectedCharPtr!)
288-
288+
289+
#if !DARWIN_COMPATIBILITY_TESTS
289290
// On Darwin decoded strings would belong to the autorelease pool, but as we don't have
290291
// one in SwiftFoundation let's explicitly deallocate it here.
291292
expectedCharPtr!.deallocate()
292-
293+
#endif
293294
return s1 == s2
294295
})
295296
}
296297

297298
func test_archive_user_class() {
299+
#if !DARWIN_COMPATIBILITY_TESTS // Causes SIGABRT
298300
let userClass = UserClass(1234)
299301
test_archive(userClass)
302+
#endif
300303
}
301304

302305
func test_archive_ns_user_class() {

TestFoundation/TestNSProgressFraction.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import SwiftFoundation
1515
import SwiftXCTest
1616
#endif
1717

18+
#if !DARWIN_COMPATIBILITY_TESTS
1819
class TestProgressFraction : XCTestCase {
1920
static var allTests: [(String, (TestProgressFraction) -> () throws -> Void)] {
2021
return [
@@ -159,4 +160,5 @@ class TestProgressFraction : XCTestCase {
159160
XCTAssertFalse(r.overflowed)
160161
}
161162
}
163+
#endif
162164

TestFoundation/TestUserDefaults.swift

+4
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ class TestUserDefaults : XCTestCase {
178178
}
179179

180180
func test_setValue_String() {
181+
#if !DARWIN_COMPATIBILITY_TESTS // Works if run on its own, hangs if all tests in class are run
181182
let defaults = UserDefaults.standard
182183

183184
// Register a String value. UserDefaults.string(forKey:) is supposed to return the String
184185
defaults.set("hello", forKey: "key1")
185186

186187
XCTAssertEqual(defaults.string(forKey: "key1"), "hello")
188+
#endif
187189
}
188190

189191
func test_setValue_NSURL() {
@@ -196,12 +198,14 @@ class TestUserDefaults : XCTestCase {
196198
}
197199

198200
func test_setValue_URL() {
201+
#if !DARWIN_COMPATIBILITY_TESTS // Works if run on its own, hangs if all tests in class are run
199202
let defaults = UserDefaults.standard
200203

201204
// Set a URL value. UserDefaults.url(forKey:) is supposed to return the URL
202205
defaults.set(URL(fileURLWithPath: "/hello/world"), forKey: "key1")
203206

204207
XCTAssertEqual(defaults.url(forKey: "key1"), URL(fileURLWithPath: "/hello/world"))
208+
#endif
205209
}
206210

207211
func test_setValue_NSData() {

TestFoundation/TestXMLParser.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import SwiftXCTest
1717
#endif
1818

19-
class OptionalParserConformance: XMLParserDelegate {}
20-
2119
enum XMLParserDelegateEvent {
2220
case startDocument
2321
case endDocument
@@ -49,7 +47,7 @@ extension XMLParserDelegateEvent: Equatable {
4947

5048
}
5149

52-
class XMLParserDelegateEventStream: XMLParserDelegate {
50+
class XMLParserDelegateEventStream: NSObject, XMLParserDelegate {
5351
var events: [XMLParserDelegateEvent] = []
5452

5553
func parserDidStartDocument(_ parser: XMLParser) {

0 commit comments

Comments
 (0)