Add new `relative-path` reporter (#4739)

This commit is contained in:
Michael S 2023-02-07 23:23:00 +01:00 committed by GitHub
parent 7eb479d546
commit 9c216baf20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 1 deletions

View File

@ -51,6 +51,10 @@
[David Steinacher](https://github.com/stonko1994)
[#4626](https://github.com/realm/SwiftLint/issues/4626)
* Add `relative-path` reporter to generate reports with relative file paths.
[Roya1v](https://github.com/roya1v)
[#4660](https://github.com/realm/SwiftLint/issues/4660)
* Let `number_separator` rule trigger on misplaced separators, e.g. `10_00`.
[SimplyDanny](https://github.com/SimplyDanny)
[#4637](https://github.com/realm/SwiftLint/issues/4637)

View File

@ -47,6 +47,8 @@ public func reporterFrom(identifier: String) -> Reporter.Type { // swiftlint:dis
return GitLabJUnitReporter.self
case CodeClimateReporter.identifier:
return CodeClimateReporter.self
case RelativePathReporter.identifier:
return RelativePathReporter.self
default:
queuedFatalError("no reporter with identifier '\(identifier)' available.")
}

View File

@ -0,0 +1,34 @@
/// Reports violations with relative paths.
public struct RelativePathReporter: Reporter {
// MARK: - Reporter Conformance
public static let identifier = "relative-path"
public static let isRealtime = true
public var description: String {
return "Reports violations with relative paths."
}
public static func generateReport(_ violations: [StyleViolation]) -> String {
return violations.map(generateForSingleViolation).joined(separator: "\n")
}
/// Generates a report for a single violation.
///
/// - parameter violation: The violation to report.
///
/// - returns: The report for a single violation.
internal static func generateForSingleViolation(_ violation: StyleViolation) -> String {
// {relative_path_to_file}{:line}{:character}: {error,warning}: {content}
return [
"\(violation.location.relativeFile ?? "<nopath>")",
":\(violation.location.line ?? 1)",
":\(violation.location.character ?? 1): ",
"\(violation.severity.rawValue): ",
"\(violation.ruleName) Violation: ",
violation.reason,
" (\(violation.ruleIdentifier))"
].joined()
}
}

View File

@ -17,7 +17,8 @@ class ReporterTests: XCTestCase {
SonarQubeReporter.self,
MarkdownReporter.self,
GitHubActionsLoggingReporter.self,
GitLabJUnitReporter.self
GitLabJUnitReporter.self,
RelativePathReporter.self
]
for reporter in reporters {
XCTAssertEqual(reporter.identifier, reporterFrom(identifier: reporter.identifier).identifier)
@ -136,4 +137,22 @@ class ReporterTests: XCTestCase {
let result = MarkdownReporter.generateReport(generateViolations())
XCTAssertEqual(result, expectedOutput)
}
func testRelativePathReporter() {
let expectedOutput = stringFromFile("CannedRelativePathReporterOutput.txt")
let result = RelativePathReporter.generateReport(generateViolations())
XCTAssertEqual(result, expectedOutput)
}
func testRelativePathReporterPaths() {
let relativePath = "filename"
let absolutePath = FileManager.default.currentDirectoryPath + "/" + relativePath
let location = Location(file: absolutePath, line: 1, character: 2)
let violation = StyleViolation(ruleDescription: LineLengthRule.description,
location: location,
reason: "Violation Reason")
let result = RelativePathReporter.generateReport([violation])
XCTAssertFalse(result.contains(absolutePath))
XCTAssertTrue(result.contains(relativePath))
}
}

View File

@ -0,0 +1,4 @@
filename:1:2: warning: Line Length Violation: Violation Reason (line_length)
filename:1:2: error: Line Length Violation: Violation Reason (line_length)
filename:1:2: error: Syntactic Sugar Violation: Shorthand syntactic sugar should be used, i.e. [Int] instead of Array<Int> (syntactic_sugar)
<nopath>:1:1: error: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)