Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find out which individual lines are covered #47

Open
alexanderwe opened this issue Jan 21, 2023 · 3 comments
Open

Find out which individual lines are covered #47

alexanderwe opened this issue Jan 21, 2023 · 3 comments

Comments

@alexanderwe
Copy link

Hey @davidahouse, I hope it's fine to ask a question in an issue.

I was just wondering if it's possible to find out if an individual line of a CodeCoverageFile has been covered by a test ? Or this this represented by the CodeCoverageFileFunction.lineNumber property ?

Thanks in advance !

@alexanderwe alexanderwe changed the title Find out which lines are covered Find out which individual lines are covered Jan 21, 2023
@davidahouse
Copy link
Owner

@alexanderwe Sure, always ok to ask questions here.

So currently this library doesn't get that level of detail. If you look at the way this code runs xccov, it uses the arguments --report and --json. For that output, we only get down to the function level in each file. For most purposes that should be far enough.

But if you wanted to get to the line level, you need to run the xccov command a different way. What you have to do is something like this:

xcrun xccov view --archive --file <FULL PATH TO THE FILE> <FULL PATH TO XCRESULT FILE>

This gives you an output with details on every single line of the file. That is likely only helpful if you are trying to build an IDE, or perhaps show a web version of the file with the covered/un-covered lines formatted somehow.

I suspect it wouldn't be hard to add this to the library, so we can keep this issue open for that if someone wants to tackle it.

@alexanderwe
Copy link
Author

@davidahouse Thanks a lot for your answer and suggestion ! What you suggested works indeed. By calling that command I was able to get the code coverage on a per file level. It was necessary too because I needed to do a per file line coverage report. So again, thanks a lot for that :)

I am fine with keeping the issue open. I can try if I find some free time to incorporate what I implemented also here. I just don't know if I find the time soon.

@Nikoloutsos
Copy link

Nikoloutsos commented Aug 9, 2024

Here is a snippet of the code I've implemented for doing that and I will share with any future reader 👇

import Foundation
import ShellOut

typealias FilesCoverage = [String: [LineCoverage]]

struct LineCoverage: Decodable {
    let isExecutable: Bool
    let line: Int
    let executionCount: Int?
}

func extractXCResultXCCovArchive(XCResultPath: String) throws -> FilesCoverage {
    try shellOut(to: ["xcrun xccov view --archive --json \(XCResultPath) > temp_xcrun_xccov_archive.json"])
    let json = try shellOut(to: ["cat temp_xcrun_xccov_archive.json"])
    return try JSONDecoder().decode(FilesCoverage.self, from: json.data(using: .utf8)!)
}

Shellout is a simple library for executing command line code from swift.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants