-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from yumemi-inc/feature/test-coverage
Feature/test coverage
- Loading branch information
Showing
5 changed files
with
231 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// Kantoku+Coverage.swift | ||
// | ||
// | ||
// Created by 史 翔新 on 2022/03/03. | ||
// | ||
|
||
import Foundation | ||
import XCResultKit | ||
|
||
extension Kantoku { | ||
|
||
var coverageNumberFormatter: NumberFormatter { | ||
|
||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .percent | ||
formatter.maximumFractionDigits = 2 | ||
return formatter | ||
|
||
} | ||
|
||
} | ||
|
||
extension Kantoku { | ||
|
||
enum CoverageAcceptance { | ||
case good | ||
case acceptable | ||
case reject | ||
} | ||
|
||
private struct CoverageCommentItem { | ||
var title: String | ||
var coverage: Double | ||
var acceptance: CoverageAcceptance | ||
} | ||
|
||
func post(_ coverage: CodeCoverage, as acceptanceDecision: (Double) -> CoverageAcceptance) { | ||
|
||
let formatter = coverageNumberFormatter | ||
let title = "Overall" | ||
let overallCoverage = coverage.lineCoverage | ||
let overallAcceptance = acceptanceDecision(overallCoverage) | ||
let coverageText = formatter.string(from: overallCoverage as NSNumber) ?? { | ||
fail("Failed to extract overall coverage from \(overallCoverage)") | ||
return "NaN" | ||
}() | ||
|
||
let markdownLines: [String] = [ | ||
"| | Coverage | Acceptance |", | ||
"|:---:|:---:|:---:|", | ||
"| \(title) | \(coverageText) | \(overallAcceptance.markdownDescription) |", | ||
// TODO: Coverage of diff files | ||
] | ||
markdown(markdownLines.joined(separator: "\n")) | ||
|
||
switch overallAcceptance { | ||
case .good: | ||
break | ||
|
||
case .acceptable: | ||
warn("Overall coverage is \(overallCoverage)") | ||
|
||
case .reject: | ||
fail("Overall coverage is \(overallCoverage), which is not enough") | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
extension Kantoku.CoverageAcceptance { | ||
|
||
var markdownDescription: String { | ||
switch self { | ||
case .good: | ||
return ":white_flower:" | ||
|
||
case .acceptable: | ||
return ":thinking:" | ||
|
||
case .reject: | ||
return ":no_good:" | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by 史 翔新 on 2022/03/03. | ||
// | ||
|
||
import Foundation | ||
|
||
extension Kantoku { | ||
|
||
enum CommentLevel { | ||
case comment | ||
case warning | ||
case failure | ||
} | ||
|
||
private func post(as level: CommentLevel) -> (_ message: String) -> Void { | ||
|
||
switch level { | ||
case .comment: | ||
return comment(_:) | ||
|
||
case .warning: | ||
return warn(_:) | ||
|
||
case .failure: | ||
return fail(_:) | ||
} | ||
|
||
} | ||
|
||
private func post(as level: CommentLevel) -> (_ message: String, _ filePath: String, _ lineNumber: Int) -> Void { | ||
|
||
switch level { | ||
case .comment: | ||
return comment(_:to:at:) | ||
|
||
case .warning: | ||
return warn(_:to:at:) | ||
|
||
case .failure: | ||
return fail(_:to:at:) | ||
} | ||
|
||
} | ||
|
||
func post(_ summaries: [PostableIssueSummary], as level: CommentLevel) { | ||
|
||
for summary in summaries { | ||
let message = summary.issueMessage | ||
let filePath = summary.documentLocation?.relativePath(against: workingDirectoryPath) | ||
|
||
if let filePath = filePath { | ||
let lineNumber = filePath.queries?.endingLineNumber | ||
// Line numbers in XCResult starts from `0`, while on web pages like GitHub starts from `1` | ||
post(as: level)(message, filePath.filePath, lineNumber.map({ $0 + 1 }) ?? 0) | ||
|
||
} else { | ||
post(as: level)(message) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters