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

Add check-latest flag #141

Merged
23 changes: 23 additions & 0 deletions .github/workflows/e2e-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ jobs:
- name: Verify Java
run: bash __tests__/verify-java.sh "${{ matrix.version }}" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-check-latest:
name: ${{ matrix.distribution }} ${{ matrix.version }} - check-latest flag - ${{ matrix.os }}
needs: setup-java-major-versions
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
distribution: ['adopt', 'zulu']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: setup-java
uses: ./
id: setup-java
with:
distribution: ${{ matrix.distribution }}
java-version: 11
check-latest: true
- name: Verify Java
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
shell: bash

setup-java-ea-versions-zulu:
name: zulu ${{ matrix.version }} (jdk-x64) - ${{ matrix.os }}
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,26 @@ Currently, the following distributions are supported:

**NOTE:** The different distributors can provide discrepant list of available versions / supported configurations. Please refer to the official documentation to see the list of supported versions.

#### Testing against different Java versions
### Check latest
In the basic examples above, the `check-latest` flag defaults to `false`. When set to `false`, the action tries to first resolve a version of Java from the local tool cache on the runner. If unable to find a specific version in the cache, the action will download a version of Java. Use the default or set `check-latest` to `false` if you prefer a faster more consistent setup experience that prioritizes trying to use the cached versions at the expense of newer versions sometimes being available for download.

If `check-latest` is set to `true`, the action first checks if the cached version is the latest one. If the locally cached version is not the most up-to-date, the latest version of Java will be downloaded. Set `check-latest` to `true` if you want the most up-to-date version of Java to always be used. Setting `check-latest` to `true` has performance implications as downloading versions of Java is slower than using cached versions.

