diff-coverage-gradle icon indicating copy to clipboard operation
diff-coverage-gradle copied to clipboard

diffSource.git.compareWith ignores uncommitted files

Open dbwiddis opened this issue 2 years ago • 8 comments

Describe the bug The diffSource.git.compareWith configuration has no effect if the files have not been committed to git.

If this is by design, it should be clearly documented. However, it does not align with the expectation of executing git diff <ref> and saving the results to a file that the plugin points to. In particular, it makes a diff reference to HEAD completely useless after the commit.

Additionally the comment in the config explicitly specifies uncommitted files:

Compares current HEAD and all uncommited with provided branch

Desktop (please complete the following information):

  • OS: Mac OS X 13.0 x86_64
  • Gradle version: 7.5.1
  • Diff Coverage plugin version 0.9.5

To Reproduce

  1. Make changes to a file that require coverage, save them but do not commit.
  2. gradle diffCoverage with the below configuration
diffCoverageReport {
    diffSource {
        git.compareWith 'refs/remotes/origin/main'
    }

    reports {
        html = true
    }
}
  1. Observe the files in build/reports/jacoco directory:
  • The diff.patch file has 0 bytes
  • The html report under diffCoverage/html/index.html says "No class files specified." (See also report in #65 for which this may have been the cause.)

Expected behavior

Changed files are found and diff.patch is not empty.

Additional context

Workaround is to configure:

    diffSource {
        file = 'build/tmp/git.diff'
    }

and then execute:

git diff refs/remotes/origin/main > build/tmp/git.diff
gradle diffCoverage

dbwiddis avatar Nov 13 '22 22:11 dbwiddis

Hi @dbwiddis

It's known issue that was caused by fix #34 (release 0.9.1).

The embedded JGit includes only changes that were staged. I played a lot with JGit to generate the full diff(including uncommited) but bad luck.

I consider this issue as minor, because diff-coverage checks extremelly useful when it runs on CI(no uncommited files there) and less important for local development. At the same time the fix #34 is critical.

Maybe, I should try one more time :)

SurpSG avatar Nov 14 '22 19:11 SurpSG

By the way, you could use the next workaround if the issue is critical for you

ext.createDiffUrl = { ->
    def diffBase = project.hasProperty('diffBase') ? project.diffBase : 'HEAD'
    afterEvaluate {
        logger.info("Computing coverage for changes between $diffBase and current state (including uncommitted)")
    }
    def file = Files.createTempFile(URLEncoder.encode(project.name, 'UTF-8'), '.diff').toFile()
    file.withOutputStream { out ->
        exec {
            commandLine 'git', 'diff', '--no-color', '--minimal', diffBase
            standardOutput = out
        }
    }
    return file.toURI().toURL()
}

diffCoverageReport {
    afterEvaluate {
        diffSource.url = createDiffUrl()
    }
}

See full example here.

SurpSG avatar Nov 14 '22 20:11 SurpSG

Thanks! I was going to try to hack my own bash script into gradle but your workaround looks better!

dbwiddis avatar Nov 15 '22 07:11 dbwiddis

Tried to apply the above example, blocked here:

Caused by: org.apache.hc.client5.http.ClientProtocolException: Target host is not specified
        at org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:173)
        at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:75)
        at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:89)
        at com.form.coverage.http.HttpClientKt.executeGetRequest(HttpClient.kt:8)
        at com.form.coverage.diff.UrlDiffSource$diffContent$2.invoke(DiffSource.kt:42)
        at com.form.coverage.diff.UrlDiffSource$diffContent$2.invoke(DiffSource.kt:37)

dbwiddis avatar Nov 15 '22 15:11 dbwiddis

Got it working by just returning the file directly instead of URL.

dbwiddis avatar Nov 15 '22 15:11 dbwiddis

Oh, forgot about changes in http client. The example above works for older versions. But changing URL to file and diffSource.url to diffSource.file should work

SurpSG avatar Nov 15 '22 19:11 SurpSG

All good, although it took me another 3 hours today to figure out that GitHub actions (1) need a not-shallow diff, and (2) you have to append origin to the destination ref. All done! https://github.com/opensearch-project/opensearch-sdk-java/pull/248

dbwiddis avatar Nov 15 '22 22:11 dbwiddis

Thanks for the prompt responses, as well!

dbwiddis avatar Nov 15 '22 22:11 dbwiddis