diff --git a/__tests__/branch.test.ts b/__tests__/branch.test.ts index 0fcacb41..066c8f01 100644 --- a/__tests__/branch.test.ts +++ b/__tests__/branch.test.ts @@ -87,6 +87,7 @@ describe('checkBranch', () => { describe('toBranchMatchConfig', () => { describe('when there are no branch keys in the config', () => { const config = {'changed-files': [{any: ['testing']}]}; + it('returns an empty object', () => { const result = toBranchMatchConfig(config); expect(result).toMatchObject({}); @@ -95,6 +96,7 @@ describe('toBranchMatchConfig', () => { describe('when the config contains a head-branch option', () => { const config = {'head-branch': ['testing']}; + it('sets headBranch in the matchConfig', () => { const result = toBranchMatchConfig(config); expect(result).toMatchObject({ diff --git a/__tests__/labeler.test.ts b/__tests__/labeler.test.ts index 0e81bce7..b8bd423b 100644 --- a/__tests__/labeler.test.ts +++ b/__tests__/labeler.test.ts @@ -1,4 +1,4 @@ -import {checkMatchConfigs, MatchConfig} from '../src/labeler'; +import {checkMatchConfigs, MatchConfig, toMatchConfig} from '../src/labeler'; import * as core from '@actions/core'; @@ -10,9 +10,47 @@ beforeAll(() => { }); }); -const matchConfig: MatchConfig[] = [{changedFiles: {any: ['*.txt']}}]; +describe('toMatchConfig', () => { + describe('when all expected config options are present', () => { + const config = { + 'changed-files': [{any: ['testing-any']}, {all: ['testing-all']}], + 'head-branch': ['testing-head'], + 'base-branch': ['testing-base'] + }; + + it('returns a MatchConfig object with all options', () => { + const result = toMatchConfig(config); + expect(result).toMatchObject({ + changedFiles: { + all: ['testing-all'], + any: ['testing-any'] + }, + headBranch: ['testing-head'], + baseBranch: ['testing-base'] + }); + }); + + describe('and there are also unexpected options present', () => { + config['test-test'] = 'testing'; + + it('does not include the unexpected items in the returned MatchConfig object', () => { + const result = toMatchConfig(config); + expect(result).toMatchObject({ + changedFiles: { + all: ['testing-all'], + any: ['testing-any'] + }, + headBranch: ['testing-head'], + baseBranch: ['testing-base'] + }); + }); + }); + }); +}); describe('checkMatchConfigs', () => { + const matchConfig: MatchConfig[] = [{changedFiles: {any: ['*.txt']}}]; + it('returns true when our pattern does match changed files', () => { const changedFiles = ['foo.txt', 'bar.txt']; const result = checkMatchConfigs(changedFiles, matchConfig); diff --git a/src/labeler.ts b/src/labeler.ts index b24753ae..aeca6470 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -127,7 +127,7 @@ function getLabelConfigMapFromObject( return labelMap; } -function toMatchConfig(config: any): MatchConfig { +export function toMatchConfig(config: any): MatchConfig { const changedFilesConfig = toChangedFilesMatchConfig(config); const branchConfig = toBranchMatchConfig(config);