mirror of
https://github.com/docker/metadata-action.git
synced 2026-03-04 15:58:19 +00:00
test: stabilize github mock setup since ESM
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -1,32 +1,11 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import {createRequire} from 'node:module';
|
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import {vi} from 'vitest';
|
import {vi} from 'vitest';
|
||||||
|
|
||||||
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-metadata-action-'));
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-metadata-action-'));
|
||||||
|
|
||||||
process.env = Object.assign({}, process.env, {
|
const githubPayload = {
|
||||||
TEMP: tmpDir,
|
|
||||||
GITHUB_REPOSITORY: 'docker/metadata-action',
|
|
||||||
RUNNER_TEMP: path.join(tmpDir, 'runner-temp'),
|
|
||||||
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
|
|
||||||
});
|
|
||||||
|
|
||||||
const require = createRequire(import.meta.url);
|
|
||||||
type RequireCacheEntry = NonNullable<(typeof require.cache)[string]>;
|
|
||||||
|
|
||||||
const githubMock = {
|
|
||||||
context: {
|
|
||||||
repo: {
|
|
||||||
owner: 'docker',
|
|
||||||
repo: 'actions-toolkit'
|
|
||||||
},
|
|
||||||
ref: 'refs/heads/dev',
|
|
||||||
sha: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
|
||||||
runId: 2188748038,
|
|
||||||
runNumber: 15,
|
|
||||||
payload: {
|
|
||||||
after: '860c1904a1ce19322e91ac35af1ab07466440c37',
|
after: '860c1904a1ce19322e91ac35af1ab07466440c37',
|
||||||
base_ref: null,
|
base_ref: null,
|
||||||
before: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
before: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
||||||
@@ -43,11 +22,11 @@ const githubMock = {
|
|||||||
username: 'crazy-max'
|
username: 'crazy-max'
|
||||||
},
|
},
|
||||||
distinct: true,
|
distinct: true,
|
||||||
id: '860c1904a1ce19322e91ac35af1ab07466440c37',
|
id: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
||||||
message: 'hello dev',
|
message: 'hello dev',
|
||||||
timestamp: '2022-04-19T11:27:24+02:00',
|
timestamp: '2024-11-13T13:42:28Z',
|
||||||
tree_id: 'd2c60af597e863787d2d27f569e30495b0b92820',
|
tree_id: 'd2c60af597e863787d2d27f569e30495b0b92820',
|
||||||
url: 'https://github.com/docker/test-docker-action/commit/860c1904a1ce19322e91ac35af1ab07466440c37'
|
url: 'https://github.com/docker/test-docker-action/commit/5f3331d7f7044c18ca9f12c77d961c4d7cf3276a'
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
compare: 'https://github.com/docker/test-docker-action/compare/5f3331d7f704...860c1904a1ce',
|
compare: 'https://github.com/docker/test-docker-action/compare/5f3331d7f704...860c1904a1ce',
|
||||||
@@ -66,11 +45,11 @@ const githubMock = {
|
|||||||
username: 'crazy-max'
|
username: 'crazy-max'
|
||||||
},
|
},
|
||||||
distinct: true,
|
distinct: true,
|
||||||
id: '860c1904a1ce19322e91ac35af1ab07466440c37',
|
id: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
||||||
message: 'hello dev',
|
message: 'hello dev',
|
||||||
timestamp: '2022-04-19T11:27:24+02:00',
|
timestamp: '2024-11-13T13:42:28Z',
|
||||||
tree_id: 'd2c60af597e863787d2d27f569e30495b0b92820',
|
tree_id: 'd2c60af597e863787d2d27f569e30495b0b92820',
|
||||||
url: 'https://github.com/docker/test-docker-action/commit/860c1904a1ce19322e91ac35af1ab07466440c37'
|
url: 'https://github.com/docker/test-docker-action/commit/5f3331d7f7044c18ca9f12c77d961c4d7cf3276a'
|
||||||
},
|
},
|
||||||
organization: {
|
organization: {
|
||||||
avatar_url: 'https://avatars.githubusercontent.com/u/5429470?v=4',
|
avatar_url: 'https://avatars.githubusercontent.com/u/5429470?v=4',
|
||||||
@@ -220,12 +199,26 @@ const githubMock = {
|
|||||||
type: 'User',
|
type: 'User',
|
||||||
url: 'https://api.github.com/users/crazy-max'
|
url: 'https://api.github.com/users/crazy-max'
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
},
|
|
||||||
getOctokit: () => ({
|
const githubEventPath = path.join(tmpDir, 'github-event.json');
|
||||||
rest: {
|
fs.writeFileSync(githubEventPath, JSON.stringify(githubPayload));
|
||||||
repos: {
|
|
||||||
getCommit: async () => ({
|
process.env = Object.assign({}, process.env, {
|
||||||
|
TEMP: tmpDir,
|
||||||
|
GITHUB_REPOSITORY: 'docker/metadata-action',
|
||||||
|
GITHUB_REF: 'refs/heads/dev',
|
||||||
|
GITHUB_RUN_ID: '2188748038',
|
||||||
|
GITHUB_RUN_ATTEMPT: '1',
|
||||||
|
GITHUB_RUN_NUMBER: '15',
|
||||||
|
GITHUB_SHA: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
||||||
|
GITHUB_EVENT_PATH: githubEventPath,
|
||||||
|
RUNNER_TEMP: path.join(tmpDir, 'runner-temp'),
|
||||||
|
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
|
||||||
|
});
|
||||||
|
|
||||||
|
const getCommitMock = vi.hoisted(() =>
|
||||||
|
vi.fn(async () => ({
|
||||||
data: {
|
data: {
|
||||||
commit: {
|
commit: {
|
||||||
committer: {
|
committer: {
|
||||||
@@ -233,27 +226,27 @@ const githubMock = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
const getOctokitMock = vi.hoisted(() =>
|
||||||
|
vi.fn(() => ({
|
||||||
|
rest: {
|
||||||
|
repos: {
|
||||||
|
getCommit: getCommitMock
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
vi.mock('@actions/github', async importOriginal => {
|
||||||
|
const actual = await importOriginal<typeof import('@actions/github')>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
context: {
|
||||||
|
...actual.context,
|
||||||
|
payload: githubPayload
|
||||||
|
},
|
||||||
|
getOctokit: getOctokitMock
|
||||||
};
|
};
|
||||||
|
});
|
||||||
vi.mock('@actions/github', () => githubMock);
|
|
||||||
|
|
||||||
for (const mod of ['@actions/github', '@docker/actions-toolkit/node_modules/@actions/github']) {
|
|
||||||
try {
|
|
||||||
const resolved = require.resolve(mod);
|
|
||||||
vi.doMock(resolved, () => githubMock);
|
|
||||||
require.cache[resolved] = {
|
|
||||||
id: resolved,
|
|
||||||
filename: resolved,
|
|
||||||
loaded: true,
|
|
||||||
exports: githubMock,
|
|
||||||
children: [],
|
|
||||||
paths: []
|
|
||||||
} as RequireCacheEntry;
|
|
||||||
} catch {
|
|
||||||
// Ignore unresolved optional paths; vi.mock handles module-level mocking.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user