Skip to content

Commit

Permalink
[TEST] Add test for DangerSwiftCommitLint
Browse files Browse the repository at this point in the history
- improve testability
  • Loading branch information
Wei Sun authored and Wei Sun committed Sep 8, 2021
1 parent 298e917 commit dbe315d
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Sources/DangerSwiftCommitLint/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public extension DangerSwiftCommitLint {
private let fail: CommitCheckerSelection
private let customCheckers: [CommitLint.Type]

/// Initialize the configuraiton.
/// Initialize the configuration.
/// - Parameters:
/// - disabled: The selected checks to skip.
/// - warn: The selected checks to warn on.
Expand Down
27 changes: 18 additions & 9 deletions Sources/DangerSwiftCommitLint/DangerSwiftCommitLint.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import Danger
import Foundation

public protocol DangerDSLProviding {
var git: Git { get }

func warn(_ message: String)
func fail(_ message: String)
}

extension DangerDSL: DangerDSLProviding {}

public final class DangerSwiftCommitLint {
private enum Constants {
static let disableAllChecksMessage = "All checks were disabled, nothing to do."
}

private let danger: DangerDSL
private let danger: DangerDSLProviding
private var configuration: Configuration!

/// Initialize an instance of the linter
/// - Parameters:
/// - danger: An instance of `Danger.DangerDSL`.
/// - configuration: Linter configuration.
init(danger: DangerDSL = Danger(), configuration: Configuration) {
public init(danger: DangerDSLProviding = Danger(), configuration: Configuration) {
self.danger = danger
self.configuration = configuration
}
Expand All @@ -28,7 +37,7 @@ public final class DangerSwiftCommitLint {
}
}

extension DangerSwiftCommitLint {
private extension DangerSwiftCommitLint {
var commitMessages: [GitCommitMessage] {
let messages = danger.git.commits.map { GitCommitMessage($0) }
guard configuration.limit > 0 else {
Expand All @@ -40,10 +49,10 @@ extension DangerSwiftCommitLint {

func checkMessages() {
// Checkers that warn the danger job.
configuration.warningCheckers.checkCommitMessages(commitMessages, checkerResultHanler: warning(_:shas:))
configuration.warningCheckers.checkCommitMessages(commitMessages, checkerResultHandler: warning(_:shas:))

// Checkers that fail the danger job.
configuration.failingCheckers.checkCommitMessages(commitMessages, checkerResultHanler: failing(_:shas:))
configuration.failingCheckers.checkCommitMessages(commitMessages, checkerResultHandler: failing(_:shas:))
}

func warning(_ message: String, shas: [String]) {
Expand All @@ -53,16 +62,16 @@ extension DangerSwiftCommitLint {

func failing(_ message: String, shas: [String]) {
let failingMessage = ([message] + shas).joined(separator: "\n")
danger.warn(failingMessage)
danger.fail(failingMessage)
}
}

extension Array where Element == CommitLint.Type {
func checkCommitMessages(_ commitMessages: [GitCommitMessage], checkerResultHanler: (String, [String]) -> Void) {
private extension Array where Element == CommitLint.Type {
func checkCommitMessages(_ commitMessages: [GitCommitMessage], checkerResultHandler: (String, [String]) -> Void) {
forEach { checker in
let shas = commitMessages.compactMap { checker.fail($0) ? $0.sha : nil }
if shas.isEmpty == false {
checkerResultHanler(checker.linterMessage, shas)
checkerResultHandler(checker.linterMessage, shas)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest

final class CommitMessageTests: XCTestCase {
func testInitializeWithDangerGitHubCommit() {
let commit = CommitParser.parseCommitJSON(with: gitCommitJSON)
let commit = CommitParser.parseGitCommit(with: gitCommitJSON)
let expectedCommitMessage = commit.message.components(separatedBy: .newlines)
let testSubject = GitCommitMessage(commit)
XCTAssertEqual(testSubject.sha, commit.sha)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import Danger
import Foundation

enum CommitParser {
static func parseCommitJSON(with commitJSON: String) -> Git.Commit {
let data = Data(commitJSON.utf8)
static func parseGitCommit(with json: String) -> Git.Commit {
decodeJSON(with: json, type: Git.Commit.self)
}

static func parseGit(with json: String) -> Git {
decodeJSON(with: json, type: Git.self)
}

private static func decodeJSON<T: Decodable>(with jsonContent: String, type: T.Type) -> T {
let data = Data(jsonContent.utf8)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom(DateFormatter.dateFormatterHandler)
return try! decoder.decode(Git.Commit.self, from: data) // swiftlint:disable:this force_try
return try! decoder.decode(type, from: data) // swiftlint:disable:this force_try
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
let gitIssuesOnLastTwoCommitsJSON = """
{
"modified_files": [
"Tests/SampleTest.swift"
],
"created_files": [],
"deleted_files": [],
"commits": [
{
"sha": "testshacommit4",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T23:16:13.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T21:56:43.000Z"
},
"message": "Hi",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit4",
"tree": null
},
{
"sha": "testshacommit3",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T21:56:43.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T21:56:43.000Z"
},
"message": "subject for test purpose - commit 3 very very long commit subject.\\nTest Body Line 1.\\nTest Body Line 2.",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit3",
"tree": null
},
{
"sha": "testshacommit2",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T20:10:33.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T20:10:33.000Z"
},
"message": "Merge branch 'master' into TestRepository/tt/tech-123-update-sample-test",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit2",
"tree": null
},
{
"sha": "testshacommit1",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T16:23:31.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T16:23:31.000Z"
},
"message": "Subject for test purpose - commit 1\\n\\nTest Body Line 1.\\nTest Body Line 2.",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit1",
"tree": null
}
]
}
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
let gitJSON = """
{
"modified_files": [
"Tests/SampleTest.swift"
],
"created_files": [],
"deleted_files": [],
"commits": [
{
"sha": "testshacommit3",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T21:56:43.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T21:56:43.000Z"
},
"message": "Subject for test purpose - commit 3\\n\\nTest Body Line 1.\\nTest Body Line 2.",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit3",
"tree": null
},
{
"sha": "testshacommit2",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T20:10:33.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T20:10:33.000Z"
},
"message": "Subject for test purpose - commit 2\\n\\nTest Body Line 1.\\nTest Body Line 2.",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit2",
"tree": null
},
{
"sha": "testshacommit1",
"author": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2021-09-08T16:23:31.000Z"
},
"committer": {
"name": "Test Testy",
"email": "[email protected]",
"date": "2019-04-10T16:23:31.000Z"
},
"message": "Subject for test purpose - commit 1\\n\\nTest Body Line 1.\\nTest Body Line 2.",
"parents": [],
"url": "https://gitlab.com/test/test.systems/commit/testshacommit1",
"tree": null
}
]
}
"""
Loading

0 comments on commit dbe315d

Please sign in to comment.