For Java distributions that are not cached on Hosted images, `check-latest` always behaves as `true` and downloads Java on-flight. Check out [Hosted Tool Cache](docs/advanced-usage.md#Hosted-Tool-Cache) for more details about pre-cached Java versions.


```yaml
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2-preview
with:
distribution: 'adopt'
java-version: '11'
check-latest: true
- run: java -cp java HelloWorldApp
```

### Testing against different Java versions
```yaml
jobs:
build:
Expand Down Expand Up @@ -89,7 +108,7 @@ jobs:
- [Testing against different platforms](docs/advanced-usage.md#Testing-against-different-platforms)
- [Publishing using Apache Maven](docs/advanced-usage.md#Publishing-using-Apache-Maven)
- [Publishing using Gradle](docs/advanced-usage.md#Publishing-using-Gradle)

- [Hosted Tool Cache](docs/advanced-usage.md#Hosted-Tool-Cache)

## License

Expand Down
23 changes: 14 additions & 9 deletions __tests__/distributors/adopt-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ describe('getAvailableVersions', () => {

it.each([
[
{ version: '11', architecture: 'x64', packageType: 'jdk' },
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
'os=mac&architecture=x64&image_type=jdk&release_type=ga&page_size=20&page=0'
],
[
{ version: '11', architecture: 'x86', packageType: 'jdk' },
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: false },
'os=mac&architecture=x86&image_type=jdk&release_type=ga&page_size=20&page=0'
],
[
{ version: '11', architecture: 'x64', packageType: 'jre' },
{ version: '11', architecture: 'x64', packageType: 'jre', checkLatest: false },
'os=mac&architecture=x64&image_type=jre&release_type=ga&page_size=20&page=0'
],
[
{ version: '11-ea', architecture: 'x64', packageType: 'jdk' },
{ version: '11-ea', architecture: 'x64', packageType: 'jdk', checkLatest: false },
'os=mac&architecture=x64&image_type=jdk&release_type=ea&page_size=20&page=0'
]
])(
Expand Down Expand Up @@ -79,7 +79,8 @@ describe('getAvailableVersions', () => {
const distribution = new AdoptDistribution({
version: '11',
architecture: 'x64',
packageType: 'jdk'
packageType: 'jdk',
checkLatest: false
});
const availableVersions = await distribution['getAvailableVersions']();
expect(availableVersions).not.toBeNull();
Expand All @@ -104,7 +105,8 @@ describe('findPackageForDownload', () => {
const distribution = new AdoptDistribution({
version: '11',
architecture: 'x64',
packageType: 'jdk'
packageType: 'jdk',
checkLatest: false
});
distribution['getAvailableVersions'] = async () => manifestData;
const resolvedVersion = await distribution['findPackageForDownload'](input);
Expand All @@ -115,7 +117,8 @@ describe('findPackageForDownload', () => {
const distribution = new AdoptDistribution({
version: '11',
architecture: 'x64',
packageType: 'jdk'
packageType: 'jdk',
checkLatest: false
});
distribution['getAvailableVersions'] = async () => manifestData;
await expect(distribution['findPackageForDownload']('9.0.8')).rejects.toThrowError(
Expand All @@ -127,7 +130,8 @@ describe('findPackageForDownload', () => {
const distribution = new AdoptDistribution({
version: '11',
architecture: 'x64',
packageType: 'jdk'
packageType: 'jdk',
checkLatest: false
});
distribution['getAvailableVersions'] = async () => manifestData;
await expect(distribution['findPackageForDownload']('7.x')).rejects.toThrowError(
Expand All @@ -139,7 +143,8 @@ describe('findPackageForDownload', () => {
const distribution = new AdoptDistribution({
version: '11',
architecture: 'x64',
packageType: 'jdk'
packageType: 'jdk',
checkLatest: false
});
distribution['getAvailableVersions'] = async () => [];
await expect(distribution['findPackageForDownload']('11')).rejects.toThrowError(
Expand Down
129 changes: 99 additions & 30 deletions __tests__/distributors/base-installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class EmptyJavaBase extends JavaBase {

protected async downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults> {
return {
version: '11.0.8',
path: `/toolcache/${this.toolcacheFolderName}/11.0.8/${this.architecture}`
version: '11.0.9',
path: path.join('toolcache', this.toolcacheFolderName, '11.0.9', this.architecture)
};
}

protected async findPackageForDownload(range: string): Promise<JavaDownloadRelease> {
const availableVersion = '11.0.8';
const availableVersion = '11.0.9';
if (!semver.satisfies(availableVersion, range)) {
throw new Error('Available version not found');
}
Expand All @@ -38,7 +38,7 @@ class EmptyJavaBase extends JavaBase {
}

describe('findInToolcache', () => {
const actualJavaVersion = '11.1.10';
const actualJavaVersion = '11.0.8';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x64');

let mockJavaBase: EmptyJavaBase;
Expand All @@ -58,21 +58,33 @@ describe('findInToolcache', () => {

it.each([
[
{ version: '11', architecture: 'x64', packageType: 'jdk' },
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1', architecture: 'x64', packageType: 'jdk' },
{ version: '11.0', architecture: 'x64', packageType: 'jdk', checkLatest: false },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.1.10', architecture: 'x64', packageType: 'jdk' },
{ version: '11.0.8', architecture: 'x64', packageType: 'jdk', checkLatest: false },
{ version: actualJavaVersion, path: javaPath }
],
[{ version: '11', architecture: 'x64', packageType: 'jre' }, null],
[{ version: '8', architecture: 'x64', packageType: 'jdk' }, null],
[{ version: '11', architecture: 'x86', packageType: 'jdk' }, null],
[{ version: '11', architecture: 'x86', packageType: 'jre' }, null]
[
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.0', architecture: 'x64', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPath }
],
[
{ version: '11.0.8', architecture: 'x64', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPath }
],
[{ version: '11', architecture: 'x64', packageType: 'jre', checkLatest: false }, null],
[{ version: '8', architecture: 'x64', packageType: 'jdk', checkLatest: false }, null],
[{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: false }, null],
[{ version: '11', architecture: 'x86', packageType: 'jre', checkLatest: false }, null]
])(`should find java for path %s -> %s`, (input, expected) => {
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
spyGetToolcachePath.mockImplementation(
Expand Down Expand Up @@ -115,15 +127,22 @@ describe('findInToolcache', () => {
(toolname: string, javaVersion: string, architecture: string) =>
`/hostedtoolcache/${toolname}/${javaVersion}/${architecture}`
);
mockJavaBase = new EmptyJavaBase({ version: input, architecture: 'x64', packageType: 'jdk' });
mockJavaBase = new EmptyJavaBase({
version: input,
architecture: 'x64',
packageType: 'jdk',
checkLatest: false
});
const foundVersion = mockJavaBase['findInToolcache']();
expect(foundVersion?.version).toEqual(expected);
});
});

describe('setupJava', () => {
const actualJavaVersion = '11.1.10';
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x86');
const actualJavaVersion = '11.0.9';
const installedJavaVersion = '11.0.8';
const javaPath = path.join('Java_Empty_jdk', installedJavaVersion, 'x86');
const javaPathInstalled = path.join('toolcache', 'Java_Empty_jdk', actualJavaVersion, 'x86');

let mockJavaBase: EmptyJavaBase;

Expand All @@ -145,12 +164,12 @@ describe('setupJava', () => {
return '';
}

return semver.satisfies(actualJavaVersion, semverVersion) ? javaPath : '';
return semver.satisfies(installedJavaVersion, semverVersion) ? javaPath : '';
}
);

spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
spyTcFindAllVersions.mockReturnValue([installedJavaVersion]);

// Spy on core methods
spyCoreDebug = jest.spyOn(core, 'debug');
Expand All @@ -177,35 +196,41 @@ describe('setupJava', () => {

it.each([
[
{ version: '11', architecture: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: false },
{ version: installedJavaVersion, path: javaPath }
],
[
{ version: '11.1', architecture: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
{ version: '11.0', architecture: 'x86', packageType: 'jdk', checkLatest: false },
{ version: installedJavaVersion, path: javaPath }
],
[
{ version: '11.1.10', architecture: 'x86', packageType: 'jdk' },
{ version: actualJavaVersion, path: javaPath }
{ version: '11.0.8', architecture: 'x86', packageType: 'jdk', checkLatest: false },
{ version: installedJavaVersion, path: javaPath }
]
])('should find java locally for %s', (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyGetToolcachePath).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${expected.version} from tool-cache`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
expect(spyCoreInfo).not.toHaveBeenCalledWith(
'Trying to resolve the latest version from remote'
);
expect(spyCoreInfo).not.toHaveBeenCalledWith('Trying to download...');
});

