Skip to content

Commit 3ef11ca

Browse files
committed
add tests
Signed-off-by: CrazyMax <[email protected]>
1 parent df78eaf commit 3ef11ca

File tree

10 files changed

+2535
-51
lines changed

10 files changed

+2535
-51
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/coverage
12
/node_modules

.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"env": {
33
"node": true,
4-
"es2021": true
4+
"es2021": true,
5+
"jest": true
56
},
67
"extends": [
78
"eslint:recommended",
89
"plugin:@typescript-eslint/recommended",
10+
"plugin:jest/recommended",
911
"plugin:prettier/recommended"
1012
],
1113
"parser": "@typescript-eslint/parser",
@@ -15,6 +17,7 @@
1517
},
1618
"plugins": [
1719
"@typescript-eslint",
20+
"jest",
1821
"prettier"
1922
]
2023
}

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
/.dev
21
node_modules
32
lib
43

5-
# Jetbrains
6-
/.idea
7-
/*.iml
8-
94
# Rest of the file pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
105
# Logs
116
logs

__tests__/context.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {beforeEach, describe, expect, test} from '@jest/globals';
2+
3+
import * as context from '../src/context';
4+
5+
describe('getInputs', () => {
6+
beforeEach(() => {
7+
process.env = Object.keys(process.env).reduce((object, key) => {
8+
if (!key.startsWith('INPUT_')) {
9+
object[key] = process.env[key];
10+
}
11+
return object;
12+
}, {});
13+
});
14+
15+
// prettier-ignore
16+
test.each([
17+
[
18+
0,
19+
new Map<string, string>([]),
20+
{
21+
image: 'tonistiigi/binfmt:latest',
22+
platforms: 'all',
23+
} as context.Inputs
24+
],
25+
[
26+
1,
27+
new Map<string, string>([
28+
['image', 'docker/binfmt:latest'],
29+
['platforms', 'arm64,riscv64,arm'],
30+
]),
31+
{
32+
image: 'docker/binfmt:latest',
33+
platforms: 'arm64,riscv64,arm',
34+
} as context.Inputs
35+
],
36+
[
37+
2,
38+
new Map<string, string>([
39+
['platforms', 'arm64, riscv64, arm '],
40+
]),
41+
{
42+
image: 'tonistiigi/binfmt:latest',
43+
platforms: 'arm64,riscv64,arm',
44+
} as context.Inputs
45+
]
46+
])(
47+
'[%d] given %p as inputs, returns %p',
48+
async (num: number, inputs: Map<string, string>, expected: context.Inputs) => {
49+
inputs.forEach((value: string, name: string) => {
50+
setInput(name, value);
51+
});
52+
const res = await context.getInputs();
53+
expect(res).toEqual(expected);
54+
}
55+
);
56+
});
57+
58+
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
59+
function getInputName(name: string): string {
60+
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
61+
}
62+
63+
function setInput(name: string, value: string): void {
64+
process.env[getInputName(name)] = value;
65+
}

dev.Dockerfile

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ COPY --from=deps /vendor /
1616

1717
FROM deps AS vendor-validate
1818
RUN --mount=type=bind,target=.,rw <<EOT
19-
set -e
20-
git add -A
21-
cp -rf /vendor/* .
22-
if [ -n "$(git status --porcelain -- yarn.lock)" ]; then
23-
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor-update"'
24-
git status --porcelain -- yarn.lock
25-
exit 1
26-
fi
19+
set -e
20+
git add -A
21+
cp -rf /vendor/* .
22+
if [ -n "$(git status --porcelain -- yarn.lock)" ]; then
23+
echo >&2 'ERROR: Vendor result differs. Please vendor your package with "docker buildx bake vendor-update"'
24+
git status --porcelain -- yarn.lock
25+
exit 1
26+
fi
2727
EOT
2828

2929
FROM deps AS build
@@ -36,14 +36,14 @@ COPY --from=build /out /
3636

3737
FROM build AS build-validate
3838
RUN --mount=type=bind,target=.,rw <<EOT
39-
set -e
40-
git add -A
41-
cp -rf /out/* .
42-
if [ -n "$(git status --porcelain -- dist)" ]; then
43-
echo >&2 'ERROR: Build result differs. Please build first with "docker buildx bake build"'
44-
git status --porcelain -- dist
45-
exit 1
46-
fi
39+
set -e
40+
git add -A
41+
cp -rf /out/* .
42+
if [ -n "$(git status --porcelain -- dist)" ]; then
43+
echo >&2 'ERROR: Build result differs. Please build first with "docker buildx bake build"'
44+
git status --porcelain -- dist
45+
exit 1
46+
fi
4747
EOT
4848

4949
FROM deps AS format
@@ -59,3 +59,11 @@ FROM deps AS lint
5959
RUN --mount=type=bind,target=.,rw \
6060
--mount=type=cache,target=/src/node_modules \
6161
yarn run lint
62+
63+
FROM deps AS test
64+
RUN --mount=type=bind,target=.,rw \
65+
--mount=type=cache,target=/src/node_modules \
66+
yarn run test --coverageDirectory=/tmp/coverage
67+
68+
FROM scratch AS test-coverage
69+
COPY --from=test /tmp/coverage /

docker-bake.hcl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ target "vendor-validate" {
4545
target = "vendor-validate"
4646
output = ["type=cacheonly"]
4747
}
48+
49+
target "test" {
50+
dockerfile = "dev.Dockerfile"
51+
target = "test-coverage"
52+
output = ["./coverage"]
53+
}

jest.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import fs from 'fs';
2+
import os from 'os';
3+
import path from 'path';
4+
5+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-qemu-action-'));
6+
7+
process.env = Object.assign({}, process.env, {
8+
TEMP: tmpDir,
9+
GITHUB_REPOSITORY: 'docker/setup-qemu-action',
10+
RUNNER_TEMP: path.join(tmpDir, 'runner-temp'),
11+
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
12+
}) as {
13+
[key: string]: string;
14+
};
15+
16+
module.exports = {
17+
clearMocks: true,
18+
moduleFileExtensions: ['js', 'ts'],
19+
testMatch: ['**/*.test.ts'],
20+
transform: {
21+
'^.+\\.ts$': 'ts-jest'
22+
},
23+
moduleNameMapper: {
24+
'^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs'
25+
},
26+
collectCoverageFrom: ['src/**/{!(main.ts),}.ts'],
27+
coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__mocks__/', '__tests__/'],
28+
verbose: true
29+
};

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"main": "lib/main.js",
55
"scripts": {
66
"build": "ncc build src/main.ts --source-map --minify --license licenses.txt",
7-
"lint": "eslint src/**/*.ts",
8-
"format": "eslint --fix src/**/*.ts",
9-
"all": "yarn run build && yarn run format"
7+
"lint": "eslint src/**/*.ts __tests__/**/*.ts",
8+
"format": "eslint --fix src/**/*.ts __tests__/**/*.ts",
9+
"test": "jest --coverage",
10+
"all": "yarn run build && yarn run format && yarn test"
1011
},
1112
"repository": {
1213
"type": "git",
@@ -36,8 +37,11 @@
3637
"@vercel/ncc": "^0.33.3",
3738
"eslint": "^8.11.0",
3839
"eslint-config-prettier": "^8.5.0",
40+
"eslint-plugin-jest": "^26.1.1",
3941
"eslint-plugin-prettier": "^4.0.0",
42+
"jest": "^27.2.5",
4043
"prettier": "^2.3.1",
44+
"ts-jest": "^27.1.2",
4145
"ts-node": "^10.7.0",
4246
"typescript": "^4.4.4"
4347
}

tsconfig.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
{
22
"compilerOptions": {
3+
"esModuleInterop": true,
34
"target": "es6",
45
"module": "commonjs",
6+
"strict": true,
57
"newLine": "lf",
68
"outDir": "./lib",
79
"rootDir": "./src",
8-
"esModuleInterop": true,
910
"forceConsistentCasingInFileNames": true,
10-
"strict": true,
1111
"noImplicitAny": false,
12+
"resolveJsonModule": true,
1213
"useUnknownInCatchVariables": false,
1314
},
1415
"exclude": [
15-
"node_modules"
16+
"./__tests__/**/*",
17+
"./lib/**/*",
18+
"node_modules",
19+
"jest.config.ts"
1620
]
1721
}

0 commit comments

Comments
 (0)