Skip to content

Commit e4201de

Browse files
authored
[APIDiff] Enable tests on non-Darwin (#3519)
1 parent 5a50cf2 commit e4201de

File tree

1 file changed

+20
-56
lines changed

1 file changed

+20
-56
lines changed

Tests/CommandsTests/APIDiffTests.swift

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@ final class APIDiffTests: XCTestCase {
2727
return try SwiftPMProduct.SwiftPackage.execute(args, packagePath: packagePath, env: environment)
2828
}
2929

30+
func skipIfApiDigesterUnsupported() throws {
31+
guard let tool = try? Resources.default.toolchain.getSwiftAPIDigester() else {
32+
throw XCTSkip("swift-api-digester unavailable")
33+
}
34+
guard localFileSystem.isSymlink(tool) else {
35+
// The version of Swift with a supported swift-api-digester doesn't have
36+
// a version number yet, so use whether or not the tool is a symlink to
37+
// determine if it's from a recent snapshot.
38+
throw XCTSkip("swift-api-digester is too old")
39+
}
40+
}
41+
3042
func testSimpleAPIDiff() throws {
31-
#if os(macOS)
32-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
33-
throw XCTSkip("swift-api-digester not available")
34-
}
43+
try skipIfApiDigesterUnsupported()
3544
fixture(name: "Miscellaneous/APIDiff/") { prefix in
3645
let packageRoot = prefix.appending(component: "Foo")
3746
// Overwrite the existing decl.
@@ -47,16 +56,10 @@ final class APIDiffTests: XCTestCase {
4756
XCTAssertTrue(output.contains("💔 API breakage: func foo() has been removed"))
4857
}
4958
}
50-
#else
51-
throw XCTSkip("Test unsupported on current platform")
52-
#endif
5359
}
5460

5561
func testMultiTargetAPIDiff() throws {
56-
#if os(macOS)
57-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
58-
throw XCTSkip("swift-api-digester not available")
59-
}
62+
try skipIfApiDigesterUnsupported()
6063
fixture(name: "Miscellaneous/APIDiff/") { prefix in
6164
let packageRoot = prefix.appending(component: "Bar")
6265
try localFileSystem.writeFileContents(packageRoot.appending(components: "Sources", "Baz", "Baz.swift")) {
@@ -77,9 +80,6 @@ final class APIDiffTests: XCTestCase {
7780
XCTAssertTrue(output.contains("💔 API breakage: func bar() has been removed"))
7881
}
7982
}
80-
#else
81-
throw XCTSkip("Test unsupported on current platform")
82-
#endif
8383
}
8484

8585
func testBreakageAllowlist() throws {
@@ -120,10 +120,7 @@ final class APIDiffTests: XCTestCase {
120120
}
121121

122122
func testCheckVendedModulesOnly() throws {
123-
#if os(macOS)
124-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
125-
throw XCTSkip("swift-api-digester not available")
126-
}
123+
try skipIfApiDigesterUnsupported()
127124
fixture(name: "Miscellaneous/APIDiff/") { prefix in
128125
let packageRoot = prefix.appending(component: "NonAPILibraryTargets")
129126
try localFileSystem.writeFileContents(packageRoot.appending(components: "Sources", "Foo", "Foo.swift")) {
@@ -156,16 +153,10 @@ final class APIDiffTests: XCTestCase {
156153
XCTAssertFalse(output.contains("💔 API breakage: var Qux.x has been removed"))
157154
}
158155
}
159-
#else
160-
throw XCTSkip("Test unsupported on current platform")
161-
#endif
162156
}
163157

164158
func testFilters() throws {
165-
#if os(macOS)
166-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
167-
throw XCTSkip("swift-api-digester not available")
168-
}
159+
try skipIfApiDigesterUnsupported()
169160
fixture(name: "Miscellaneous/APIDiff/") { prefix in
170161
let packageRoot = prefix.appending(component: "NonAPILibraryTargets")
171162
try localFileSystem.writeFileContents(packageRoot.appending(components: "Sources", "Foo", "Foo.swift")) {
@@ -234,16 +225,10 @@ final class APIDiffTests: XCTestCase {
234225
XCTAssertTrue(stderr.contains("'Exec' is not a library target"))
235226
}
236227
}
237-
#else
238-
throw XCTSkip("Test unsupported on current platform")
239-
#endif
240228
}
241229

242230
func testAPIDiffOfModuleWithCDependency() throws {
243-
#if os(macOS)
244-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
245-
throw XCTSkip("swift-api-digester not available")
246-
}
231+
try skipIfApiDigesterUnsupported()
247232
fixture(name: "Miscellaneous/APIDiff/") { prefix in
248233
let packageRoot = prefix.appending(component: "CTargetDep")
249234
// Overwrite the existing decl.
@@ -276,16 +261,10 @@ final class APIDiffTests: XCTestCase {
276261
XCTAssertTrue(stderr.contains("error: 'Foo' is not a Swift language target"))
277262
}
278263
}
279-
#else
280-
throw XCTSkip("Test unsupported on current platform")
281-
#endif
282264
}
283265

284266
func testNoBreakingChanges() throws {
285-
#if os(macOS)
286-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
287-
throw XCTSkip("swift-api-digester not available")
288-
}
267+
try skipIfApiDigesterUnsupported()
289268
fixture(name: "Miscellaneous/APIDiff/") { prefix in
290269
let packageRoot = prefix.appending(component: "Bar")
291270
// Introduce an API-compatible change
@@ -296,16 +275,10 @@ final class APIDiffTests: XCTestCase {
296275
XCTAssertTrue(output.contains("No breaking changes detected in Baz"))
297276
XCTAssertTrue(output.contains("No breaking changes detected in Qux"))
298277
}
299-
#else
300-
throw XCTSkip("Test unsupported on current platform")
301-
#endif
302278
}
303279

304280
func testAPIDiffAfterAddingNewTarget() throws {
305-
#if os(macOS)
306-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
307-
throw XCTSkip("swift-api-digester not available")
308-
}
281+
try skipIfApiDigesterUnsupported()
309282
fixture(name: "Miscellaneous/APIDiff/") { prefix in
310283
let packageRoot = prefix.appending(component: "Bar")
311284
try localFileSystem.createDirectory(packageRoot.appending(components: "Sources", "Foo"))
@@ -336,16 +309,10 @@ final class APIDiffTests: XCTestCase {
336309
XCTAssertTrue(output.contains("No breaking changes detected in Qux"))
337310
XCTAssertTrue(output.contains("Skipping Foo because it does not exist in the baseline"))
338311
}
339-
#else
340-
throw XCTSkip("Test unsupported on current platform")
341-
#endif
342312
}
343313

344314
func testBadTreeish() throws {
345-
#if os(macOS)
346-
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
347-
throw XCTSkip("swift-api-digester not available")
348-
}
315+
try skipIfApiDigesterUnsupported()
349316
fixture(name: "Miscellaneous/APIDiff/") { prefix in
350317
let packageRoot = prefix.appending(component: "Foo")
351318
XCTAssertThrowsError(try execute(["experimental-api-diff", "7.8.9"], packagePath: packageRoot)) { error in
@@ -356,9 +323,6 @@ final class APIDiffTests: XCTestCase {
356323
XCTAssertTrue(stderr.contains("error: Couldn’t check out revision ‘7.8.9’"))
357324
}
358325
}
359-
#else
360-
throw XCTSkip("Test unsupported on current platform")
361-
#endif
362326
}
363327

364328
func testBaselineDirOverride() throws {

0 commit comments

Comments
 (0)