Skip to content

Commit 24f913d

Browse files
committed
encoding
1 parent 0fb1e99 commit 24f913d

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Sources/Commands/SwiftTestCommand.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ package enum CoverageFormat: String, ExpressibleByArgument, CaseIterable, Compar
237237
}
238238
}
239239

240+
extension CoverageFormat: Encodable {}
241+
240242
enum CoveragePrintPathMode: String, ExpressibleByArgument, CaseIterable {
241243
case json
242244
case text
@@ -390,7 +392,7 @@ package func getOutputDir(from content: String) throws -> AbsolutePath? {
390392

391393
return returnValue
392394
}
393-
package struct CoverageFormatOutput {
395+
package struct CoverageFormatOutput: Encodable {
394396
private var _underlying: [CoverageFormat : AbsolutePath]
395397

396398
package init() {
@@ -1104,13 +1106,13 @@ extension SwiftTestCommand {
11041106
formats: [CoverageFormat],
11051107
printMode: CoveragePrintPathMode,
11061108
) async throws {
1107-
var data = [CoverageFormat : AbsolutePath]()
1109+
var coverageData = [CoverageFormat : AbsolutePath]()
11081110
for format in formats {
11091111
let config = try await self.getCodeCoverageConfiguration(swiftCommandState, format: format)
1110-
data[format] = config.outputDir
1112+
coverageData[format] = config.outputDir
11111113
}
11121114

1113-
let coverageOutput = CoverageFormatOutput(data: data)
1115+
let coverageOutput = CoverageFormatOutput(data: coverageData)
11141116

11151117
switch printMode {
11161118
case .json:
@@ -1120,6 +1122,20 @@ extension SwiftTestCommand {
11201122
let textOutput = coverageOutput.encodeAsText()
11211123
print(textOutput)
11221124
}
1125+
1126+
print("-----------------------")
1127+
let data: Data
1128+
switch printMode {
1129+
case .json:
1130+
let encoder = JSONEncoder.makeWithDefaults()
1131+
encoder.keyEncodingStrategy = .convertToSnakeCase
1132+
data = try encoder.encode(coverageOutput)
1133+
case .text:
1134+
var encoder = PlainTextEncoder()
1135+
encoder.formattingOptions = [.prettyPrinted]
1136+
data = try encoder.encode(coverageOutput)
1137+
}
1138+
print(String(decoding: data, as: UTF8.self))
11231139
}
11241140
}
11251141

0 commit comments

Comments
 (0)