Skip to content
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
32 changes: 14 additions & 18 deletions Sources/fxios/Commands/Lint/LintRun.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,24 @@ extension Lint {
try runLint(lintAll: lintAll, repoRoot: repo.root)
}

// MARK: - Flags

/// Builds the swiftlint flag arguments based on parsed options.
func swiftlintFlags() -> [String] {
var flags: [String] = []
if strict { flags.append("--strict") }
if quiet { flags.append("--quiet") }
return flags
}

// MARK: - Lint

private func runLint(lintAll: Bool, repoRoot: URL) throws {
if lintAll {
Herald.declare("Linting entire codebase...", isNewCommand: true)

do {
try ShellRunner.run("swiftlint", arguments: [], workingDirectory: repoRoot)
try ShellRunner.run("swiftlint", arguments: swiftlintFlags(), workingDirectory: repoRoot)
Herald.declare("Linting complete!", asConclusion: true)
} catch let error as ShellRunnerError {
if case .commandFailed(_, let exitCode) = error {
Expand Down Expand Up @@ -80,14 +90,7 @@ extension Lint {
var hasViolations = false
for file in changedFiles {
var args: [String] = ["lint", "--config", configPath, "--path", file]

if strict {
args.append("--strict")
}

if quiet {
args.append("--quiet")
}
args.append(contentsOf: swiftlintFlags())

do {
try ShellRunner.run("swiftlint", arguments: args, workingDirectory: repoRoot)
Expand Down Expand Up @@ -116,7 +119,7 @@ extension Lint {
private func printExposedCommands(lintAll: Bool, repoRoot: URL) {
if lintAll {
Herald.raw("# Lint entire codebase")
Herald.raw("swiftlint")
Herald.raw(CommandHelpers.formatCommand("swiftlint", arguments: swiftlintFlags()))
} else {
let configPath = repoRoot.appendingPathComponent(".swiftlint.yaml").path

Expand All @@ -129,14 +132,7 @@ extension Lint {
Herald.raw("")

var args: [String] = ["lint", "--config", configPath, "--path", "<file>"]

if strict {
args.append("--strict")
}

if quiet {
args.append("--quiet")
}
args.append(contentsOf: swiftlintFlags())

Herald.raw("# Lint each changed file")
Herald.raw(CommandHelpers.formatCommand("swiftlint", arguments: args))
Expand Down
32 changes: 32 additions & 0 deletions Tests/fxiosTests/LintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,38 @@ struct LintTests {
#expect(command.quiet == true)
}

// MARK: - Run swiftlintFlags Tests

@Test("Run swiftlintFlags returns empty array by default")
func runSwiftlintFlagsDefault() throws {
let command = try Lint.Run.parse([])
#expect(command.swiftlintFlags().isEmpty)
}

@Test("Run swiftlintFlags includes --quiet when quiet is set")
func runSwiftlintFlagsQuiet() throws {
let command = try Lint.Run.parse(["--quiet"])
let flags = command.swiftlintFlags()
#expect(flags.contains("--quiet"))
#expect(!flags.contains("--strict"))
}

@Test("Run swiftlintFlags includes --strict when strict is set")
func runSwiftlintFlagsStrict() throws {
let command = try Lint.Run.parse(["--strict"])
let flags = command.swiftlintFlags()
#expect(flags.contains("--strict"))
#expect(!flags.contains("--quiet"))
}

@Test("Run swiftlintFlags includes both flags when both are set")
func runSwiftlintFlagsBoth() throws {
let command = try Lint.Run.parse(["--strict", "--quiet"])
let flags = command.swiftlintFlags()
#expect(flags.contains("--strict"))
#expect(flags.contains("--quiet"))
}

// MARK: - Fix Subcommand Tests

@Test("Fix subcommand has correct name")
Expand Down