Repository navigation
Omit help text on runtime errors in xcprojformatter #31
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
Open
zachshallbetter
wants to merge
2
commits into
apple:main
Choose a base branch
from
zachshallbetter:fix/xcprojformatter-error-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // Copyright © 2026 Apple Inc. and the xcode-project-format project authors | ||
| // | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // SPDX-License-Identifier: Apache-2.0 WITH Swift-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #if os(macOS) || os(Linux) || os(Windows) | ||
|
|
||
| import Foundation | ||
| import Testing | ||
|
|
||
| struct FormatterErrorOutputTests { | ||
| private struct ProcessResult { | ||
| let terminationStatus: Int32 | ||
| let stdout: String | ||
| let stderr: String | ||
| } | ||
|
|
||
| private func executableURL() throws -> URL { | ||
| #if os(Windows) | ||
| let binaryName = "xcprojformatter.exe" | ||
| #else | ||
| let binaryName = "xcprojformatter" | ||
| #endif | ||
|
|
||
| var candidateDirectories: [URL] = [] | ||
| if let builtProductsDir = ProcessInfo.processInfo.environment["BUILT_PRODUCTS_DIR"] { | ||
| candidateDirectories.append(URL(fileURLWithPath: builtProductsDir)) | ||
| } | ||
| for bundle in Bundle.allBundles where bundle.bundleURL.pathExtension == "xctest" { | ||
| candidateDirectories.append(bundle.bundleURL.deletingLastPathComponent()) | ||
| } | ||
| for argument in CommandLine.arguments { | ||
| var url = URL(fileURLWithPath: argument) | ||
| while url.path != "/" && !url.lastPathComponent.hasSuffix(".xctest") { | ||
| url.deleteLastPathComponent() | ||
| } | ||
| if url.lastPathComponent.hasSuffix(".xctest") { | ||
| candidateDirectories.append(url.deletingLastPathComponent()) | ||
| } | ||
| } | ||
|
|
||
| for directory in candidateDirectories { | ||
| let candidate = directory.appendingPathComponent(binaryName) | ||
| if FileManager.default.isExecutableFile(atPath: candidate.path) { | ||
| return candidate | ||
| } | ||
| } | ||
|
|
||
| Issue.record("Could not locate \(binaryName) executable in \(candidateDirectories.map(\.path))") | ||
| throw CancellationError() | ||
| } | ||
|
|
||
| private func runFormatter(_ arguments: [String], stdin: Data? = nil) throws -> ProcessResult { | ||
| let process = Process() | ||
| process.executableURL = try executableURL() | ||
| process.arguments = arguments | ||
|
|
||
| let stdoutPipe = Pipe() | ||
| let stderrPipe = Pipe() | ||
| process.standardOutput = stdoutPipe | ||
| process.standardError = stderrPipe | ||
|
|
||
| let stdinPipe = Pipe() | ||
| process.standardInput = stdinPipe | ||
|
|
||
| try process.run() | ||
| if let stdin { | ||
| try stdinPipe.fileHandleForWriting.write(contentsOf: stdin) | ||
| } | ||
| try stdinPipe.fileHandleForWriting.close() | ||
| process.waitUntilExit() | ||
|
|
||
| let stdout = String(decoding: stdoutPipe.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self) | ||
| let stderr = String(decoding: stderrPipe.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self) | ||
| return ProcessResult(terminationStatus: process.terminationStatus, stdout: stdout, stderr: stderr) | ||
| } | ||
|
|
||
| private func withTemporaryDirectory(_ body: (URL) throws -> ()) throws { | ||
| let dir = FileManager.default.temporaryDirectory.appendingPathComponent("FormatterErrorOutputTests-\(UUID().uuidString)") | ||
| try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) | ||
| defer { try? FileManager.default.removeItem(at: dir) } | ||
| try body(dir) | ||
| } | ||
|
|
||
| @Test func decodeFailurePrintsDiagnosticWithoutHelpText() throws { | ||
| let projectMissingTargetName = Data(#"{"default-configuration":"Debug","files":[],"localizations":{"development":"en"},"targets":[{}]}"#.utf8) | ||
|
|
||
| try withTemporaryDirectory { dir in | ||
| let projectFile = dir.appendingPathComponent("project.xcproj") | ||
| try projectMissingTargetName.write(to: projectFile) | ||
|
|
||
| let result = try runFormatter(["--input", projectFile.path]) | ||
| #expect(result.terminationStatus == 1) | ||
| #expect(result.stdout.isEmpty) | ||
| #expect(result.stderr.contains("Missing required value for key “name” at “/targets[0]”")) | ||
| #expect(!result.stderr.contains("Usage:")) | ||
| #expect(!result.stderr.contains("----------------------------")) | ||
| } | ||
| } | ||
|
|
||
| @Test func malformedJSONPrintsDiagnosticWithoutHelpText() throws { | ||
| let result = try runFormatter([], stdin: Data("{invalid".utf8)) | ||
| #expect(result.terminationStatus == 1) | ||
| #expect(result.stdout.isEmpty) | ||
| #expect(!result.stderr.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) | ||
| #expect(!result.stderr.contains("Usage:")) | ||
| } | ||
|
|
||
| @Test func unreadableInputPrintsDiagnosticWithoutHelpText() throws { | ||
| try withTemporaryDirectory { dir in | ||
| let missingFile = dir.appendingPathComponent("missing.xcproj") | ||
| let result = try runFormatter(["--input", missingFile.path]) | ||
| #expect(result.terminationStatus == 1) | ||
| #expect(result.stdout.isEmpty) | ||
| #expect(result.stderr.contains("Could not open")) | ||
| #expect(!result.stderr.contains("Usage:")) | ||
| } | ||
| } | ||
|
|
||
| @Test func invalidArgumentsStillPrintHelpText() throws { | ||
| let result = try runFormatter(["--unknown-flag"]) | ||
| #expect(result.terminationStatus == 1) | ||
| #expect(result.stdout.isEmpty) | ||
| #expect(result.stderr.contains("Unexpected argument")) | ||
| #expect(result.stderr.contains("Usage:")) | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Want to take a dependency on swift-subprocess? @jakepetroules
There are a handful of other PRs also exercising this "find build products and run the executable behavior." (#30, #2) Ideally, if possible, all 3 of these new tests don't copy the same code everywhere just to get something specific from stdio or exit statuses.