Skip to content

Commit e381d71

Browse files
committed
encoding
1 parent 4b7e5fa commit e381d71

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
@@ -238,6 +238,8 @@ package enum CoverageFormat: String, ExpressibleByArgument, CaseIterable, Compar
238238
}
239239
}
240240

241+
extension CoverageFormat: Encodable {}
242+
241243
enum CoveragePrintPathMode: String, ExpressibleByArgument, CaseIterable {
242244
case json
243245
case text
@@ -391,7 +393,7 @@ package func getOutputDir(from content: String) throws -> AbsolutePath? {
391393

392394
return returnValue
393395
}
394-
package struct CoverageFormatOutput {
396+
package struct CoverageFormatOutput: Encodable {
395397
private var _underlying: [CoverageFormat : AbsolutePath]
396398

397399
package init() {
@@ -1115,13 +1117,13 @@ extension SwiftTestCommand {
11151117
formats: [CoverageFormat],
11161118
printMode: CoveragePrintPathMode,
11171119
) async throws {
1118-
var data = [CoverageFormat : AbsolutePath]()
1120+
var coverageData = [CoverageFormat : AbsolutePath]()
11191121
for format in formats {
11201122
let config = try await self.getCodeCoverageConfiguration(swiftCommandState, format: format)
1121-
data[format] = config.outputDir
1123+
coverageData[format] = config.outputDir
11221124
}
11231125

1124-
let coverageOutput = CoverageFormatOutput(data: data)
1126+
let coverageOutput = CoverageFormatOutput(data: coverageData)
11251127

11261128
switch printMode {
11271129
case .json:
@@ -1131,6 +1133,20 @@ extension SwiftTestCommand {
11311133
let textOutput = coverageOutput.encodeAsText()
11321134
print(textOutput)
11331135
}
1136+
1137+
print("-----------------------")
1138+
let data: Data
1139+
switch printMode {
1140+
case .json:
1141+
let encoder = JSONEncoder.makeWithDefaults()
1142+
encoder.keyEncodingStrategy = .convertToSnakeCase
1143+
data = try encoder.encode(coverageOutput)
1144+
case .text:
1145+
var encoder = PlainTextEncoder()
1146+
encoder.formattingOptions = [.prettyPrinted]
1147+
data = try encoder.encode(coverageOutput)
1148+
}
1149+
print(String(decoding: data, as: UTF8.self))
11341150
}
11351151
}
11361152

0 commit comments

Comments
 (0)