Files
labeler/__tests__/main.test.ts
T

194 lines
5.2 KiB
TypeScript
Raw Normal View History

import {run} from '../src/labeler';
import * as github from '@actions/github';
import * as core from '@actions/core';
2021-06-04 14:46:22 -04:00
const fs = jest.requireActual('fs');
2021-06-04 14:46:22 -04:00
jest.mock('@actions/core');
jest.mock('@actions/github');
2021-06-04 14:46:22 -04:00
const gh = github.getOctokit('_');
const setLabelsMock = jest.spyOn(gh.rest.issues, 'setLabels');
const reposMock = jest.spyOn(gh.rest.repos, 'getContent');
const paginateMock = jest.spyOn(gh, 'paginate');
const getPullMock = jest.spyOn(gh.rest.pulls, 'get');
const coreWarningMock = jest.spyOn(core, 'warning');
2021-06-04 14:46:22 -04:00
const yamlFixtures = {
'only_pdfs.yml': fs.readFileSync('__tests__/fixtures/only_pdfs.yml')
2021-06-04 14:46:22 -04:00
};
2022-02-04 13:26:54 +00:00
const configureInput = (
mockInput: Partial<{
'repo-token': string;
'configuration-path': string;
'sync-labels': boolean;
2023-05-31 01:50:00 +01:00
dot: boolean;
2022-02-04 13:26:54 +00:00
}>
) => {
jest
.spyOn(core, 'getInput')
2022-02-04 13:26:54 +00:00
.mockImplementation((name: string, ...opts) => mockInput[name]);
2023-05-31 18:46:23 +01:00
jest
.spyOn(core, 'getBooleanInput')
.mockImplementation((name: string, ...opts) => mockInput[name]);
2022-02-04 13:26:54 +00:00
};
2021-06-04 14:46:22 -04:00
afterAll(() => jest.restoreAllMocks());
describe('run', () => {
it('(with dot: false) adds labels to PRs that match our glob patterns', async () => {
2022-02-04 13:26:54 +00:00
configureInput({});
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.pdf');
getPullMock.mockResolvedValue(<any>{
data: {
labels: []
}
});
2021-06-04 14:46:22 -04:00
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(1);
expect(setLabelsMock).toHaveBeenCalledWith({
owner: 'monalisa',
repo: 'helloworld',
2021-06-04 14:46:22 -04:00
issue_number: 123,
labels: ['touched-a-pdf-file']
2021-06-04 14:46:22 -04:00
});
});
it('(with dot: true) adds labels to PRs that match our glob patterns', async () => {
2023-05-31 01:50:00 +01:00
configureInput({dot: true});
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('.foo.pdf');
getPullMock.mockResolvedValue(<any>{
data: {
labels: []
}
});
2022-02-04 13:26:54 +00:00
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(1);
expect(setLabelsMock).toHaveBeenCalledWith({
owner: 'monalisa',
repo: 'helloworld',
2022-02-04 13:26:54 +00:00
issue_number: 123,
labels: ['touched-a-pdf-file']
2022-02-04 13:26:54 +00:00
});
});
it('(with dot: false) does not add labels to PRs that do not match our glob patterns', async () => {
2022-02-04 13:26:54 +00:00
configureInput({});
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('.foo.pdf');
getPullMock.mockResolvedValue(<any>{
data: {
labels: []
}
});
2022-02-04 13:26:54 +00:00
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(0);
2022-02-04 13:26:54 +00:00
});
it('(with dot: true) does not add labels to PRs that do not match our glob patterns', async () => {
2023-05-31 01:50:00 +01:00
configureInput({dot: true});
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.txt');
2021-06-04 14:46:22 -04:00
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(0);
2021-06-04 14:46:22 -04:00
});
it('(with sync-labels: true) it deletes preexisting PR labels that no longer match the glob pattern', async () => {
2023-03-27 09:44:05 +01:00
configureInput({
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': true
2023-03-27 09:44:05 +01:00
});
2021-06-04 14:46:22 -04:00
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.txt');
2021-06-04 14:46:22 -04:00
getPullMock.mockResolvedValue(<any>{
data: {
labels: [{name: 'touched-a-pdf-file'}, {name: 'manually-added'}]
}
2021-06-04 14:46:22 -04:00
});
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(1);
expect(setLabelsMock).toHaveBeenCalledWith({
owner: 'monalisa',
repo: 'helloworld',
2021-06-04 14:46:22 -04:00
issue_number: 123,
labels: ['manually-added']
2021-06-04 14:46:22 -04:00
});
});
it('(with sync-labels: false) it issues no delete calls even when there are preexisting PR labels that no longer match the glob pattern', async () => {
2023-03-27 09:44:05 +01:00
configureInput({
'repo-token': 'foo',
'configuration-path': 'bar',
2023-05-31 01:45:27 +01:00
'sync-labels': false
2023-03-27 09:44:05 +01:00
});
2021-06-04 14:46:22 -04:00
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.txt');
2021-06-04 14:46:22 -04:00
getPullMock.mockResolvedValue(<any>{
data: {
labels: [{name: 'touched-a-pdf-file'}, {name: 'manually-added'}]
}
2021-06-04 14:46:22 -04:00
});
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(0);
});
it('(with sync-labels: false) it only logs the excess labels', async () => {
configureInput({
'repo-token': 'foo',
'configuration-path': 'bar',
'sync-labels': false
});
usingLabelerConfigYaml('only_pdfs.yml');
mockGitHubResponseChangedFiles('foo.pdf');
const existingLabels = Array.from({length: 100}).map((_, idx) => ({
name: `existing-label-${idx}`
}));
getPullMock.mockResolvedValue(<any>{
data: {
labels: existingLabels
}
});
await run();
expect(setLabelsMock).toHaveBeenCalledTimes(0);
expect(coreWarningMock).toHaveBeenCalledTimes(1);
expect(coreWarningMock).toHaveBeenCalledWith(
'Maximum of 100 labels allowed. Excess labels: touched-a-pdf-file',
{title: 'Label limit for a PR exceeded'}
);
2021-06-04 14:46:22 -04:00
});
});
function usingLabelerConfigYaml(fixtureName: keyof typeof yamlFixtures): void {
reposMock.mockResolvedValue(<any>{
data: {content: yamlFixtures[fixtureName], encoding: 'utf8'}
2021-06-04 14:46:22 -04:00
});
}
function mockGitHubResponseChangedFiles(...files: string[]): void {
const returnValue = files.map(f => ({filename: f}));
2021-06-04 14:46:22 -04:00
paginateMock.mockReturnValue(<any>returnValue);
}