Skip to content

[APIDiff] Support forwarding breakage allowlists to the api digester #3547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/Commands/APIDigester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,18 @@ public struct SwiftAPIDigester {
public func compareAPIToBaseline(
at baselinePath: AbsolutePath,
for module: String,
buildPlan: BuildPlan
buildPlan: BuildPlan,
except breakageAllowlistPath: AbsolutePath?
) -> ComparisonResult? {
var args = [
"-diagnose-sdk",
"-baseline-path", baselinePath.pathString,
"-module", module
]
args.append(contentsOf: buildPlan.createAPIToolCommonArgs(includeLibrarySearchPaths: false))
if let breakageAllowlistPath = breakageAllowlistPath {
args.append(contentsOf: ["-breakage-allowlist-path", breakageAllowlistPath.pathString])
}

return try? withTemporaryFile(deleteOnClose: false) { file in
args.append(contentsOf: ["-serialize-diagnostics-path", file.path.pathString])
Expand Down
10 changes: 9 additions & 1 deletion Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ extension SwiftPackageTool {
@OptionGroup(_hiddenFromHelp: true)
var swiftOptions: SwiftToolOptions

@Option(help: """
The path to a text file containing breaking changes which should be ignored by the API comparison. \
Each ignored breaking change in the file should appear on its own line and contain the exact message \
to be ignored (e.g. 'API breakage: func foo() has been removed').
""")
var breakageAllowlistPath: AbsolutePath?

@Argument(help: "The baseline treeish to compare to (e.g. a commit hash, branch name, tag, etc.)")
var treeish: String

Expand Down Expand Up @@ -360,7 +367,8 @@ extension SwiftPackageTool {
if let comparisonResult = apiDigesterTool.compareAPIToBaseline(
at: moduleBaselinePath,
for: module,
buildPlan: buildOp.buildPlan!
buildPlan: buildOp.buildPlan!,
except: breakageAllowlistPath
) {
results.append(comparisonResult)
}
Expand Down
37 changes: 37 additions & 0 deletions Tests/CommandsTests/APIDiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,43 @@ final class APIDiffTests: XCTestCase {
#endif
}

func testBreakageAllowlist() throws {
#if os(macOS)
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
throw XCTSkip("swift-api-digester not available")
}
fixture(name: "Miscellaneous/APIDiff/") { prefix in
let packageRoot = prefix.appending(component: "Bar")
try localFileSystem.writeFileContents(packageRoot.appending(components: "Sources", "Baz", "Baz.swift")) {
$0 <<< "public func baz() -> String { \"hello, world!\" }"
}
try localFileSystem.writeFileContents(packageRoot.appending(components: "Sources", "Qux", "Qux.swift")) {
$0 <<< "public class Qux<T, U> { private let x = 1 }"
}
let customAllowlistPath = packageRoot.appending(components: "foo", "allowlist.txt")
try localFileSystem.writeFileContents(customAllowlistPath) {
$0 <<< "API breakage: class Qux has generic signature change from <T> to <T, U>\n"
}
XCTAssertThrowsError(try execute(["experimental-api-diff", "1.2.3", "-j", "2",
"--breakage-allowlist-path", customAllowlistPath.pathString],
packagePath: packageRoot)) { error in
guard case SwiftPMProductError.executionFailure(error: _, output: let output, stderr: _) = error else {
XCTFail("Unexpected error")
return
}
XCTAssertTrue(output.contains("1 breaking change detected in Qux"))
XCTAssertFalse(output.contains("💔 API breakage: class Qux has generic signature change from <T> to <T, U>"))
XCTAssertTrue(output.contains("💔 API breakage: var Qux.x has been removed"))
XCTAssertTrue(output.contains("1 breaking change detected in Baz"))
XCTAssertTrue(output.contains("💔 API breakage: func bar() has been removed"))
}

}
#else
throw XCTSkip("Test unsupported on current platform")
#endif
}

func testCheckVendedModulesOnly() throws {
#if os(macOS)
guard (try? Resources.default.toolchain.getSwiftAPIDigester()) != nil else {
Expand Down