This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
/
executableTests.ts
761 lines (676 loc) · 31.6 KB
/
executableTests.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
* Copyright 2018 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from "chai";
import * as cp from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as semver from "semver";
import { Logger, Options, run, Status } from "../../src/runner";
import { denormalizeWinPath } from "../../src/utils";
import { getNormalizedTypescriptVersion } from "../../src/verify/parse";
import { createTempFile } from "../utils";
// when tests are run with mocha from npm scripts CWD points to project root
const EXECUTABLE_DIR = path.resolve(process.cwd(), "test", "executable");
const EXECUTABLE_PATH = path.resolve(EXECUTABLE_DIR, "npm-like-executable");
const TEMP_JSON_PATH = path.resolve(EXECUTABLE_DIR, "tslint.json");
const dummyLogger: Logger = {
log() {
/* do nothing */
},
error() {
/* do nothing */
},
};
describe("Executable", function(this: Mocha.Suite) {
this.slow(3000); // the executable is JIT-ed each time it runs; avoid showing slowness warnings
this.timeout(10000);
const tsVersion = getNormalizedTypescriptVersion();
describe("Files", () => {
it("exits with code 1 if no arguments passed", done => {
execCli([], (err, stdout, stderr) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 1, "error code should be 1");
assert.include(
stderr,
"No files specified. Use --project to lint a project folder.",
"stderr should contain notification about missing files",
);
assert.strictEqual(stdout, "", "shouldn't contain any output in stdout");
done();
});
});
it("exits with code 0 if correct file is passed", done => {
execCli(["src/configuration.ts"], err => {
assert.isNull(err, "process should exit without an error");
done();
});
});
it("exits with code 0 if several files passed without `-f` flag", done => {
execCli(["src/configuration.ts", "src/formatterLoader.ts"], err => {
assert.isNull(err, "process should exit without an error");
done();
});
});
it("exits with code 1 if removed `-f` flag is passed", done => {
execCli(
["src/configuration.ts", "-f", "src/formatterLoader.ts"],
(err, stdout, stderr) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 1, "error code should be 1");
assert.include(stderr, "error: unknown option `-f'");
assert.strictEqual(stdout, "", "shouldn't contain any output in stdout");
done();
},
);
});
it("warns if file does not exist", async () => {
const result = await execRunnerWithOutput({ files: ["foo/bar.ts"] });
assert.strictEqual(result.status, Status.Ok, "process should exit without error");
assert.include(result.stderr, "'foo/bar.ts' does not exist");
});
it("doesn't warn if non-existent file is excluded by --exclude", async () => {
const result = await execRunnerWithOutput({
exclude: ["**/*.js"],
files: ["foo/bar.js"],
});
assert.strictEqual(result.status, Status.Ok, "process should exit without error");
assert.notInclude(result.stderr, "does not exist");
});
it("doesn't warn if glob pattern doesn't match any file", async () => {
const result = await execRunnerWithOutput({ files: ["foobar/*.js"] });
assert.strictEqual(result.status, Status.Ok, "process should exit without error");
assert.notInclude(result.stderr, "does not exist");
});
});
describe("Configuration file", () => {
it("exits with code 0 if relative path is passed without `./`", async () => {
const status = await execRunner({
config: "test/config/tslint-almost-empty.json",
files: ["src/test.ts"],
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 0 if config file that extends relative config file", async () => {
const status = await execRunner({
config: "test/config/tslint-extends-package-no-mod.json",
files: ["src/test.ts"],
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 1 if config file is invalid", async () => {
const result = await execRunnerWithOutput({
config: "test/config/tslint-invalid.json",
files: ["src/test.ts"],
});
assert.equal(result.status, Status.FatalError, "process should exit with error");
assert.include(
result.stderr,
"Failed to load",
"stderr should contain notification about failing to load json config",
);
assert.strictEqual(result.stdout, "", "shouldn't contain any output in stdout");
});
it("exits with code 1 if yaml config file is invalid", async () => {
const result = await execRunnerWithOutput({
config: "test/config/tslint-invalid.yaml",
files: ["src/test.ts"],
});
assert.strictEqual(result.status, Status.FatalError, "error code should be 1");
assert.include(
result.stderr,
"Failed to load",
"stderr should contain notification about failing to load yaml config",
);
assert.strictEqual(result.stdout, "", "shouldn't contain any output in stdout");
});
it("mentions the root cause if a config file extends from an invalid file", async () => {
const result = await execRunnerWithOutput({
config: "test/config/tslint-extends-invalid.json",
files: ["src/test.ts"],
});
assert.equal(result.status, Status.FatalError, "process should exit with error");
assert.include(
result.stderr,
"Failed to load",
"stderr should contain notification about failing to load json config",
);
assert.include(
result.stderr,
"tslint-invalid.json",
"stderr should mention the problem file",
);
assert.strictEqual(result.stdout, "", "shouldn't contain any output in stdout");
});
});
describe("Custom formatters", () => {
const createFormatVerifier = (done: Mocha.Done): ExecFileCallback => (err, stdout) => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 2, "error code should be 2");
assert.include(
stdout,
"hello from custom formatter",
"stdout should contain output of custom formatter",
);
done();
};
it("can be loaded from node_modules", done => {
execCli(
[
"-c",
"tslint-custom-rules-with-dir.json",
"../../src/test.ts",
"-t",
"tslint-test-custom-formatter",
],
{
cwd: "./test/config",
},
createFormatVerifier(done),
);
});
it("can be specified from config", done => {
execCli(
["-c", "tslint-custom-rules-with-dir-and-format.json", "../../src/test.ts"],
{
cwd: "./test/config",
},
createFormatVerifier(done),
);
});
});
describe("Custom rules", () => {
it("exits with code 1 if nonexisting custom rules directory is passed", async () => {
const status = await execRunner({
config: "./test/config/tslint-custom-rules.json",
files: ["src/test.ts"],
rulesDirectory: "./someRandomDir",
});
assert.equal(status, Status.FatalError, "error code should be 1");
});
it("exits with code 2 if custom rules directory is passed and file contains lint errors", async () => {
const status = await execRunner({
config: "./test/config/tslint-custom-rules.json",
files: ["src/test.ts"],
rulesDirectory: "./test/files/custom-rules",
});
assert.equal(status, Status.LintError, "error code should be 2");
});
it("exits with code 0 if custom rules directory is passed and file contains lint warnings", async () => {
const status = await execRunner({
config: "./test/config/tslint-extends-package-warning.json",
files: ["src/test.ts"],
rulesDirectory: "./test/files/custom-rules",
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 2 if custom rules directory is specified in config file and file contains lint errors", async () => {
const status = await execRunner({
config: "./test/config/tslint-custom-rules-with-dir.json",
files: ["src/test.ts"],
});
assert.equal(status, Status.LintError, "error code should be 2");
});
it("are compiled just in time when using ts-node", done => {
execCli(
["-c", "./test/config/tslint-custom-rules-uncompiled.json", "src/test.ts"],
{
env: {
...process.env, // tslint:disable-line:no-unsafe-any
NODE_OPTIONS: "-r ts-node/register",
TS_NODE_CACHE: "0",
TS_NODE_FAST: "1",
},
},
err => {
assert.isNull(err, "process should exit without an error");
done();
},
);
});
});
describe("Config with excluded files", () => {
it("exits with code 2 if linter options doesn't exclude file with lint errors", async () => {
const status = await execRunner({
config: "./test/files/config-exclude/tslint-exclude-one.json",
files: ["./test/files/config-exclude/included.ts"],
});
assert.equal(status, Status.LintError, "error code should be 2");
});
it("exits with code 0 if linter options exclude one file with lint errors", async () => {
const status = await execRunner({
config: "./test/files/config-exclude/tslint-exclude-one.json",
files: ["./test/files/config-exclude/excluded.ts"],
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 0 if linter options excludes many files with lint errors", async () => {
const status = await execRunner({
config: "./test/files/config-exclude/tslint-exclude-many.json",
files: [
"./test/rules/config-exclude/excluded1.ts",
"./test/rules/config-exclude/subdir/excluded2.ts",
],
});
assert.strictEqual(status, Status.Ok, "process should exit without an error");
});
it("excludes files relative to tslint.json", async () => {
const status = await execRunner({
config: "./test/files/config-exclude/tslint-exclude-one.json",
files: ["./test/files/config-exclude/subdir/excluded.ts"],
});
assert.equal(status, Status.LintError, "exit code should be 2");
});
it("excludes files relative to tslint.json they were declared in", async () => {
const status = await execRunner({
config: "./test/files/config-exclude/subdir/tslint-extending.json",
files: ["./test/files/config-exclude/subdir/excluded.ts"],
});
assert.equal(status, Status.LintError, "exit code should be 2");
});
});
it("finds configuration above current directory", done => {
execCli(
["index.test.ts"],
{
cwd: "./test/files/config-findup/no-config",
},
err => {
assert.isNotNull(err, "process should exit with an error");
assert.equal(err.code, 2, "exit code should be 2");
done();
},
);
});
describe("--fix flag", () => {
it("fixes multiple rules without overwriting each other", async () => {
const tempFile = path.relative(process.cwd(), createTempFile("ts"));
fs.writeFileSync(
tempFile,
'import * as x from "b"\nimport * as y from "a_long_module";\n',
);
const result = await execRunnerWithOutput({
config: "test/files/multiple-fixes-test/tslint.json",
files: [tempFile],
fix: true,
});
const content = fs.readFileSync(tempFile, "utf8");
// compare against file name which will be returned by formatter (used in TypeScript)
const denormalizedFileName = denormalizeWinPath(tempFile);
fs.unlinkSync(tempFile);
assert.equal(result.status, Status.Ok, "process should exit without an error");
assert.strictEqual(
content,
'import * as y from "a_long_module";\nimport * as x from "b";\n',
);
assert.strictEqual(result.stdout.trim(), `Fixed 2 error(s) in ${denormalizedFileName}`);
}).timeout(8000);
});
describe("--force flag", () => {
it("exits with code 0 if `--force` flag is passed", async () => {
const result = await execRunnerWithOutput({
config: "./test/config/tslint-custom-rules.json",
files: ["src/test.ts"],
force: true,
rulesDirectory: "./test/files/custom-rules",
});
assert.equal(result.status, Status.Ok, "process should exit without an error");
assert.include(result.stdout, "failure", "errors should be reported");
});
});
describe("--test flag", () => {
it("exits with code 0 if `--test` flag is used", async () => {
const status = await execRunner({ test: true, files: ["test/rules/no-eval"] });
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 0 if `--test` flag is used with a wildcard", async () => {
const status = await execRunner({ test: true, files: ["test/rules/no-e*"] });
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 1 if `--test` flag is used with incorrect rule", async () => {
const status = await execRunner({
files: ["test/files/incorrect-rule-test"],
test: true,
});
assert.equal(status, Status.FatalError, "error code should be 1");
});
it("exits with code 1 if `--test` flag is used with incorrect rule in a wildcard", async () => {
const status = await execRunner({ test: true, files: ["test/files/incorrect-rule-*"] });
assert.equal(status, Status.FatalError, "error code should be 1");
});
it("exits with code 0 if `--test` flag is used with custom rule", async () => {
const status = await execRunner({
files: ["test/files/custom-rule-rule-test"],
test: true,
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 0 if `--test` and `-r` flags are used with custom rule", async () => {
const status = await execRunner({
files: ["test/files/custom-rule-cli-rule-test"],
rulesDirectory: "test/files/custom-rules-2",
test: true,
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 0 if `--test` flag is used with fixes", async () => {
const status = await execRunner({ test: true, files: ["test/files/fixes-test"] });
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with code 1 if `--test` flag is used with incorrect fixes", async () => {
const status = await execRunner({
files: ["test/files/incorrect-fixes-test"],
test: true,
});
assert.equal(status, Status.FatalError, "error code should be 1");
});
it("can be used with multiple paths", async () => {
// pass a failing test as second path to make sure it gets executed
const status = await execRunner({
files: ["test/files/custom-rule-rule-test", "test/files/incorrect-fixes-test"],
test: true,
});
assert.equal(status, Status.FatalError, "error code should be 1");
});
});
describe("--project flag", () => {
it("exits with code 0 if `tsconfig.json` is passed and it specifies files without errors", async () => {
const status = await execRunner({
config: "test/files/tsconfig-test/tslint.json",
project: "test/files/tsconfig-test/tsconfig.json",
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("can be passed a directory and defaults to tsconfig.json", async () => {
const status = await execRunner({
config: "test/files/tsconfig-test/tslint.json",
project: "test/files/tsconfig-test",
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("exits with error if passed a directory and there is not tsconfig.json", async () => {
const status = await execRunner({
config: "test/files/tsconfig-test/tslint.json",
project: "test/files",
});
assert.equal(status, Status.FatalError, "exit code should be 1");
});
it("exits with error if passed directory does not exist", async () => {
const status = await execRunner({
config: "test/files/tsconfig-test/tslint.json",
project: "test/files/non-existent",
});
assert.equal(status, Status.FatalError, "exit code should be 1");
});
it("exits with code 1 if file is not included in project", async () => {
const status = await execRunner({
config: "test/files/tsconfig-test/tslint.json",
files: ["test/files/tsconfig-test/other.test.ts"],
project: "test/files/tsconfig-test/tsconfig.json",
});
assert.equal(status, Status.FatalError, "exit code should be 1");
});
it("exits with code 0 if `tsconfig.json` is passed but it includes no ts files", async () => {
const status = await execRunner({
config: "test/files/tsconfig-no-ts-files/tslint.json",
project: "test/files/tsconfig-no-ts-files/tsconfig.json",
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("can extend `tsconfig.json` with relative path", async () => {
const status1 = await execRunner({
config: "test/files/tsconfig-extends-relative/tslint-ok.json",
project: "test/files/tsconfig-extends-relative/test/tsconfig.json",
});
assert.equal(status1, Status.Ok, "process should exit without an error");
const status2 = await execRunner({
config: "test/files/tsconfig-extends-relative/tslint-fail.json",
project: "test/files/tsconfig-extends-relative/test/tsconfig.json",
});
assert.equal(status2, Status.LintError, "exit code should be 2");
});
it("warns if file-to-lint does not exist", async () => {
const result = await execRunnerWithOutput({
files: ["test/files/tsconfig-test/non-existent.test.ts"],
project: "test/files/tsconfig-test/tsconfig.json",
});
assert.strictEqual(result.status, Status.Ok, "process should exit without error");
assert.include(
result.stderr,
`${path.normalize(
"test/files/tsconfig-test/non-existent.test.ts",
)}' does not exist`,
);
});
it("doesn't warn for non-existent file-to-lint if excluded by --exclude", async () => {
const result = await execRunnerWithOutput({
exclude: ["**/*"],
files: ["test/files/tsconfig-test/non-existent.test.ts"],
project: "test/files/tsconfig-test/tsconfig.json",
});
assert.strictEqual(result.status, Status.Ok, "process should exit without error");
assert.notInclude(result.stderr, "does not exist");
});
it("doesn't warn if glob pattern doesn't match any file", async () => {
const result = await execRunnerWithOutput({
files: ["*.js"],
project: "test/files/tsconfig-test/tsconfig.json",
});
assert.strictEqual(result.status, Status.Ok, "process should exit without error");
assert.notInclude(result.stderr, "does not exist");
});
it("reports errors from parsing tsconfig.json", async () => {
const result = await execRunnerWithOutput({
project: "test/files/tsconfig-invalid/syntax-error.json",
});
assert.strictEqual(result.status, Status.FatalError, "exit code should be 1");
assert.include(result.stderr, "error TS");
});
it("reports errors from validating tsconfig.json", async () => {
const result = await execRunnerWithOutput({
project: "test/files/tsconfig-invalid/empty-files.json",
});
assert.strictEqual(result.status, Status.FatalError, "exit code should be 1");
assert.include(result.stderr, "error TS");
});
it("does not report an error if tsconfig.json matches no files", async () => {
const status = await execRunner({
project: "test/files/tsconfig-invalid/no-match.json",
});
assert.strictEqual(status, Status.Ok, "process should exit without an error");
});
it("can execute typed rules without --type-check", async () => {
const status = await execRunner({ project: "test/files/typed-rule/tsconfig.json" });
assert.equal(status, Status.LintError, "exit code should be 2");
});
it("handles 'allowJs' correctly", async () => {
const status = await execRunner({
project: "test/files/tsconfig-allow-js/tsconfig.json",
});
assert.equal(status, Status.LintError, "exit code should be 2");
});
it("doesn't lint external dependencies with 'allowJs'", async () => {
const status = await execRunner({
project: "test/files/allow-js-exclude-node-modules/tsconfig.json",
});
assert.equal(status, Status.Ok, "process should exit without error");
});
it("works with '--exclude'", async () => {
const status = await execRunner({
exclude: ["test/files/tsconfig-allow-js/testfile.test.js"],
project: "test/files/tsconfig-allow-js/tsconfig.json",
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
it("can apply fixes from multiple rules", async () => {
fs.writeFileSync(
"test/files/project-multiple-fixes/testfile.test.ts",
fs.readFileSync("test/files/project-multiple-fixes/before.test.ts", "utf-8"),
);
const status = await execRunner({
fix: true,
project: "test/files/project-multiple-fixes/",
});
const actual = fs.readFileSync(
"test/files/project-multiple-fixes/testfile.test.ts",
"utf-8",
);
fs.unlinkSync("test/files/project-multiple-fixes/testfile.test.ts");
assert.equal(status, Status.Ok, "process should exit without an error");
assert.strictEqual(
actual,
fs.readFileSync("test/files/project-multiple-fixes/after.test.ts", "utf-8"),
);
}).timeout(8000);
if (semver.satisfies(tsVersion, ">=2.9")) {
it("does not try to parse JSON files with --resolveJsonModule with TS >= 2.9", async () => {
const status = await execRunner({
project: "test/files/tsconfig-resolve-json-module/tsconfig.json",
});
assert.equal(status, Status.Ok, "process should exit without an error");
});
}
});
describe("--type-check", () => {
it("exits with code 1 if --project is not passed", done => {
execCli(["--type-check"], err => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 1, "error code should be 1");
done();
});
});
});
describe("--init flag", () => {
// remove temp file before calling tslint --init
beforeEach(cleanTempInitFile);
// clean up temp file after tests
afterEach(cleanTempInitFile);
it("exits with code 0 if `--init` flag is used in folder without tslint.json", done => {
execCli(["--init"], { cwd: EXECUTABLE_DIR }, err => {
assert.isNull(err, "process should exit without an error");
assert.strictEqual(fs.existsSync(TEMP_JSON_PATH), true, "file should be created");
done();
});
});
it("exits with code 1 if `--init` flag is used in folder with tslint.json", done => {
// make sure that file exists before test
fs.writeFileSync(TEMP_JSON_PATH, "{}");
execCli(["--init"], { cwd: EXECUTABLE_DIR }, err => {
assert.isNotNull(err, "process should exit with error");
assert.strictEqual(err.code, 1, "error code should be 1");
done();
});
});
});
describe("globs and quotes", () => {
// when glob pattern is passed without quotes in npm script `process.env` will contain:
// on Windows - pattern string without any quotes
// on Linux - list of files that matches glob (may differ from `glob` module results)
it("exits with code 2 if correctly finds file containing lint errors when glob is in double quotes", async () => {
// when glob pattern is passed in double quotes in npm script `process.env` will contain:
// on Windows - pattern string without any quotes
// on Linux - pattern string without any quotes (glob is not expanded)
const status = await execRunner({
config: "./test/config/tslint-custom-rules.json",
files: ["src/**/test.ts"],
rulesDirectory: "./test/files/custom-rules",
});
assert.equal(status, Status.LintError, "error code should be 2");
});
it("exits with code 2 if correctly finds file containing lint errors when glob is in single quotes", async () => {
// when glob pattern is passed in single quotes in npm script `process.env` will contain:
// on Windows - pattern string wrapped in single quotes
// on Linux - pattern string without any quotes (glob is not expanded)
const status = await execRunner({
config: "./test/config/tslint-custom-rules.json",
files: ["'src/**/test.ts'"],
rulesDirectory: "./test/files/custom-rules",
});
assert.equal(status, Status.LintError, "error code should be 2");
});
it("can handle multiple '--exclude' globs", done => {
execCli(
[
"-c",
"test/files/multiple-excludes/tslint.json",
"--exclude",
"'test/files/multiple-excludes/invalid.test.ts'",
"--exclude",
"'test/files/multiple-excludes/invalid2*'",
"'test/files/multiple-excludes/**.ts'",
],
err => {
assert.isNull(err, "process should exit without an error");
done();
},
);
});
});
});
type ExecFileCallback = (error: Error & { code: number }, stdout: string, stderr: string) => void;
function execCli(args: string[], cb: ExecFileCallback): cp.ChildProcess;
function execCli(
args: string[],
options: cp.ExecFileOptions,
cb: ExecFileCallback,
): cp.ChildProcess;
function execCli(
args: string[],
options: cp.ExecFileOptions | ExecFileCallback,
cb?: ExecFileCallback,
): cp.ChildProcess {
let filePath = EXECUTABLE_PATH;
// Specify extension for Windows executable to avoid ENOENT errors
if (os.platform() === "win32") {
filePath += ".cmd";
}
if (isFunction(options)) {
cb = options;
options = {};
}
return cp.execFile(filePath, args, options, (error, stdout, stderr) => {
if (cb === undefined) {
throw new Error("cb not defined");
}
cb(error as Error & { code: number }, stdout.trim(), stderr.trim());
});
}
// tslint:disable-next-line:promise-function-async
function execRunnerWithOutput(options: Partial<Options>) {
let stdout = "";
let stderr = "";
return execRunner(options, {
log(text) {
stdout += text;
},
error(text) {
stderr += text;
},
}).then(status => ({ status, stderr, stdout }));
}
// tslint:disable-next-line:promise-function-async
function execRunner(options: Partial<Options>, logger: Logger = dummyLogger) {
return run({ exclude: [], files: [], ...options }, logger);
}
// tslint:disable-next-line:ban-types
function isFunction(fn: any): fn is Function {
return {}.toString.call(fn) === "[object Function]";
}
function cleanTempInitFile(): void {
if (fs.existsSync(TEMP_JSON_PATH)) {
fs.unlinkSync(TEMP_JSON_PATH);
}
}