Rework output

Always display all known and unknown found wrapper jars
alongside their checksum.

The display string building was pushed down from the Github Action main
function, so it's easier to reuse and test it.

Signed-off-by: Paul Merlin <paul@gradle.com>
This commit is contained in:
Paul Merlin 2020-01-11 15:35:09 +01:00
parent 4432e91432
commit 33646cf935
3 changed files with 112 additions and 33 deletions

View file

@ -1,26 +1,64 @@
import * as path from 'path'
import * as validate from '../src/validate'
test('validates wrapper jars', async () => {
const invalidWrapperJars = await validate.findInvalidWrapperJars(
path.resolve('.'),
2,
false,
[]
const baseDir = path.resolve('.')
test('succeeds if all found wrapper jars are valid', async () => {
const result = await validate.findInvalidWrapperJars(baseDir, 2, false, [
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
])
expect(result.isValid()).toBe(true)
expect(result.toDisplayString()).toBe(
'✓ Found known Gradle Wrapper JAR files\n' +
' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 __tests__/data/invalid/gradle-wrapper.jar\n' +
' 3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce __tests__/data/valid/gradle-wrapper.jar'
)
expect(invalidWrapperJars.length).toBe(1)
expect(invalidWrapperJars[0]).toEqual(
new validate.InvalidWrapperJar(
})
test('fails if invalid wrapper jars are found', async () => {
const result = await validate.findInvalidWrapperJars(baseDir, 2, false, [])
expect(result.isValid()).toBe(false)
expect(result.valid).toEqual([
new validate.WrapperJar(
'__tests__/data/valid/gradle-wrapper.jar',
'3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce'
)
])
expect(result.invalid).toEqual([
new validate.WrapperJar(
'__tests__/data/invalid/gradle-wrapper.jar',
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
)
])
expect(result.toDisplayString()).toBe(
'✗ Found unknown Gradle Wrapper JAR files\n' +
' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 __tests__/data/invalid/gradle-wrapper.jar\n' +
'✓ Found known Gradle Wrapper JAR files\n' +
' 3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce __tests__/data/valid/gradle-wrapper.jar'
)
})
test('fails if not enough wrapper jars are found', async () => {
await expect(
validate.findInvalidWrapperJars(path.resolve('.'), 3, false, [])
).rejects.toThrowError(
const result = await validate.findInvalidWrapperJars(baseDir, 3, false, [])
expect(result.isValid()).toBe(false)
expect(result.errors).toEqual([
'Expected to find at least 3 Gradle Wrapper JARs but got only 2'
])
expect(result.toDisplayString()).toBe(
'✗ Found unknown Gradle Wrapper JAR files\n' +
' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 __tests__/data/invalid/gradle-wrapper.jar\n' +
'✗ Other validation errors\n' +
' Expected to find at least 3 Gradle Wrapper JARs but got only 2\n' +
'✓ Found known Gradle Wrapper JAR files\n' +
' 3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce __tests__/data/valid/gradle-wrapper.jar'
)
})

View file

@ -5,22 +5,16 @@ import * as validate from './validate'
export async function run(): Promise<void> {
try {
const minWrapperCount = +core.getInput('min-wrapper-count')
const allowSnapshots = core.getInput('allow-snapshots') === 'true'
const allowChecksums = core.getInput('allow-checksums').split(',')
const invalidWrapperJars = await validate.findInvalidWrapperJars(
const result = await validate.findInvalidWrapperJars(
path.resolve('.'),
minWrapperCount,
allowSnapshots,
allowChecksums
+core.getInput('min-wrapper-count'),
core.getInput('allow-snapshots') === 'true',
core.getInput('allow-checksums').split(',')
)
if (invalidWrapperJars.length > 0) {
const list = invalidWrapperJars.map(
invalid => `${invalid.checksum} ${invalid.path}`
)
core.setFailed(
`Found unknown Gradle Wrapper JAR files\n${list.join('\n- ')}`
)
if (result.isValid()) {
core.info(result.toDisplayString())
} else {
core.setFailed(result.toDisplayString())
}
} catch (error) {
core.setFailed(error.message)

View file

@ -7,33 +7,80 @@ export async function findInvalidWrapperJars(
minWrapperCount: number,
allowSnapshots: boolean,
allowChecksums: string[]
): Promise<InvalidWrapperJar[]> {
): Promise<ValidationResult> {
const wrapperJars = await find.findWrapperJars(gitRepoRoot)
const result = new ValidationResult([], [])
if (wrapperJars.length < minWrapperCount) {
throw new Error(
result.errors.push(
`Expected to find at least ${minWrapperCount} Gradle Wrapper JARs but got only ${wrapperJars.length}`
)
}
if (wrapperJars.length > 0) {
const validChecksums = await checksums.fetchValidChecksums(allowSnapshots)
validChecksums.push(...allowChecksums)
const invalidWrapperJars: InvalidWrapperJar[] = []
for (const wrapperJar of wrapperJars) {
const sha = await hash.sha256File(wrapperJar)
if (!validChecksums.includes(sha)) {
invalidWrapperJars.push(new InvalidWrapperJar(wrapperJar, sha))
result.invalid.push(new WrapperJar(wrapperJar, sha))
} else {
result.valid.push(new WrapperJar(wrapperJar, sha))
}
}
return invalidWrapperJars
}
return []
return result
}
export class InvalidWrapperJar {
export class ValidationResult {
valid: WrapperJar[]
invalid: WrapperJar[]
errors: string[] = []
constructor(valid: WrapperJar[], invalid: WrapperJar[]) {
this.valid = valid
this.invalid = invalid
}
isValid(): boolean {
return this.invalid.length === 0 && this.errors.length === 0
}
toDisplayString(): string {
let displayString = ''
if (this.invalid.length > 0) {
displayString += `✗ Found unknown Gradle Wrapper JAR files\n${ValidationResult.toDisplayList(
this.invalid
)}`
}
if (this.errors.length > 0) {
if (displayString.length > 0) displayString += '\n'
displayString += `✗ Other validation errors\n ${this.errors.join(
`\n `
)}`
}
if (this.valid.length > 0) {
if (displayString.length > 0) displayString += '\n'
displayString += `✓ Found known Gradle Wrapper JAR files\n${ValidationResult.toDisplayList(
this.valid
)}`
}
return displayString
}
private static toDisplayList(wrapperJars: WrapperJar[]): string {
return ` ${wrapperJars.map(wj => wj.toDisplayString()).join(`\n `)}`
}
}
export class WrapperJar {
path: string
checksum: string
constructor(path: string, checksum: string) {
this.path = path
this.checksum = checksum
}
toDisplayString(): string {
return `${this.checksum} ${this.path}`
}
}