From 191ba8c6ba49737c558c820f47161f35b262c87d Mon Sep 17 00:00:00 2001 From: Erwin Morrhey Date: Tue, 4 Apr 2023 11:40:40 +0200 Subject: [PATCH] always check postfix "Contents/Home" on macOS (#397) --- .../distributors/local-installer.test.ts | 87 +++++++++++++++++++ dist/setup/index.js | 12 +-- src/distributions/local/installer.ts | 19 ++-- 3 files changed, 103 insertions(+), 15 deletions(-) diff --git a/__tests__/distributors/local-installer.test.ts b/__tests__/distributors/local-installer.test.ts index 717f97a..8e9d5d4 100644 --- a/__tests__/distributors/local-installer.test.ts +++ b/__tests__/distributors/local-installer.test.ts @@ -214,6 +214,93 @@ describe('setupJava', () => { ); }); + it('java is resolved from toolcache including Contents/Home on MacOS', async () => { + const inputs = { + version: actualJavaVersion, + architecture: 'x86', + packageType: 'jdk', + checkLatest: false + }; + const jdkFile = 'not_existing_one'; + const expected = { + version: actualJavaVersion, + path: path.join( + 'Java_jdkfile_jdk', + inputs.version, + inputs.architecture, + 'Contents', + 'Home' + ) + }; + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'darwin' + }); + + spyFsStat = jest.spyOn(fs, 'existsSync'); + spyFsStat.mockImplementation((file: string) => { + return file.endsWith('Home'); + }); + + mockJavaBase = new LocalDistribution(inputs, jdkFile); + await expect(mockJavaBase.setupJava()).resolves.toEqual(expected); + expect(spyGetToolcachePath).toHaveBeenCalled(); + expect(spyCoreInfo).toHaveBeenCalledWith( + `Resolved Java ${actualJavaVersion} from tool-cache` + ); + expect(spyCoreInfo).not.toHaveBeenCalledWith( + `Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...` + ); + + Object.defineProperty(process, 'platform', { + value: originalPlatform + }); + }); + + it('java is unpacked from jdkfile including Contents/Home on MacOS', async () => { + const inputs = { + version: '11.0.289', + architecture: 'x86', + packageType: 'jdk', + checkLatest: false + }; + const jdkFile = expectedJdkFile; + const expected = { + version: '11.0.289', + path: path.join( + 'Java_jdkfile_jdk', + inputs.version, + inputs.architecture, + 'Contents', + 'Home' + ) + }; + const originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'darwin' + }); + spyFsStat = jest.spyOn(fs, 'existsSync'); + spyFsStat.mockImplementation((file: string) => { + return file.endsWith('Home'); + }); + + mockJavaBase = new LocalDistribution(inputs, jdkFile); + await expect(mockJavaBase.setupJava()).resolves.toEqual(expected); + expect(spyTcFindAllVersions).toHaveBeenCalled(); + expect(spyCoreInfo).not.toHaveBeenCalledWith( + `Resolved Java ${actualJavaVersion} from tool-cache` + ); + expect(spyCoreInfo).toHaveBeenCalledWith( + `Extracting Java from '${jdkFile}'` + ); + expect(spyCoreInfo).toHaveBeenCalledWith( + `Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...` + ); + Object.defineProperty(process, 'platform', { + value: originalPlatform + }); + }); + it.each([ [ { diff --git a/dist/setup/index.js b/dist/setup/index.js index 2d095d4..b930883 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -104608,17 +104608,17 @@ class LocalDistribution extends base_installer_1.JavaBase { const archiveName = fs_1.default.readdirSync(extractedJavaPath)[0]; const archivePath = path_1.default.join(extractedJavaPath, archiveName); const javaVersion = this.version; - let javaPath = yield tc.cacheDir(archivePath, this.toolcacheFolderName, this.getToolcacheVersionName(javaVersion), this.architecture); - // for different Java distributions, postfix can exist or not so need to check both cases - if (process.platform === 'darwin' && - fs_1.default.existsSync(path_1.default.join(javaPath, constants_1.MACOS_JAVA_CONTENT_POSTFIX))) { - javaPath = path_1.default.join(javaPath, constants_1.MACOS_JAVA_CONTENT_POSTFIX); - } + const javaPath = yield tc.cacheDir(archivePath, this.toolcacheFolderName, this.getToolcacheVersionName(javaVersion), this.architecture); foundJava = { version: javaVersion, path: javaPath }; } + // JDK folder may contain postfix "Contents/Home" on macOS + const macOSPostfixPath = path_1.default.join(foundJava.path, constants_1.MACOS_JAVA_CONTENT_POSTFIX); + if (process.platform === 'darwin' && fs_1.default.existsSync(macOSPostfixPath)) { + foundJava.path = macOSPostfixPath; + } core.info(`Setting Java ${foundJava.version} as default`); this.setJavaDefault(foundJava.version, foundJava.path); return foundJava; diff --git a/src/distributions/local/installer.ts b/src/distributions/local/installer.ts index 72c2d27..adfac49 100644 --- a/src/distributions/local/installer.ts +++ b/src/distributions/local/installer.ts @@ -47,27 +47,28 @@ export class LocalDistribution extends JavaBase { const archivePath = path.join(extractedJavaPath, archiveName); const javaVersion = this.version; - let javaPath = await tc.cacheDir( + const javaPath = await tc.cacheDir( archivePath, this.toolcacheFolderName, this.getToolcacheVersionName(javaVersion), this.architecture ); - // for different Java distributions, postfix can exist or not so need to check both cases - if ( - process.platform === 'darwin' && - fs.existsSync(path.join(javaPath, MACOS_JAVA_CONTENT_POSTFIX)) - ) { - javaPath = path.join(javaPath, MACOS_JAVA_CONTENT_POSTFIX); - } - foundJava = { version: javaVersion, path: javaPath }; } + // JDK folder may contain postfix "Contents/Home" on macOS + const macOSPostfixPath = path.join( + foundJava.path, + MACOS_JAVA_CONTENT_POSTFIX + ); + if (process.platform === 'darwin' && fs.existsSync(macOSPostfixPath)) { + foundJava.path = macOSPostfixPath; + } + core.info(`Setting Java ${foundJava.version} as default`); this.setJavaDefault(foundJava.version, foundJava.path);