Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from mysugr/feature/multiple_archive_support
Browse files Browse the repository at this point in the history
make archive parameter accept array of archives
  • Loading branch information
heinzl authored Mar 24, 2020
2 parents 1596a23 + 7c3ad04 commit 5045cfe
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
9 changes: 5 additions & 4 deletions cococo/Cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class Cli {
)
let archive = parser.add(
positional: "xcresult",
kind: String.self,
kind: [String].self,
optional: false,
usage: "Path to the xcresult",
strategy: .upToNextOption,
usage: "Multiple paths to xcresult archives",
completion: .filename
)
let excluded = parser.add(
Expand All @@ -39,10 +40,10 @@ class Cli {
let argsv = Array(CommandLine.arguments.dropFirst())
let parsedArguments = try parser.parse(argsv)

let archivePath = parsedArguments.get(archive) ?? ""
let archivePaths = parsedArguments.get(archive) ?? []
let excludedFileExtensions = parsedArguments.get(excluded)

let xml = try Converter().convert(archivePath, excludedFileExtensions: excludedFileExtensions)
let xml = try Converter().convert(archivePaths, excludedFileExtensions: excludedFileExtensions)
io.print(xml)

} catch ArgumentParserError.expectedValue(let value) {
Expand Down
96 changes: 54 additions & 42 deletions cococoLibrary/Converter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,32 @@ public class Converter {
private let xmlEscaper = XMLEscaper()

public init() {}

/// Converts an array of xcresult archives to SonarQube's generic code coverage format
///
/// - Parameters:
/// - archivePaths: Paths to the xcresult archives to be processed
/// - Returns: The converted XML string
public func convert(_ archivePaths: [String], excludedFileExtensions: [String]?) throws -> String {
var finalOutput = [String?]()
try archivePaths.forEach {
try finalOutput += convert($0, excludedFileExtensions: excludedFileExtensions)
}
finalOutput.insert("<coverage version=\"1\">", at: 0)
finalOutput.append("</coverage>\n")

return finalOutput
.lazy
.compactMap { $0 }
.joined(separator: "\n")
}

/// Converts a single code coverage file to SonarQube's generic code coverage format
///
/// - Parameters:
/// - filePath: Path to the code coverage file
/// - archivePath: Path to the xcresult archive
/// - Returns: The resulting XML string for the given file
internal func convertFile(_ filePath: String, archivePath: String) throws -> String {
let bash = Bash()
let viewOutput = try bash.execute(commandName: "xcrun", arguments: ["xccov", "view", "--archive", "--file", filePath, archivePath])
let lines = viewOutput.split(separator: "\n")

var output = [String]()
let escapedPath = xmlEscaper.escape(filePath)
output.append(" <file path=\"\(escapedPath)\">")

for line in lines {
if line.hasSuffix("*") {
continue
}
let components = line.components(separatedBy: ": ")
guard components.count == 2 else {
continue
}
let lineNumber = components[0].trimmingCharacters(in: .whitespaces)
let isTested = components[1].hasPrefix("0") ? "false" : "true"
output.append(" <lineToCover lineNumber=\"\(lineNumber)\" covered=\"\(isTested)\"/>")
}
output.append(" </file>")
return output.joined(separator: "\n")
}

/// Converts a xcresult archive to SonarQube's generic code coverage format
/// Converts an xcresult archive to SonarQube's generic code coverage format
///
/// - Parameters:
/// - archivePath: Path to the xcresult archive
/// - Returns: The converted XML string
public func convert(_ archivePath: String, excludedFileExtensions: [String]?) throws -> String {
/// - Returns: An array containing converted XML nodes for the archive at the given path
internal func convert(_ archivePath: String, excludedFileExtensions: [String]?) throws -> [String?] {
let bash = Bash()
let listOutput = try bash.execute(
commandName: "xcrun",
Expand Down Expand Up @@ -81,15 +69,39 @@ public class Converter {
io.print("Conversion failed for: \(filePath)", to: .error)
}
}

finalOutput.insert("<coverage version=\"1\">", at: 0)
finalOutput.append("</coverage>\n")

return finalOutput
.lazy
.compactMap { $0 }
.joined(separator: "\n")
return finalOutput
}

/// Converts a single code coverage file to SonarQube's generic code coverage format
///
/// - Parameters:
/// - filePath: Path to the code coverage file
/// - archivePath: Path to the xcresult archive
/// - Returns: The resulting XML string for the given file
internal func convertFile(_ filePath: String, archivePath: String) throws -> String {
let bash = Bash()
let viewOutput = try bash.execute(commandName: "xcrun", arguments: ["xccov", "view", "--archive", "--file", filePath, archivePath])
let lines = viewOutput.split(separator: "\n")

var output = [String]()
let escapedPath = xmlEscaper.escape(filePath)
output.append(" <file path=\"\(escapedPath)\">")

for line in lines {
if line.hasSuffix("*") {
continue
}
let components = line.components(separatedBy: ": ")
guard components.count == 2 else {
continue
}
let lineNumber = components[0].trimmingCharacters(in: .whitespaces)
let isTested = components[1].hasPrefix("0") ? "false" : "true"
output.append(" <lineToCover lineNumber=\"\(lineNumber)\" covered=\"\(isTested)\"/>")
}
output.append(" </file>")
return output.joined(separator: "\n")
}

internal func filterFilePaths(_ paths: [String], excludedFileExtensions: [String]) -> [String] {
return paths.filter({ (filePath) -> Bool in
Expand Down
2 changes: 1 addition & 1 deletion cococoLibraryTests/ConverterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConverterTests: XCTestCase {
}

func testExample() throws {
let result = try sut.convert(XCResultExamples.example, excludedFileExtensions: nil)
let result = try sut.convert([XCResultExamples.example], excludedFileExtensions: nil)
let expectedResult = try String(contentsOf: XCResultExamples.exampleResult)
XCTAssertEqual(result, expectedResult)
}
Expand Down

0 comments on commit 5045cfe

Please sign in to comment.