diff --git a/src/java.ts b/src/java.ts index b2e544d..7db691c 100644 --- a/src/java.ts +++ b/src/java.ts @@ -38,6 +38,7 @@ import { download, fetch } from './request'; import { AnalysisJreMetaData, AnalysisJresResponseType, + CacheStatus, ScannerProperties, ScannerProperty, } from './types'; @@ -113,7 +114,9 @@ export async function fetchJRE(properties: ScannerProperties): Promise { checksum: jreMetaData.sha256, filename: jreMetaData.filename + UNARCHIVE_SUFFIX, }); - properties[ScannerProperty.SonarScannerWasJreCacheHit] = Boolean(cachedJrePath).toString(); + properties[ScannerProperty.SonarScannerWasJreCacheHit] = cachedJrePath + ? CacheStatus.Hit + : CacheStatus.Miss; if (cachedJrePath) { log(LogLevel.INFO, 'Using Cached JRE'); return path.join(cachedJrePath, jreMetaData.javaPath); diff --git a/src/properties.ts b/src/properties.ts index b9bd724..bbfe697 100644 --- a/src/properties.ts +++ b/src/properties.ts @@ -34,9 +34,9 @@ import { SONAR_DIR_DEFAULT, SONAR_PROJECT_FILENAME, } from './constants'; -import { log, LogLevel } from './logging'; +import { LogLevel, log } from './logging'; import { getArch, getSupportedOS } from './platform'; -import { CliArgs, ScannerProperties, ScannerProperty, ScanOptions } from './types'; +import { CacheStatus, CliArgs, ScanOptions, ScannerProperties, ScannerProperty } from './types'; function getDefaultProperties(): ScannerProperties { return { @@ -307,8 +307,9 @@ function getBootstrapperProperties(startTimestampMs: number): ScannerProperties 'sonar.scanner.app': SCANNER_BOOTSTRAPPER_NAME, 'sonar.scanner.appVersion': version, 'sonar.scanner.bootstrapStartTime': startTimestampMs.toString(), - // Bootstrap cache hit/miss is set later after the bootstrapper has run and before scanner engine is started - 'sonar.scanner.wasJreCacheHit': 'false', + // These cache statuses are set during the bootstrapping process. + // We already set them here to prevent them from being overridden. + 'sonar.scanner.wasJreCacheHit': CacheStatus.Disabled, 'sonar.scanner.wasEngineCacheHit': 'false', }; } diff --git a/src/types.ts b/src/types.ts index 1041e24..df86e93 100644 --- a/src/types.ts +++ b/src/types.ts @@ -98,3 +98,9 @@ export type AnalysisEngineResponseType = { sha256: string; downloadUrl?: string; }; + +export enum CacheStatus { + Hit = 'hit', + Miss = 'miss', + Disabled = 'disabled', +} diff --git a/test/unit/java.test.ts b/test/unit/java.test.ts index 0df1191..72a1f5c 100644 --- a/test/unit/java.test.ts +++ b/test/unit/java.test.ts @@ -26,7 +26,12 @@ import { API_V2_JRE_ENDPOINT, SONARQUBE_JRE_PROVISIONING_MIN_VERSION } from '../ import * as file from '../../src/file'; import { fetchJRE, fetchServerVersion, serverSupportsJREProvisioning } from '../../src/java'; import * as request from '../../src/request'; -import { AnalysisJresResponseType, ScannerProperties, ScannerProperty } from '../../src/types'; +import { + AnalysisJresResponseType, + CacheStatus, + ScannerProperties, + ScannerProperty, +} from '../../src/types'; const mock = new MockAdapter(axios); @@ -142,15 +147,14 @@ describe('java', () => { describe('when the JRE is cached', () => { it('should fetch the latest supported JRE and use the cached version', async () => { - await fetchJRE(MOCKED_PROPERTIES); + const properties = { ...MOCKED_PROPERTIES }; + await fetchJRE(properties); expect(request.fetch).toHaveBeenCalledTimes(1); expect(request.download).not.toHaveBeenCalled(); - - // Check for the cache expect(file.getCacheFileLocation).toHaveBeenCalledTimes(1); - expect(file.extractArchive).not.toHaveBeenCalled(); + expect(properties[ScannerProperty.SonarScannerWasJreCacheHit]).toBe(CacheStatus.Hit); }); }); @@ -167,7 +171,8 @@ describe('java', () => { }); it('should download the JRE', async () => { - await fetchJRE({ ...MOCKED_PROPERTIES }); + const properties = { ...MOCKED_PROPERTIES }; + await fetchJRE(properties); expect(request.fetch).toHaveBeenCalledWith({ url: API_V2_JRE_ENDPOINT, @@ -176,17 +181,14 @@ describe('java', () => { arch: MOCKED_PROPERTIES[ScannerProperty.SonarScannerArch], }, }); - expect(file.getCacheFileLocation).toHaveBeenCalledTimes(1); - expect(request.download).toHaveBeenCalledWith( `${API_V2_JRE_ENDPOINT}/${serverResponse[0].id}`, mockCacheDirectories.archivePath, ); - expect(file.validateChecksum).toHaveBeenCalledTimes(1); - expect(file.extractArchive).toHaveBeenCalledTimes(1); + expect(properties[ScannerProperty.SonarScannerWasJreCacheHit]).toBe(CacheStatus.Miss); }); it('should fail if no JRE matches', async () => { @@ -200,7 +202,7 @@ describe('java', () => { .reply(200, []); // Check that it rejects with a specific error - expect(fetchJRE({ ...MOCKED_PROPERTIES })).rejects.toThrowError( + expect(fetchJRE({ ...MOCKED_PROPERTIES })).rejects.toThrow( 'No JREs available for your platform linux arm64', ); }); diff --git a/test/unit/mocks/FakeProjectMock.ts b/test/unit/mocks/FakeProjectMock.ts index 4b938a5..a6d919f 100644 --- a/test/unit/mocks/FakeProjectMock.ts +++ b/test/unit/mocks/FakeProjectMock.ts @@ -20,6 +20,7 @@ import path from 'path'; import sinon from 'sinon'; import { SCANNER_BOOTSTRAPPER_NAME } from '../../../src/constants'; +import { CacheStatus } from '../../../src/types'; const baseEnvVariables = process.env; @@ -59,7 +60,7 @@ export class FakeProjectMock { 'sonar.scanner.app': SCANNER_BOOTSTRAPPER_NAME, 'sonar.scanner.appVersion': '1.2.3', 'sonar.scanner.wasEngineCacheHit': 'false', - 'sonar.scanner.wasJreCacheHit': 'false', + 'sonar.scanner.wasJreCacheHit': CacheStatus.Disabled, 'sonar.userHome': expect.stringMatching(/\.sonar$/), 'sonar.scanner.os': 'windows', 'sonar.scanner.arch': 'aarch64', diff --git a/test/unit/properties.test.ts b/test/unit/properties.test.ts index 21f757f..898e236 100644 --- a/test/unit/properties.test.ts +++ b/test/unit/properties.test.ts @@ -26,7 +26,7 @@ import { } from '../../src/constants'; import { LogLevel, log } from '../../src/logging'; import { getHostProperties, getProperties } from '../../src/properties'; -import { ScannerProperty } from '../../src/types'; +import { CacheStatus, ScannerProperty } from '../../src/types'; import { FakeProjectMock } from './mocks/FakeProjectMock'; jest.mock('../../src/logging'); @@ -615,7 +615,7 @@ describe('getProperties', () => { 'sonar.scanner.app': 'ignored', 'sonar.scanner.appVersion': 'ignored', 'sonar.scanner.bootstrapStartTime': '0000', - 'sonar.scanner.wasJreCacheHit': 'true', + 'sonar.scanner.wasJreCacheHit': CacheStatus.Hit, 'sonar.scanner.wasEngineCacheHit': 'true', }), }); @@ -627,7 +627,7 @@ describe('getProperties', () => { 'sonar.scanner.app': 'ignored', 'sonar.scanner.appVersion': 'ignored', 'sonar.scanner.bootstrapStartTime': '0000', - 'sonar.scanner.wasJreCacheHit': 'true', + 'sonar.scanner.wasJreCacheHit': CacheStatus.Hit, 'sonar.scanner.wasEngineCacheHit': 'true', }, },