it.each([
[
{ version: '11', architecture: 'x86', packageType: 'jre' },
{ path: `/toolcache/Java_Empty_jre/11.0.8/x86`, version: '11.0.8' }
{ version: '11', architecture: 'x86', packageType: 'jre', checkLatest: false },
{ path: path.join('toolcache', 'Java_Empty_jre', '11.0.9', 'x86'), version: '11.0.9' }
],
[
{ version: '11', architecture: 'x64', packageType: 'jdk' },
{ path: `/toolcache/Java_Empty_jdk/11.0.8/x64`, version: '11.0.8' }
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
{ path: path.join('toolcache', 'Java_Empty_jdk', '11.0.9', 'x64'), version: '11.0.9' }
],
[
{ version: '11', architecture: 'x64', packageType: 'jre' },
{ path: `/toolcache/Java_Empty_jre/11.0.8/x64`, version: '11.0.8' }
{ version: '11', architecture: 'x64', packageType: 'jre', checkLatest: false },
{ path: path.join('toolcache', 'Java_Empty_jre', '11.0.9', 'x64'), version: '11.0.9' }
]
])('download java with configuration %s', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
Expand All @@ -214,11 +239,55 @@ describe('setupJava', () => {
expect(spyCoreAddPath).toHaveBeenCalled();
expect(spyCoreExportVariable).toHaveBeenCalled();
expect(spyCoreSetOutput).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${expected.version}`);
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
expect(spyCoreInfo).toHaveBeenCalledWith(`Java ${expected.version} was downloaded`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
});

it.each([
[
{ version: '11.0.9', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: '11.0.9', path: javaPathInstalled }
]
])('should check the latest java version for %s and resolve locally', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
mockJavaBase['findInToolcache'] = () => ({ version: '11.0.9', path: expected.path });
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${expected.version}`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${expected.version} from tool-cache`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
});

it.each([
[
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPathInstalled }
],
[
{ version: '11.0', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPathInstalled }
],
[
{ version: '11.0.x', architecture: 'x86', packageType: 'jdk', checkLatest: true },
{ version: actualJavaVersion, path: javaPathInstalled }
]
])('should check the latest java version for %s and download', async (input, expected) => {
mockJavaBase = new EmptyJavaBase(input);
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
expect(spyGetToolcachePath).toHaveBeenCalled();
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${actualJavaVersion}`);
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
expect(spyCoreInfo).toHaveBeenCalledWith(`Java ${actualJavaVersion} was downloaded`);
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
});

it.each([
[{ version: '15', architecture: 'x86', packageType: 'jre' }],
[{ version: '11.0.7', architecture: 'x64', packageType: 'jre' }]
[{ version: '15', architecture: 'x86', packageType: 'jre', checkLatest: false }],
[{ version: '11.0.7', architecture: 'x64', packageType: 'jre', checkLatest: false }]
])('should throw an error for Available version not found for %s', async input => {
mockJavaBase = new EmptyJavaBase(input);
await expect(mockJavaBase.setupJava()).rejects.toThrowError('Available version not found');
Expand Down
Loading