Skip to content

Commit

Permalink
added new test for instance of header without license
Browse files Browse the repository at this point in the history
  • Loading branch information
ctw-joao-luis committed Oct 15, 2024
1 parent 8a2e033 commit 6427432
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions rules/license-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
type: "suggestion",
fixable: "code",
schema: [{

Check failure on line 10 in rules/license-header.js

View workflow job for this annotation

GitHub Actions / test

Delete `{`

Check failure on line 11 in rules/license-header.js

View workflow job for this annotation

GitHub Actions / test

Insert `{`
type: "object",

Check failure on line 12 in rules/license-header.js

View workflow job for this annotation

GitHub Actions / test

Insert `··`
properties: {

Check failure on line 13 in rules/license-header.js

View workflow job for this annotation

GitHub Actions / test

Insert `··`
licenseType: {
Expand Down
61 changes: 43 additions & 18 deletions rules/license-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ 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)<[email protected]>
// SPDX-License-Identifier: ${LICENSE_TYPE}
`.trim();

const rule =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("./license-header") as TSESLint.RuleModule<"missingLicenseError", Array<Record< string, string >>>;
require("./license-header") as TSESLint.RuleModule<
"missingLicenseError" | "missingTypeOfLicense",
Array<Record< string, string >>
>;

const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
Expand All @@ -25,7 +29,7 @@ const ruleTester = new RuleTester({

const validLichtblickHeader = `
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
// SPDX-License-Identifier: ${LICENSE_TYPE}
// Rest of file
`;
Expand All @@ -35,7 +39,7 @@ const validLichtblickHeaderWithSpaces = `
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
// SPDX-License-Identifier: ${LICENSE_TYPE}
Expand All @@ -45,7 +49,7 @@ const validLichtblickHeaderWithSpacesWithJsdom = `
/** @jest-environment jsdom */
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MPL-2.0
// SPDX-License-Identifier: ${LICENSE_TYPE}
`;

const invalidLichtblickHeaderEmpty = `
Expand All @@ -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)<[email protected]>
// 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" },
]
}
]
});

0 comments on commit 6427432

Please sign in to comment.