Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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