mirror of
https://github.com/docker/metadata-action.git
synced 2026-03-04 07:58:16 +00:00
switch from jest to vitest
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {afterEach, beforeEach, describe, expect, test, it, jest} from '@jest/globals';
|
||||
import {afterEach, beforeEach, describe, expect, test, it, vi} from 'vitest';
|
||||
import * as dotenv from 'dotenv';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
@@ -7,14 +7,25 @@ import {Git} from '@docker/actions-toolkit/lib/git';
|
||||
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
||||
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
||||
|
||||
import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
|
||||
import * as context from '../src/context';
|
||||
|
||||
const toolkit = new Toolkit({githubToken: 'fake-github-token'});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
jest.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
|
||||
return new Context();
|
||||
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;
|
||||
});
|
||||
});
|
||||
|
||||
@@ -29,14 +40,14 @@ describe('getInputs', () => {
|
||||
});
|
||||
|
||||
// prettier-ignore
|
||||
test.each([
|
||||
const cases: [number, Map<string, string>, context.Inputs][] = [
|
||||
[
|
||||
0,
|
||||
new Map<string, string>([
|
||||
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
|
||||
]),
|
||||
{
|
||||
context: ContextSource.workflow,
|
||||
context: context.ContextSource.workflow,
|
||||
bakeTarget: 'docker-metadata-action',
|
||||
flavor: [],
|
||||
githubToken: '',
|
||||
@@ -47,7 +58,7 @@ describe('getInputs', () => {
|
||||
sepTags: '\n',
|
||||
sepAnnotations: '\n',
|
||||
tags: [],
|
||||
} as Inputs
|
||||
}
|
||||
],
|
||||
[
|
||||
1,
|
||||
@@ -59,7 +70,7 @@ describe('getInputs', () => {
|
||||
['sep-annotations', ',']
|
||||
]),
|
||||
{
|
||||
context: ContextSource.workflow,
|
||||
context: context.ContextSource.workflow,
|
||||
bakeTarget: 'metadata',
|
||||
flavor: [],
|
||||
githubToken: '',
|
||||
@@ -70,7 +81,7 @@ describe('getInputs', () => {
|
||||
sepTags: ',',
|
||||
sepAnnotations: ',',
|
||||
tags: [],
|
||||
} as Inputs
|
||||
}
|
||||
],
|
||||
[
|
||||
2,
|
||||
@@ -78,7 +89,7 @@ describe('getInputs', () => {
|
||||
['images', 'moby/buildkit\n#comment\nghcr.io/moby/mbuildkit'],
|
||||
]),
|
||||
{
|
||||
context: ContextSource.workflow,
|
||||
context: context.ContextSource.workflow,
|
||||
bakeTarget: 'docker-metadata-action',
|
||||
flavor: [],
|
||||
githubToken: '',
|
||||
@@ -89,23 +100,21 @@ describe('getInputs', () => {
|
||||
sepTags: '\n',
|
||||
sepAnnotations: '\n',
|
||||
tags: [],
|
||||
} as Inputs
|
||||
}
|
||||
],
|
||||
])(
|
||||
'[%d] given %p as inputs, returns %p',
|
||||
async (num: number, inputs: Map<string, string>, expected: Inputs) => {
|
||||
inputs.forEach((value: string, name: string) => {
|
||||
setInput(name, value);
|
||||
});
|
||||
expect(await getInputs()).toEqual(expected);
|
||||
}
|
||||
);
|
||||
];
|
||||
test.each(cases)('[%d] given %o as inputs, returns %o', async (num: number, inputs: Map<string, string>, expected: context.Inputs) => {
|
||||
inputs.forEach((value: string, name: string) => {
|
||||
setInput(name, value);
|
||||
});
|
||||
const res = await context.getInputs();
|
||||
expect(res).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getContext', () => {
|
||||
const originalEnv = process.env;
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
...dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')))
|
||||
@@ -116,26 +125,26 @@ describe('getContext', () => {
|
||||
});
|
||||
|
||||
it('workflow', async () => {
|
||||
const context = await getContext(ContextSource.workflow, toolkit);
|
||||
expect(context.ref).toEqual('refs/heads/dev');
|
||||
expect(context.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
|
||||
expect(context.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
|
||||
const ctx = await context.getContext(context.ContextSource.workflow, toolkit);
|
||||
expect(ctx.ref).toEqual('refs/heads/dev');
|
||||
expect(ctx.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
|
||||
expect(ctx.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
|
||||
});
|
||||
|
||||
it('git', async () => {
|
||||
jest.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
|
||||
vi.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
|
||||
return Promise.resolve({
|
||||
ref: 'refs/heads/git-test',
|
||||
sha: 'git-test-sha'
|
||||
} as Context);
|
||||
});
|
||||
jest.spyOn(Git, 'commitDate').mockImplementation(async (): Promise<Date> => {
|
||||
vi.spyOn(Git, 'commitDate').mockImplementation(async (): Promise<Date> => {
|
||||
return new Date('2023-01-01T13:42:28.000Z');
|
||||
});
|
||||
const context = await getContext(ContextSource.git, toolkit);
|
||||
expect(context.ref).toEqual('refs/heads/git-test');
|
||||
expect(context.sha).toEqual('git-test-sha');
|
||||
expect(context.commitDate).toEqual(new Date('2023-01-01T13:42:28.000Z'));
|
||||
const ctx = await context.getContext(context.ContextSource.git, toolkit);
|
||||
expect(ctx.ref).toEqual('refs/heads/git-test');
|
||||
expect(ctx.sha).toEqual('git-test-sha');
|
||||
expect(ctx.commitDate).toEqual(new Date('2023-01-01T13:42:28.000Z'));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user