mirror of
https://github.com/docker/metadata-action.git
synced 2026-03-04 15:58:19 +00:00
test: use github mock instead of fixture for context
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -1,34 +1,12 @@
|
|||||||
import {afterEach, beforeEach, describe, expect, test, it, vi} from 'vitest';
|
import {beforeEach, describe, expect, test, it, vi} from 'vitest';
|
||||||
import * as dotenv from 'dotenv';
|
|
||||||
import * as fs from 'fs';
|
|
||||||
import * as path from 'path';
|
|
||||||
import {Context} from '@actions/github/lib/context.js';
|
import {Context} from '@actions/github/lib/context.js';
|
||||||
import {Git} from '@docker/actions-toolkit/lib/git.js';
|
import {Git} from '@docker/actions-toolkit/lib/git.js';
|
||||||
import {GitHub} from '@docker/actions-toolkit/lib/github.js';
|
|
||||||
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';
|
||||||
|
|
||||||
import * as context from '../src/context.js';
|
import * as context from '../src/context.js';
|
||||||
|
|
||||||
const toolkit = new Toolkit({githubToken: 'fake-github-token'});
|
const toolkit = new Toolkit({githubToken: 'fake-github-token'});
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.clearAllMocks();
|
|
||||||
vi.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
|
|
||||||
const ctx = new Context();
|
|
||||||
const payload = ctx.payload as {
|
|
||||||
commits?: Array<{id: string; timestamp: string}>;
|
|
||||||
head_commit?: {id: string; timestamp: string};
|
|
||||||
};
|
|
||||||
if (ctx.sha && !payload.commits?.length && !payload.head_commit) {
|
|
||||||
payload.head_commit = {
|
|
||||||
id: ctx.sha,
|
|
||||||
timestamp: '2024-11-13T13:42:28.000Z'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return ctx;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('getInputs', () => {
|
describe('getInputs', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env = Object.keys(process.env).reduce((object, key) => {
|
process.env = Object.keys(process.env).reduce((object, key) => {
|
||||||
@@ -113,24 +91,12 @@ describe('getInputs', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('getContext', () => {
|
describe('getContext', () => {
|
||||||
const originalEnv = process.env;
|
|
||||||
beforeEach(() => {
|
|
||||||
process.env = {
|
|
||||||
...originalEnv,
|
|
||||||
...dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')))
|
|
||||||
};
|
|
||||||
});
|
|
||||||
afterEach(() => {
|
|
||||||
process.env = originalEnv;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('workflow', async () => {
|
it('workflow', async () => {
|
||||||
const ctx = await context.getContext(context.ContextSource.workflow, toolkit);
|
const ctx = await context.getContext(context.ContextSource.workflow, toolkit);
|
||||||
expect(ctx.ref).toEqual('refs/heads/dev');
|
expect(ctx.ref).toEqual('refs/heads/dev');
|
||||||
expect(ctx.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
|
expect(ctx.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
|
||||||
expect(ctx.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
|
expect(ctx.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('git', async () => {
|
it('git', async () => {
|
||||||
vi.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
|
vi.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
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';
|
||||||
@@ -12,13 +13,17 @@ process.env = Object.assign({}, process.env, {
|
|||||||
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
|
RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache')
|
||||||
});
|
});
|
||||||
|
|
||||||
vi.mock('@actions/github', () => ({
|
const require = createRequire(import.meta.url);
|
||||||
|
type RequireCacheEntry = NonNullable<(typeof require.cache)[string]>;
|
||||||
|
|
||||||
|
const githubMock = {
|
||||||
context: {
|
context: {
|
||||||
repo: {
|
repo: {
|
||||||
owner: 'docker',
|
owner: 'docker',
|
||||||
repo: 'actions-toolkit'
|
repo: 'actions-toolkit'
|
||||||
},
|
},
|
||||||
ref: 'refs/heads/master',
|
ref: 'refs/heads/dev',
|
||||||
|
sha: '5f3331d7f7044c18ca9f12c77d961c4d7cf3276a',
|
||||||
runId: 2188748038,
|
runId: 2188748038,
|
||||||
runNumber: 15,
|
runNumber: 15,
|
||||||
payload: {
|
payload: {
|
||||||
@@ -232,4 +237,23 @@ vi.mock('@actions/github', () => ({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}));
|
};
|
||||||
|
|
||||||
|
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