From 90b88c7f656298ff75f080c31e956aec5c478a4a Mon Sep 17 00:00:00 2001 From: Jan Macku Date: Tue, 13 Dec 2022 16:01:27 +0100 Subject: [PATCH] WIP --- .../__snapshots__/issue-form.test.ts.snap | 86 +++++++++++++++++++ test/unit/__snapshots__/labeler.test.ts.snap | 2 + test/unit/fixtures/issue-form.fixture.ts | 47 ++++++++++ test/unit/issue-form.test.ts | 80 ++++++++++++++--- test/unit/labeler.test.ts | 14 ++- 5 files changed, 215 insertions(+), 14 deletions(-) create mode 100644 test/unit/__snapshots__/issue-form.test.ts.snap create mode 100644 test/unit/fixtures/issue-form.fixture.ts diff --git a/test/unit/__snapshots__/issue-form.test.ts.snap b/test/unit/__snapshots__/issue-form.test.ts.snap new file mode 100644 index 00000000..d09c3bf0 --- /dev/null +++ b/test/unit/__snapshots__/issue-form.test.ts.snap @@ -0,0 +1,86 @@ +// Vitest Snapshot v1 + +exports[`IssueForm Object > getProperty() 1`] = `undefined`; + +exports[`IssueForm Object > getProperty() 2`] = `"Security guidelines provided by GitHub: [Security hardening for GitHub Actions ](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#overview)"`; + +exports[`IssueForm Object > getProperty() 3`] = `undefined`; + +exports[`IssueForm Object > getSafeProperty() 1`] = `undefined`; + +exports[`IssueForm Object > getSafeProperty() 2`] = `"Security guidelines provided by GitHub: [Security hardening for GitHub Actions ](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#overview)"`; + +exports[`IssueForm Object > getSafeProperty() 3`] = `undefined`; + +exports[`IssueForm Object > isProperty() 1`] = `false`; + +exports[`IssueForm Object > isProperty() 2`] = `true`; + +exports[`IssueForm Object > isProperty() 3`] = `false`; + +exports[`IssueForm Object > listKeywords() 1`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 2`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 3`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 4`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 5`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 6`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 7`] = ` +[ + "Feature Request", + "none", + "other", +] +`; + +exports[`IssueForm Object > listKeywords() 8`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 9`] = ` +[ + "Feature Request", + "none", +] +`; + +exports[`IssueForm Object > listKeywords() 10`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 11`] = ` +[ + "Feature Request", +] +`; + +exports[`IssueForm Object > listKeywords() 12`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 13`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 14`] = ` +[ + "bootctl", + "systemd-boot", + "other", +] +`; + +exports[`IssueForm Object > listKeywords() 15`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 16`] = ` +[ + "bootctl", + "systemd-boot", +] +`; + +exports[`IssueForm Object > listKeywords() 17`] = `undefined`; + +exports[`IssueForm Object > listKeywords() 18`] = ` +[ + "bootctl", + "systemd-boot", +] +`; diff --git a/test/unit/__snapshots__/labeler.test.ts.snap b/test/unit/__snapshots__/labeler.test.ts.snap index 148913bf..375a4b52 100644 --- a/test/unit/__snapshots__/labeler.test.ts.snap +++ b/test/unit/__snapshots__/labeler.test.ts.snap @@ -1,5 +1,7 @@ // Vitest Snapshot v1 +exports[`IssueForm Object > gatherLabels() 1`] = `undefined`; + exports[`IssueForm Object > get config() 1`] = `undefined`; exports[`IssueForm Object > get inputs() 1`] = ` diff --git a/test/unit/fixtures/issue-form.fixture.ts b/test/unit/fixtures/issue-form.fixture.ts new file mode 100644 index 00000000..778b481c --- /dev/null +++ b/test/unit/fixtures/issue-form.fixture.ts @@ -0,0 +1,47 @@ +import { IssueForm } from '../../../src/issue-form'; + +export interface IIssueFormTestContext { + issueForms: IssueForm[]; + blockLists: string[][]; + invalid: IssueForm[]; +} + +export const issueFormContextFixture: IIssueFormTestContext = { + issueForms: [ + new IssueForm({}), + // issue: https://github.com/redhat-plumbers-in-action/advanced-issue-labeler/issues/171 + new IssueForm({ + type: 'Feature Request, none, other', + description: + 'Security guidelines provided by GitHub: [Security hardening for GitHub Actions ](https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#overview)', + solution: + "Let's review the document from GitHub regarding GitHub Actions and security. And make Advanced Issue Labeler more secure!\n\nSecurity is a long-term goal, not one of, so it would be great to have some ci check or checkbox in the Pull Request template to ensure that when reviewing new code, we comply with best security policies and guidelines.", + }), + // issue: https://github.com/systemd/systemd/issues/25737 + new IssueForm({ + version: '252.3-1', + distro: 'Arch Linux', + kernel: '6.0.12-arch1-1 and 5.15.82-1-lts', + architecture: 'x86_64', + component: 'bootctl, systemd-boot, other', + 'expected-behaviour': 'System booting correctly with systemd-boot.', + 'unexpected-behaviour': + 'System hangs immediately after choosing the boot-entry, just showing a black screen with the following message in top left corner in red letters:\n\nFailed to open random seed file: Media changed\r\nError opening root path: Invalid Parameter', + 'steps-to-reproduce': + "- Coming from systemd 251.7-4 (which worked flawlessly) and updating to 252.3-1.\r\n- Running 'bootctl update'\r\n- Reboot\n\nUpdating to 252.3-1 WITHOUT running 'bootctl update' afterwards works fine. The system boots without any problems.\n\nSo the problem seems really systemd-boot related. Maybe something in 'systemd-bootx64.efi\" have changed and my Lenovo E15 Gen 2 doesn't like that.", + 'additional-information': '', + }), + ], + + blockLists: [[], ['other'], ['block1', 'block2', 'other', 'none']], + + invalid: [ + // @ts-expect-error: Let's ignore a type error, it's required for testing + new IssueForm(), + // @ts-expect-error: Let's ignore a type error, it's required for testing + new IssueForm(undefined), + // @ts-expect-error: Let's ignore a type error, it's required for testing + new IssueForm(null), + new IssueForm('form'), + ], +}; diff --git a/test/unit/issue-form.test.ts b/test/unit/issue-form.test.ts index bfe09880..ec5628df 100644 --- a/test/unit/issue-form.test.ts +++ b/test/unit/issue-form.test.ts @@ -2,22 +2,80 @@ import { describe, it, expect, beforeEach, test } from 'vitest'; import { IssueForm } from '../../src/issue-form'; +import { + IIssueFormTestContext, + issueFormContextFixture, +} from './fixtures/issue-form.fixture'; + describe('IssueForm Object', () => { - let issueForms: IssueForm[]; + beforeEach(context => { + context.issueForms = issueFormContextFixture.issueForms; + context.blockLists = issueFormContextFixture.blockLists; + context.invalid = issueFormContextFixture.invalid; + }); + + it('can be instantiated', context => + context.issueForms.map(issueFormItem => + expect(issueFormItem).toBeDefined() + )); + + // private + test.todo('get parsed()'); + + test('isProperty()', context => { + context.issueForms.map(issueFormItem => { + expect(issueFormItem.isProperty('')).toEqual(false); + expect(issueFormItem.isProperty('description')).toMatchSnapshot(); + }); - beforeEach(() => { - issueForms = [new IssueForm({})]; + let issueForm = new IssueForm({ section: 'content' }); + expect(issueForm.isProperty('section')).toEqual(true); }); - it('can be instantiated', () => { - issueForms.map(item => expect(item).toBeDefined()); + test('getProperty()', context => { + context.issueForms.map(issueFormItem => { + expect(issueFormItem.getProperty('')).toBeUndefined(); + expect(issueFormItem.getProperty('description')).toMatchSnapshot(); + }); + + let issueForm = new IssueForm({ section: 'content' }); + expect(issueForm.getProperty('section')).toEqual('content'); }); - test.todo('get parsed()'); + test('getSafeProperty()', context => { + context.issueForms.map(issueFormItem => { + expect(issueFormItem.getSafeProperty('')).toBeUndefined(); + expect(issueFormItem.getSafeProperty('description')).toMatchSnapshot(); + }); - test.todo('isProperty()'); - test.todo('getProperty()'); - test.todo('getSafeProperty()'); - test.todo('listKeywords()'); - test.todo('isCompliant()'); + let issueForm = new IssueForm({ section: 'content' }); + expect(issueForm.getSafeProperty('section')).toEqual('content'); + }); + + test('listKeywords()', context => { + context.issueForms.map(issueFormItem => + context.blockLists.map(blockListItem => { + expect( + issueFormItem.listKeywords('type', blockListItem) + ).toMatchSnapshot(); + expect( + issueFormItem.listKeywords('component', blockListItem) + ).toMatchSnapshot(); + }) + ); + }); + + test('isCompliant()', () => { + const issueForm = new IssueForm({}); + // @ts-expect-error: Let's ignore a type error, it's required for testing + expect(issueForm.isCompliant(['item1'], undefined)).toEqual(false); + + expect(issueForm.isCompliant(['item1', 'item2', 'item3'], 'item1')).toEqual( + true + ); + + expect(issueForm.isCompliant(['item1', 'item2', 'item3'], 'item4')).toEqual( + false + ); + }); }); diff --git a/test/unit/labeler.test.ts b/test/unit/labeler.test.ts index 7bad7b43..6876ebb0 100644 --- a/test/unit/labeler.test.ts +++ b/test/unit/labeler.test.ts @@ -31,6 +31,7 @@ describe('IssueForm Object', () => { test.todo('set inputs()'); test('setInputs()', () => { + //! FIXME: There is probably a better way how to do this ... let labeler = new Labeler(new IssueForm({})); labeler.setInputs({ section: 'section' }); expect(labeler.inputs).toMatchInlineSnapshot(` @@ -111,7 +112,10 @@ describe('IssueForm Object', () => { test.todo('get issueForm()'); test.todo('set issueForm()'); - test.todo('gatherLabels()'); + test('gatherLabels()', context => + context.labelers.map(labelerItem => + expect(labelerItem.gatherLabels()).toMatchSnapshot() + )); // private test.todo('inputBasedLabels()'); @@ -120,8 +124,12 @@ describe('IssueForm Object', () => { it('is valid', async context => context.labelers.map(async labelerItem => { //! FIXME: There is probably a better way how to do this ... - labelerItem.setInputs({ section: 'section' }); - expect(Labeler.validate(labelerItem)).resolves.toMatchObject([]); + let localLabeler = Object.assign( + Object.create(Object.getPrototypeOf(labelerItem)), + labelerItem + ); + localLabeler.setInputs({ section: 'section' }); + expect(Labeler.validate(localLabeler)).resolves.toMatchObject([]); })); it('is invalid', async context =>