-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.ts
213 lines (198 loc) · 5.72 KB
/
mod_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2023 Chris Knight. All rights reserved. MIT license.
import { assert, assertEquals, assertRejects } from "@std/assert";
import {
checkCopyrightHeaders,
Options,
updateCopyrightHeaders,
} from "./mod.ts";
Deno.test({
name: "Options are validated",
async fn() {
const options: Options = {
extensions: [".java", ".jsx"],
exclusions: ["mod.ts"],
headerText:
`// Copyright {TIMEFRAME} the code authors. All rights reserved. MIT license.`,
rootDir: ".",
firstYear: 2022,
quiet: false,
};
options.firstYear = 1900;
await assertRejects(
() => checkCopyrightHeaders(options),
Error,
"Invalid first year",
);
await assertRejects(
() => updateCopyrightHeaders(options),
Error,
"Invalid first year",
);
options.firstYear = new Date().getFullYear() + 1;
await assertRejects(
() => checkCopyrightHeaders(options),
Error,
"Invalid first year",
);
await assertRejects(
() => updateCopyrightHeaders(options),
Error,
"Invalid first year",
);
options.firstYear = 1901;
options.extensions = [];
await assertRejects(
() => checkCopyrightHeaders(options),
Error,
"No extensions provided",
);
await assertRejects(
() => updateCopyrightHeaders(options),
Error,
"No extensions provided",
);
options.firstYear = new Date().getFullYear();
options.extensions = [".java", ".jsx"];
options.rootDir = "";
await assertRejects(
() => checkCopyrightHeaders(options),
Error,
"No root directory provided",
);
await assertRejects(
() => updateCopyrightHeaders(options),
Error,
"No root directory provided",
);
options.rootDir = ".";
options.headerText = "";
await assertRejects(
() => checkCopyrightHeaders(options),
Error,
"No license text provided",
);
await assertRejects(
() => updateCopyrightHeaders(options),
Error,
"No license text provided",
);
},
});
Deno.test({
name: "Files are checked with first year specified",
async fn(t) {
const license = "// Copyright 2018-{TIMEFRAME}. MIT license.".replace(
"{TIMEFRAME}",
new Date().getFullYear().toString(),
);
const licenseOld = "// Copyright 2018-{TIMEFRAME}. MIT license.".replace(
"{TIMEFRAME}",
(new Date().getFullYear() - 1).toString(),
);
await Deno.mkdir("testdir");
Deno.writeFileSync(
"my_file.test",
new TextEncoder().encode("hello world\n"),
);
Deno.writeFileSync(
"./testdir/should_not_be_checked.java",
new TextEncoder().encode("hello world\n"),
);
Deno.writeFileSync(
"./testdir/my_file2.test",
new TextEncoder().encode("hello world\n"),
);
Deno.writeFileSync(
"./testdir/my_file3.test",
new TextEncoder().encode(license + "\nhello world\n"),
);
Deno.writeFileSync(
"./testdir/my_file4.test",
new TextEncoder().encode(licenseOld + "\nhello world\n"),
);
await t.step("Check files with first year specified", async () => {
const options: Options = {
extensions: [".test"],
exclusions: [],
headerText: `// Copyright {TIMEFRAME}. MIT license.`,
rootDir: ".",
firstYear: 2018,
quiet: false,
};
//Files my_file.test, my_file2.test and my_file4.test should fail
assert(!await checkCopyrightHeaders(options));
//Now exlude the failing files
options.exclusions = [
"**/my_file2.test",
"**/my_file4.test",
"my_file.test",
];
assert(await checkCopyrightHeaders(options));
});
await t.step("Update files with first year specified", async () => {
const options: Options = {
extensions: [".test"],
exclusions: [],
headerText: `// Copyright {TIMEFRAME}. MIT license.`,
rootDir: ".",
firstYear: 2018,
quiet: false,
};
await updateCopyrightHeaders(options);
assert(await checkCopyrightHeaders(options));
assertEquals(
Deno.readTextFileSync("my_file.test"),
license + "\nhello world\n",
);
});
await Deno.remove("my_file.test");
await Deno.remove("./testdir", { recursive: true });
},
});
Deno.test({
name: "Files are checked without first year specified",
async fn(t) {
const license = "// Copyright {TIMEFRAME}. MIT license.".replace(
"{TIMEFRAME}",
new Date().getFullYear().toString(),
);
const licenseOld = "// Copyright {TIMEFRAME}. MIT license.".replace(
"{TIMEFRAME}",
(new Date().getFullYear() - 1).toString(),
);
await Deno.mkdir("testdir");
Deno.writeFileSync(
"my_file.test",
new TextEncoder().encode("hello world\n"),
);
Deno.writeFileSync(
"./testdir/my_file2.test",
new TextEncoder().encode(license + "\nhello world\n"),
);
Deno.writeFileSync(
"./testdir/my_file3.test",
new TextEncoder().encode(licenseOld + "\nhello world\n"),
);
await t.step(
"Check and update files with first year specified",
async () => {
const options: Options = {
extensions: [".test"],
exclusions: [],
headerText: `// Copyright {TIMEFRAME}. MIT license.`,
rootDir: ".",
quiet: true,
};
assert(!await checkCopyrightHeaders(options));
await updateCopyrightHeaders(options);
assert(await checkCopyrightHeaders(options));
assertEquals(
Deno.readTextFileSync("my_file.test"),
license + "\nhello world\n",
);
},
);
await Deno.remove("my_file.test");
await Deno.remove("./testdir", { recursive: true });
},
});