always check postfix "Contents/Home" on macOS (#397)

This commit is contained in:
Erwin Morrhey 2023-04-04 11:40:40 +02:00 committed by GitHub
parent e42168ca1a
commit 191ba8c6ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 103 additions and 15 deletions

View file

@ -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([ it.each([
[ [
{ {

12
dist/setup/index.js vendored
View file

@ -104608,17 +104608,17 @@ class LocalDistribution extends base_installer_1.JavaBase {
const archiveName = fs_1.default.readdirSync(extractedJavaPath)[0]; const archiveName = fs_1.default.readdirSync(extractedJavaPath)[0];
const archivePath = path_1.default.join(extractedJavaPath, archiveName); const archivePath = path_1.default.join(extractedJavaPath, archiveName);
const javaVersion = this.version; 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);
// 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 = { foundJava = {
version: javaVersion, version: javaVersion,
path: javaPath 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`); core.info(`Setting Java ${foundJava.version} as default`);
this.setJavaDefault(foundJava.version, foundJava.path); this.setJavaDefault(foundJava.version, foundJava.path);
return foundJava; return foundJava;

View file

@ -47,27 +47,28 @@ export class LocalDistribution extends JavaBase {
const archivePath = path.join(extractedJavaPath, archiveName); const archivePath = path.join(extractedJavaPath, archiveName);
const javaVersion = this.version; const javaVersion = this.version;
let javaPath = await tc.cacheDir( const javaPath = await tc.cacheDir(
archivePath, archivePath,
this.toolcacheFolderName, this.toolcacheFolderName,
this.getToolcacheVersionName(javaVersion), this.getToolcacheVersionName(javaVersion),
this.architecture 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 = { foundJava = {
version: javaVersion, version: javaVersion,
path: javaPath 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`); core.info(`Setting Java ${foundJava.version} as default`);
this.setJavaDefault(foundJava.version, foundJava.path); this.setJavaDefault(foundJava.version, foundJava.path);