Skip to content

Commit 022e86d

Browse files
dmitry-shibanovMaxim Lobanovkonradpabjan
authored
Add check-latest flag (#141)
* add changes for check-latest * run prerelease script * resolving comments * fixing tests * fix spelling * improve core.info messages * run format * run prerelease * change version to fix test * resolve comment for check-latest * Update README.md * added hosted tool cache section * Apply suggestions from code review Co-authored-by: Maxim Lobanov <[email protected]> Co-authored-by: Konrad Pabjan <[email protected]>
1 parent 502a665 commit 022e86d

File tree

14 files changed

+279
-77
lines changed

14 files changed

+279
-77
lines changed

.github/workflows/e2e-versions.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ jobs:
6969
- name: Verify Java
7070
run: bash __tests__/verify-java.sh "${{ matrix.version }}" "${{ steps.setup-java.outputs.path }}"
7171
shell: bash
72+
73+
setup-java-check-latest:
74+
name: ${{ matrix.distribution }} ${{ matrix.version }} - check-latest flag - ${{ matrix.os }}
75+
needs: setup-java-major-versions
76+
runs-on: ${{ matrix.os }}
77+
strategy:
78+
fail-fast: false
79+
matrix:
80+
os: [macos-latest, windows-latest, ubuntu-latest]
81+
distribution: ['adopt', 'zulu']
82+
steps:
83+
- name: Checkout
84+
uses: actions/checkout@v2
85+
- name: setup-java
86+
uses: ./
87+
id: setup-java
88+
with:
89+
distribution: ${{ matrix.distribution }}
90+
java-version: 11
91+
check-latest: true
92+
- name: Verify Java
93+
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
94+
shell: bash
7295

7396
setup-java-ea-versions-zulu:
7497
name: zulu ${{ matrix.version }} (jdk-x64) - ${{ matrix.os }}

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,26 @@ Currently, the following distributions are supported:
5959

6060
**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.
6161

62-
#### Testing against different Java versions
62+
### Check latest
63+
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.
64+
65+
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.
66+
67+
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.
68+
69+
70+
```yaml
71+
steps:
72+
- uses: actions/checkout@v2
73+
- uses: actions/setup-java@v2-preview
74+
with:
75+
distribution: 'adopt'
76+
java-version: '11'
77+
check-latest: true
78+
- run: java -cp java HelloWorldApp
79+
```
80+
81+
### Testing against different Java versions
6382
```yaml
6483
jobs:
6584
build:
@@ -89,7 +108,7 @@ jobs:
89108
- [Testing against different platforms](docs/advanced-usage.md#Testing-against-different-platforms)
90109
- [Publishing using Apache Maven](docs/advanced-usage.md#Publishing-using-Apache-Maven)
91110
- [Publishing using Gradle](docs/advanced-usage.md#Publishing-using-Gradle)
92-
111+
- [Hosted Tool Cache](docs/advanced-usage.md#Hosted-Tool-Cache)
93112

94113
## License
95114

__tests__/distributors/adopt-installer.test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ describe('getAvailableVersions', () => {
2727

2828
it.each([
2929
[
30-
{ version: '11', architecture: 'x64', packageType: 'jdk' },
30+
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
3131
'os=mac&architecture=x64&image_type=jdk&release_type=ga&page_size=20&page=0'
3232
],
3333
[
34-
{ version: '11', architecture: 'x86', packageType: 'jdk' },
34+
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: false },
3535
'os=mac&architecture=x86&image_type=jdk&release_type=ga&page_size=20&page=0'
3636
],
3737
[
38-
{ version: '11', architecture: 'x64', packageType: 'jre' },
38+
{ version: '11', architecture: 'x64', packageType: 'jre', checkLatest: false },
3939
'os=mac&architecture=x64&image_type=jre&release_type=ga&page_size=20&page=0'
4040
],
4141
[
42-
{ version: '11-ea', architecture: 'x64', packageType: 'jdk' },
42+
{ version: '11-ea', architecture: 'x64', packageType: 'jdk', checkLatest: false },
4343
'os=mac&architecture=x64&image_type=jdk&release_type=ea&page_size=20&page=0'
4444
]
4545
])(
@@ -79,7 +79,8 @@ describe('getAvailableVersions', () => {
7979
const distribution = new AdoptDistribution({
8080
version: '11',
8181
architecture: 'x64',
82-
packageType: 'jdk'
82+
packageType: 'jdk',
83+
checkLatest: false
8384
});
8485
const availableVersions = await distribution['getAvailableVersions']();
8586
expect(availableVersions).not.toBeNull();
@@ -104,7 +105,8 @@ describe('findPackageForDownload', () => {
104105
const distribution = new AdoptDistribution({
105106
version: '11',
106107
architecture: 'x64',
107-
packageType: 'jdk'
108+
packageType: 'jdk',
109+
checkLatest: false
108110
});
109111
distribution['getAvailableVersions'] = async () => manifestData;
110112
const resolvedVersion = await distribution['findPackageForDownload'](input);
@@ -115,7 +117,8 @@ describe('findPackageForDownload', () => {
115117
const distribution = new AdoptDistribution({
116118
version: '11',
117119
architecture: 'x64',
118-
packageType: 'jdk'
120+
packageType: 'jdk',
121+
checkLatest: false
119122
});
120123
distribution['getAvailableVersions'] = async () => manifestData;
121124
await expect(distribution['findPackageForDownload']('9.0.8')).rejects.toThrowError(
@@ -127,7 +130,8 @@ describe('findPackageForDownload', () => {
127130
const distribution = new AdoptDistribution({
128131
version: '11',
129132
architecture: 'x64',
130-
packageType: 'jdk'
133+
packageType: 'jdk',
134+
checkLatest: false
131135
});
132136
distribution['getAvailableVersions'] = async () => manifestData;
133137
await expect(distribution['findPackageForDownload']('7.x')).rejects.toThrowError(
@@ -139,7 +143,8 @@ describe('findPackageForDownload', () => {
139143
const distribution = new AdoptDistribution({
140144
version: '11',
141145
architecture: 'x64',
142-
packageType: 'jdk'
146+
packageType: 'jdk',
147+
checkLatest: false
143148
});
144149
distribution['getAvailableVersions'] = async () => [];
145150
await expect(distribution['findPackageForDownload']('11')).rejects.toThrowError(

__tests__/distributors/base-installer.test.ts

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class EmptyJavaBase extends JavaBase {
1919

2020
protected async downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults> {
2121
return {
22-
version: '11.0.8',
23-
path: `/toolcache/${this.toolcacheFolderName}/11.0.8/${this.architecture}`
22+
version: '11.0.9',
23+
path: path.join('toolcache', this.toolcacheFolderName, '11.0.9', this.architecture)
2424
};
2525
}
2626

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

4040
describe('findInToolcache', () => {
41-
const actualJavaVersion = '11.1.10';
41+
const actualJavaVersion = '11.0.8';
4242
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x64');
4343

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

5959
it.each([
6060
[
61-
{ version: '11', architecture: 'x64', packageType: 'jdk' },
61+
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
6262
{ version: actualJavaVersion, path: javaPath }
6363
],
6464
[
65-
{ version: '11.1', architecture: 'x64', packageType: 'jdk' },
65+
{ version: '11.0', architecture: 'x64', packageType: 'jdk', checkLatest: false },
6666
{ version: actualJavaVersion, path: javaPath }
6767
],
6868
[
69-
{ version: '11.1.10', architecture: 'x64', packageType: 'jdk' },
69+
{ version: '11.0.8', architecture: 'x64', packageType: 'jdk', checkLatest: false },
7070
{ version: actualJavaVersion, path: javaPath }
7171
],
72-
[{ version: '11', architecture: 'x64', packageType: 'jre' }, null],
73-
[{ version: '8', architecture: 'x64', packageType: 'jdk' }, null],
74-
[{ version: '11', architecture: 'x86', packageType: 'jdk' }, null],
75-
[{ version: '11', architecture: 'x86', packageType: 'jre' }, null]
72+
[
73+
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: true },
74+
{ version: actualJavaVersion, path: javaPath }
75+
],
76+
[
77+
{ version: '11.0', architecture: 'x64', packageType: 'jdk', checkLatest: true },
78+
{ version: actualJavaVersion, path: javaPath }
79+
],
80+
[
81+
{ version: '11.0.8', architecture: 'x64', packageType: 'jdk', checkLatest: true },
82+
{ version: actualJavaVersion, path: javaPath }
83+
],
84+
[{ version: '11', architecture: 'x64', packageType: 'jre', checkLatest: false }, null],
85+
[{ version: '8', architecture: 'x64', packageType: 'jdk', checkLatest: false }, null],
86+
[{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: false }, null],
87+
[{ version: '11', architecture: 'x86', packageType: 'jre', checkLatest: false }, null]
7688
])(`should find java for path %s -> %s`, (input, expected) => {
7789
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
7890
spyGetToolcachePath.mockImplementation(
@@ -115,15 +127,22 @@ describe('findInToolcache', () => {
115127
(toolname: string, javaVersion: string, architecture: string) =>
116128
`/hostedtoolcache/${toolname}/${javaVersion}/${architecture}`
117129
);
118-
mockJavaBase = new EmptyJavaBase({ version: input, architecture: 'x64', packageType: 'jdk' });
130+
mockJavaBase = new EmptyJavaBase({
131+
version: input,
132+
architecture: 'x64',
133+
packageType: 'jdk',
134+
checkLatest: false
135+
});
119136
const foundVersion = mockJavaBase['findInToolcache']();
120137
expect(foundVersion?.version).toEqual(expected);
121138
});
122139
});
123140

124141
describe('setupJava', () => {
125-
const actualJavaVersion = '11.1.10';
126-
const javaPath = path.join('Java_Empty_jdk', actualJavaVersion, 'x86');
142+
const actualJavaVersion = '11.0.9';
143+
const installedJavaVersion = '11.0.8';
144+
const javaPath = path.join('Java_Empty_jdk', installedJavaVersion, 'x86');
145+
const javaPathInstalled = path.join('toolcache', 'Java_Empty_jdk', actualJavaVersion, 'x86');
127146

128147
let mockJavaBase: EmptyJavaBase;
129148

@@ -145,12 +164,12 @@ describe('setupJava', () => {
145164
return '';
146165
}
147166

148-
return semver.satisfies(actualJavaVersion, semverVersion) ? javaPath : '';
167+
return semver.satisfies(installedJavaVersion, semverVersion) ? javaPath : '';
149168
}
150169
);
151170

152171
spyTcFindAllVersions = jest.spyOn(tc, 'findAllVersions');
153-
spyTcFindAllVersions.mockReturnValue([actualJavaVersion]);
172+
spyTcFindAllVersions.mockReturnValue([installedJavaVersion]);
154173

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

178197
it.each([
179198
[
180-
{ version: '11', architecture: 'x86', packageType: 'jdk' },
181-
{ version: actualJavaVersion, path: javaPath }
199+
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: false },
200+
{ version: installedJavaVersion, path: javaPath }
182201
],
183202
[
184-
{ version: '11.1', architecture: 'x86', packageType: 'jdk' },
185-
{ version: actualJavaVersion, path: javaPath }
203+
{ version: '11.0', architecture: 'x86', packageType: 'jdk', checkLatest: false },
204+
{ version: installedJavaVersion, path: javaPath }
186205
],
187206
[
188-
{ version: '11.1.10', architecture: 'x86', packageType: 'jdk' },
189-
{ version: actualJavaVersion, path: javaPath }
207+
{ version: '11.0.8', architecture: 'x86', packageType: 'jdk', checkLatest: false },
208+
{ version: installedJavaVersion, path: javaPath }
190209
]
191210
])('should find java locally for %s', (input, expected) => {
192211
mockJavaBase = new EmptyJavaBase(input);
193212
expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
194213
expect(spyGetToolcachePath).toHaveBeenCalled();
214+
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${expected.version} from tool-cache`);
215+
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
216+
expect(spyCoreInfo).not.toHaveBeenCalledWith(
217+
'Trying to resolve the latest version from remote'
218+
);
219+
expect(spyCoreInfo).not.toHaveBeenCalledWith('Trying to download...');
195220
});
196221

