Skip to content

Commit

Permalink
Avoid "+" sign in Java path in v2-preview (#145)
Browse files Browse the repository at this point in the history
* try to handle _ versions

* more logs

* more debug

* test 1

* more fixes

* fix typo

* Update e2e-versions.yml

* add unit-tests

* remove debug info from tests

* debug pre-cached versions

* change e2e tests to ubuntu-latest
  • Loading branch information
maxim-lobanov authored Mar 27, 2021
1 parent 022e86d commit 46d5f06
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 50 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/e2e-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,7 @@ jobs:
with:
distribution: ${{ matrix.distribution }}
java-version: ${{ matrix.version }}
architecture: x86
architecture: x86
- name: Verify Java
run: bash __tests__/verify-java.sh "${{ matrix.version }}" "${{ steps.setup-java.outputs.path }}"
shell: bash
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ The `java-version` input supports an exact version or a version range using [Sem
- more specific versions: `11.0`, `11.0.4`, `8.0.232`, `8.0.282+8`
- early access (EA) versions: `15-ea`, `15.0.0-ea`, `15.0.0-ea.2`, `15.0.0+2-ea`

**Note:** 4-digit notation will always force action to skip checking pre-cached versions and download version in runtime.

#### Supported distributions
Currently, the following distributions are supported:
| Keyword | Distribution | Official site | License |
Expand Down
57 changes: 44 additions & 13 deletions __tests__/distributors/base-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,27 @@ describe('findInToolcache', () => {
});

it.each([
['11', '11.0.3'],
['11.0', '11.0.3'],
['11.0.1', '11.0.1'],
['11.0.3', '11.0.3'],
['15', '15.0.2'],
['x', '15.0.2'],
['x-ea', '17.4.4-ea'],
['11-ea', '11.3.2-ea'],
['11.2-ea', '11.2.1-ea'],
['11.2.1-ea', '11.2.1-ea']
['11', { version: '11.0.3+2', versionInPath: '11.0.3-2' }],
['11.0', { version: '11.0.3+2', versionInPath: '11.0.3-2' }],
['11.0.1', { version: '11.0.1', versionInPath: '11.0.1' }],
['11.0.3', { version: '11.0.3+2', versionInPath: '11.0.3-2' }],
['15', { version: '15.0.2+4', versionInPath: '15.0.2-4' }],
['x', { version: '15.0.2+4', versionInPath: '15.0.2-4' }],
['x-ea', { version: '17.4.4', versionInPath: '17.4.4-ea' }],
['11-ea', { version: '11.3.3+5.2.1231421', versionInPath: '11.3.3-ea.5.2.1231421' }],
['11.2-ea', { version: '11.2.1', versionInPath: '11.2.1-ea' }],
['11.2.1-ea', { version: '11.2.1', versionInPath: '11.2.1-ea' }]
])('should choose correct java from tool-cache for input %s', (input, expected) => {
spyTcFindAllVersions.mockReturnValue([
'17.4.4-ea',
'11.0.2',
'15.0.2',
'11.0.3',
'15.0.2-4',
'11.0.3-2',
'11.2.1-ea',
'11.3.2-ea',
'11.3.2-ea.5',
'11.3.3-ea.5.2.1231421',
'12.3.2-0',
'11.0.1'
]);
spyGetToolcachePath.mockImplementation(
Expand All @@ -134,7 +137,10 @@ describe('findInToolcache', () => {
checkLatest: false
});
const foundVersion = mockJavaBase['findInToolcache']();
expect(foundVersion?.version).toEqual(expected);
expect(foundVersion).toEqual({
version: expected.version,
path: `/hostedtoolcache/Java_Empty_jdk/${expected.versionInPath}/x64`
});
});
});

Expand Down Expand Up @@ -318,3 +324,28 @@ describe('normalizeVersion', () => {
);
});
});

