From 3b03a0a9d7f0d3071b728c9f36bd9117551269e0 Mon Sep 17 00:00:00 2001 From: Mike Penz Date: Thu, 24 Aug 2023 09:04:32 +0000 Subject: [PATCH] - update and introduce testcases for the new configuration option --- __tests__/checksums.test.ts | 15 ++++- .../data/invalid/gradle-wrapper.properties | 7 ++ .../data/valid/gradle-wrapper.properties | 5 ++ __tests__/find.test.ts | 10 +++ __tests__/validate.test.ts | 64 +++++++++++++++++-- 5 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 __tests__/data/invalid/gradle-wrapper.properties create mode 100644 __tests__/data/valid/gradle-wrapper.properties diff --git a/__tests__/checksums.test.ts b/__tests__/checksums.test.ts index bda8d17f..5ab71a90 100644 --- a/__tests__/checksums.test.ts +++ b/__tests__/checksums.test.ts @@ -5,10 +5,17 @@ import {afterEach, describe, expect, test, jest} from '@jest/globals' jest.setTimeout(30000) test('fetches wrapper jars checksums', async () => { - const validChecksums = await checksums.fetchValidChecksums(false) + const validChecksums = await checksums.fetchValidChecksums(false, false, []) expect(validChecksums.length).toBeGreaterThan(10) }) +test('fetches wrapper jars checksums only for detected versions', async () => { + const validChecksums = await checksums.fetchValidChecksums(false, true, [ + '8.2.1' + ]) + expect(validChecksums.length).toBe(1) +}) + describe('retry', () => { afterEach(() => { nock.cleanAll() @@ -24,7 +31,11 @@ describe('retry', () => { code: 'ECONNREFUSED' }) - const validChecksums = await checksums.fetchValidChecksums(false) + const validChecksums = await checksums.fetchValidChecksums( + false, + false, + [] + ) expect(validChecksums.length).toBeGreaterThan(10) nock.isDone() }) diff --git a/__tests__/data/invalid/gradle-wrapper.properties b/__tests__/data/invalid/gradle-wrapper.properties new file mode 100644 index 00000000..5af612c1 --- /dev/null +++ b/__tests__/data/invalid/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle8.2.1bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/__tests__/data/valid/gradle-wrapper.properties b/__tests__/data/valid/gradle-wrapper.properties new file mode 100644 index 00000000..2d79b772 --- /dev/null +++ b/__tests__/data/valid/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-milestone-3-bin.zip +networkTimeout=10000 +validateDistributionUrl=true \ No newline at end of file diff --git a/__tests__/find.test.ts b/__tests__/find.test.ts index 2667fed1..27bf7afd 100644 --- a/__tests__/find.test.ts +++ b/__tests__/find.test.ts @@ -10,3 +10,13 @@ test('finds test data wrapper jars', async () => { expect(wrapperJars).toContain('__tests__/data/invalid/gradle-wrapper.jar') expect(wrapperJars).toContain('__tests__/data/invalid/gradlе-wrapper.jar') // homoglyph }) + +test('detect version from `gradle-wrapper.properties` alongside wrappers', async () => { + const repoRoot = path.resolve('.') + const wrapperJars = await find.findWrapperJars(repoRoot) + + const detectedVersions = await find.detectVersions(wrapperJars) + + expect(detectedVersions.length).toBe(1) + expect(detectedVersions).toContain('6.1-milestone-3') +}) diff --git a/__tests__/validate.test.ts b/__tests__/validate.test.ts index 4abc79d2..41665290 100644 --- a/__tests__/validate.test.ts +++ b/__tests__/validate.test.ts @@ -7,9 +7,13 @@ jest.setTimeout(30000) const baseDir = path.resolve('.') test('succeeds if all found wrapper jars are valid', async () => { - const result = await validate.findInvalidWrapperJars(baseDir, 3, false, [ - 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' - ]) + const result = await validate.findInvalidWrapperJars( + baseDir, + 3, + false, + ['e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'], + false + ) expect(result.isValid()).toBe(true) @@ -22,7 +26,51 @@ test('succeeds if all found wrapper jars are valid', async () => { }) test('fails if invalid wrapper jars are found', async () => { - const result = await validate.findInvalidWrapperJars(baseDir, 3, false, []) + const result = await validate.findInvalidWrapperJars( + baseDir, + 3, + false, + [], + 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' + ), + new validate.WrapperJar( + '__tests__/data/invalid/gradlе-wrapper.jar', // homoglyph + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' + ) + ]) + + expect(result.toDisplayString()).toBe( + '✗ Found unknown Gradle Wrapper JAR files:\n' + + ' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 __tests__/data/invalid/gradle-wrapper.jar\n' + + ' e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 __tests__/data/invalid/gradlе-wrapper.jar\n' + // homoglyph + '✓ Found known Gradle Wrapper JAR files:\n' + + ' 3888c76faa032ea8394b8a54e04ce2227ab1f4be64f65d450f8509fe112d38ce __tests__/data/valid/gradle-wrapper.jar' + ) +}) + +test('fails if invalid wrapper jars are found when detection versions from `gradle-wrapper.properties`', async () => { + const result = await validate.findInvalidWrapperJars( + baseDir, + 3, + false, + [], + true + ) expect(result.isValid()).toBe(false) @@ -54,7 +102,13 @@ test('fails if invalid wrapper jars are found', async () => { }) test('fails if not enough wrapper jars are found', async () => { - const result = await validate.findInvalidWrapperJars(baseDir, 4, false, []) + const result = await validate.findInvalidWrapperJars( + baseDir, + 4, + false, + [], + false + ) expect(result.isValid()).toBe(false)