Skip to content
Open
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
25 changes: 22 additions & 3 deletions Sources/DangerSwiftPeriphery/DangerPeriphery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
}

Expand All @@ -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:
Expand All @@ -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)
}

Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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
}
}
Expand Down
39 changes: 36 additions & 3 deletions Tests/DangerSwiftPeripheryTests/DangerSwiftPeripheryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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 }
Expand Down