2023-04-17 04:42:21 +02:00
|
|
|
import {afterEach, beforeEach, describe, expect, test, it, jest} from '@jest/globals';
|
2022-12-15 14:46:34 +01:00
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2023-04-17 04:42:21 +02:00
|
|
|
import {Context} from '@actions/github/lib/context';
|
|
|
|
import {Git} from '@docker/actions-toolkit/lib/git';
|
|
|
|
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
|
|
|
|
|
|
|
import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
jest.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
|
|
|
|
return new Context();
|
|
|
|
});
|
|
|
|
});
|
2020-10-25 02:25:23 +01:00
|
|
|
|
2023-02-20 22:32:55 +01:00
|
|
|
describe('getInputs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
process.env = Object.keys(process.env).reduce((object, key) => {
|
|
|
|
if (!key.startsWith('INPUT_')) {
|
|
|
|
object[key] = process.env[key];
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}, {});
|
2020-12-23 22:09:38 +01:00
|
|
|
});
|
|
|
|
|
2023-02-20 22:32:55 +01:00
|
|
|
// prettier-ignore
|
|
|
|
test.each([
|
|
|
|
[
|
|
|
|
0,
|
|
|
|
new Map<string, string>([
|
|
|
|
['images', 'moby/buildkit\nghcr.io/moby/mbuildkit'],
|
|
|
|
]),
|
|
|
|
{
|
2022-12-15 14:46:34 +01:00
|
|
|
context: ContextSource.workflow,
|
2023-02-20 22:32:55 +01:00
|
|
|
bakeTarget: 'docker-metadata-action',
|
|
|
|
flavor: [],
|
|
|
|
githubToken: '',
|
|
|
|
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
|
|
|
|
labels: [],
|
2023-11-30 15:03:24 +01:00
|
|
|
annotations: [],
|
2023-02-20 22:32:55 +01:00
|
|
|
sepLabels: '\n',
|
|
|
|
sepTags: '\n',
|
2023-11-30 15:03:24 +01:00
|
|
|
sepAnnotations: '\n',
|
2023-02-20 22:32:55 +01:00
|
|
|
tags: [],
|
2022-12-15 14:46:34 +01:00
|
|
|
} as Inputs
|
2023-02-20 22:32:55 +01:00
|
|
|
],
|
|
|
|
[
|
|
|
|
1,
|
|
|
|
new Map<string, string>([
|
|
|
|
['bake-target', 'metadata'],
|
|
|
|
['images', 'moby/buildkit'],
|
|
|
|
['sep-labels', ','],
|
|
|
|
['sep-tags', ','],
|
2023-11-30 15:03:24 +01:00
|
|
|
['sep-annotations', ',']
|
2023-02-20 22:32:55 +01:00
|
|
|
]),
|
|
|
|
{
|
2022-12-15 14:46:34 +01:00
|
|
|
context: ContextSource.workflow,
|
2023-02-20 22:32:55 +01:00
|
|
|
bakeTarget: 'metadata',
|
|
|
|
flavor: [],
|
|
|
|
githubToken: '',
|
|
|
|
images: ['moby/buildkit'],
|
|
|
|
labels: [],
|
2023-11-30 15:03:24 +01:00
|
|
|
annotations: [],
|
2023-02-20 22:32:55 +01:00
|
|
|
sepLabels: ',',
|
|
|
|
sepTags: ',',
|
2023-11-30 15:03:24 +01:00
|
|
|
sepAnnotations: ',',
|
2023-02-20 22:32:55 +01:00
|
|
|
tags: [],
|
2022-12-15 14:46:34 +01:00
|
|
|
} as Inputs
|
2023-06-13 12:23:13 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
2,
|
|
|
|
new Map<string, string>([
|
|
|
|
['images', 'moby/buildkit\n#comment\nghcr.io/moby/mbuildkit'],
|
|
|
|
]),
|
|
|
|
{
|
|
|
|
context: ContextSource.workflow,
|
|
|
|
bakeTarget: 'docker-metadata-action',
|
|
|
|
flavor: [],
|
|
|
|
githubToken: '',
|
|
|
|
images: ['moby/buildkit', 'ghcr.io/moby/mbuildkit'],
|
|
|
|
labels: [],
|
2023-11-30 15:03:24 +01:00
|
|
|
annotations: [],
|
2023-06-13 12:23:13 +02:00
|
|
|
sepLabels: '\n',
|
|
|
|
sepTags: '\n',
|
2023-11-30 15:03:24 +01:00
|
|
|
sepAnnotations: '\n',
|
2023-06-13 12:23:13 +02:00
|
|
|
tags: [],
|
|
|
|
} as Inputs
|
|
|
|
],
|
2023-02-20 22:32:55 +01:00
|
|
|
])(
|
|
|
|
'[%d] given %p as inputs, returns %p',
|
2022-12-15 14:46:34 +01:00
|
|
|
async (num: number, inputs: Map<string, string>, expected: Inputs) => {
|
2023-02-20 22:32:55 +01:00
|
|
|
inputs.forEach((value: string, name: string) => {
|
|
|
|
setInput(name, value);
|
|
|
|
});
|
2022-12-15 14:46:34 +01:00
|
|
|
expect(await getInputs()).toEqual(expected);
|
2023-02-20 22:32:55 +01:00
|
|
|
}
|
|
|
|
);
|
2020-10-25 02:25:23 +01:00
|
|
|
});
|
|
|
|
|
2022-12-15 14:46:34 +01:00
|
|
|
describe('getContext', () => {
|
2023-04-17 04:42:21 +02:00
|
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
2022-12-15 14:46:34 +01:00
|
|
|
jest.resetModules();
|
2023-04-17 04:42:21 +02:00
|
|
|
process.env = {
|
|
|
|
...originalEnv,
|
|
|
|
...dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')))
|
|
|
|
};
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
process.env = originalEnv;
|
2022-12-15 14:46:34 +01:00
|
|
|
});
|
|
|
|
|
2023-04-17 04:42:21 +02:00
|
|
|
it('workflow', async () => {
|
|
|
|
const context = await getContext(ContextSource.workflow);
|
|
|
|
expect(context.ref).toEqual('refs/heads/dev');
|
|
|
|
expect(context.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
|
|
|
|
});
|
2022-12-15 14:46:34 +01:00
|
|
|
|
2023-04-17 04:42:21 +02:00
|
|
|
it('git', async () => {
|
|
|
|
jest.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
|
2022-12-15 14:46:34 +01:00
|
|
|
return Promise.resolve({
|
|
|
|
ref: 'refs/heads/git-test',
|
|
|
|
sha: 'git-test-sha'
|
|
|
|
} as Context);
|
|
|
|
});
|
2023-04-17 04:42:21 +02:00
|
|
|
const context = await getContext(ContextSource.git);
|
|
|
|
expect(context.ref).toEqual('refs/heads/git-test');
|
|
|
|
expect(context.sha).toEqual('git-test-sha');
|
2022-12-15 14:46:34 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-10-25 02:25:23 +01:00
|
|
|
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
|
|
|
|
function getInputName(name: string): string {
|
|
|
|
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setInput(name: string, value: string): void {
|
|
|
|
process.env[getInputName(name)] = value;
|
|
|
|
}
|