describe('getToolcacheVersionName', () => {
const DummyJavaBase = JavaBase as any;

it.each([
[{ version: '11', stable: true }, '11'],
[{ version: '11.0.2', stable: true }, '11.0.2'],
[{ version: '11.0.2+4', stable: true }, '11.0.2-4'],
[{ version: '11.0.2+4.1.2563234', stable: true }, '11.0.2-4.1.2563234'],
[{ version: '11.0', stable: false }, '11.0-ea'],
[{ version: '11.0.3', stable: false }, '11.0.3-ea'],
[{ version: '11.0.3+4', stable: false }, '11.0.3-ea.4'],
[{ version: '11.0.3+4.2.256', stable: false }, '11.0.3-ea.4.2.256']
])('returns correct version name for %s', (input, expected) => {
const inputVersion = input.stable ? '11' : '11-ea';
const mockJavaBase = new EmptyJavaBase({
version: inputVersion,
packageType: 'jdk',
architecture: 'x64',
checkLatest: false
});
const actual = mockJavaBase['getToolcacheVersionName'](input.version);
expect(actual).toBe(expected);
});
});
46 changes: 31 additions & 15 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3970,7 +3970,6 @@ class JavaBase {
this.checkLatest = installerOptions.checkLatest;
}
setupJava() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
let foundJava = this.findInToolcache();
if (foundJava && !this.checkLatest) {
Expand All @@ -3980,8 +3979,6 @@ class JavaBase {
core.info('Trying to resolve the latest version from remote');
const javaRelease = yield this.findPackageForDownload(this.version);
core.info(`Resolved latest version as ${javaRelease.version}`);
core.info((_a = foundJava === null || foundJava === void 0 ? void 0 : foundJava.version) !== null && _a !== void 0 ? _a : '');
core.info((_b = javaRelease.version) !== null && _b !== void 0 ? _b : '');
if ((foundJava === null || foundJava === void 0 ? void 0 : foundJava.version) === javaRelease.version) {
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
}
Expand All @@ -4006,30 +4003,49 @@ class JavaBase {
}
getToolcacheVersionName(version) {
if (!this.stable) {
const cleanVersion = semver_1.default.clean(version);
return `${cleanVersion}-ea`;
if (version.includes('+')) {
return version.replace('+', '-ea.');
}
else {
return `${version}-ea`;
}
}
return version;
// Kotlin and some Java dependencies don't work properly when Java path contains "+" sign
// so replace "/hostedtoolcache/Java/11.0.3+4/x64" to "/hostedtoolcache/Java/11.0.3-4/x64" when saves to cache
// related issue: https://github.com/actions/virtual-environments/issues/3014
return version.replace('+', '-');
}
findInToolcache() {
// we can't use tc.find directly because firstly, we need to filter versions by stability flag
// if *-ea is provided, take only ea versions from toolcache, otherwise - only stable versions
const availableVersions = tc
.findAllVersions(this.toolcacheFolderName, this.architecture)
.filter(item => item.endsWith('-ea') === !this.stable);
.map(item => {
return {
version: item
.replace('-ea.', '+')
.replace(/-ea$/, '')
// Kotlin and some Java dependencies don't work properly when Java path contains "+" sign
// so replace "/hostedtoolcache/Java/11.0.3-4/x64" to "/hostedtoolcache/Java/11.0.3+4/x64" when retrieves to cache
// related issue: https://github.com/actions/virtual-environments/issues/3014
.replace('-', '+'),
path: util_1.getToolcachePath(this.toolcacheFolderName, item, this.architecture) || '',
stable: !item.includes('-ea')
};
})
.filter(item => item.stable === this.stable);
const satisfiedVersions = availableVersions
.filter(item => util_1.isVersionSatisfies(this.version, item.replace(/-ea$/, '')))
.sort(semver_1.default.rcompare);
.filter(item => util_1.isVersionSatisfies(this.version, item.version))
.filter(item => item.path)
.sort((a, b) => {
return -semver_1.default.compareBuild(a.version, b.version);
});
if (!satisfiedVersions || satisfiedVersions.length === 0) {
return null;
}
const javaPath = util_1.getToolcachePath(this.toolcacheFolderName, satisfiedVersions[0], this.architecture);
if (!javaPath) {
return null;
}
return {
version: util_1.getVersionFromToolcachePath(javaPath),
path: javaPath
version: satisfiedVersions[0].version,
path: satisfiedVersions[0].path
};
}
normalizeVersion(version) {
Expand Down
50 changes: 31 additions & 19 deletions src/distributions/base-installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export abstract class JavaBase {
core.info('Trying to resolve the latest version from remote');
const javaRelease = await this.findPackageForDownload(this.version);
core.info(`Resolved latest version as ${javaRelease.version}`);
core.info(foundJava?.version ?? '');
core.info(javaRelease.version ?? '');
if (foundJava?.version === javaRelease.version) {
core.info(`Resolved Java ${foundJava.version} from tool-cache`);
} else {
Expand Down Expand Up @@ -70,38 +68,52 @@ export abstract class JavaBase {

protected getToolcacheVersionName(version: string): string {
if (!this.stable) {
const cleanVersion = semver.clean(version);
return `${cleanVersion}-ea`;
if (version.includes('+')) {
return version.replace('+', '-ea.');
} else {
return `${version}-ea`;
}
}
return version;

// Kotlin and some Java dependencies don't work properly when Java path contains "+" sign
// so replace "/hostedtoolcache/Java/11.0.3+4/x64" to "/hostedtoolcache/Java/11.0.3-4/x64" when saves to cache
// related issue: https://github.com/actions/virtual-environments/issues/3014
return version.replace('+', '-');
}

protected findInToolcache(): JavaInstallerResults | null {
// we can't use tc.find directly because firstly, we need to filter versions by stability flag
// if *-ea is provided, take only ea versions from toolcache, otherwise - only stable versions
const availableVersions = tc
.findAllVersions(this.toolcacheFolderName, this.architecture)
.filter(item => item.endsWith('-ea') === !this.stable);
.map(item => {
return {
version: item
.replace('-ea.', '+')
.replace(/-ea$/, '')
// Kotlin and some Java dependencies don't work properly when Java path contains "+" sign
// so replace "/hostedtoolcache/Java/11.0.3-4/x64" to "/hostedtoolcache/Java/11.0.3+4/x64" when retrieves to cache
// related issue: https://github.com/actions/virtual-environments/issues/3014
.replace('-', '+'),
path: getToolcachePath(this.toolcacheFolderName, item, this.architecture) || '',
stable: !item.includes('-ea')
};
})
.filter(item => item.stable === this.stable);

const satisfiedVersions = availableVersions
.filter(item => isVersionSatisfies(this.version, item.replace(/-ea$/, '')))
.sort(semver.rcompare);
.filter(item => isVersionSatisfies(this.version, item.version))
.filter(item => item.path)
.sort((a, b) => {
return -semver.compareBuild(a.version, b.version);
});
if (!satisfiedVersions || satisfiedVersions.length === 0) {
return null;
}

const javaPath = getToolcachePath(
this.toolcacheFolderName,
satisfiedVersions[0],
this.architecture
);
if (!javaPath) {
return null;
}

return {
version: getVersionFromToolcachePath(javaPath),
path: javaPath
version: satisfiedVersions[0].version,
path: satisfiedVersions[0].path
};
}

Expand Down

0 comments on commit 46d5f06

Please sign in to comment.