Skip to content

Commit 7b6c562

Browse files
committed
refactor
Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>
1 parent 9450481 commit 7b6c562

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

.github/workflows/test-and-build-workflow.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ jobs:
5555
timeout-minutes: 20
5656
run: |
5757
chown -R 1000:1000 `pwd`
58-
su `id -un 1000` -c "./gradlew build ${{ env.TEST_FILTER }} || (exit_code=$?; ./gradlew jacocoTestReport; exit $exit_code)"
58+
su `id -un 1000` -c "./gradlew build ${{ env.TEST_FILTER }}"
59+
- name: Generate Jacoco Test Report
60+
if: always()
61+
run: |
62+
su `id -un 1000` -c "./gradlew jacocoTestReport -Dtests.coverage=true"
63+
- name: Upload coverage XML
64+
uses: actions/upload-artifact@v4
65+
if: always()
66+
with:
67+
name: coverage-xml-${{ matrix.java }}-${{ matrix.feature }}
68+
path: build/reports/jacoco/**/jacocoTestReport.xml
69+
if-no-files-found: warn
70+
overwrite: 'true'
5971
- name: Upload failed logs
6072
uses: actions/upload-artifact@v4
6173
if: ${{ failure() }}
@@ -70,14 +82,6 @@ jobs:
7082
name: test-reports-linux-${{ matrix.java }}-${{ matrix.feature }}
7183
path: build/reports/
7284
overwrite: 'true'
73-
- name: Upload coverage XML
74-
uses: actions/upload-artifact@v4
75-
if: always()
76-
with:
77-
name: coverage-xml-${{ matrix.java }}-${{ matrix.feature }}
78-
path: build/reports/jacoco/**/jacocoTestReport.xml
79-
if-no-files-found: warn
80-
overwrite: 'true'
8185
- name: Create Artifact Path
8286
run: |
8387
mkdir -p index-management-artifacts
@@ -153,7 +157,6 @@ jobs:
153157
if: always()
154158
runs-on: ubuntu-latest
155159
steps:
156-
- uses: actions/checkout@v4
157160
- uses: actions/download-artifact@v4
158161
with:
159162
path: downloaded-artifacts

build-tools/coverage.gradle

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
// Get gradle to generate the required jvm agent arg for us using a dummy tasks of type Test. Unfortunately Elastic's
2121
// testing tasks don't derive from Test so the jacoco plugin can't do this automatically.
22-
def jacocoDir = "${getLayout().getBuildDirectory()}/jacoco"
22+
def jacocoDir = layout.buildDirectory.dir("jacoco").get().asFile.absolutePath
2323

2424
tasks.register("dummyTest", Test) {
2525
enabled = false
@@ -42,7 +42,7 @@ tasks.register("dummyIntegTest", Test) {
4242
}
4343

4444
integTest {
45-
systemProperty 'jacoco.dir', "${jacocoDir}"
45+
systemProperty 'jacoco.dir', jacocoDir
4646
}
4747

4848
jacocoTestReport {
@@ -51,11 +51,17 @@ jacocoTestReport {
5151
mustRunAfter test, integTest
5252

5353
// Configure execution data to use any available .exec files
54-
executionData.setFrom(fileTree(dir: jacocoDir, include: '*.exec').files.findAll { it.exists() })
54+
executionData.from fileTree(dir: jacocoDir, include: '*.exec')
5555

5656
// Make the task skip only if no execution data exists at all
57+
// Using a closure to defer the check until execution time
5758
onlyIf {
58-
fileTree(dir: jacocoDir, include: '*.exec').files.any { it.exists() }
59+
def execFiles = fileTree(dir: jacocoDir, include: '*.exec').files
60+
def hasExecFiles = execFiles.any { it.exists() }
61+
if (!hasExecFiles) {
62+
println("Skipping jacocoTestReport: No execution data files found in ${jacocoDir}")
63+
}
64+
hasExecFiles
5965
}
6066

6167
sourceDirectories.from = "src/main/kotlin"
@@ -77,4 +83,11 @@ allprojects{
7783
systemProperty 'java.rmi.server.hostname', "127.0.0.1"
7884
}
7985
}
86+
}
87+
88+
// Attach code coverage report task to Gradle check task when coverage is enabled
89+
if (System.getProperty("tests.coverage")) {
90+
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME).configure {
91+
dependsOn tasks.named('jacocoTestReport', JacocoReport)
92+
}
8093
}

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def usingMultiNode = project.properties.containsKey('numNodes')
134134
if (!usingRemoteCluster && !usingMultiNode) {
135135
apply from: 'build-tools/coverage.gradle'
136136
}
137-
check.dependsOn jacocoTestReport
138137

139138
opensearchplugin {
140139
name 'opensearch-index-management'

spi/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ jacocoTestReport {
4444
html.destination file("${buildDir}/jacoco/")
4545
}
4646
}
47-
check.dependsOn jacocoTestReport
4847

4948
repositories {
5049
mavenLocal()

0 commit comments

Comments
 (0)