Correct unit-tests for GitHub actions workflows

This commit is contained in:
Thach Nguyen 2022-08-04 14:02:51 +07:00
parent 684bcc4c64
commit 187689f108
5 changed files with 69 additions and 19 deletions

View File

@ -72,7 +72,7 @@
"plugins": ["jest"],
"extends": ["plugin:jest/all"],
"rules": {
"max-lines-per-function": ["warn", 100],
"max-lines-per-function": ["warn", 120],
"jest/no-hooks": "off",
"jest/prefer-expect-assertions": "off"
}

View File

@ -26,4 +26,4 @@ jobs:
run: yarn --no-bin-links
- run: yarn build
- run: yarn lint
- run: yarn test --coverage
- run: yarn test

36
.github/workflows/tests.yml vendored Normal file
View File

@ -0,0 +1,36 @@
name: Integration Tests
on: [push, pull_request]
jobs:
tests:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- version: 3.3
expected: 3.3.9
- version: '=3.2.5'
expected: 3.2.5
- version: '~3.3.1'
expected: 3.3.9
- version: '>=3.2.1 <3.2.5'
expected: 3.2.3
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Setup OpenJDK 8
uses: actions/setup-java@v3
with:
java-version: 8
distribution: temurin
- name: Setup Maven ${{ matrix.version }}
id: setup-maven
uses: thachnn/setup-maven@${{ github.ref_name }}
with:
maven-version: ${{ matrix.version }}
- if: steps.setup-maven.outputs.version != matrix.expected
run: mvn -B --file pom.xml

View File

@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { existsSync, promises as fs } from 'fs';
import * as core from '@actions/core';
import * as hm from '@actions/http-client';
@ -100,12 +100,15 @@ describe('download & setup Maven', () => {
process.env.RUNNER_TEMP = os.tmpdir();
process.env.RUNNER_TOOL_CACHE = CACHE_PATH;
afterEach(async () => {
await fs.rmdir(CACHE_PATH, { recursive: true });
});
describe('downloadMaven', () => {
const TEST_VERSION = '3.3.3';
const TOOL_PATH = path.join(CACHE_PATH, 'maven', TEST_VERSION);
afterEach(() => {
if (fs.existsSync(TOOL_PATH)) {
fs.rmdirSync(TOOL_PATH, { recursive: true });
}
});
it('download a real version of Maven', async () => {
const toolPath = await installer.downloadMaven(TEST_VERSION);
@ -114,8 +117,8 @@ describe('download & setup Maven', () => {
expect.stringMatching(new RegExp(`Downloading Maven ${TEST_VERSION} from`, 'i'))
);
expect(existsSync(`${toolPath}.complete`)).toBe(true);
expect(existsSync(path.join(CACHE_PATH, 'maven', TEST_VERSION))).toBe(true);
expect(fs.existsSync(`${toolPath}.complete`)).toBe(true);
expect(fs.existsSync(TOOL_PATH)).toBe(true);
expect(getVersionFromToolcachePath(toolPath)).toBe(TEST_VERSION);
});
@ -132,7 +135,9 @@ describe('download & setup Maven', () => {
it('raises error when extracting failed', async () => {
const spyDownload = jest.spyOn(tc, 'downloadTool').mockResolvedValue(__filename);
await expect(installer.downloadMaven(TEST_VERSION)).rejects.toThrow(/failed.* exit code 1/i);
await expect(installer.downloadMaven(TEST_VERSION)).rejects.toThrow(
/process .*tar.* failed.* exit code [1-2]/i
);
expect(spyDownload).toHaveBeenCalledWith(expect.stringContaining(TEST_VERSION));
expect(core.debug).toHaveBeenCalledWith(expect.stringContaining('tar'));
@ -143,9 +148,15 @@ describe('download & setup Maven', () => {
const TEST_VERSION = '3.2.5';
const TOOL_PATH = path.join(CACHE_PATH, 'maven', TEST_VERSION, os.arch());
beforeEach(async () => {
await fs.mkdir(TOOL_PATH, { recursive: true });
await fs.writeFile(`${TOOL_PATH}.complete`, '');
beforeEach(() => {
fs.mkdirSync(TOOL_PATH, { recursive: true });
fs.writeFileSync(`${TOOL_PATH}.complete`, '');
});
afterEach(() => {
if (fs.existsSync(TOOL_PATH)) {
fs.rmdirSync(path.dirname(TOOL_PATH), { recursive: true });
}
});
describe('reuses the cached version of Maven', () => {

View File

@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { existsSync, promises as fs } from 'fs';
import * as core from '@actions/core';
@ -67,9 +67,12 @@ describe('integration tests', () => {
process.env.PATH = `${MVN_PATH}${path.delimiter}${ORIGINAL_PATH ?? ''}`;
});
afterEach(async () => {
afterEach(() => {
process.env.PATH = ORIGINAL_PATH;
await fs.rmdir(CACHE_PATH, { recursive: true });
if (fs.existsSync(TOOL_PATH)) {
fs.rmdirSync(path.dirname(TOOL_PATH), { recursive: true });
}
});
it('uses system Maven if real version =~ default version', async () => {
@ -91,12 +94,12 @@ describe('integration tests', () => {
expect(core.addPath).toHaveBeenCalledWith(path.join(TOOL_PATH, 'bin'));
expect(core.setOutput).toHaveBeenCalledWith('version', TEST_VERSION);
expect(existsSync(`${TOOL_PATH}.complete`)).toBe(true);
expect(fs.existsSync(`${TOOL_PATH}.complete`)).toBe(true);
});
it('uses system Maven if real version > cached version', async () => {
await fs.mkdir(TOOL_PATH, { recursive: true });
await fs.writeFile(`${TOOL_PATH}.complete`, '');
fs.mkdirSync(TOOL_PATH, { recursive: true });
fs.writeFileSync(`${TOOL_PATH}.complete`, '');
(core.getInput as jest.Mock).mockReturnValue('3.x ');
await run();