From 642743277afef4da48ae5df5db42bdf266ee9e9c Mon Sep 17 00:00:00 2001 From: ctw-joao-luis Date: Tue, 15 Oct 2024 17:22:43 +0100 Subject: [PATCH] added new test for instance of header without license --- rules/license-header.js | 1 + rules/license-header.test.ts | 61 +++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/rules/license-header.js b/rules/license-header.js index ec33e19..0e7c85f 100644 --- a/rules/license-header.js +++ b/rules/license-header.js @@ -8,6 +8,7 @@ module.exports = { type: "suggestion", fixable: "code", schema: [{ + type: "object", properties: { licenseType: { diff --git a/rules/license-header.test.ts b/rules/license-header.test.ts index 240dc82..da95f68 100644 --- a/rules/license-header.test.ts +++ b/rules/license-header.test.ts @@ -6,6 +6,7 @@ import { TSESLint } from "@typescript-eslint/utils"; // should be the same of LICENSE_HEADER defined on license-header.js file. const LICENSE_TYPE = "MPL-2.0"; +const noLicense = ""; const LICENSE_HEADER = ` // SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) // SPDX-License-Identifier: ${LICENSE_TYPE} @@ -13,7 +14,10 @@ const LICENSE_HEADER = ` const rule = // eslint-disable-next-line @typescript-eslint/no-var-requires - require("./license-header") as TSESLint.RuleModule<"missingLicenseError", Array>>; + require("./license-header") as TSESLint.RuleModule< + "missingLicenseError" | "missingTypeOfLicense", + Array> + >; const ruleTester = new RuleTester({ parser: "@typescript-eslint/parser", @@ -25,7 +29,7 @@ const ruleTester = new RuleTester({ const validLichtblickHeader = ` // SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) -// SPDX-License-Identifier: MPL-2.0 +// SPDX-License-Identifier: ${LICENSE_TYPE} // Rest of file `; @@ -35,7 +39,7 @@ const validLichtblickHeaderWithSpaces = ` // SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) -// SPDX-License-Identifier: MPL-2.0 +// SPDX-License-Identifier: ${LICENSE_TYPE} @@ -45,7 +49,7 @@ const validLichtblickHeaderWithSpacesWithJsdom = ` /** @jest-environment jsdom */ // SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) -// SPDX-License-Identifier: MPL-2.0 +// SPDX-License-Identifier: ${LICENSE_TYPE} `; const invalidLichtblickHeaderEmpty = ` @@ -64,33 +68,54 @@ var b = 2 console.log(1 + 2) `; -const invalidLichtblickHeaderCases = [ - invalidLichtblickHeaderEmpty, - invalidLichtblickHeaderOlder, - invalidLichtblickHeaderRandom, -]; +const invalidLichtblickHeaderWithMissingTypeOfLicense = ` +// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) +// SPDX-License-Identifier: ${noLicense} + +`; ruleTester.run("check-license-header", rule, { valid: [ { code: validLichtblickHeader, - options: [{licenseType: "MPL-2.0"}] + options: [{ licenseType: "MPL-2.0" }] }, { code: validLichtblickHeaderWithSpaces, - options: [{licenseType: "MPL-2.0"}] + options: [{ licenseType: "MPL-2.0" }] }, { code: validLichtblickHeaderWithSpacesWithJsdom, - options: [{licenseType: "MPL-2.0"}] + options: [{ licenseType: "MPL-2.0" }] }, ], // Test if the lint fix were successfull, adding the LICENSE_HEADER followed by two empty lines - invalid: invalidLichtblickHeaderCases.map((invalidHeader) => ({ - code: invalidHeader, - options: [{licenseType: "MPL-2.0"}], - errors: [{ messageId: "missingLicenseError" }], - output: LICENSE_HEADER + "\n\n" + invalidHeader, - })), + invalid: [ + { + code: invalidLichtblickHeaderEmpty, + options: [{ licenseType: "MPL-2.0" }], + errors: [{ messageId: "missingLicenseError" }], + output: LICENSE_HEADER + "\n\n" + invalidLichtblickHeaderEmpty + }, + { + code: invalidLichtblickHeaderOlder, + options: [{ licenseType: "MPL-2.0" }], + errors: [{ messageId: "missingLicenseError" }], + output: LICENSE_HEADER + "\n\n" + invalidLichtblickHeaderOlder + }, + { + code: invalidLichtblickHeaderRandom, + options: [{ licenseType: "MPL-2.0" }], + errors: [{ messageId: "missingLicenseError" }], + output: LICENSE_HEADER + "\n\n" + invalidLichtblickHeaderRandom + }, + { + code: invalidLichtblickHeaderWithMissingTypeOfLicense, + options: [], + errors: [ + { messageId: "missingTypeOfLicense" }, + ] + } + ] });