From 723adf2bd0d88fe2bc2dd0ba54a6c1d73f927fd5 Mon Sep 17 00:00:00 2001 From: Patrick A Wallace Date: Sat, 23 May 2015 00:15:27 +0100 Subject: [PATCH 1/2] Changed to return an error status code if serious violations found, for Xcode compatibility. --- .../ViolationSeverity.swift | 6 ++++- Source/swiftlint/Lint.swift | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Source/SwiftLintFramework/ViolationSeverity.swift b/Source/SwiftLintFramework/ViolationSeverity.swift index d9d642f8f4..657241a804 100644 --- a/Source/SwiftLintFramework/ViolationSeverity.swift +++ b/Source/SwiftLintFramework/ViolationSeverity.swift @@ -28,8 +28,12 @@ public enum ViolationSeverity: Int, Printable, Comparable { } } + public var isError: Bool { + return self > Medium + } + public var xcodeSeverityDescription: String { - return self <= Medium ? "warning" : "error" + return self.isError ? "error" : "warning" } } diff --git a/Source/swiftlint/Lint.swift b/Source/swiftlint/Lint.swift index ea32a455e1..301d45de37 100644 --- a/Source/swiftlint/Lint.swift +++ b/Source/swiftlint/Lint.swift @@ -22,20 +22,33 @@ struct LintCommand: CommandType { func run(mode: CommandMode) -> Result<(), CommandantError<()>> { println("Finding Swift files in current directory...") let files = recursivelyFindSwiftFilesInDirectory(fileManager.currentDirectoryPath) - var numberOfViolations = 0 + var numberOfViolations = 0, numberOfSeriousViolations = 0 for (index, file) in enumerate(files) { println("Linting '\(file.lastPathComponent)' (\(index + 1)/\(files.count))") for violation in Linter(file: File(path: file)!).styleViolations { println(violation) numberOfViolations++ + if violation.severity.isError { + numberOfSeriousViolations++ + } } } + let violationSuffix = (numberOfViolations != 1 ? "s" : "") + let filesSuffix = (files.count != 1 ? "s." : ".") println( - "Done linting! Found \(numberOfViolations) violation" + - (numberOfViolations != 1 ? "s" : "") + - " in \(files.count) file" + (files.count != 1 ? "s." : ".") + "Done linting!" + + " Found \(numberOfViolations) violation\(violationSuffix)," + + " \(numberOfSeriousViolations) serious" + + " in \(files.count) file\(filesSuffix)" ) - return success() + if numberOfSeriousViolations <= 0 { + return success() + } else { + // This represents failure of the content (i.e. violations in the files linted) + // and not failure of the scanning process itself. The current command architecture + // doesn't discriminate between these types. + return failure(CommandantError<()>.CommandError(Box())) + } } } From 7e61ce5df6af2f6163be0545f516d3ff8a05c9e3 Mon Sep 17 00:00:00 2001 From: Patrick A Wallace Date: Sat, 23 May 2015 19:04:23 +0100 Subject: [PATCH 2/2] Removed reference to self to fit with Github code style. --- CHANGELOG.md | 5 +++++ Source/SwiftLintFramework/ViolationSeverity.swift | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 546a1ccce0..3d173c736b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ FunctionBodyLength, Nesting, TypeBodyLength, TypeName, VariableName. [JP Simard](https://github.com/jpsim) +* swiftlint returns a non-zero error code when a warning of high-severity + or above is found in the source files being linted. + [Pat Wallace](https://github.com/pawrsccouk) + [#30](https://github.com/realm/SwiftLint/issues/30) + ##### Bug Fixes None. diff --git a/Source/SwiftLintFramework/ViolationSeverity.swift b/Source/SwiftLintFramework/ViolationSeverity.swift index 657241a804..823e21f450 100644 --- a/Source/SwiftLintFramework/ViolationSeverity.swift +++ b/Source/SwiftLintFramework/ViolationSeverity.swift @@ -33,7 +33,7 @@ public enum ViolationSeverity: Int, Printable, Comparable { } public var xcodeSeverityDescription: String { - return self.isError ? "error" : "warning" + return isError ? "error" : "warning" } }