From a9e07ce8ffdac59ef2f3becf3404a61ab4987bd0 Mon Sep 17 00:00:00 2001 From: Josh Dales Date: Fri, 24 Mar 2023 21:09:16 -0400 Subject: [PATCH] Add some new tests --- __tests__/changedFiles.test.ts | 102 ++++------------------------- __tests__/fixtures/all_options.yml | 14 ++++ __tests__/fixtures/only_pdfs.yml | 3 +- __tests__/labeler.test.ts | 63 +++++++++++++++--- 4 files changed, 81 insertions(+), 101 deletions(-) create mode 100644 __tests__/fixtures/all_options.yml diff --git a/__tests__/changedFiles.test.ts b/__tests__/changedFiles.test.ts index d529b82f..9efa2193 100644 --- a/__tests__/changedFiles.test.ts +++ b/__tests__/changedFiles.test.ts @@ -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({ - 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({ - 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({ - 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({ - 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({ - 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({ - 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({ - 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({ - changedFiles: { - any: ['testing'] - } - }); + expect(result).toEqual({}); }); }); }); diff --git a/__tests__/fixtures/all_options.yml b/__tests__/fixtures/all_options.yml new file mode 100644 index 00000000..f4d5ecc5 --- /dev/null +++ b/__tests__/fixtures/all_options.yml @@ -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'] diff --git a/__tests__/fixtures/only_pdfs.yml b/__tests__/fixtures/only_pdfs.yml index 2909dce7..a645acfc 100644 --- a/__tests__/fixtures/only_pdfs.yml +++ b/__tests__/fixtures/only_pdfs.yml @@ -1,3 +1,2 @@ touched-a-pdf-file: - - changed-files: - - any: ['*.pdf'] + - changed-files: ['*.pdf'] diff --git a/__tests__/labeler.test.ts b/__tests__/labeler.test.ts index 28768651..d5ac569b 100644 --- a/__tests__/labeler.test.ts +++ b/__tests__/labeler.test.ts @@ -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(); + 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'];