Skip to content

Commit

Permalink
Fixing code coverage reports
Browse files Browse the repository at this point in the history
  • Loading branch information
rockbruno committed Mar 25, 2019
1 parent 0ebf881 commit 901f742
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ SwiftInfo is a simple CLI tool that extracts, tracks and analyzes metrics that a

## Usage

SwiftInfo requires the raw logs of a succesful test/archive build combo to work, so it's better used as the last step of a CI pipeline. If you use Fastlane, you can easily expose the raw logs by adding a `buildlog_path` to `scan` and `gym`. Here's a simple example of a Fastlane step that runs tests, submits an archive to TestFlight and runs SwiftInfo (be sure to edit the folder paths to what's being used by your project):
SwiftInfo requires the raw logs of a succesful test/archive build combo to work, so it's better used as the last step of a CI pipeline.

If you use Fastlane, you can easily expose the raw logs by adding `buildlog_path` to `scan` and `gym`. Here's a simple example of a Fastlane step that runs tests, submits an archive to TestFlight and runs SwiftInfo (be sure to edit the folder paths to what's being used by your project):

```ruby
desc "Submits a new beta build and runs SwiftInfo"
Expand Down Expand Up @@ -70,9 +72,15 @@ api.save(output: output)

The full list of providers you can use and the documentation for the `SwiftInfo` api is available here. --WIP--

## Output

After successfully extracting data, SwiftInfo will add/update a json file in the `{Infofile path}/SwiftInfo-output` folder. It's important to commit this file after the running the tool as this is what SwiftInfo uses to compare new pieces of information.

Although you can't do anything with the output for now besides sending it to Slack, we'll develop tools in the future that allow you to convert this JSON to graphs inside a HTML page.

## Tracking custom info

If you wish to track something that's not handled by the default providers, you can do so by creating your own provider by creating a `struct` that [inherits from InfoProvider](https://github.com/rockbruno/SwiftInfo/blob/master/Sources/SwiftInfoCore/InfoProvider.swift) and adding it to your Infofile. Here's a simple provider that tracks the number of files in a project where adding new files is bad:
If you wish to track something that's not handled by the default providers, you can create your own provider by creating a `struct` that [inherits from InfoProvider](https://github.com/rockbruno/SwiftInfo/blob/master/Sources/SwiftInfoCore/InfoProvider.swift) inside your Infofile. Here's a simple provider that tracks the number of files in a project where adding new files is bad:

```swift
struct FileCountProvider: InfoProvider {
Expand Down Expand Up @@ -113,12 +121,6 @@ struct FileCountProvider: InfoProvider {

**If you end up creating a custom provider, consider submitting it here as a pull request to have it added as a default one!**

## Output

After successfully extracting data, SwiftInfo will add/update a json file in the `{Infofile path}/SwiftInfo-output` folder. It's important to commit this file after the running the tool as this is what SwiftInfo uses to compare new pieces of information.

Although for now you can't do anything with the output besides sending it to Slack, we'll develop tools in the future that allow you to convert this JSON to graphs inside a HTML page.

## Installation

### CocoaPods
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftInfoCore/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ public struct FileUtils {
public let outputFileName = "SwiftInfoOutput.json"

public var testLog: String? {
let url = URL(fileURLWithPath: FileUtils.testLogFilePath)
let folder = infofileFolder() ?? ""
let url = URL(fileURLWithPath: folder + FileUtils.testLogFilePath)
return try? String(contentsOf: url)
}

public var buildLog: String? {
let url = URL(fileURLWithPath: FileUtils.buildLogFilePath)
let folder = infofileFolder() ?? ""
let url = URL(fileURLWithPath: folder + FileUtils.buildLogFilePath)
return try? String(contentsOf: url)
}

Expand Down
11 changes: 6 additions & 5 deletions Sources/SwiftInfoCore/ProjectInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public struct ProjectInfo: CustomStringConvertible {
guard let contents = try? FileManager.default.contentsOfDirectory(atPath: projectFolder) else {
fail("FileManager failed.")
}
guard let project = contents.first(where: { $0.hasSuffix(".xcodeproj") }) else {
guard let project = contents.first(where: { $0.hasSuffix(xcodeproj) }) else {
fail("Project file not found.")
}
guard let xcodeproj = try? XcodeProj(path: Path(project)) else {
fail("Failed to load .pbxproj!")
guard let xcodeproj = try? XcodeProj(path: Path(projectFolder + project)) else {
fail("Failed to load .pbxproj! (\(projectFolder + project))")
}
guard let pbxTarget = xcodeproj.pbxproj.targets(named: target).first else {
fail("Target not found.")
Expand All @@ -51,8 +51,9 @@ public struct ProjectInfo: CustomStringConvertible {
}

func plistDict() -> NSDictionary {
guard let dictionary = NSDictionary(contentsOfFile: plistPath) else {
fail("Failed to load plist \(plistPath)")
let folder = fileUtils.infofileFolder() ?? ""
guard let dictionary = NSDictionary(contentsOfFile: folder + plistPath) else {
fail("Failed to load plist \(folder + plistPath)")
}
return dictionary
}
Expand Down
22 changes: 14 additions & 8 deletions Sources/SwiftInfoCore/Providers/CodeCoverageProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public struct CodeCoverageProvider: InfoProvider {
}

public let description: String = "Code Coverage"
public let percentage: Int
public let percentageInt: Int

public init(percentage: Int) {
self.percentage = percentage
public init(percentageInt: Int) {
self.percentageInt = percentageInt
}

public static func extract() throws -> CodeCoverageProvider {
Expand All @@ -39,7 +39,7 @@ public struct CodeCoverageProvider: InfoProvider {
}
let codeCoverage = desiredTarget["lineCoverage"] as! Double
let rounded = Int(1000 * codeCoverage)
return CodeCoverageProvider(percentage: rounded)
return CodeCoverageProvider(percentageInt: rounded)
}

static func removeTemporaryFile() {
Expand All @@ -48,23 +48,29 @@ public struct CodeCoverageProvider: InfoProvider {

public func summary(comparingWith other: CodeCoverageProvider?) -> Summary {
let prefix = "📊 Code Coverage"
let percentage = toPercentage(percentageInt: percentageInt)
guard let other = other else {
return Summary(text: prefix + ": \(percentage)", style: .neutral)
}
guard percentage != other.percentage else {
guard percentageInt != other.percentageInt else {
return Summary(text: prefix + ": Unchanged. (\(percentage))", style: .neutral)
}
let modifier: String
let style: Summary.Style
if percentage > other.percentage {
if percentageInt > other.percentageInt {
modifier = "*grew*"
style = .positive
} else {
modifier = "was *reduced*"
style = .negative
}
let difference = abs(other.percentage - percentage)
let text = prefix + " \(modifier) by \(Double(difference) / 10) (\(Double(percentage) / 10))"
let differenceInt = abs(other.percentageInt - percentageInt)
let differencePercentage = toPercentage(percentageInt: differenceInt)
let text = prefix + " \(modifier) by \(percentage) (\(differencePercentage))"
return Summary(text: text, style: style)
}

func toPercentage(percentageInt: Int) -> String {
return "\(Double(percentageInt) / 10)%"
}
}
2 changes: 1 addition & 1 deletion SwiftInfo.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = 'SwiftInfo'
s.module_name = 'SwiftInfo'
s.version = '0.0.1'
s.version = '0.1.0'
s.license = { type: 'GNU GPL v3.0', file: 'LICENSE.md' }
s.summary = 'Extract and analyze the evolution of an iOS app\'s code.'
s.homepage = 'https://github.com/rockbruno/SwiftInfo'
Expand Down

0 comments on commit 901f742

Please sign in to comment.