move GitHub specific to its own class
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -3,22 +3,18 @@ import path from 'path';
|
||||
import rimraf from 'rimraf';
|
||||
import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
|
||||
|
||||
import {Context, ReposGetResponseData} from '../src/context';
|
||||
import {Context} from '../src/context';
|
||||
|
||||
const tmpDir = path.join('/tmp/.docker-actions-toolkit-jest').split(path.sep).join(path.posix.sep);
|
||||
const tmpName = path.join(tmpDir, '.tmpname-jest').split(path.sep).join(path.posix.sep);
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
jest.spyOn(Context.prototype as any, 'tmpDir').mockImplementation((): string => {
|
||||
jest.spyOn(Context.prototype, 'tmpDir').mockImplementation((): string => {
|
||||
if (!fs.existsSync(tmpDir)) {
|
||||
fs.mkdirSync(tmpDir, {recursive: true});
|
||||
}
|
||||
return tmpDir;
|
||||
});
|
||||
jest.spyOn(Context.prototype as any, 'tmpName').mockImplementation((): string => {
|
||||
jest.spyOn(Context.prototype, 'tmpName').mockImplementation((): string => {
|
||||
return tmpName;
|
||||
});
|
||||
|
||||
@@ -30,34 +26,6 @@ afterEach(() => {
|
||||
rimraf.sync(tmpDir);
|
||||
});
|
||||
|
||||
import * as repoFixture from './fixtures/repo.json';
|
||||
jest.spyOn(Context.prototype as any, 'repoData').mockImplementation((): Promise<ReposGetResponseData> => {
|
||||
return <Promise<ReposGetResponseData>>(repoFixture as unknown);
|
||||
});
|
||||
|
||||
describe('serverURL', () => {
|
||||
const originalEnv = process.env;
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
GITHUB_SERVER_URL: 'https://foo.github.com'
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
it('returns default', async () => {
|
||||
process.env.GITHUB_SERVER_URL = '';
|
||||
const context = new Context();
|
||||
expect(context.serverURL).toEqual('https://github.com');
|
||||
});
|
||||
it('returns from env', async () => {
|
||||
const context = new Context();
|
||||
expect(context.serverURL).toEqual('https://foo.github.com');
|
||||
});
|
||||
});
|
||||
|
||||
describe('gitContext', () => {
|
||||
it('returns refs/heads/master', async () => {
|
||||
const context = new Context();
|
||||
@@ -71,17 +39,3 @@ describe('provenanceBuilderID', () => {
|
||||
expect(context.provenanceBuilderID).toEqual('https://github.com/docker/actions-toolkit/actions/runs/123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('repo', () => {
|
||||
it('returns GitHub repository', async () => {
|
||||
const context = new Context();
|
||||
expect((await context.repoData()).name).toEqual('Hello-World');
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromPayload', () => {
|
||||
it('returns repository name from payload', async () => {
|
||||
const context = new Context();
|
||||
expect(await context.fromPayload('repository.name')).toEqual('test-docker-action');
|
||||
});
|
||||
});
|
||||
|
||||
53
__tests__/github.test.ts
Normal file
53
__tests__/github.test.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import {describe, expect, jest, it, beforeEach, afterEach} from '@jest/globals';
|
||||
|
||||
import {GitHub, GitHubRepo} from '../src/github';
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
import * as repoFixture from './fixtures/repo.json';
|
||||
jest.spyOn(GitHub.prototype, 'repoData').mockImplementation((): Promise<GitHubRepo> => {
|
||||
return <Promise<GitHubRepo>>(repoFixture as unknown);
|
||||
});
|
||||
|
||||
describe('context', () => {
|
||||
it('returns repository name from payload', async () => {
|
||||
const github = new GitHub();
|
||||
expect(github.context.payload.repository?.name).toEqual('test-docker-action');
|
||||
});
|
||||
it('is repository private', async () => {
|
||||
const github = new GitHub();
|
||||
expect(github.context.payload.repository?.private).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('serverURL', () => {
|
||||
const originalEnv = process.env;
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env = {
|
||||
...originalEnv,
|
||||
GITHUB_SERVER_URL: 'https://foo.github.com'
|
||||
};
|
||||
});
|
||||
afterEach(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
it('returns default', async () => {
|
||||
process.env.GITHUB_SERVER_URL = '';
|
||||
const github = new GitHub();
|
||||
expect(github.serverURL).toEqual('https://github.com');
|
||||
});
|
||||
it('returns from env', async () => {
|
||||
const github = new GitHub();
|
||||
expect(github.serverURL).toEqual('https://foo.github.com');
|
||||
});
|
||||
});
|
||||
|
||||
describe('repoData', () => {
|
||||
it('returns GitHub repository', async () => {
|
||||
const github = new GitHub();
|
||||
expect((await github.repoData()).name).toEqual('Hello-World');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user