197222
it.each([
198223
[
199-
{ version: '11', architecture: 'x86', packageType: 'jre' },
200-
{ path: `/toolcache/Java_Empty_jre/11.0.8/x86`, version: '11.0.8' }
224+
{ version: '11', architecture: 'x86', packageType: 'jre', checkLatest: false },
225+
{ path: path.join('toolcache', 'Java_Empty_jre', '11.0.9', 'x86'), version: '11.0.9' }
201226
],
202227
[
203-
{ version: '11', architecture: 'x64', packageType: 'jdk' },
204-
{ path: `/toolcache/Java_Empty_jdk/11.0.8/x64`, version: '11.0.8' }
228+
{ version: '11', architecture: 'x64', packageType: 'jdk', checkLatest: false },
229+
{ path: path.join('toolcache', 'Java_Empty_jdk', '11.0.9', 'x64'), version: '11.0.9' }
205230
],
206231
[
207-
{ version: '11', architecture: 'x64', packageType: 'jre' },
208-
{ path: `/toolcache/Java_Empty_jre/11.0.8/x64`, version: '11.0.8' }
232+
{ version: '11', architecture: 'x64', packageType: 'jre', checkLatest: false },
233+
{ path: path.join('toolcache', 'Java_Empty_jre', '11.0.9', 'x64'), version: '11.0.9' }
209234
]
210235
])('download java with configuration %s', async (input, expected) => {
211236
mockJavaBase = new EmptyJavaBase(input);
@@ -214,11 +239,55 @@ describe('setupJava', () => {
214239
expect(spyCoreAddPath).toHaveBeenCalled();
215240
expect(spyCoreExportVariable).toHaveBeenCalled();
216241
expect(spyCoreSetOutput).toHaveBeenCalled();
242+
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
243+
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${expected.version}`);
244+
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
245+
expect(spyCoreInfo).toHaveBeenCalledWith(`Java ${expected.version} was downloaded`);
246+
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
247+
});
248+
249+
it.each([
250+
[
251+
{ version: '11.0.9', architecture: 'x86', packageType: 'jdk', checkLatest: true },
252+
{ version: '11.0.9', path: javaPathInstalled }
253+
]
254+
])('should check the latest java version for %s and resolve locally', async (input, expected) => {
255+
mockJavaBase = new EmptyJavaBase(input);
256+
mockJavaBase['findInToolcache'] = () => ({ version: '11.0.9', path: expected.path });
257+
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
258+
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
259+
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${expected.version}`);
260+
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved Java ${expected.version} from tool-cache`);
261+
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
262+
});
263+
264+
it.each([
265+
[
266+
{ version: '11', architecture: 'x86', packageType: 'jdk', checkLatest: true },
267+
{ version: actualJavaVersion, path: javaPathInstalled }
268+
],
269+
[
270+
{ version: '11.0', architecture: 'x86', packageType: 'jdk', checkLatest: true },
271+
{ version: actualJavaVersion, path: javaPathInstalled }
272+
],
273+
[
274+
{ version: '11.0.x', architecture: 'x86', packageType: 'jdk', checkLatest: true },
275+
{ version: actualJavaVersion, path: javaPathInstalled }
276+
]
277+
])('should check the latest java version for %s and download', async (input, expected) => {
278+
mockJavaBase = new EmptyJavaBase(input);
279+
await expect(mockJavaBase.setupJava()).resolves.toEqual(expected);
280+
expect(spyGetToolcachePath).toHaveBeenCalled();
281+
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to resolve the latest version from remote');
282+
expect(spyCoreInfo).toHaveBeenCalledWith(`Resolved latest version as ${actualJavaVersion}`);
283+
expect(spyCoreInfo).toHaveBeenCalledWith('Trying to download...');
284+
expect(spyCoreInfo).toHaveBeenCalledWith(`Java ${actualJavaVersion} was downloaded`);
285+
expect(spyCoreInfo).toHaveBeenCalledWith(`Setting Java ${expected.version} as the default`);
217286
});
218287

219288
it.each([
220-
[{ version: '15', architecture: 'x86', packageType: 'jre' }],
221-
[{ version: '11.0.7', architecture: 'x64', packageType: 'jre' }]
289+
[{ version: '15', architecture: 'x86', packageType: 'jre', checkLatest: false }],
290+
[{ version: '11.0.7', architecture: 'x64', packageType: 'jre', checkLatest: false }]
222291
])('should throw an error for Available version not found for %s', async input => {
223292
mockJavaBase = new EmptyJavaBase(input);
224293
await expect(mockJavaBase.setupJava()).rejects.toThrowError('Available version not found');

0 commit comments

Comments
 (0)