Skip to content

Commit 0719002

Browse files
authored
Improve error messages for min-wrapper-count (#321)
2 parents 833b05f + ac3aebd commit 0719002

File tree

5 files changed

+76
-41
lines changed

5 files changed

+76
-41
lines changed

.github/workflows/integ-test-wrapper-validation.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
with:
5555
# to allow the invalid wrapper jar present in test data
5656
allow-checksums: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
57+
min-wrapper-count: 10
5758

5859
- name: Check outcome
5960
env:
@@ -98,3 +99,62 @@ jobs:
9899
echo "'outputs.failed-wrapper' has unexpected content: $FAILED_WRAPPERS"
99100
exit 1
100101
fi
102+
103+
test-validation-minimum-wrapper-count:
104+
runs-on: ubuntu-latest
105+
steps:
106+
- name: Checkout sources
107+
uses: actions/checkout@v4
108+
- name: Initialize integ-test
109+
uses: ./.github/actions/init-integ-test
110+
111+
- name: Run wrapper-validation-action
112+
id: action-test
113+
uses: ./wrapper-validation
114+
with:
115+
# to allow the invalid wrapper jar present in test data
116+
allow-checksums: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
117+
min-wrapper-count: 11
118+
# Expected to fail; validated below
119+
continue-on-error: true
120+
121+
- name: Check outcome
122+
env:
123+
# Evaluate workflow expressions here as env variable values instead of inside shell script
124+
# below to not accidentally inject code into shell script or break its syntax
125+
VALIDATION_FAILED: ${{ steps.action-test.outcome == 'failure' }}
126+
run: |
127+
if [ "$VALIDATION_FAILED" != "true" ] ; then
128+
echo "Expected validation to fail, but it didn't"
129+
exit 1
130+
fi
131+
132+
test-validation-zero-wrappers:
133+
runs-on: ubuntu-latest
134+
steps:
135+
- name: Checkout sources
136+
uses: actions/checkout@v4 # Checkout the repository with no wrappers
137+
with:
138+
sparse-checkout: |
139+
.github/actions
140+
dist
141+
wrapper-validation
142+
- name: Initialize integ-test
143+
uses: ./.github/actions/init-integ-test
144+
145+
- name: Run wrapper-validation-action
146+
id: action-test
147+
uses: ./wrapper-validation
148+
# Expected to fail; validated below
149+
continue-on-error: true
150+
151+
- name: Check outcome
152+
env:
153+
# Evaluate workflow expressions here as env variable values instead of inside shell script
154+
# below to not accidentally inject code into shell script or break its syntax
155+
VALIDATION_FAILED: ${{ steps.action-test.outcome == 'failure' }}
156+
run: |
157+
if [ "$VALIDATION_FAILED" != "true" ] ; then
158+
echo "Expected validation to fail, but it didn't"
159+
exit 1
160+
fi

sources/src/actions/wrapper-validation/main.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@ export async function run(): Promise<void> {
1818

1919
const result = await validate.findInvalidWrapperJars(
2020
path.resolve('.'),
21-
+core.getInput('min-wrapper-count'),
2221
core.getInput('allow-snapshots') === 'true',
2322
core.getInput('allow-checksums').split(',')
2423
)
2524
if (result.isValid()) {
2625
core.info(result.toDisplayString())
26+
27+
const minWrapperCount = +core.getInput('min-wrapper-count')
28+
if (result.valid.length < minWrapperCount) {
29+
const message =
30+
result.valid.length === 0
31+
? 'Wrapper validation failed: no Gradle Wrapper jars found. Did you forget to checkout the repository?'
32+
: `Wrapper validation failed: expected at least ${minWrapperCount} Gradle Wrapper jars, but found ${result.valid.length}.`
33+
core.setFailed(message)
34+
}
2735
} else {
2836
core.setFailed(
29-
`Gradle Wrapper Validation Failed!\n See https://github.com/gradle/actions/blob/main/docs/wrapper-validation.md#reporting-failures\n${result.toDisplayString()}`
37+
`At least one Gradle Wrapper Jar failed validation!\n See https://github.com/gradle/actions/blob/main/docs/wrapper-validation.md#validation-failures\n${result.toDisplayString()}`
3038
)
3139
if (result.invalid.length > 0) {
3240
core.setOutput('failed-wrapper', `${result.invalid.map(w => w.path).join('|')}`)

sources/src/wrapper-validation/validate.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ import {resolve} from 'path'
55

66
export async function findInvalidWrapperJars(
77
gitRepoRoot: string,
8-
minWrapperCount: number,
98
allowSnapshots: boolean,
109
allowedChecksums: string[],
1110
previouslyValidatedChecksums: string[] = [],
1211
knownValidChecksums: checksums.WrapperChecksums = checksums.KNOWN_CHECKSUMS
1312
): Promise<ValidationResult> {
1413
const wrapperJars = await find.findWrapperJars(gitRepoRoot)
1514
const result = new ValidationResult([], [])
16-
if (wrapperJars.length < minWrapperCount) {
17-
result.errors.push(
18-
`Expected to find at least ${minWrapperCount} Gradle Wrapper JARs but got only ${wrapperJars.length}`
19-
)
20-
}
2115
if (wrapperJars.length > 0) {
2216
const notYetValidatedWrappers = []
2317
for (const wrapperJar of wrapperJars) {
@@ -54,15 +48,14 @@ export class ValidationResult {
5448
valid: WrapperJar[]
5549
invalid: WrapperJar[]
5650
fetchedChecksums = false
57-
errors: string[] = []
5851

5952
constructor(valid: WrapperJar[], invalid: WrapperJar[]) {
6053
this.valid = valid
6154
this.invalid = invalid
6255
}
6356

6457
isValid(): boolean {
65-
return this.invalid.length === 0 && this.errors.length === 0
58+
return this.invalid.length === 0
6659
}
6760

6861
toDisplayString(): string {
@@ -72,10 +65,6 @@ export class ValidationResult {
7265
this.invalid
7366
)}`
7467
}
75-
if (this.errors.length > 0) {
76-
if (displayString.length > 0) displayString += '\n'
77-
displayString += `✗ Other validation errors:\n ${this.errors.join(`\n `)}`
78-
}
7968
if (this.valid.length > 0) {
8069
if (displayString.length > 0) displayString += '\n'
8170
displayString += `✓ Found known Gradle Wrapper JAR files:\n${ValidationResult.toDisplayList(this.valid)}`

sources/src/wrapper-validation/wrapper-validator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ export async function validateWrappers(
2020

2121
const result = await findInvalidWrapperJars(
2222
workspaceRoot,
23-
0,
2423
config.allowSnapshotWrappers(),
2524
allowedChecksums,
2625
previouslyValidatedChecksums
2726
)
2827
if (result.isValid()) {
2928
await core.group('All Gradle Wrapper jars are valid', async () => {
30-
core.info(`Loaded previously validated checksums from cache: ${previouslyValidatedChecksums.join(', ')}`)
29+
core.debug(`Loaded previously validated checksums from cache: ${previouslyValidatedChecksums.join(', ')}`)
3130
core.info(result.toDisplayString())
3231
})
3332
} else {
3433
core.info(result.toDisplayString())
3534
throw new JobFailure(
36-
`Gradle Wrapper Validation Failed!\n See https://github.com/gradle/actions/blob/main/docs/wrapper-validation.md#validation-failures\n${result.toDisplayString()}`
35+
`At least one Gradle Wrapper Jar failed validation!\n See https://github.com/gradle/actions/blob/main/docs/wrapper-validation.md#validation-failures\n${result.toDisplayString()}`
3736
)
3837
}
3938

sources/test/jest/wrapper-validation/validate.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const baseDir = path.resolve('./test/jest/wrapper-validation')
1212
const tmpDir = path.resolve('./test/jest/tmp')
1313

1414
test('succeeds if all found wrapper jars are valid', async () => {
15-
const result = await validate.findInvalidWrapperJars(baseDir, 3, false, [
15+
const result = await validate.findInvalidWrapperJars(baseDir, false, [
1616
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
1717
])
1818

@@ -29,7 +29,7 @@ test('succeeds if all found wrapper jars are valid', async () => {
2929
})
3030

3131
test('succeeds if all found wrapper jars are previously valid', async () => {
32-
const result = await validate.findInvalidWrapperJars(baseDir, 3, false, [], [
32+
const result = await validate.findInvalidWrapperJars(baseDir, false, [], [
3333
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
3434
'3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce'
3535
])
@@ -50,7 +50,6 @@ test('succeeds if all found wrapper jars are valid (and checksums are fetched fr
5050
const knownValidChecksums = new WrapperChecksums()
5151
const result = await validate.findInvalidWrapperJars(
5252
baseDir,
53-
1,
5453
false,
5554
['e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'],
5655
[],
@@ -71,7 +70,7 @@ test('succeeds if all found wrapper jars are valid (and checksums are fetched fr
7170
})
7271

7372
test('fails if invalid wrapper jars are found', async () => {
74-
const result = await validate.findInvalidWrapperJars(baseDir, 3, false, [])
73+
const result = await validate.findInvalidWrapperJars(baseDir, false, [])
7574

7675
expect(result.isValid()).toBe(false)
7776

@@ -102,26 +101,6 @@ test('fails if invalid wrapper jars are found', async () => {
102101
)
103102
})
104103

105-
test('fails if not enough wrapper jars are found', async () => {
106-
const result = await validate.findInvalidWrapperJars(baseDir, 4, false, [])
107-
108-
expect(result.isValid()).toBe(false)
109-
110-
expect(result.errors).toEqual([
111-
'Expected to find at least 4 Gradle Wrapper JARs but got only 3'
112-
])
113-
114-
expect(result.toDisplayString()).toBe(
115-
'✗ Found unknown Gradle Wrapper JAR files:\n' +
116-
' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 data/invalid/gradle-wrapper.jar\n' +
117-
' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 data/invalid/gradlе-wrapper.jar\n' + // homoglyph
118-
'✗ Other validation errors:\n' +
119-
' Expected to find at least 4 Gradle Wrapper JARs but got only 3\n' +
120-
'✓ Found known Gradle Wrapper JAR files:\n' +
121-
' 3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce data/valid/gradle-wrapper.jar'
122-
)
123-
})
124-
125104
test('can save and load checksums', async () => {
126105
const cacheDir = path.join(tmpDir, 'wrapper-validation-cache')
127106
fs.rmSync(cacheDir, {recursive: true, force: true})

0 commit comments

Comments
 (0)