Add some new tests

This commit is contained in:
Josh Dales
2023-03-24 21:09:16 -04:00
parent e51b118725
commit a9e07ce8ff
4 changed files with 81 additions and 101 deletions

View File

@@ -63,72 +63,13 @@ describe('toChangedFilesMatchConfig', () => {
});
describe(`when there is a 'changed-files' key in the config`, () => {
describe(`and both 'any' and 'all' keys are present`, () => {
const config = {'changed-files': [{all: 'testing'}, {any: 'testing'}]};
describe('and the value is an array of strings', () => {
const config = {'changed-files': ['testing']};
it('sets both values in the config object', () => {
it('sets the value 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', () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({
changedFiles: {
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']
}
});
changedFiles: ['testing']
});
});
});
@@ -136,31 +77,16 @@ describe('toChangedFilesMatchConfig', () => {
describe('and the value is a string', () => {
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);
expect(result).toEqual<ChangedFilesMatchConfig>({
changedFiles: {
any: ['testing']
}
changedFiles: ['testing']
});
});
});
describe('and the value is an array of strings', () => {
const config = {'changed-files': ['testing']};
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': []};
describe('but the value is an empty string', () => {
const config = {'changed-files': ''};
it(`returns an empty object`, () => {
const result = toChangedFilesMatchConfig(config);
@@ -168,16 +94,12 @@ describe('toChangedFilesMatchConfig', () => {
});
});
describe('and there is an unknown value in the config', () => {
const config = {'changed-files': [{any: 'testing'}, {sneaky: 'test'}]};
describe('but the value is an empty array', () => {
const config = {'changed-files': []};
it(`does not set the value in the match config`, () => {
it(`returns an empty object`, () => {
const result = toChangedFilesMatchConfig(config);
expect(result).toEqual<ChangedFilesMatchConfig>({
changedFiles: {
any: ['testing']
}
});
expect(result).toEqual<ChangedFilesMatchConfig>({});
});
});
});

View 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']

View File

@@ -1,3 +1,2 @@
touched-a-pdf-file:
- changed-files:
- any: ['*.pdf']
- changed-files: ['*.pdf']

View File

@@ -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 fs from 'fs';
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('when all expected config options are present', () => {
const config = {
'changed-files': [{any: ['testing-any']}, {all: ['testing-all']}],
'changed-files': ['testing-files'],
'head-branch': ['testing-head'],
'base-branch': ['testing-base']
};
const expected: MatchConfig = {
changedFiles: {
all: ['testing-all'],
any: ['testing-any']
},
const expected: BaseMatchConfig = {
changedFiles: ['testing-files'],
headBranch: ['testing-head'],
baseBranch: ['testing-base']
};
@@ -43,7 +88,7 @@ describe('toMatchConfig', () => {
});
describe('checkMatchConfigs', () => {
const matchConfig: MatchConfig[] = [{changedFiles: {any: ['*.txt']}}];
const matchConfig: MatchConfig[] = [{any: [{changedFiles: ['*.txt']}]}];
it('returns true when our pattern does match changed files', () => {
const changedFiles = ['foo.txt', 'bar.txt'];