fix tests implementation

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-04-17 04:42:21 +02:00
parent d8f57c18fa
commit 5076359ba6
5 changed files with 255 additions and 50 deletions

View File

@ -1,9 +1,19 @@
import {Context} from '@actions/github/lib/context';
import {beforeEach, describe, expect, test, it, jest} from '@jest/globals';
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 {ContextSource, getInputs, Inputs} from '../src/context';
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();
});
});
describe('getInputs', () => {
beforeEach(() => {
@ -66,37 +76,34 @@ describe('getInputs', () => {
});
describe('getContext', () => {
it('get context with workflow', async () => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')));
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {getContext} = require('../src/context');
const contextResult = await getContext(ContextSource.workflow);
expect(contextResult.ref).toEqual('refs/heads/dev');
expect(contextResult.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
process.env = {
...originalEnv,
...dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures/event_create_branch.env')))
};
});
afterEach(() => {
process.env = originalEnv;
});
it('get context with git', async () => {
jest.resetModules();
it('workflow', async () => {
const context = await getContext(ContextSource.workflow);
expect(context.ref).toEqual('refs/heads/dev');
expect(context.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const git = require('@docker/actions-toolkit/lib/git');
jest.spyOn(git.Git, 'context').mockImplementation((): Promise<Context> => {
it('git', async () => {
jest.spyOn(Git, 'context').mockImplementation((): Promise<Context> => {
return Promise.resolve({
ref: 'refs/heads/git-test',
sha: 'git-test-sha'
} as Context);
});
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {getContext} = require('../src/context');
const contextResult = await getContext(ContextSource.git);
expect(contextResult.ref).toEqual('refs/heads/git-test');
expect(contextResult.sha).toEqual('git-test-sha');
const context = await getContext(ContextSource.git);
expect(context.ref).toEqual('refs/heads/git-test');
expect(context.sha).toEqual('git-test-sha');
});
});

View File

@ -87,7 +87,7 @@ describe('transform', () => {
[
[`name/foo,name=name/bar,enable=true`], undefined, true
]
])('given %p', async (l: string[], expected: Image[], invalid: boolean) => {
])('given %p', async (l: string[], expected: Image[] | undefined, invalid: boolean) => {
try {
const images = Transform(l);
expect(images).toEqual(expected);

View File

@ -5,10 +5,13 @@ import * as dotenv from 'dotenv';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github';
import {ContextSource, getInputs, Inputs} from '../src/context';
import {Context} from '@actions/github/lib/context';
import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
import {Meta, Version} from '../src/meta';
import repoFixture from './fixtures/repo.json';
jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
return <Promise<GitHubRepo>>(repoFixture as unknown);
});
@ -21,17 +24,6 @@ jest.mock('moment-timezone', () => {
return () => (jest.requireActual('moment-timezone') as typeof import('moment-timezone'))('2020-01-10T00:30:00.000Z');
});
/**
* Get a workflow context based on the current environment variables.
*/
const getFreshWorkflowContext = async () => {
jest.resetModules();
// eslint-disable-next-line @typescript-eslint/no-var-requires
const {getContext} = require('../src/context');
const context = await getContext(ContextSource.workflow);
return context;
};
beforeEach(() => {
jest.clearAllMocks();
Object.keys(process.env).forEach(function (key) {
@ -39,6 +31,9 @@ beforeEach(() => {
delete process.env[key];
}
});
jest.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
return new Context();
});
});
describe('isRawStatement', () => {
@ -58,8 +53,7 @@ const tagsLabelsTest = async (name: string, envFile: string, inputs: Inputs, exV
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const version = meta.version;
expect(version).toEqual(exVersion);
@ -2776,8 +2770,7 @@ describe('pr-head-sha', () => {
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const version = meta.version;
expect(version).toEqual(exVersion);
@ -3719,8 +3712,7 @@ describe('json', () => {
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const jsonOutput = meta.getJSON();
expect(jsonOutput).toEqual(exJSON);
@ -4026,8 +4018,7 @@ describe('bake', () => {
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const bakeFile = meta.getBakeFile();
expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition);
@ -4069,13 +4060,12 @@ describe('sepTags', () => {
"user/app:dev,user/app:my,user/app:custom,user/app:tags"
]
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, expTags: string) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const context = await getFreshWorkflowContext();
const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
expect(meta.getTags().join(inputs.sepTags)).toEqual(expTags);
});