Skip to content

Commit 6e010ff

Browse files
authored
Merge pull request #1169 from mxcl/fix-NSError.isCancelled
Fix NSError().isCancelled
2 parents 9108bfb + 74a22e1 commit 6e010ff

File tree

10 files changed

+36
-29
lines changed

10 files changed

+36
-29
lines changed

.github/workflows/ci-shallow.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ jobs:
4141
needs: auto-cancel
4242
steps:
4343
- uses: actions/checkout@v2
44-
# we’re using the Swift 4 engine because otherwise some tests don’t make it in
45-
- run: swift test --generate-linuxmain -Xswiftc -swift-version -Xswiftc 4
44+
- run: swift test --generate-linuxmain
4645
- run: git diff --exit-code

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ pmk.exclude = [
2525
pkg.swiftLanguageVersions = [3, 4, 5]
2626
pkg.targets = [
2727
pmk,
28-
.testTarget(name: "A+", dependencies: ["PromiseKit"]),
28+
.testTarget(name: "APlus", dependencies: ["PromiseKit"], path: "Tests/A+"),
2929
.testTarget(name: "CorePromise", dependencies: ["PromiseKit"], path: "Tests/CorePromise"),
3030
]

Package@swift-4.2.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ pmk.exclude = [
2525
pkg.swiftLanguageVersions = [.v3, .v4, .v4_2]
2626
pkg.targets = [
2727
pmk,
28-
.testTarget(name: "A+", dependencies: ["PromiseKit"]),
28+
.testTarget(name: "APlus", dependencies: ["PromiseKit"], path: "Tests/A+"),
2929
.testTarget(name: "CorePromise", dependencies: ["PromiseKit"], path: "Tests/CorePromise"),
3030
]

Package@swift-5.3.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ pmk.exclude = [
2929
pkg.swiftLanguageVersions = [.v4, .v4_2, .v5]
3030
pkg.targets = [
3131
pmk,
32-
.testTarget(name: "A+", dependencies: ["PromiseKit"]),
32+
.testTarget(name: "APlus", dependencies: ["PromiseKit"], path: "Tests/A+"),
3333
.testTarget(name: "CorePromise", dependencies: ["PromiseKit"], path: "Tests/CorePromise"),
3434
]

Sources/Error.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ extension Error {
8888
} catch CocoaError.userCancelled {
8989
return true
9090
} catch {
91-
#if os(macOS) || os(iOS) || os(tvOS)
92-
let pair = { ($0.domain, $0.code) }(error as NSError)
93-
return ("SKErrorDomain", 2) == pair
91+
#if canImport(StoreKit)
92+
let domain = (error as AnyObject).value(forKey: "domain") as? String
93+
let code = (error as AnyObject).value(forKey: "code") as? Int
94+
return ("SKErrorDomain", 2) == (domain, code)
9495
#else
9596
return false
9697
#endif

Sources/when.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ public func when<It: IteratorProtocol>(fulfilled promiseIterator: It, concurrent
158158

159159
barrier.sync(flags: .barrier) {
160160
guard let next = generator.next() else { return }
161-
162161
promise = next
163-
164162
pendingPromises += 1
165163
promises.append(next)
166164
}

Tests/CorePromise/CancellableErrorTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22
import PromiseKit
33
import XCTest
44

5+
#if canImport(StoreKit)
6+
import StoreKit
7+
#endif
8+
59
class CancellationTests: XCTestCase {
610
func testCancellation() {
711
let ex1 = expectation(description: "")
@@ -95,6 +99,28 @@ class CancellationTests: XCTestCase {
9599
waitForExpectations(timeout: 1)
96100
}
97101

102+
func testDoesntCrashSwift() {
103+
#if os(macOS)
104+
// Previously exposed a bridging crash in Swift
105+
// NOTE nobody was brave enough or diligent enough to report this to Apple :{
106+
// NOTE no Linux test since this constructor doesn’t exist there
107+
XCTAssertFalse(NSError().isCancelled)
108+
#endif
109+
110+
#if canImport(StoreKit)
111+
do {
112+
let err = SKError(.paymentCancelled)
113+
XCTAssertTrue(err.isCancelled)
114+
throw err
115+
} catch {
116+
XCTAssertTrue(error.isCancelled)
117+
}
118+
119+
XCTAssertFalse(SKError(.clientInvalid).isCancelled)
120+
121+
#endif
122+
}
123+
98124
#if swift(>=3.2)
99125
func testIsCancelled() {
100126
XCTAssertTrue(PMKError.cancelled.isCancelled)

Tests/CorePromise/GuaranteeTests.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class GuaranteeTests: XCTestCase {
2626
wait(for: [ex], timeout: 10)
2727
}
2828

29-
#if swift(>=4) && !swift(>=5.2)
3029
func testMapByKeyPath() {
3130
let ex = expectation(description: "")
3231

@@ -37,7 +36,6 @@ class GuaranteeTests: XCTestCase {
3736

3837
wait(for: [ex], timeout: 10)
3938
}
40-
#endif
4139

4240
func testWait() {
4341
XCTAssertEqual(after(.milliseconds(100)).map(on: nil){ 1 }.wait(), 1)
@@ -56,7 +54,6 @@ class GuaranteeTests: XCTestCase {
5654
wait(for: [ex], timeout: 10)
5755
}
5856

59-
#if swift(>=4) && !swift(>=5.2)
6057
func testMapValuesByKeyPath() {
6158
let ex = expectation(description: "")
6259

@@ -69,7 +66,6 @@ class GuaranteeTests: XCTestCase {
6966

7067
wait(for: [ex], timeout: 10)
7168
}
72-
#endif
7369

7470
func testFlatMapValues() {
7571
let ex = expectation(description: "")
@@ -97,7 +93,6 @@ class GuaranteeTests: XCTestCase {
9793
wait(for: [ex], timeout: 10)
9894
}
9995

100-
#if swift(>=4) && !swift(>=5.2)
10196
func testCompactMapValuesByKeyPath() {
10297
let ex = expectation(description: "")
10398

@@ -110,7 +105,6 @@ class GuaranteeTests: XCTestCase {
110105

111106
wait(for: [ex], timeout: 10)
112107
}
113-
#endif
114108

115109
func testThenMap() {
116110

@@ -154,7 +148,6 @@ class GuaranteeTests: XCTestCase {
154148
wait(for: [ex], timeout: 10)
155149
}
156150

157-
#if swift(>=4) && !swift(>=5.2)
158151
func testFilterValuesByKeyPath() {
159152

160153
let ex = expectation(description: "")
@@ -168,7 +161,6 @@ class GuaranteeTests: XCTestCase {
168161

169162
wait(for: [ex], timeout: 10)
170163
}
171-
#endif
172164

173165
func testSorted() {
174166

Tests/CorePromise/ThenableTests.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class ThenableTests: XCTestCase {
4343
wait(for: [ex], timeout: 10)
4444
}
4545

46-
#if swift(>=4) && !swift(>=5.2)
4746
func testMapByKeyPath() {
4847
let ex = expectation(description: "")
4948
Promise.value(Person(name: "Max")).map(\.name).done {
@@ -52,7 +51,6 @@ class ThenableTests: XCTestCase {
5251
}.silenceWarning()
5352
wait(for: [ex], timeout: 10)
5453
}
55-
#endif
5654

5755
func testCompactMap() {
5856
let ex = expectation(description: "")
@@ -110,7 +108,6 @@ class ThenableTests: XCTestCase {
110108
wait(for: [ex], timeout: 10)
111109
}
112110

113-
#if swift(>=4) && !swift(>=5.2)
114111
func testCompactMapByKeyPath() {
115112
let ex = expectation(description: "")
116113
Promise.value(Person(name: "Roman", age: 26)).compactMap(\.age).done {
@@ -119,7 +116,6 @@ class ThenableTests: XCTestCase {
119116
}.silenceWarning()
120117
wait(for: [ex], timeout: 10)
121118
}
122-
#endif
123119

124120
func testMapValues() {
125121
let ex = expectation(description: "")
@@ -132,7 +128,6 @@ class ThenableTests: XCTestCase {
132128
wait(for: [ex], timeout: 10)
133129
}
134130

135-
#if swift(>=4) && !swift(>=5.2)
136131
func testMapValuesByKeyPath() {
137132
let ex = expectation(description: "")
138133
Promise.value([Person(name: "Max"), Person(name: "Roman"), Person(name: "John")]).mapValues(\.name).done {
@@ -141,7 +136,6 @@ class ThenableTests: XCTestCase {
141136
}.silenceWarning()
142137
wait(for: [ex], timeout: 10)
143138
}
144-
#endif
145139

146140
func testCompactMapValues() {
147141
let ex = expectation(description: "")
@@ -154,7 +148,6 @@ class ThenableTests: XCTestCase {
154148
wait(for: [ex], timeout: 10)
155149
}
156150

157-
#if swift(>=4) && !swift(>=5.2)
158151
func testCompactMapValuesByKeyPath() {
159152
let ex = expectation(description: "")
160153
Promise.value([Person(name: "Max"), Person(name: "Roman", age: 26), Person(name: "John", age: 23)]).compactMapValues(\.age).done {
@@ -163,7 +156,6 @@ class ThenableTests: XCTestCase {
163156
}.silenceWarning()
164157
wait(for: [ex], timeout: 10)
165158
}
166-
#endif
167159

168160
func testThenMap() {
169161
let ex = expectation(description: "")
@@ -198,7 +190,6 @@ class ThenableTests: XCTestCase {
198190
wait(for: [ex], timeout: 10)
199191
}
200192

201-
#if swift(>=4) && !swift(>=5.2)
202193
func testFilterValuesByKeyPath() {
203194
let ex = expectation(description: "")
204195
Promise.value([Person(name: "Max"), Person(name: "Roman", age: 26, isStudent: false), Person(name: "John", age: 23, isStudent: true)]).filterValues(\.isStudent).done {
@@ -207,8 +198,7 @@ class ThenableTests: XCTestCase {
207198
}.silenceWarning()
208199
wait(for: [ex], timeout: 10)
209200
}
210-
#endif
211-
201+
212202
func testLastValueForEmpty() {
213203
XCTAssertTrue(Promise.value([]).lastValue.isRejected)
214204
}

Tests/CorePromise/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extension CancellationTests {
1818
// to regenerate.
1919
static let __allTests__CancellationTests = [
2020
("testCancellation", testCancellation),
21+
("testDoesntCrashSwift", testDoesntCrashSwift),
2122
("testFoundationBridging1", testFoundationBridging1),
2223
("testFoundationBridging2", testFoundationBridging2),
2324
("testIsCancelled", testIsCancelled),

0 commit comments

Comments
 (0)