Skip to content

Commit

Permalink
test: add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teatimeguest committed Oct 10, 2024
1 parent 4efd0d1 commit 83469d8
Show file tree
Hide file tree
Showing 13 changed files with 1,345 additions and 797 deletions.
1,931 changes: 1,154 additions & 777 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"ts-essentials": "^10.0.2",
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"vitest": "^2.1.1"
"vitest": "^2.1.2"
},
"overrides": {
"ajv-cli": {
Expand Down
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@tsconfig/strictest": "^2.0.5",
"@types/nunjucks": "^3.2.6",
"@types/semver": "^7.5.8",
"@vitest/coverage-v8": "^2.1.1",
"@vitest/coverage-v8": "^2.1.2",
"esbuild-loader": "^4.2.2",
"eslint-plugin-import-x": "^4.1.1",
"eslint-plugin-jsdoc": "^50.2.4",
Expand Down
5 changes: 2 additions & 3 deletions packages/config/vitest/vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// @ts-check
import { fileURLToPath } from 'node:url';

import tsConfigPaths from 'vite-tsconfig-paths';

import esbuildConfig, {
transformConfig,
} from '@setup-texlive-action/config/esbuild';
import esbuildConfig, { transformConfig } from '../esbuild.config.mjs';

/** @type {import('vitest/config').UserConfig} */
export default {
Expand Down
24 changes: 13 additions & 11 deletions packages/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

> E2E test files and helper scripts
Local testing is done inside a container using [`act`].
The `act` binary is automatically installed during `npm ci` by [`@kie/act-js`].

## Prerequisites

- <!-- dprint-ignore-start -->
<code>[act](https://github.com/nektos/act) >=0.2.53</code>
<!-- dprint-ignore-end -->
- `docker`, or
[compatible container engine](https://nektosact.com/usage/custom_engine.html)
- `docker`

### Container Images

- <!-- dprint-ignore-start -->
<code>[node](https://hub.docker.com/_/node):20.0</code>
<!-- dprint-ignore-end -->
- <!-- dprint-ignore-start -->
<code>[ubuntu/squid](https://hub.docker.com/r/ubuntu/squid):latest</code>
<!-- dprint-ignore-end -->
<!-- dprint-ignore-start -->
- <code>[node]:20.0</code>
- <code>[ubuntu/squid]:latest</code>
<!-- dprint-ignore-end -->

## Testing

Expand All @@ -28,3 +25,8 @@ npm run e2e [target]
```sh
npm run e2e -- --list # Lists all test targets
```

[`@kie/act-js`]: https://www.npmjs.com/package/@kie/act-js
[`act`]: https://nektosact.com
[node]: https://hub.docker.com/_/node
[ubuntu/squid]: https://hub.docker.com/r/ubuntu/squid
21 changes: 19 additions & 2 deletions packages/e2e/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
version: '3'
vars:
basedir: '{{ .TASKFILE_DIR | relPath .npm_config_local_prefix }}'
tasks:
act:
internal: true
Expand All @@ -8,7 +10,7 @@ tasks:
desc: 'Run: {{ .workflows }}'
dir: '{{ .npm_config_local_prefix }}'
cmd: >-
act
act-js
--container-architecture linux/{{ default ARCH .architecture }}
--workflows {{ .workflows }}
{{ .CLI_ARGS }}
Expand Down Expand Up @@ -38,7 +40,22 @@ tasks:
<<: *act
deps: [clear-cache]
vars:
basedir: '{{ .TASKFILE_DIR | relPath .npm_config_local_prefix }}'
workflows: '{{ .basedir }}/workflows/{{ .TASK }}.yml'
move-to-historic: *run-local-workflow
tlpretest: *run-local-workflow

cache-on-failure:
<<: &cache-on-failure
vars:
workflow: '{{ .TASK | splitList ":" | first }}'
case: '{{ .TASK | trimPrefix .workflow | trimPrefix ":" }}'
desc: >-
Run: {{ .basedir }}/workflows/{{ .workflow }}.yml
{{ if .case }}({{ .case }}){{ end }}
cmd: >-
vitest {{- if .case }} --testNamePattern '^{{ .case }}$'{{ end }}
cache-on-failure:default: *cache-on-failure
cache-on-failure:with-0: *cache-on-failure
cache-on-failure:with-1: *cache-on-failure
cache-on-failure:with-true: *cache-on-failure
cache-on-failure:with-: *cache-on-failure
37 changes: 37 additions & 0 deletions packages/e2e/__mocks__/@kie/act-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { vi } from 'vitest';

import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import * as path from 'node:path';

import { Act } from '@kie/act-js';

const originalRun = Reflect.get(Act.prototype, 'run') as typeof run;

// Ensure that a new cache entry is created each time
vi
.spyOn(Act.prototype, 'run' as keyof Act)
.mockImplementation(run as ReturnType<typeof vi.fn>);

// Prevent `act-js` from creating `.actrc` file in home directory
vi
.spyOn(Act.prototype, 'setDefaultImage' as keyof Act)
.mockImplementation(vi.fn());

async function run(
this: Act,
cmd: string[],
...args: unknown[]
): Promise<unknown> {
const tmp = await mkdtemp(path.join(tmpdir(), 'act-'));
await using _ = { // eslint-disable-line @typescript-eslint/no-unused-vars
async [Symbol.asyncDispose]() {
await rm(tmp, { force: true, recursive: true });
},
};
console.error(`Using ${tmp} as cache server path`);
cmd.push('--cache-server-path', tmp);
return await originalRun.call(this, cmd, ...args);
}

export { Act };
25 changes: 25 additions & 0 deletions packages/e2e/__snapshots__/cache-on-failure.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions packages/e2e/__tests__/cache-on-failure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { beforeEach, expect, test, vi } from 'vitest';

import * as path from 'node:path';
import { env } from 'node:process';

import { Act, type RunOpts } from '@kie/act-js';

vi.mock('@kie/act-js');

// Use the repository root as cwd:
const cwd = env['npm_config_local_prefix']
?? path.resolve(import.meta.dirname, '../../../');

const job = 'test';
const workflow = path.resolve(
import.meta.dirname,
'../workflows/cache-on-failure.yml',
);
// Send logs to stderr:
const opts = { logFile: '/dev/stderr' } as const satisfies RunOpts;

const key = 'SETUP_TEXLIVE_ACTION_NO_CACHE_ON_FAILURE';
const msg = /^Cache saved successfully$/gmv;
const act = new Act(cwd, workflow);

beforeEach(() => {
act.clearEnv();
});

test('default', async () => {
const steps = await act.runJob(job, opts);
expect(steps[1]?.name).toMatchSnapshot();
expect(steps[1]?.status).toBe(0);
expect(steps[2]?.name).toMatchSnapshot();
expect(steps[2]?.status).toBe(0);
expect(steps[3]?.status).toBe(1);
expect(steps[4]?.name).toMatchSnapshot();
expect(steps[4]?.status).toBe(0);
expect(steps[4]?.output).toMatch(msg);
});

test('with-0', async () => {
act.setEnv(key, '0');
const steps = await act.runJob(job, opts);
expect(steps[1]?.name).toMatchSnapshot();
expect(steps[1]?.status).toBe(0);
expect(steps[2]?.name).toMatchSnapshot();
expect(steps[2]?.status).toBe(0);
expect(steps[3]?.status).toBe(1);
expect(steps[4]?.name).toMatchSnapshot();
expect(steps[4]?.status).toBe(0);
expect(steps[4]?.output).toMatch(msg);
});

test.for(['1', 'true', ''])('with-%s', async (value) => {
act.setEnv(key, value);
const steps = await act.runJob(job, opts);
expect(steps[1]?.name).toMatchSnapshot();
expect(steps[1]?.status).toBe(0);
expect(steps[2]?.name).toMatchSnapshot();
expect(steps[2]?.status).toBe(0);
expect(steps[3]?.status).toBe(1);
expect(steps[4]).toBeUndefined();
});
4 changes: 3 additions & 1 deletion packages/e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"scripts": {
"e2e": "task"
},
"type": "module",
"main": "index.cjs",
"devDependencies": {
"@actions/github": "^6.0.0"
"@actions/github": "^6.0.0",
"@kie/act-js": "^2.6.2"
}
}
9 changes: 9 additions & 0 deletions packages/e2e/vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { mergeConfig } from 'vitest/config';

import defaultConfig from '@setup-texlive-action/config/vitest';

export default mergeConfig(defaultConfig, {
test: {
testTimeout: 5 * 60 * 1000, // 5min
},
});
13 changes: 13 additions & 0 deletions packages/e2e/workflows/cache-on-failure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
on: workflow_dispatch
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup TeX Live
id: setup
uses: ./
- name: Check that a new installation has been made
run: exit ${{ fromJSON(steps.setup.outputs.cache-restored) || 1 && 0 }}
- name: Always fails
run: exit 1
5 changes: 4 additions & 1 deletion packages/vitest.workspace.json
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
["*/vitest.config.{js,mjs,ts}"]
[
"*/vitest.config.{js,mjs,ts}",
"!e2e/vitest.config.mjs"
]

0 comments on commit 83469d8

Please sign in to comment.