Skip to content

Commit

Permalink
Merge pull request #47 from a7ex/fix-projectRoot
Browse files Browse the repository at this point in the history
Fix project root
  • Loading branch information
a7ex authored Dec 6, 2024
2 parents 8135494 + 2fcb440 commit 6a68e37
Show file tree
Hide file tree
Showing 18 changed files with 467 additions and 208 deletions.
2 changes: 2 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--swiftversion 5.9

--allman false
--assetliterals visual-width
--beforemarks
Expand Down
8 changes: 4 additions & 4 deletions .swiftpm/xcode/xcshareddata/xcschemes/xcresultparser.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
argument = "/Users/alex/xcodebuild_result.xcresult"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-d"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-o junit"
isEnabled = "NO">
Expand Down Expand Up @@ -142,10 +146,6 @@
argument = "-h"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "/Users/adf/Desktop/Sparkasse.xcresult"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-o md"
isEnabled = "NO">
Expand Down
2 changes: 1 addition & 1 deletion CommandlineTool/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ArgumentParser
import Foundation
import XcresultparserLib

private let marketingVersion = "1.8.1"
private let marketingVersion = "1.8.2"

struct xcresultparser: ParsableCommand {
static let configuration = CommandConfiguration(
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ let package = Package(
.copy("TestAssets/cobertura.xml"),
.copy("TestAssets/coberturaExcludingDirectory.xml"),
.copy("TestAssets/warnings.json"),
.copy("TestAssets/resultWithCompileError.xcresult")
.copy("TestAssets/resultWithCompileError.xcresult"),
.copy("TestAssets/sonarTestExecutionWithProjectRootAbsolute.xml"),
.copy("TestAssets/sonarTestExecutionWithProjectRootRelative.xml")
]
)
]
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ You should see the tool respond like this:
```
Error: Missing expected argument '<xcresult-file>'
OVERVIEW: xcresultparser 1.8.1
OVERVIEW: xcresultparser 1.8.2
Interpret binary .xcresult files and print summary in different formats: txt,
xml, html or colored cli output.
Expand Down Expand Up @@ -231,7 +231,14 @@ xcresultparser -o errors test.xcresult > errors.json
The tools to get the data from the xcresult archive yield absolute path names.
So you must provide an absolute pathname to the *sonar.sources* paramater of the *sonar-scanner* CLI tool and it must of course match the directory, where *xcodebuild* ran the tests and created the *.xcresult* archive.

If you want to use the test results for sonarqube, there is another twist: the .xcresult bundle only lists the test by testclass, but not by file. However sonarqube expects the file paths of the tests. In this case you must provide a --project-root to *xcresultparser*. Only then *xcresultparser* can convert the classnames to file names, by *grep*-ing for "class NameOfClass". If such a file is found in the directory provided in *--project-root*, then the file path can be detrmined and the *sonar-scanner* happily can scan the files for tests.
If you want to use the test results for sonarqube, there is another twist: the .xcresult bundle only lists the test by testclass, but not by file. However the sonarqube CLI tool expects the file paths of the tests. In this case you must provide a --project-root to *xcresultparser*. Only then *xcresultparser* can convert the classnames to file names, by *egrep*-ing for `^(?:public )?(?:final )?(?:public )?(?:(class|\@implementation) )\w+`. If such a file is found in the directory provided in *--project-root*, then the file path can be detrmined and the *sonar-scanner* happily can scan the files for tests. The pattern matches all swift and objective-c classes.

Since searching all files in `project-root` takes some time, an index path names for class names is created beforehand and used as lookup table.

The following egrep expression is used to create the lookup table for the filenames of classes:
```
egrep -rio --include "*.swift" --include "*.m" "^(?:public )?(?:final )?(?:public )?(?:(class|\@implementation) )\w+" $project-root
```

In cases where the xcresult archive is not created on the same machine and the paths used for *sonar-scanner* differ, the pathnames need to be adjusted.
In such a case you can use a relative path for the *sonar.sources* paramater of the *sonar-scanner* CLI tool and convert the output of xcresultparser to also return relative path names.
Expand Down
128 changes: 64 additions & 64 deletions Sources/xcresultparser/CoberturaCoverageConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class CoberturaCoverageConverter: CoverageConverter, XmlSerializable {
dtd.name = "coverage"
// dtd.systemID = "http://cobertura.sourceforge.net/xml/coverage-04.dtd"
dtd.systemID =
"https://github.com/cobertura/cobertura/blob/master/cobertura/src/site/htdocs/xml/coverage-04.dtd"
"https://github.com/cobertura/cobertura/blob/master/cobertura/src/site/htdocs/xml/coverage-04.dtd"

let rootElement = makeRootElement()

Expand Down Expand Up @@ -178,7 +178,7 @@ public class CoberturaCoverageConverter: CoverageConverter, XmlSerializable {

return rootElement
}

// this ised to be fetched online from http://cobertura.sourceforge.net/xml/coverage-04.dtd
// that broke, when the URL changed to:
// https://github.com/cobertura/cobertura/blob/master/cobertura/src/site/htdocs/xml/coverage-04.dtd
Expand All @@ -187,68 +187,68 @@ public class CoberturaCoverageConverter: CoverageConverter, XmlSerializable {
// IMO all that was overengineered for the followong 60 lines string...
// ...which will probably never ever change!
private var dtd04 = """
<!-- Portions (C) International Organization for Standardization 1986:
Permission to copy in any form is granted for use with
conforming SGML systems and applications as defined in
ISO 8879, provided this notice is included in all copies.
-->
<!ELEMENT coverage (sources?,packages)>
<!ATTLIST coverage line-rate CDATA #REQUIRED>
<!ATTLIST coverage branch-rate CDATA #REQUIRED>
<!ATTLIST coverage lines-covered CDATA #REQUIRED>
<!ATTLIST coverage lines-valid CDATA #REQUIRED>
<!ATTLIST coverage branches-covered CDATA #REQUIRED>
<!ATTLIST coverage branches-valid CDATA #REQUIRED>
<!ATTLIST coverage complexity CDATA #REQUIRED>
<!ATTLIST coverage version CDATA #REQUIRED>
<!ATTLIST coverage timestamp CDATA #REQUIRED>
<!ELEMENT sources (source*)>
<!ELEMENT source (#PCDATA)>
<!ELEMENT packages (package*)>
<!ELEMENT package (classes)>
<!ATTLIST package name CDATA #REQUIRED>
<!ATTLIST package line-rate CDATA #REQUIRED>
<!ATTLIST package branch-rate CDATA #REQUIRED>
<!ATTLIST package complexity CDATA #REQUIRED>
<!ELEMENT classes (class*)>
<!ELEMENT class (methods,lines)>
<!ATTLIST class name CDATA #REQUIRED>
<!ATTLIST class filename CDATA #REQUIRED>
<!ATTLIST class line-rate CDATA #REQUIRED>
<!ATTLIST class branch-rate CDATA #REQUIRED>
<!ATTLIST class complexity CDATA #REQUIRED>
<!ELEMENT methods (method*)>
<!ELEMENT method (lines)>
<!ATTLIST method name CDATA #REQUIRED>
<!ATTLIST method signature CDATA #REQUIRED>
<!ATTLIST method line-rate CDATA #REQUIRED>
<!ATTLIST method branch-rate CDATA #REQUIRED>
<!ATTLIST method complexity CDATA #REQUIRED>
<!ELEMENT lines (line*)>
<!ELEMENT line (conditions*)>
<!ATTLIST line number CDATA #REQUIRED>
<!ATTLIST line hits CDATA #REQUIRED>
<!ATTLIST line branch CDATA "false">
<!ATTLIST line condition-coverage CDATA "100%">
<!ELEMENT conditions (condition*)>
<!ELEMENT condition EMPTY>
<!ATTLIST condition number CDATA #REQUIRED>
<!ATTLIST condition type CDATA #REQUIRED>
<!ATTLIST condition coverage CDATA #REQUIRED>
"""
<!-- Portions (C) International Organization for Standardization 1986:
Permission to copy in any form is granted for use with
conforming SGML systems and applications as defined in
ISO 8879, provided this notice is included in all copies.
-->
<!ELEMENT coverage (sources?,packages)>
<!ATTLIST coverage line-rate CDATA #REQUIRED>
<!ATTLIST coverage branch-rate CDATA #REQUIRED>
<!ATTLIST coverage lines-covered CDATA #REQUIRED>
<!ATTLIST coverage lines-valid CDATA #REQUIRED>
<!ATTLIST coverage branches-covered CDATA #REQUIRED>
<!ATTLIST coverage branches-valid CDATA #REQUIRED>
<!ATTLIST coverage complexity CDATA #REQUIRED>
<!ATTLIST coverage version CDATA #REQUIRED>
<!ATTLIST coverage timestamp CDATA #REQUIRED>
<!ELEMENT sources (source*)>
<!ELEMENT source (#PCDATA)>
<!ELEMENT packages (package*)>
<!ELEMENT package (classes)>
<!ATTLIST package name CDATA #REQUIRED>
<!ATTLIST package line-rate CDATA #REQUIRED>
<!ATTLIST package branch-rate CDATA #REQUIRED>
<!ATTLIST package complexity CDATA #REQUIRED>
<!ELEMENT classes (class*)>
<!ELEMENT class (methods,lines)>
<!ATTLIST class name CDATA #REQUIRED>
<!ATTLIST class filename CDATA #REQUIRED>
<!ATTLIST class line-rate CDATA #REQUIRED>
<!ATTLIST class branch-rate CDATA #REQUIRED>
<!ATTLIST class complexity CDATA #REQUIRED>
<!ELEMENT methods (method*)>
<!ELEMENT method (lines)>
<!ATTLIST method name CDATA #REQUIRED>
<!ATTLIST method signature CDATA #REQUIRED>
<!ATTLIST method line-rate CDATA #REQUIRED>
<!ATTLIST method branch-rate CDATA #REQUIRED>
<!ATTLIST method complexity CDATA #REQUIRED>
<!ELEMENT lines (line*)>
<!ELEMENT line (conditions*)>
<!ATTLIST line number CDATA #REQUIRED>
<!ATTLIST line hits CDATA #REQUIRED>
<!ATTLIST line branch CDATA "false">
<!ATTLIST line condition-coverage CDATA "100%">
<!ELEMENT conditions (condition*)>
<!ELEMENT condition EMPTY>
<!ATTLIST condition number CDATA #REQUIRED>
<!ATTLIST condition type CDATA #REQUIRED>
<!ATTLIST condition coverage CDATA #REQUIRED>
"""
}

private struct LineInfo {
Expand Down
10 changes: 7 additions & 3 deletions Sources/xcresultparser/CoverageConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class CoverageConverter {
let coverageTargets: Set<String>
let excludedPaths: Set<String>

// MARK: - Dependencies

let shell = DependencyFactory.createShell()

public init?(
with url: URL,
projectRoot: String = "",
Expand Down Expand Up @@ -81,7 +85,7 @@ public class CoverageConverter {
}
arguments.append("--json")
arguments.append(resultFile.url.path)
let coverageData = try Shell.execute(program: "/usr/bin/xcrun", with: arguments)
let coverageData = try shell.execute(program: "/usr/bin/xcrun", with: arguments)
return try JSONDecoder().decode(FileCoverage.self, from: coverageData)
}

Expand All @@ -107,7 +111,7 @@ public class CoverageConverter {
arguments.append("--file")
arguments.append(path)
arguments.append(resultFile.url.path)
let coverageData = try Shell.execute(program: "/usr/bin/xcrun", with: arguments)
let coverageData = try shell.execute(program: "/usr/bin/xcrun", with: arguments)
return String(decoding: coverageData, as: UTF8.self)
}

Expand All @@ -121,7 +125,7 @@ public class CoverageConverter {
}
arguments.append("--file-list")
arguments.append(resultFile.url.path)
let filelistData = try Shell.execute(program: "/usr/bin/xcrun", with: arguments)
let filelistData = try shell.execute(program: "/usr/bin/xcrun", with: arguments)
return String(decoding: filelistData, as: UTF8.self).components(separatedBy: "\n")
}
}
Loading

0 comments on commit 6a68e37

Please sign in to comment.