Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
"arrowParens": "avoid"
}
223 changes: 0 additions & 223 deletions __mocks__/@actions/github.ts

This file was deleted.

85 changes: 30 additions & 55 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import {afterEach, beforeEach, describe, expect, test, it, jest} from '@jest/globals';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import {Context} from '@actions/github/lib/context';
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 {beforeEach, describe, expect, test, it, vi} from 'vitest';
import {Context} from '@actions/github/lib/context.js';
import {Git} from '@docker/actions-toolkit/lib/git.js';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit.js';

import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
import * as context from '../src/context.js';

const toolkit = new Toolkit({githubToken: 'fake-github-token'});

beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
return new Context();
});
});

describe('getInputs', () => {
beforeEach(() => {
process.env = Object.keys(process.env).reduce((object, key) => {
Expand All @@ -29,14 +18,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: '',
Expand All @@ -47,7 +36,7 @@ describe('getInputs', () => {
sepTags: '\n',
sepAnnotations: '\n',
tags: [],
} as Inputs
}
],
[
1,
Expand All @@ -59,7 +48,7 @@ describe('getInputs', () => {
['sep-annotations', ',']
]),
{
context: ContextSource.workflow,
context: context.ContextSource.workflow,
bakeTarget: 'metadata',
flavor: [],
githubToken: '',
Expand All @@ -70,15 +59,15 @@ describe('getInputs', () => {
sepTags: ',',
sepAnnotations: ',',
tags: [],
} as Inputs
}
],
[
2,
new Map<string, string>([
['images', 'moby/buildkit\n#comment\nghcr.io/moby/mbuildkit'],
]),
{
context: ContextSource.workflow,
context: context.ContextSource.workflow,
bakeTarget: 'docker-metadata-action',
flavor: [],
githubToken: '',
Expand All @@ -89,53 +78,39 @@ 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')))
};
});
afterEach(() => {
process.env = originalEnv;
});

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'));
});
});

Expand Down
Loading
Loading