Skip to content

Commit

Permalink
Improve unit tests (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Oct 19, 2023
1 parent 8947f8e commit cd99a26
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
10 changes: 0 additions & 10 deletions Sources/AirbnbSwiftFormatTool/AirbnbSwiftFormatTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,6 @@ struct AirbnbSwiftFormatTool: ParsableCommand {
}
}

/// Runs the `AirbnbSwiftFormatTool` command with the given closure that executes each individual command.
func run(executeCommand: @escaping (Command) -> Int32) throws {
let existingImplementation = Command.runCommand

Command.runCommand = executeCommand
defer { Command.runCommand = existingImplementation }

try run()
}

// MARK: Private

/// Whether the command should autocorrect invalid code, or only emit lint errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,33 @@ final class AirbnbSwiftFormatToolTest: XCTestCase {
XCTAssertFalse(ranSwiftLintAutocorrect)
}

func testLintWithOnlySwiftFormatViolation() {
var ranSwiftFormat = false
var ranSwiftLint = false
var ranSwiftLintAutocorrect = false

let error = runFormatTool(
arguments: ["--lint"],
with: MockCommands(
swiftFormat: {
ranSwiftFormat = true
return SwiftFormatExitCode.lintFailure
},
swiftLint: {
ranSwiftLint = true
return EXIT_SUCCESS
},
swiftLintAutocorrect: {
ranSwiftLintAutocorrect = true
return EXIT_SUCCESS
}))

XCTAssertEqual(error as? ExitCode, ExitCode.failure)
XCTAssertTrue(ranSwiftFormat)
XCTAssertTrue(ranSwiftLint)
XCTAssertFalse(ranSwiftLintAutocorrect)
}

func testHandlesUnexpectedErrorCode() {
let unexpectedSwiftFormatExitCode = runFormatTool(
with: MockCommands(swiftFormat: { 1234 }))
Expand All @@ -209,6 +236,11 @@ final class AirbnbSwiftFormatToolTest: XCTestCase {

/// Runs `AirbnbSwiftFormatTool` with the `Command` calls mocked using the given mocks
private func runFormatTool(arguments: [String]? = nil, with mocks: MockCommands) -> Error? {
let existingRunCommandImplementation = Command.runCommand

Command.runCommand = mocks.mockRunCommand(_:)
defer { Command.runCommand = existingRunCommandImplementation }

let formatTool = try! AirbnbSwiftFormatTool.parse([
"Sources",
"--swift-format-path",
Expand All @@ -218,7 +250,7 @@ final class AirbnbSwiftFormatToolTest: XCTestCase {
] + (arguments ?? []))

do {
try formatTool.run(executeCommand: mocks.mockRunCommand(_:))
try formatTool.run()
return nil
} catch {
return error
Expand Down

0 comments on commit cd99a26

Please sign in to comment.