Skip to content

Commit

Permalink
always check postfix "Contents/Home" on macOS (actions#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
erwin1 authored and fniephaus committed Jun 20, 2023
1 parent e8e2eca commit 4ba24dd
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 15 deletions.
87 changes: 87 additions & 0 deletions __tests__/distributors/local-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
[
{
Expand Down
12 changes: 6 additions & 6 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 10 additions & 9 deletions src/distributions/local/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4ba24dd

Please sign in to comment.