forked from foxglove/eslint-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlicense-header.test.ts
133 lines (107 loc) · 3.42 KB
/
license-header.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: MIT
import { RuleTester } from "@typescript-eslint/rule-tester";
import { TSESLint } from "@typescript-eslint/utils";
// should be the same of LICENSE_HEADER defined on license-header.js file.
const mplLicense = "MPL-2.0";
const mitLicense = "MIT";
const noLicense = "";
function createHeader(license: string) {
return `
// SPDX-FileCopyrightText: Copyright (C) 2023-2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)<[email protected]>
// SPDX-License-Identifier: ${license}
`.trim();
}
const rule =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("./license-header") as TSESLint.RuleModule<
"wrongHeaderError" | "missingTypeOfLicenseError",
Array<Record<string, string>>
>;
const ruleTester = new RuleTester({
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 2020,
project: "./tsconfig.test.json",
},
});
const validLichtblickHeader = `
${createHeader(mitLicense)}
// Rest of file
`;
const validLichtblickHeaderWithSpaces = `
${createHeader(mplLicense)}
`;
const validLichtblickHeaderWithSpacesWithJsdom = `
/** @jest-environment jsdom */
${createHeader(mplLicense)}
`;
const invalidLichtblickHeaderEmpty = `
`;
const invalidLichtblickHeaderOlder = `
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
`;
const invalidLichtblickHeaderRandom = `
var a = 1
var b = 2
console.log(1 + 2)
`;
const invalidLichtblickHeaderWithMissingTypeOfLicense = `
${createHeader(noLicense)}
`;
const invalidLichtblickHeaderWithWrongTypeOfLicense = `
${createHeader(mplLicense)}
`;
ruleTester.run("check-license-header", rule, {
valid: [
{
code: validLichtblickHeader,
options: [{ licenseType: "MIT" }],
},
{
code: validLichtblickHeaderWithSpaces,
options: [{ licenseType: "MPL-2.0" }],
},
{
code: validLichtblickHeaderWithSpacesWithJsdom,
options: [{ licenseType: "MPL-2.0" }],
},
],
// Test if the lint fix were successfull, adding the LICENSE_HEADER followed by two empty lines
invalid: [
{
code: invalidLichtblickHeaderEmpty,
options: [{ licenseType: "MPL-2.0" }],
errors: [{ messageId: "wrongHeaderError" }],
output: createHeader(mplLicense) + "\n\n" + invalidLichtblickHeaderEmpty,
},
{
code: invalidLichtblickHeaderOlder,
options: [{ licenseType: "MPL-2.0" }],
errors: [{ messageId: "wrongHeaderError" }],
output: createHeader(mplLicense) + "\n\n" + invalidLichtblickHeaderOlder,
},
{
code: invalidLichtblickHeaderRandom,
options: [{ licenseType: "MPL-2.0" }],
errors: [{ messageId: "wrongHeaderError" }],
output: createHeader(mplLicense) + "\n\n" + invalidLichtblickHeaderRandom,
},
{
code: invalidLichtblickHeaderWithMissingTypeOfLicense,
options: [],
errors: [{ messageId: "missingTypeOfLicenseError" }],
},
{
code: invalidLichtblickHeaderWithWrongTypeOfLicense,
options: [{ licenseType: "MIT" }],
errors: [{ messageId: "wrongHeaderError" }],
output:
createHeader(mitLicense) +
"\n\n" +
invalidLichtblickHeaderWithWrongTypeOfLicense,
},
],
});