/
ignored-paths.js
776 lines (569 loc) · 33.6 KB
/
ignored-paths.js
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
/**
* @fileoverview Tests for IgnoredPaths object.
* @author Jonathan Rajavuori
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert,
path = require("path"),
os = require("os"),
IgnoredPaths = require("../../../lib/util/ignored-paths.js"),
sinon = require("sinon"),
fs = require("fs"),
includes = require("lodash").includes;
require("shelljs/global");
/* global mkdir, rm, cp */
//------------------------------------------------------------------------------
// Helper
//------------------------------------------------------------------------------
let fixtureDir;
/**
* get raw rules from IgnorePaths instance
* @param {IgnoredPaths} ignoredPaths, instance of IgnoredPaths
* @returns {string[]} raw ignore rules
*/
function getIgnoreRules(ignoredPaths) {
const ignoreRulesProperty = "_rules";
let ignoreRules = [];
Object.keys(ignoredPaths.ig).forEach(key => {
const rules = ignoredPaths.ig[key][ignoreRulesProperty];
rules.forEach(rule => {
const ruleOrigins = ignoreRules.map(ruleObj => ruleObj.origin);
/*
* Don't include duplicate ignore rules.
* (Duplicates occur because we add custom ignore patterns to the
* defaults as well, to allow unignoring default ignores)
*/
if (!includes(ruleOrigins, rule.origin)) {
ignoreRules = ignoreRules.concat(rule);
}
});
});
return ignoreRules;
}
/**
* Get a list of paths of loaded ignore files (e.g. .eslintignore) from IgnorePaths instance
* @param {IgnoredPaths} ignoredPaths Instance of IgnoredPaths
* @returns {string[]} loaded ignore files
*/
function getIgnoreFiles(ignoredPaths) {
return ignoredPaths.ig.custom.ignoreFiles;
}
/**
* Get a list of ignore patterns that are loaded
* @param {Object[]} ignoredPaths Instance of IgnoredPaths
* @returns {string[]} Ignore patterns
*/
function getIgnorePatterns(ignoredPaths) {
const ignoreRules = getIgnoreRules(ignoredPaths);
return ignoreRules.map(rule => rule.origin);
}
/**
* count the number of default patterns applied to IgnoredPaths instance
* @param {IgnoredPaths} ignoredPaths, instance of IgnoredPaths
* @returns {integer} count of default patterns
*/
function countDefaultPatterns(ignoredPaths) {
let count = ignoredPaths.defaultPatterns.length;
if (!ignoredPaths.options || (ignoredPaths.options.dotfiles !== true)) {
count += 2; // Two patterns for ignoring dotfiles
}
return count;
}
/**
* Returns the path inside of the fixture directory.
* @returns {string} The path inside the fixture directory.
* @private
*/
function getFixturePath(...args) {
return path.join(fs.realpathSync(fixtureDir), ...args);
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("IgnoredPaths", () => {
// copy into clean area so as not to get "infected" by this project's .eslintrc files
before(() => {
fixtureDir = path.join(os.tmpdir(), "/eslint/fixtures/ignored-paths/");
mkdir("-p", fixtureDir);
cp("-r", "./tests/fixtures/ignored-paths/.", fixtureDir);
});
after(() => {
rm("-r", fixtureDir);
});
describe("initialization", () => {
it("should load .eslintignore from cwd when explicitly passed", () => {
const expectedIgnoreFile = getFixturePath(".eslintignore");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
const ignorePatterns = getIgnorePatterns(ignoredPaths);
assert.isNotNull(ignoredPaths.ignoreFileDir);
assert.deepStrictEqual(getIgnoreFiles(ignoredPaths), [expectedIgnoreFile]);
assert.include(ignorePatterns, "sampleignorepattern");
});
it("should set baseDir to cwd when no ignore file was loaded", () => {
const ignoredPaths = new IgnoredPaths({ cwd: getFixturePath("no-ignore-file") });
assert.strictEqual(ignoredPaths.ignoreFileDir, getFixturePath("no-ignore-file"));
});
it("should not travel to parent directories to find .eslintignore when it's missing and cwd is provided", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("configurations") });
assert.lengthOf(getIgnoreRules(ignoredPaths), 4);
assert.lengthOf(getIgnoreFiles(ignoredPaths), 0);
});
it("should load empty array with ignorePath set to false", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: false, cwd: getFixturePath("no-ignore-file") });
assert.isArray(getIgnoreRules(ignoredPaths));
assert.lengthOf(getIgnoreRules(ignoredPaths), countDefaultPatterns(ignoredPaths));
});
it("should accept an array for options.ignorePattern", () => {
const ignorePattern = ["a", "b"];
const ignoredPaths = new IgnoredPaths({
ignorePattern
});
assert.ok(
ignorePattern.every(pattern => (
getIgnoreRules(ignoredPaths).some(rule => rule.pattern === pattern)
))
);
});
it("should use package.json's eslintIgnore files if no specified .eslintignore file", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("package-json-ignore") });
assert.isTrue(ignoredPaths.contains("hello.js"));
assert.isTrue(ignoredPaths.contains("world.js"));
});
it("should use correct message template if failed to parse package.json", () => {
assert.throw(() => {
try {
// eslint-disable-next-line no-new
new IgnoredPaths({ ignore: true, cwd: getFixturePath("broken-package-json") });
} catch (error) {
assert.strictEqual(error.messageTemplate, "failed-to-read-json");
throw error;
}
});
});
it("should not use package.json's eslintIgnore files if specified .eslintignore file", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains("hello.js"));
assert.isFalse(ignoredPaths.contains("world.js"));
assert.isTrue(ignoredPaths.contains("sampleignorepattern"));
});
it("should error if package.json's eslintIgnore is not an array of file paths", () => {
assert.throws(() => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("bad-package-json-ignore") });
assert.ok(ignoredPaths);
}, "Package.json eslintIgnore property requires an array of paths");
});
});
describe("caching file reads", () => {
let readFileSyncCount;
before(() => {
readFileSyncCount = sinon.spy(fs, "readFileSync");
});
after(() => {
readFileSyncCount.restore();
});
it("should cache readFileSync on same file paths", () => {
const ignoreFilePath = getFixturePath(".eslintignore");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
ignoredPaths.readIgnoreFile(ignoreFilePath);
assert.isTrue(ignoredPaths.contains(ignoreFilePath));
sinon.assert.calledOnce(readFileSyncCount);
});
});
describe("initialization with ignorePattern", () => {
it("should ignore a normal pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "ignore-me.txt", cwd: getFixturePath("ignore-pattern") });
assert.isTrue(ignoredPaths.contains(getFixturePath("ignore-pattern", "ignore-me.txt")));
});
});
describe("initialization with ignorePath", () => {
let ignoreFilePath;
before(() => {
ignoreFilePath = getFixturePath(".eslintignore");
});
it("should set baseDir to directory containing ignorePath if provided", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath() });
assert.strictEqual(ignoredPaths.ignoreFileDir, path.dirname(ignoreFilePath));
});
it("should set the common ancestor directory of cwd and ignorePath to baseDir (in the case that 'ignoreFilePath' and 'cwd' are siblings)", () => {
const baseDir = path.dirname(ignoreFilePath);
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: path.resolve(baseDir, "testcwd") });
assert.strictEqual(ignoredPaths.getBaseDir(), baseDir);
});
it("should set the common ancestor directory of cwd and ignorePath to baseDir", () => {
const baseDir = path.resolve(ignoreFilePath, "../../..");
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: path.resolve(baseDir, "fix/testcwd") });
assert.strictEqual(ignoredPaths.getBaseDir(), baseDir);
});
});
describe("initialization with ignorePath file not named .eslintignore", () => {
let ignoreFilePath;
before(() => {
ignoreFilePath = getFixturePath("custom-name", "ignore-file");
});
it("should work when cwd is a parent directory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath() });
assert.notStrictEqual(getIgnoreRules(ignoredPaths).length, countDefaultPatterns(ignoredPaths));
});
it("should work when the file is in the cwd", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath("custom-name") });
assert.notStrictEqual(getIgnoreRules(ignoredPaths).length, countDefaultPatterns(ignoredPaths));
});
it("should work when cwd is a subdirectory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath("custom-name", "subdirectory") });
assert.notStrictEqual(getIgnoreRules(ignoredPaths).length, countDefaultPatterns(ignoredPaths));
});
});
describe("initialization without ignorePath", () => {
it("should not load an ignore file if none is in cwd", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("no-ignore-file") });
assert.lengthOf(getIgnoreFiles(ignoredPaths), 0);
assert.lengthOf(getIgnoreRules(ignoredPaths), countDefaultPatterns(ignoredPaths));
});
});
describe("initialization with invalid file", () => {
let invalidFilepath;
before(() => {
invalidFilepath = getFixturePath("not-a-directory", ".foobaz");
});
it("should throw error", () => {
assert.throws(() => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: invalidFilepath, cwd: getFixturePath() });
assert.ok(ignoredPaths);
}, "Cannot read ignore file");
});
});
describe("contains", () => {
it("should throw if initialized with invalid options", () => {
const ignoredPaths = new IgnoredPaths(null);
assert.throw(ignoredPaths.contains, Error);
});
it("should not throw if given a relative filename", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "undef.js", cwd: getFixturePath() });
ignoredPaths.contains("undef.js");
});
it("should return true for files which match an ignorePattern even if they do not exist on the filesystem", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "not-a-file", cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("not-a-file")));
});
it("should return false for files outside of the cwd (with no ignore file provided)", () => {
// Default ignore patterns should not inadvertantly ignore files in parent directories
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("no-ignore-file") });
assert.isFalse(ignoredPaths.contains(getFixturePath("undef.js")));
});
it("should return false for files outside of ignorePath's directory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath("custom-name", "ignore-file"), cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("undef.js")));
});
it("should return true for file matching an ignore pattern exactly", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "undef.js", cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("undef.js")));
});
it("should return false for file matching an invalid ignore pattern with leading './'", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "./undef.js", cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("undef.js")));
});
it("should return false for file in subfolder of cwd matching an ignore pattern with leading '/'", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "/undef.js", cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("subdir", "undef.js")));
});
it("should return true for file matching a child of an ignore pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "ignore-pattern", cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("ignore-pattern", "ignore-me.txt")));
});
it("should return true for file matching a grandchild of an ignore pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "ignore-pattern", cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("ignore-pattern", "subdir", "ignore-me.txt")));
});
it("should return true for file matching a child of an ignore pattern with windows line termination", () => {
sinon.stub(fs, "readFileSync")
.withArgs(".eslintignore")
.returns("subdir\r\n");
sinon.stub(fs, "statSync")
.withArgs("subdir")
.returns();
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ".eslintignore", cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("subdir/undef.js")));
fs.readFileSync.restore();
fs.statSync.restore();
});
it("should return false for file not matching any ignore pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "failing.js", cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("unignored.js")));
});
it("should return false for ignored file when unignored with ignore pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignore"), ignorePattern: "!sampleignorepattern", cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("sampleignorepattern")));
});
it("should resolve relative paths from the ignorePath, not cwd", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignoreForDifferentCwd"), cwd: getFixturePath("subdir") });
assert.isFalse(ignoredPaths.contains(getFixturePath("subdir/undef.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("undef.js")));
});
it("should resolve relative paths from the ignorePath when it's in a child directory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath("subdir/.eslintignoreInChildDir"), cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("subdir/undef.js")));
assert.isFalse(ignoredPaths.contains(getFixturePath("undef.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("subdir/foo.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("node_modules/bar.js")));
});
it("should resolve relative paths from the ignorePath when it contains negated globs", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath("subdir/.eslintignoreInChildDir"), cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains("subdir/blah.txt"));
assert.isTrue(ignoredPaths.contains("blah.txt"));
assert.isFalse(ignoredPaths.contains("subdir/bar.txt"));
assert.isTrue(ignoredPaths.contains("bar.txt"));
assert.isFalse(ignoredPaths.contains("subdir/baz.txt"));
assert.isFalse(ignoredPaths.contains("baz.txt"));
});
it("should resolve default ignore patterns from the CWD even when the ignorePath is in a subdirectory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath("subdir/.eslintignoreInChildDir"), cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains("node_modules/blah.js"));
});
it("should resolve default ignore patterns from the CWD even when the ignorePath is in a parent directory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignoreForDifferentCwd"), cwd: getFixturePath("subdir") });
assert.isTrue(ignoredPaths.contains("node_modules/blah.js"));
});
it("should handle .eslintignore which contains CRLF correctly.", () => {
const ignoreFileContent = fs.readFileSync(getFixturePath("crlf/.eslintignore"), "utf8");
assert.isTrue(ignoreFileContent.includes("\r"), "crlf/.eslintignore should contains CR.");
const ignoredPaths = new IgnoredPaths({
ignore: true,
ignorePath: getFixturePath("crlf/.eslintignore"),
cwd: getFixturePath()
});
assert.isTrue(ignoredPaths.contains(getFixturePath("crlf/hide1/a.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("crlf/hide2/a.js")));
assert.isFalse(ignoredPaths.contains(getFixturePath("crlf/hide3/a.js")));
});
});
describe("initialization with ignorePath containing commented lines", () => {
let ignoreFilePath;
before(() => {
ignoreFilePath = getFixturePath(".eslintignoreWithComments");
});
it("should not include comments in ignore rules", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath() });
const ignorePatterns = getIgnorePatterns(ignoredPaths);
assert.strictEqual(getIgnoreRules(ignoredPaths).length, countDefaultPatterns(ignoredPaths) + 1);
assert.include(ignorePatterns, "this_one_not");
});
});
describe("initialization with ignorePath containing negations", () => {
let ignoreFilePath;
before(() => {
ignoreFilePath = getFixturePath(".eslintignoreWithNegation");
});
it("should ignore a non-negated pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("negation", "ignore.js")));
});
it("should not ignore a negated pattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: ignoreFilePath, cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("negation", "unignore.js")));
});
});
describe("default ignores", () => {
it("should contain /bower_components/*", () => {
const ignoredPaths = new IgnoredPaths();
assert.include(ignoredPaths.defaultPatterns, "/bower_components/*");
});
it("should contain /node_modules/*", () => {
const ignoredPaths = new IgnoredPaths();
assert.include(ignoredPaths.defaultPatterns, "/node_modules/*");
});
it("should always apply defaultPatterns if ignore option is true", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("bower_components/package/file.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("node_modules/package/file.js")));
});
it("should still apply defaultPatterns if ignore option is is false", () => {
const ignoredPaths = new IgnoredPaths({ ignore: false, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("bower_components/package/file.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("node_modules/package/file.js")));
});
it("should not ignore files in defaultPatterns within a subdirectory", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath("subdir/bower_components/package/file.js")));
assert.isFalse(ignoredPaths.contains(getFixturePath("subdir/node_modules/package/file.js")));
});
it("should allow subfolders of defaultPatterns to be unignored by ignorePattern", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath(), ignorePattern: "!/node_modules/package" });
assert.isFalse(ignoredPaths.contains(getFixturePath("node_modules", "package", "file.js")));
});
it("should allow subfolders of defaultPatterns to be unignored by ignorePath", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath(), ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults") });
assert.isFalse(ignoredPaths.contains(getFixturePath("node_modules", "package", "file.js")));
});
it("should ignore dotfiles", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath(".foo")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo/.bar")));
});
it("should ignore directories beginning with a dot", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath(".foo/bar")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo/.bar/baz")));
});
it("should still ignore dotfiles when ignore option disabled", () => {
const ignoredPaths = new IgnoredPaths({ ignore: false, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath(".foo")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo/.bar")));
});
it("should still ignore directories beginning with a dot when ignore option disabled", () => {
const ignoredPaths = new IgnoredPaths({ ignore: false, cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath(".foo/bar")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo/.bar/baz")));
});
it("should not ignore absolute paths containing '..'", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(`${getFixturePath("foo")}/../unignored.js`));
});
it("should ignore /node_modules/ at top level relative to .eslintignore when loaded", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignore"), cwd: getFixturePath() });
assert.isTrue(ignoredPaths.contains(getFixturePath("node_modules", "existing.js")));
assert.isFalse(ignoredPaths.contains(getFixturePath("foo", "node_modules", "existing.js")));
});
it("should ignore /node_modules/ at top level relative to cwd without an .eslintignore", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath("no-ignore-file") });
assert.isTrue(ignoredPaths.contains(getFixturePath("no-ignore-file", "node_modules", "existing.js")));
assert.isFalse(ignoredPaths.contains(getFixturePath("no-ignore-file", "foo", "node_modules", "existing.js")));
});
});
describe("two globstar '**' ignore pattern", () => {
it("should ignore files in nested directories", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePattern: "**/*.js", cwd: getFixturePath() });
assert.isTrue(ignoredPaths instanceof IgnoredPaths);
assert.isTrue(ignoredPaths.contains(getFixturePath("foo.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo/bar.js")));
assert.isTrue(ignoredPaths.contains(getFixturePath("foo/bar/baz.js")));
assert.isFalse(ignoredPaths.contains(getFixturePath("foo.j2")));
assert.isFalse(ignoredPaths.contains(getFixturePath("foo/bar.j2")));
assert.isFalse(ignoredPaths.contains(getFixturePath("foo/bar/baz.j2")));
});
});
describe("dotfiles option", () => {
it("should add at least one pattern when false", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, dotfiles: false, cwd: getFixturePath("no-ignore-file") });
assert(getIgnoreRules(ignoredPaths).length > ignoredPaths.defaultPatterns.length);
});
it("should add no patterns when true", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, dotfiles: true, cwd: getFixturePath("no-ignore-file") });
assert.lengthOf(getIgnoreRules(ignoredPaths), ignoredPaths.defaultPatterns.length);
});
it("should not ignore dotfiles when true", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, dotfiles: true, cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath(".foo")));
assert.isFalse(ignoredPaths.contains(getFixturePath("foo/.bar")));
});
it("should not ignore directories beginning with a dot when true", () => {
const ignoredPaths = new IgnoredPaths({ ignore: true, dotfiles: true, cwd: getFixturePath() });
assert.isFalse(ignoredPaths.contains(getFixturePath(".foo/bar")));
assert.isFalse(ignoredPaths.contains(getFixturePath("foo/.bar/baz")));
});
});
describe("getIgnoredFoldersGlobChecker", () => {
/**
* Creates a function to resolve the given relative path according to the `cwd`
* @param {path} cwd The cwd of `ignorePaths`
* @returns {function()} the function described above.
*/
function createResolve(cwd) {
return function(relative) {
return path.join(cwd, relative);
};
}
it("should ignore default folders when there is no eslintignore file", () => {
const cwd = getFixturePath("no-ignore-file");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isTrue(shouldIgnore(resolve("node_modules/a")));
assert.isTrue(shouldIgnore(resolve("node_modules/a/b")));
assert.isTrue(shouldIgnore(resolve("bower_components/a")));
assert.isTrue(shouldIgnore(resolve("bower_components/a/b")));
assert.isFalse(shouldIgnore(resolve(".hidden")));
assert.isTrue(shouldIgnore(resolve(".hidden/a")));
assert.isFalse(shouldIgnore(resolve("..")));
assert.isFalse(shouldIgnore(resolve("../..")));
assert.isFalse(shouldIgnore(resolve("../foo")));
assert.isFalse(shouldIgnore(resolve("../../..")));
assert.isFalse(shouldIgnore(resolve("../../foo")));
});
it("should ignore default folders when there is an ignore file without unignored defaults", () => {
const cwd = getFixturePath();
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignore"), cwd });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isTrue(shouldIgnore(resolve("node_modules/a")));
assert.isTrue(shouldIgnore(resolve("node_modules/a/b")));
assert.isTrue(shouldIgnore(resolve("bower_components/a")));
assert.isTrue(shouldIgnore(resolve("bower_components/a/b")));
assert.isFalse(shouldIgnore(resolve(".hidden")));
assert.isTrue(shouldIgnore(resolve(".hidden/a")));
});
it("should not ignore things which are re-included in ignore file", () => {
const cwd = getFixturePath();
const ignoredPaths = new IgnoredPaths({ ignore: true, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isTrue(shouldIgnore(resolve("node_modules/a")));
assert.isTrue(shouldIgnore(resolve("node_modules/a/b")));
assert.isTrue(shouldIgnore(resolve("bower_components/a")));
assert.isTrue(shouldIgnore(resolve("bower_components/a/b")));
assert.isFalse(shouldIgnore(resolve(".hidden")));
assert.isTrue(shouldIgnore(resolve(".hidden/a")));
assert.isFalse(shouldIgnore(resolve("node_modules/package")));
assert.isFalse(shouldIgnore(resolve("bower_components/package")));
assert.isFalse(shouldIgnore(resolve(".hidden/package")));
});
it("should ignore files which we try to re-include in ignore file when ignore option is disabled", () => {
const cwd = getFixturePath();
const ignoredPaths = new IgnoredPaths({ ignore: false, ignorePath: getFixturePath(".eslintignoreWithUnignoredDefaults"), cwd });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isTrue(shouldIgnore(resolve("node_modules/a")));
assert.isTrue(shouldIgnore(resolve("node_modules/a/b")));
assert.isTrue(shouldIgnore(resolve("bower_components/a")));
assert.isTrue(shouldIgnore(resolve("bower_components/a/b")));
assert.isFalse(shouldIgnore(resolve(".hidden")));
assert.isTrue(shouldIgnore(resolve(".hidden/a")));
assert.isTrue(shouldIgnore(resolve("node_modules/package")));
assert.isTrue(shouldIgnore(resolve("bower_components/package")));
assert.isTrue(shouldIgnore(resolve(".hidden/package")));
});
it("should not ignore dirs which are re-included by ignorePattern", () => {
const cwd = getFixturePath("no-ignore-file");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd, ignorePattern: "!/node_modules/package" });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isTrue(shouldIgnore(resolve("node_modules/a")));
assert.isTrue(shouldIgnore(resolve("node_modules/a/b")));
assert.isTrue(shouldIgnore(resolve("bower_components/a")));
assert.isTrue(shouldIgnore(resolve("bower_components/a/b")));
assert.isFalse(shouldIgnore(resolve(".hidden")));
assert.isTrue(shouldIgnore(resolve(".hidden/a")));
assert.isFalse(shouldIgnore(resolve("node_modules/package")));
assert.isTrue(shouldIgnore(resolve("bower_components/package")));
});
it("should not ignore hidden dirs when dotfiles is enabled", () => {
const cwd = getFixturePath("no-ignore-file");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd, dotfiles: true });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isFalse(shouldIgnore(resolve(".hidden")));
assert.isFalse(shouldIgnore(resolve(".hidden/a")));
});
it("should use the ignorePath's directory as the base to resolve relative paths, not cwd", () => {
const cwd = getFixturePath("subdir");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd, ignorePath: getFixturePath(".eslintignoreForDifferentCwd") });
const shouldIgnore = ignoredPaths.getIgnoredFoldersGlobChecker();
const resolve = createResolve(cwd);
assert.isFalse(shouldIgnore(resolve("undef.js")));
assert.isTrue(shouldIgnore(resolve("../undef.js")));
});
});
});