From d242f3f935aa96b6d7d6db46cf0002b7720e13f7 Mon Sep 17 00:00:00 2001 From: Erwin Morrhey Date: Thu, 27 Oct 2022 09:11:45 +0200 Subject: [PATCH 1/6] always check postfix "Contents/Home" on macOS --- dist/setup/index.js | 10 +++++----- src/distributions/local/installer.ts | 14 ++++++-------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 2820ec370..cbbe51427 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -104417,16 +104417,16 @@ class LocalDistribution extends base_installer_1.JavaBase { 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); - } 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 1402bc85b..0b91febf5 100644 --- a/src/distributions/local/installer.ts +++ b/src/distributions/local/installer.ts @@ -46,20 +46,18 @@ export class LocalDistribution extends JavaBase { 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); From 3647a1d3c57a661b65b30237a4a3ab719377a87c Mon Sep 17 00:00:00 2001 From: Erwin Morrhey Date: Sun, 30 Oct 2022 09:02:13 +0100 Subject: [PATCH 2/6] added unit tests --- .../distributors/local-installer.test.ts | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/__tests__/distributors/local-installer.test.ts b/__tests__/distributors/local-installer.test.ts index c1618971b..ac3af1c7c 100644 --- a/__tests__/distributors/local-installer.test.ts +++ b/__tests__/distributors/local-installer.test.ts @@ -189,6 +189,77 @@ 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') + }; + let originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'darwin' + }); + + spyFsStat = jest.spyOn(fs, 'existsSync'); + spyFsStat.mockImplementation((file: string) => { + return file.endsWith('Contents/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') + }; + let originalPlatform = process.platform; + Object.defineProperty(process, 'platform', { + value: 'darwin' + }); + spyFsStat = jest.spyOn(fs, 'existsSync'); + spyFsStat.mockImplementation((file: string) => { + return file.endsWith('Contents/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([ [ { version: '8.0.289', architecture: 'x64', packageType: 'jdk', checkLatest: false }, From 3eb41a67963c073179f9ccc14746d7d77d1fe81c Mon Sep 17 00:00:00 2001 From: Erwin Morrhey Date: Mon, 31 Oct 2022 08:18:07 +0100 Subject: [PATCH 3/6] format code --- .../distributors/local-installer.test.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/__tests__/distributors/local-installer.test.ts b/__tests__/distributors/local-installer.test.ts index ac3af1c7c..79edc4dc6 100644 --- a/__tests__/distributors/local-installer.test.ts +++ b/__tests__/distributors/local-installer.test.ts @@ -202,13 +202,13 @@ describe('setupJava', () => { path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents/Home') }; let originalPlatform = process.platform; - Object.defineProperty(process, 'platform', { + Object.defineProperty(process, 'platform', { value: 'darwin' - }); + }); spyFsStat = jest.spyOn(fs, 'existsSync'); spyFsStat.mockImplementation((file: string) => { - return file.endsWith('Contents/Home'); + return file.endsWith('Contents/Home'); }); mockJavaBase = new LocalDistribution(inputs, jdkFile); @@ -219,9 +219,9 @@ describe('setupJava', () => { `Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...` ); - Object.defineProperty(process, 'platform', { + Object.defineProperty(process, 'platform', { value: originalPlatform - }); + }); }); it('java is unpacked from jdkfile including Contents/Home on MacOS', async () => { @@ -237,12 +237,12 @@ describe('setupJava', () => { path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents/Home') }; let originalPlatform = process.platform; - Object.defineProperty(process, 'platform', { + Object.defineProperty(process, 'platform', { value: 'darwin' - }); + }); spyFsStat = jest.spyOn(fs, 'existsSync'); spyFsStat.mockImplementation((file: string) => { - return file.endsWith('Contents/Home'); + return file.endsWith('Contents/Home'); }); mockJavaBase = new LocalDistribution(inputs, jdkFile); @@ -255,9 +255,9 @@ describe('setupJava', () => { expect(spyCoreInfo).toHaveBeenCalledWith( `Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...` ); - Object.defineProperty(process, 'platform', { + Object.defineProperty(process, 'platform', { value: originalPlatform - }); + }); }); it.each([ From 10137ee6a897d7611379d1780eca62640a09e253 Mon Sep 17 00:00:00 2001 From: Erwin Morrhey Date: Wed, 2 Nov 2022 16:32:23 +0100 Subject: [PATCH 4/6] fix tests on windows --- __tests__/distributors/local-installer.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/__tests__/distributors/local-installer.test.ts b/__tests__/distributors/local-installer.test.ts index 79edc4dc6..e1a4a4bb7 100644 --- a/__tests__/distributors/local-installer.test.ts +++ b/__tests__/distributors/local-installer.test.ts @@ -199,7 +199,7 @@ describe('setupJava', () => { const jdkFile = 'not_existing_one'; const expected = { version: actualJavaVersion, - path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents/Home') + path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents', 'Home') }; let originalPlatform = process.platform; Object.defineProperty(process, 'platform', { @@ -208,7 +208,7 @@ describe('setupJava', () => { spyFsStat = jest.spyOn(fs, 'existsSync'); spyFsStat.mockImplementation((file: string) => { - return file.endsWith('Contents/Home'); + return file.endsWith('Home'); }); mockJavaBase = new LocalDistribution(inputs, jdkFile); @@ -234,7 +234,7 @@ describe('setupJava', () => { const jdkFile = expectedJdkFile; const expected = { version: '11.0.289', - path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents/Home') + path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents', 'Home') }; let originalPlatform = process.platform; Object.defineProperty(process, 'platform', { @@ -242,7 +242,7 @@ describe('setupJava', () => { }); spyFsStat = jest.spyOn(fs, 'existsSync'); spyFsStat.mockImplementation((file: string) => { - return file.endsWith('Contents/Home'); + return file.endsWith('Home'); }); mockJavaBase = new LocalDistribution(inputs, jdkFile); From 55a453935fe6e3c62a9af5f4ff30ccc4a6de35b8 Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Mon, 3 Apr 2023 16:43:33 +0200 Subject: [PATCH 5/6] Format and Lint code --- .../distributors/local-installer.test.ts | 28 +++++++++++++++---- src/distributions/local/installer.ts | 7 +++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/__tests__/distributors/local-installer.test.ts b/__tests__/distributors/local-installer.test.ts index 825ea983a..8e9d5d437 100644 --- a/__tests__/distributors/local-installer.test.ts +++ b/__tests__/distributors/local-installer.test.ts @@ -224,9 +224,15 @@ describe('setupJava', () => { const jdkFile = 'not_existing_one'; const expected = { version: actualJavaVersion, - path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents', 'Home') + path: path.join( + 'Java_jdkfile_jdk', + inputs.version, + inputs.architecture, + 'Contents', + 'Home' + ) }; - let originalPlatform = process.platform; + const originalPlatform = process.platform; Object.defineProperty(process, 'platform', { value: 'darwin' }); @@ -239,7 +245,9 @@ describe('setupJava', () => { 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).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...` ); @@ -259,9 +267,15 @@ describe('setupJava', () => { const jdkFile = expectedJdkFile; const expected = { version: '11.0.289', - path: path.join('Java_jdkfile_jdk', inputs.version, inputs.architecture, 'Contents', 'Home') + path: path.join( + 'Java_jdkfile_jdk', + inputs.version, + inputs.architecture, + 'Contents', + 'Home' + ) }; - let originalPlatform = process.platform; + const originalPlatform = process.platform; Object.defineProperty(process, 'platform', { value: 'darwin' }); @@ -276,7 +290,9 @@ describe('setupJava', () => { expect(spyCoreInfo).not.toHaveBeenCalledWith( `Resolved Java ${actualJavaVersion} from tool-cache` ); - expect(spyCoreInfo).toHaveBeenCalledWith(`Extracting Java from '${jdkFile}'`); + expect(spyCoreInfo).toHaveBeenCalledWith( + `Extracting Java from '${jdkFile}'` + ); expect(spyCoreInfo).toHaveBeenCalledWith( `Java ${inputs.version} was not found in tool-cache. Trying to unpack JDK file...` ); diff --git a/src/distributions/local/installer.ts b/src/distributions/local/installer.ts index 828260645..adfac4998 100644 --- a/src/distributions/local/installer.ts +++ b/src/distributions/local/installer.ts @@ -47,7 +47,7 @@ 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), @@ -61,7 +61,10 @@ export class LocalDistribution extends JavaBase { } // JDK folder may contain postfix "Contents/Home" on macOS - const macOSPostfixPath = path.join(foundJava.path, MACOS_JAVA_CONTENT_POSTFIX); + const macOSPostfixPath = path.join( + foundJava.path, + MACOS_JAVA_CONTENT_POSTFIX + ); if (process.platform === 'darwin' && fs.existsSync(macOSPostfixPath)) { foundJava.path = macOSPostfixPath; } From 760b6c9aad70e5e3a601600dd961a4f849d5cccd Mon Sep 17 00:00:00 2001 From: IvanZosimov Date: Mon, 3 Apr 2023 17:07:57 +0200 Subject: [PATCH 6/6] Rebuild action --- dist/setup/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 0680d6a01..b9308830c 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -104608,7 +104608,7 @@ 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); + const javaPath = yield tc.cacheDir(archivePath, this.toolcacheFolderName, this.getToolcacheVersionName(javaVersion), this.architecture); foundJava = { version: javaVersion, path: javaPath