-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
ignored-paths.js
179 lines (132 loc) · 6.8 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
/**
* @fileoverview Tests for IgnoredPaths object.
* @author Jonathan Rajavuori
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
var assert = require("chai").assert,
path = require("path"),
IgnoredPaths = require("../../lib/ignored-paths.js"),
sinon = require("sinon"),
fs = require("fs");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("IgnoredPaths", function() {
describe("initialization", function() {
it("should travel to parent directories to find .eslintignore", function() {
var cwd, ignoredPaths;
cwd = process.cwd();
process.chdir(path.resolve(__dirname, "..", "fixtures", "configurations"));
try {
ignoredPaths = IgnoredPaths.load({ ignore: true });
assert.ok(ignoredPaths.patterns.length > 1);
assert.equal(ignoredPaths.patterns[0], "node_modules/**");
assert.equal(ignoredPaths.patterns[1], "bower_components/**");
} finally {
process.chdir(cwd);
}
});
it("should load empty array with ignore option off or missing", function() {
var ignoredPaths = IgnoredPaths.load();
assert.isArray(ignoredPaths.patterns);
assert.lengthOf(ignoredPaths.patterns, 0);
});
it("should accept an array for options.ignorePattern", function() {
var ignorePattern = ["a", "b"];
var ignoredPaths = IgnoredPaths.load({
ignore: false,
ignorePattern: ignorePattern
});
assert.ok(ignorePattern.every(function(pattern) {
return ignoredPaths.patterns.indexOf(pattern) >= 0;
}));
});
});
describe("initialization with specific file", function() {
var filepath = path.resolve(__dirname, "..", "fixtures", ".eslintignore2");
it("should work", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.notEqual(ignoredPaths.patterns.length, 1);
});
});
describe("initialization with invalid file", function() {
var filepath = path.resolve(__dirname, "..", "fixtures", "configurations", ".foobaz");
it("should throw error", function() {
assert.throws(function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths);
}, "Cannot read ignore file");
});
});
describe("contains", function() {
var filepath = path.resolve(__dirname, "..", "fixtures", ".eslintignore2");
it("should throw if load wasn't called", function() {
var ignoredPaths = new IgnoredPaths(null);
assert.throw(ignoredPaths.contains, Error);
});
it("should return true for file matching an ignore pattern", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("undef.js"));
});
it("should return true for file matching an ignore pattern with leading './'", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("undef2.js"));
});
it("should return true for file with leading './' matching an ignore pattern without leading './'", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("./undef3.js"));
});
it("should return true for file matching a child of an ignore pattern", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("undef.js/subdir/grandsubdir"));
});
it("should return true for file matching a child of an ignore pattern with windows line termination", function() {
var stub = sinon.stub(fs, "readFileSync");
stub.withArgs("test", "utf8").returns("undef.js\r\n");
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: "test" });
assert.ok(ignoredPaths.contains("undef.js/subdir/grandsubdir"));
stub.restore();
});
it("should always ignore files in node_modules", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("node_modules/mocha/bin/mocha"));
});
it("should always ignore files in bower_components", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("bower_components/mocha/bin/mocha"));
});
it("should not ignore files in node_modules in a subdirectory", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.notOk(ignoredPaths.contains("subdir/node_modules/test.js"));
});
it("should not ignore files in bower_components in a subdirectory", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.notOk(ignoredPaths.contains("subdir/bower_components/test.js"));
});
it("should return false for file not matching any ignore pattern", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.notOk(ignoredPaths.contains("./passing.js"));
});
});
describe("initialization with commented lines", function() {
var filepath = path.resolve(__dirname, "..", "fixtures", ".eslintignore3");
it("should ignore comments", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.equal(ignoredPaths.patterns.length, 3);
});
});
describe("initialization with negations", function() {
var filepath = path.resolve(__dirname, "..", "fixtures", ".eslintignore4");
it("should ignore a normal pattern", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.ok(ignoredPaths.contains("/dir/bar.js"));
});
it("should not ignore a negated pattern", function() {
var ignoredPaths = IgnoredPaths.load({ ignore: true, ignorePath: filepath });
assert.notOk(ignoredPaths.contains("/dir/foo.js"));
});
});
});