diff --git a/Sources/DangerSwiftPeriphery/DangerPeriphery.swift b/Sources/DangerSwiftPeriphery/DangerPeriphery.swift index 226a270..5196787 100644 --- a/Sources/DangerSwiftPeriphery/DangerPeriphery.swift +++ b/Sources/DangerSwiftPeriphery/DangerPeriphery.swift @@ -14,6 +14,9 @@ public enum DangerPeriphery { /// - shouldComment: /// Flag if you want to comment using Danger or not. /// Set to false if you want to use the return value to create your own comments, for example. + /// - filterUnrelatedFiles: + /// Flag to determine whether to filter out files not affected by the current pull request. + /// Set to true to focus on detecting violations in files that are part of the pull request's changes. /// - verbose: /// Set to true if logging is to be output. /// - Returns: @@ -23,10 +26,12 @@ public enum DangerPeriphery { public static func scan(peripheryExecutable: String = "swift run periphery", @PeripheryScanOptionsBuilder arguments: () -> [String], shouldComment: Bool = true, + filterUnrelatedFiles: Bool = true, verbose: Bool = false) -> Result<[Violation], Error> { scan(peripheryExecutable: peripheryExecutable, arguments: arguments(), shouldComment: shouldComment, + filterUnrelatedFiles: filterUnrelatedFiles, verbose: verbose) } @@ -43,6 +48,9 @@ public enum DangerPeriphery { /// - shouldComment: /// Flag if you want to comment using Danger or not. /// Set to false if you want to use the return value to create your own comments, for example. + /// - filterUnrelatedFiles: + /// Flag to determine whether to filter out files not affected by the current pull request. + /// Set to true to focus on detecting violations in files that are part of the pull request's changes. /// - verbose: /// Set to true if logging is to be output. /// - Returns: @@ -52,10 +60,12 @@ public enum DangerPeriphery { public static func scan(peripheryExecutable: String = "swift run periphery", arguments: [PeripheryScanOptions], shouldComment: Bool = true, + filterUnrelatedFiles: Bool = true, verbose: Bool = false) -> Result<[Violation], Error> { scan(peripheryExecutable: peripheryExecutable, arguments: arguments.map { $0.optionString }, shouldComment: shouldComment, + filterUnrelatedFiles: filterUnrelatedFiles, verbose: verbose) } @@ -72,6 +82,9 @@ public enum DangerPeriphery { /// - shouldComment: /// Flag if you want to comment using Danger or not. /// Set to false if you want to use the return value to create your own comments, for example. + /// - filterUnrelatedFiles: + /// Flag to determine whether to filter out files not affected by the current pull request. + /// Set to true to focus on detecting violations in files that are part of the pull request's changes. /// - verbose: /// Set to true if logging is to be output. /// - Returns: @@ -81,6 +94,7 @@ public enum DangerPeriphery { public static func scan(peripheryExecutable: String = "swift run periphery", arguments: [String] = [], shouldComment: Bool = true, + filterUnrelatedFiles: Bool = true, verbose: Bool = false) -> Result<[Violation], Error> { Logger.shared.verbose = verbose @@ -95,7 +109,8 @@ public enum DangerPeriphery { // execute scan let result = self.scan(scanExecutor: scanExecutor, outputParser: outputParser, - diffProvider: danger) + diffProvider: danger, + filterUnrelatedFiles: filterUnrelatedFiles) // handle scan result handleScanResult(result, danger: danger, shouldComment: shouldComment) @@ -107,11 +122,15 @@ public enum DangerPeriphery { DP: PullRequestDiffProvidable>( scanExecutor: PSE, outputParser: OP, - diffProvider: DP) -> Result<[Violation], Error> { + diffProvider: DP, + filterUnrelatedFiles: Bool + ) -> Result<[Violation], Error> { Result { let output = try scanExecutor.execute() let allViolations = try outputParser.parse(xml: output) - let violationsForComment = allViolations.filter({ isViolationIncludedInInsertions($0, diffProvider: diffProvider) }) + guard filterUnrelatedFiles else { return allViolations } + let violationsForComment = allViolations.filter { isViolationIncludedInInsertions($0, diffProvider: diffProvider) + } return violationsForComment } } diff --git a/Tests/DangerSwiftPeripheryTests/DangerSwiftPeripheryTests.swift b/Tests/DangerSwiftPeripheryTests/DangerSwiftPeripheryTests.swift index 75b83c8..ff77917 100644 --- a/Tests/DangerSwiftPeripheryTests/DangerSwiftPeripheryTests.swift +++ b/Tests/DangerSwiftPeripheryTests/DangerSwiftPeripheryTests.swift @@ -29,7 +29,8 @@ final class DangerSwiftPeripheryTests: XCTestCase { } let result = DangerPeriphery.scan(scanExecutor: scanExecutor, outputParser: outputParser, - diffProvider: diffProvider) + diffProvider: diffProvider, + filterUnrelatedFiles: true) switch result { case .success: XCTFail("Unexpected success") @@ -58,7 +59,8 @@ final class DangerSwiftPeripheryTests: XCTestCase { } let result = DangerPeriphery.scan(scanExecutor: scanExecutor, outputParser: outputParser, - diffProvider: diffProvider) + diffProvider: diffProvider, + filterUnrelatedFiles: true) switch result { case .success: XCTFail("Unexpected success") @@ -95,7 +97,8 @@ final class DangerSwiftPeripheryTests: XCTestCase { } let result = DangerPeriphery.scan(scanExecutor: scanExecutor, outputParser: outputParser, - diffProvider: diffProvider) + diffProvider: diffProvider, + filterUnrelatedFiles: true) switch result { case let .success(violationsForComment): XCTAssertEqual(violationsForComment.count, 0) @@ -104,6 +107,36 @@ final class DangerSwiftPeripheryTests: XCTestCase { } } + func testScanReturnsAllViolationsWhenFilterUnrelatedFilesIsFalse() { + scanExecutor.executeHandler = { "test" } + outputParser.parseHandler = { _ in + [ + .init(filePath: "path1", line: 1, message: "1"), + .init(filePath: "path2", line: 2, message: "2"), + ] + } + diffProvider.diffHandler = { filePath in + switch filePath { + case "path1": + return .deleted(deletedLines: []) + case "path2": + return .renamed(oldPath: "", hunks: []) + default: + return .deleted(deletedLines: []) + } + } + let result = DangerPeriphery.scan(scanExecutor: scanExecutor, + outputParser: outputParser, + diffProvider: diffProvider, + filterUnrelatedFiles: false) + switch result { + case let .success(violationsForComment): + XCTAssertEqual(violationsForComment.count, 2) + case .failure: + XCTFail("Unexpected error") + } + } + func testHandleScanResultSuccessShouldComment() { XCTAssertEqual(dangerCommentable.warnCallCount, 0) dangerCommentable.warnHandler = { _ in }