Skip to content

Commit 2887241

Browse files
GErP83Kanstantsin Bucha
andauthored
Version 1.1.2 (#6)
* Feature: Add GFM (GitHub Flavored Markdown) format support. (#5) * Add the `gfm` (Github Flavored Markdown) output format and improve the handling logic of the OutputFormat cases. * Implement an encoder for the gfm format. * Update README.md --------- Co-authored-by: Kanstantsin Bucha <[email protected]>
1 parent 25aa799 commit 2887241

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Testify
22

3-
Testify converts XCTest output into a proper structure (JSON, JUNIT, MD), or it'll miserably fail. 😉
3+
Testify converts XCTest output into a proper structure (JSON, JUNIT, MD, GFM), or it'll miserably fail. 😉
44

55

66
## Install command line utility
77

8-
You can use the command line utility to convert test results into JSON, JUNIT and MD on the fly.
8+
You can use the command line utility to convert test results into JSON, JUNIT, MD and GFM on the fly.
99

1010
```
1111
git clone https://github.com/BinaryBirds/Testify.git && cd Testify
@@ -17,14 +17,15 @@ which testify
1717

1818
In your project folder run:
1919

20-
* for JSON format: `swift test | testify json`
20+
* for JSON format: `swift test | testify json`
2121
* for JUNIT format: `swift test | testify junit`
2222
* for MD format: `swift test | testify md`
23+
* for GFM format: `swift test | testify gfm`
2324

2425
You can just use the [Swift Package Manager](https://theswiftdev.com/2017/11/09/swift-package-manager-tutorial/) as usual:
2526

2627
```swift
27-
.package(url: "https://github.com/binarybirds/testify", from: "1.1.1"),
28+
.package(url: "https://github.com/binarybirds/testify", from: "1.1.2"),
2829
```
2930

3031
⚠️ Don't forget to add "Testify" to your target as a dependency!
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import Foundation
2+
3+
public struct TestResultGitHubFlavoredMarkdownEncoder: TestResultEncoder {
4+
5+
public init() {
6+
7+
}
8+
9+
public func encode(_ input: TestSuite) throws -> String {
10+
var totalTestsCount = 0
11+
var totalTime: Double = 0
12+
var totalSucceedCount = 0
13+
var totalFailedCount = 0
14+
var result = ""
15+
16+
let suites: [TestSuite] = input.children.reduce([]) { $0 + $1.children }
17+
for suite in suites {
18+
result += "\n"
19+
let name = suite.name
20+
let count = suite.cases.count
21+
let time = suite.cases.reduce(0) { $0 + $1.duration }
22+
let successCount = suite.cases.reduce(0) { $0 + ($1.outcome == .success ? 1 : 0) }
23+
let failureCount = suite.cases.reduce(0) { $0 + ($1.outcome == .failure ? 1 : 0) }
24+
25+
totalTestsCount += count
26+
totalTime += time
27+
totalSucceedCount += successCount
28+
totalFailedCount += totalFailedCount
29+
30+
result += "<details>\n"
31+
let suiteResult = count == successCount ? "" : ""
32+
result += "<summary> \(suiteResult) \(name): \(count) tests were completed in \(timeString(time)) with \(successCount) passed, \(failureCount) failed.</summary>\n"
33+
34+
for testCase in suite.cases {
35+
let name = testCase.testName
36+
let testResult = testCase.outcome == .success ? "" : ""
37+
let time = timeString(testCase.duration)
38+
result += "| \(testResult) \(time) | \(name) <br>\n"
39+
}
40+
41+
result += "<br>\n"
42+
result += "</details>\n"
43+
}
44+
45+
let testsRunResult = totalTestsCount == totalSucceedCount ? "" : ""
46+
var testsResult = "# \(testsRunResult) \(totalTestsCount) tests were completed in \(timeString(totalTime))"
47+
testsResult += "\n \n"
48+
testsResult += "\(totalSucceedCount) tests passed, \(totalFailedCount) test failed.\n"
49+
testsResult += "\n----\n"
50+
testsResult += result
51+
52+
return testsResult
53+
}
54+
55+
private func timeString(_ sec: Double) -> String {
56+
"\(String(format: "%.2f", sec))s"
57+
}
58+
}

Sources/TestifySDK/Models/OutputFormat.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import Foundation
99

10-
public enum OutputFormat : String {
10+
public enum OutputFormat : String, CaseIterable {
1111
case json
1212
case junit
1313
case md
14+
case gfm
1415
}

Sources/testify/main.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import Foundation
99
import TestifySDK
1010

1111
let args = CommandLine.arguments
12-
var format: String = OutputFormat.json.rawValue
12+
var outputFormat: OutputFormat = .json
1313
if (args.count >= 2) {
14-
if let enumCase = OutputFormat(rawValue: args[1]) {
15-
format = enumCase.rawValue
14+
if let format = OutputFormat(rawValue: args[1]) {
15+
outputFormat = format
1616
} else {
17-
fatalError("Error: Unknown output format. Available formats: 'json', 'junit', 'md'")
17+
let formats = OutputFormat.allCases
18+
.map { "'\($0)'"}
19+
.joined(separator: ", ")
20+
fatalError("Error: Unknown output format. Available formats: \(formats)")
1821
}
1922
}
2023

@@ -28,23 +31,25 @@ repeat {
2831
let decoder = RawTestResultDecoder()
2932
let suite = try decoder.decode(input)
3033

31-
switch format {
32-
case OutputFormat.json.rawValue:
34+
switch outputFormat {
35+
case .json:
3336
let encoder = JSONEncoder()
3437
encoder.outputFormatting = .prettyPrinted
3538
let jsonData = try! encoder.encode(suite)
3639
print("\n", String(data: jsonData, encoding: .utf8)!, "\n")
3740

38-
case OutputFormat.junit.rawValue:
41+
case .junit:
3942
let encoder = TestResultJunitEncoder()
4043
let junitData = try! encoder.encode(suite)
4144
print(junitData)
4245

43-
case OutputFormat.md.rawValue:
46+
case .md:
4447
let encoder = TestResultMarkdownEncoder()
4548
let mdData = try! encoder.encode(suite)
4649
print(mdData)
4750

48-
default:
49-
fatalError("Error: Unknown output format")
51+
case .gfm:
52+
let encoder = TestResultGitHubFlavoredMarkdownEncoder()
53+
let mdData = try! encoder.encode(suite)
54+
print(mdData)
5055
}

0 commit comments

Comments
 (0)