mirror of
https://github.com/actions/labeler.git
synced 2025-12-13 13:07:24 +00:00
Add some new tests
This commit is contained in:
@@ -63,72 +63,13 @@ describe('toChangedFilesMatchConfig', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe(`when there is a 'changed-files' key in the config`, () => {
|
describe(`when there is a 'changed-files' key in the config`, () => {
|
||||||
describe(`and both 'any' and 'all' keys are present`, () => {
|
describe('and the value is an array of strings', () => {
|
||||||
const config = {'changed-files': [{all: 'testing'}, {any: 'testing'}]};
|
const config = {'changed-files': ['testing']};
|
||||||
|
|
||||||
it('sets both values in the config object', () => {
|
|
||||||
const result = toChangedFilesMatchConfig(config);
|
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
|
||||||
changedFiles: {
|
|
||||||
any: ['testing'],
|
|
||||||
all: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe(`and it contains a 'all' key`, () => {
|
|
||||||
describe('with a value of a string', () => {
|
|
||||||
const config = {'changed-files': [{all: 'testing'}]};
|
|
||||||
|
|
||||||
it('sets the value to be an array of strings in the config object', () => {
|
|
||||||
const result = toChangedFilesMatchConfig(config);
|
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
|
||||||
changedFiles: {
|
|
||||||
all: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('with a value of an array of strings', () => {
|
|
||||||
const config = {'changed-files': [{all: ['testing']}]};
|
|
||||||
|
|
||||||
it('sets the value in the config object', () => {
|
it('sets the value in the config object', () => {
|
||||||
const result = toChangedFilesMatchConfig(config);
|
const result = toChangedFilesMatchConfig(config);
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
expect(result).toEqual<ChangedFilesMatchConfig>({
|
||||||
changedFiles: {
|
changedFiles: ['testing']
|
||||||
all: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe(`and it contains a 'any' key`, () => {
|
|
||||||
describe('with a value of a string', () => {
|
|
||||||
const config = {'changed-files': [{any: 'testing'}]};
|
|
||||||
|
|
||||||
it('sets the value to be an array of strings on the config object', () => {
|
|
||||||
const result = toChangedFilesMatchConfig(config);
|
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
|
||||||
changedFiles: {
|
|
||||||
any: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('with a value of an array of strings', () => {
|
|
||||||
const config = {'changed-files': [{any: ['testing']}]};
|
|
||||||
|
|
||||||
it('sets the value in the config object', () => {
|
|
||||||
const result = toChangedFilesMatchConfig(config);
|
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
|
||||||
changedFiles: {
|
|
||||||
any: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -136,31 +77,16 @@ describe('toChangedFilesMatchConfig', () => {
|
|||||||
describe('and the value is a string', () => {
|
describe('and the value is a string', () => {
|
||||||
const config = {'changed-files': 'testing'};
|
const config = {'changed-files': 'testing'};
|
||||||
|
|
||||||
it(`sets the string as an array under an 'any' key`, () => {
|
it(`sets the string as an array in the config object`, () => {
|
||||||
const result = toChangedFilesMatchConfig(config);
|
const result = toChangedFilesMatchConfig(config);
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
expect(result).toEqual<ChangedFilesMatchConfig>({
|
||||||
changedFiles: {
|
changedFiles: ['testing']
|
||||||
any: ['testing']
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('and the value is an array of strings', () => {
|
describe('but the value is an empty string', () => {
|
||||||
const config = {'changed-files': ['testing']};
|
const config = {'changed-files': ''};
|
||||||
|
|
||||||
it(`sets the array under an 'any' key`, () => {
|
|
||||||
const result = toChangedFilesMatchConfig(config);
|
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
|
||||||
changedFiles: {
|
|
||||||
any: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('but it has empty values', () => {
|
|
||||||
const config = {'changed-files': []};
|
|
||||||
|
|
||||||
it(`returns an empty object`, () => {
|
it(`returns an empty object`, () => {
|
||||||
const result = toChangedFilesMatchConfig(config);
|
const result = toChangedFilesMatchConfig(config);
|
||||||
@@ -168,16 +94,12 @@ describe('toChangedFilesMatchConfig', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('and there is an unknown value in the config', () => {
|
describe('but the value is an empty array', () => {
|
||||||
const config = {'changed-files': [{any: 'testing'}, {sneaky: 'test'}]};
|
const config = {'changed-files': []};
|
||||||
|
|
||||||
it(`does not set the value in the match config`, () => {
|
it(`returns an empty object`, () => {
|
||||||
const result = toChangedFilesMatchConfig(config);
|
const result = toChangedFilesMatchConfig(config);
|
||||||
expect(result).toEqual<ChangedFilesMatchConfig>({
|
expect(result).toEqual<ChangedFilesMatchConfig>({});
|
||||||
changedFiles: {
|
|
||||||
any: ['testing']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
14
__tests__/fixtures/all_options.yml
Normal file
14
__tests__/fixtures/all_options.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
label1:
|
||||||
|
- any:
|
||||||
|
- changed-files: ['glob']
|
||||||
|
- head-branch: ['regexp']
|
||||||
|
- base-branch: ['regexp']
|
||||||
|
- all:
|
||||||
|
- changed-files: ['glob']
|
||||||
|
- head-branch: ['regexp']
|
||||||
|
- base-branch: ['regexp']
|
||||||
|
|
||||||
|
label2:
|
||||||
|
- changed-files: ['glob']
|
||||||
|
- head-branch: ['regexp']
|
||||||
|
- base-branch: ['regexp']
|
||||||
@@ -1,3 +1,2 @@
|
|||||||
touched-a-pdf-file:
|
touched-a-pdf-file:
|
||||||
- changed-files:
|
- changed-files: ['*.pdf']
|
||||||
- any: ['*.pdf']
|
|
||||||
|
|||||||
@@ -1,6 +1,13 @@
|
|||||||
import {checkMatchConfigs, MatchConfig, toMatchConfig} from '../src/labeler';
|
import {
|
||||||
|
checkMatchConfigs,
|
||||||
|
MatchConfig,
|
||||||
|
toMatchConfig,
|
||||||
|
getLabelConfigMapFromObject,
|
||||||
|
BaseMatchConfig
|
||||||
|
} from '../src/labeler';
|
||||||
|
import * as yaml from 'js-yaml';
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
|
import * as fs from 'fs';
|
||||||
|
|
||||||
jest.mock('@actions/core');
|
jest.mock('@actions/core');
|
||||||
|
|
||||||
@@ -10,18 +17,56 @@ beforeAll(() => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const loadYaml = (filepath: string) => {
|
||||||
|
const loadedFile = fs.readFileSync(filepath);
|
||||||
|
const content = Buffer.from(loadedFile).toString();
|
||||||
|
return yaml.load(content);
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('getLabelConfigMapFromObject', () => {
|
||||||
|
const yamlObject = loadYaml('__tests__/fixtures/all_options.yml');
|
||||||
|
const expected = new Map<string, MatchConfig[]>();
|
||||||
|
expected.set('label1', [
|
||||||
|
{
|
||||||
|
any: [
|
||||||
|
{changedFiles: ['glob']},
|
||||||
|
{baseBranch: undefined, headBranch: ['regexp']},
|
||||||
|
{baseBranch: ['regexp'], headBranch: undefined}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
all: [
|
||||||
|
{changedFiles: ['glob']},
|
||||||
|
{baseBranch: undefined, headBranch: ['regexp']},
|
||||||
|
{baseBranch: ['regexp'], headBranch: undefined}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
expected.set('label2', [
|
||||||
|
{
|
||||||
|
any: [
|
||||||
|
{changedFiles: ['glob']},
|
||||||
|
{baseBranch: undefined, headBranch: ['regexp']},
|
||||||
|
{baseBranch: ['regexp'], headBranch: undefined}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
it('returns a MatchConfig', () => {
|
||||||
|
const result = getLabelConfigMapFromObject(yamlObject);
|
||||||
|
expect(result).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('toMatchConfig', () => {
|
describe('toMatchConfig', () => {
|
||||||
describe('when all expected config options are present', () => {
|
describe('when all expected config options are present', () => {
|
||||||
const config = {
|
const config = {
|
||||||
'changed-files': [{any: ['testing-any']}, {all: ['testing-all']}],
|
'changed-files': ['testing-files'],
|
||||||
'head-branch': ['testing-head'],
|
'head-branch': ['testing-head'],
|
||||||
'base-branch': ['testing-base']
|
'base-branch': ['testing-base']
|
||||||
};
|
};
|
||||||
const expected: MatchConfig = {
|
const expected: BaseMatchConfig = {
|
||||||
changedFiles: {
|
changedFiles: ['testing-files'],
|
||||||
all: ['testing-all'],
|
|
||||||
any: ['testing-any']
|
|
||||||
},
|
|
||||||
headBranch: ['testing-head'],
|
headBranch: ['testing-head'],
|
||||||
baseBranch: ['testing-base']
|
baseBranch: ['testing-base']
|
||||||
};
|
};
|
||||||
@@ -43,7 +88,7 @@ describe('toMatchConfig', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('checkMatchConfigs', () => {
|
describe('checkMatchConfigs', () => {
|
||||||
const matchConfig: MatchConfig[] = [{changedFiles: {any: ['*.txt']}}];
|
const matchConfig: MatchConfig[] = [{any: [{changedFiles: ['*.txt']}]}];
|
||||||
|
|
||||||
it('returns true when our pattern does match changed files', () => {
|
it('returns true when our pattern does match changed files', () => {
|
||||||
const changedFiles = ['foo.txt', 'bar.txt'];
|
const changedFiles = ['foo.txt', 'bar.txt'];
|
||||||
|
|||||||
Reference in New Issue
Block a user