Debian Codegraph @

fs2/test/is-ignored.js

raw
1 /* eslint max-lines: off */
2
3 "use strict";
4
5 const fs = require("fs")
6 , { resolve } = require("path")
7 , noop = require("es5-ext/function/noop")
8 , deferred = require("deferred")
9 , { delay } = deferred
10 , { promisify } = deferred
11 , mkdir = promisify(fs.mkdir)
12 , writeFile = promisify(fs.writeFile)
13 , unlink = promisify(fs.unlink)
14 , rmdir = promisify(fs.rmdir)
15 , modes = require("../lib/ignore-modes")
16 , pgPath = resolve(__dirname, "./__playground/is-ignored");
17
18 module.exports = function (t, a, d) {
19 let invoked = null, testIsRoot, watcher;
20 const DELAY = 100
21 , gitRoot = resolve(pgPath, ".git")
22 , rootFile = resolve(pgPath, ".gitignore")
23 , onePath = resolve(pgPath, "one")
24 , oneFile = resolve(onePath, ".gitignore")
25 , twoPath = resolve(onePath, "two")
26 , twoFile = resolve(twoPath, ".gitignore")
27 , twoOtherFile = resolve(twoPath, ".ignore")
28 , twoFooPath = resolve(twoPath, "foo");
29
30 modes.test = {
31 filename: ".ignore",
32 isRoot: (testIsRoot = function (path) {
33 const promise = deferred(path === onePath);
34 promise.close = noop;
35 return promise;
36 }),
37 isRootWatcher: testIsRoot
38 };
39
40 // Make .git dir
41 deferred(
42 mkdir(gitRoot), mkdir(onePath)(() => mkdir(twoPath))
43 )(
44 delay(() => {
45 t(
46 "git", resolve(gitRoot, "foo/bar")
47 )(value => { a(value, true, "Ignore gitrepo file"); }).done();
48 watcher = t("git", twoFooPath, { watch: true });
49 watcher.on("change", arg => {
50 a(invoked, null, "Invoked once");
51 invoked = arg;
52 });
53 watcher.done();
54 return t("git", twoFooPath);
55 }, DELAY)
56 )(
57 delay(value => {
58 a(value, false, "#1");
59
60 // Write root .gitignore with "foo"
61 return writeFile(rootFile, "foo");
62 }, DELAY)
63 )(
64 delay(() => {
65 a(invoked, true, "#2 event");
66 invoked = null;
67 return t("git", twoFooPath);
68 }, DELAY)
69 )(value => {
70 a(value, true, "#2");
71
72 // Write root/one .gitignore with "/two/foo"
73 return writeFile(oneFile, "/two/foo");
74 })(
75 delay(() => {
76 a(invoked, null, "#3 event");
77 return t("git", twoFooPath);
78 }, DELAY)
79 )(value => {
80 a(value, true, "#3");
81
82 // Write root/one/two .gitignore with "!foo"
83 return writeFile(twoFile, "!foo");
84 })(
85 delay(() => {
86 a(invoked, false, "#4 event");
87 invoked = null;
88 return t("git", twoFooPath);
89 }, DELAY)
90 )(value => {
91 a(value, false, "#4");
92
93 // Remove root .gitignore
94 // Remove root/one .gitignore
95 return deferred(unlink(rootFile), unlink(oneFile));
96 })(
97 delay(() => {
98 a(invoked, null, "#5 event");
99 return t("git", twoFooPath);
100 }, DELAY)
101 )(value => {
102 a(value, false, "#5");
103
104 // Remove root/one/two .gitignore
105 return unlink(twoFile);
106 })(
107 delay(() => {
108 a(invoked, null, "#6 event");
109 invoked = null;
110 return t("git", twoFooPath);
111 }, DELAY)
112 )(value => {
113 a(value, false, "#6");
114
115 // Write root/one .gitignore with "/two/foo\n!/two/foo"
116 return writeFile(oneFile, "/two/foo\n!/two/foo");
117 })(
118 delay(() => {
119 a(invoked, null, "#7 event");
120 invoked = null;
121 return t("git", twoFooPath);
122 }, DELAY)
123 )(value => {
124 a(value, false, "#7");
125
126 // Write root .gitignore with "two"
127 return writeFile(rootFile, "two");
128 })(
129 delay(() => {
130 a(invoked, null, "#8 event");
131 return t("git", twoFooPath);
132 }, DELAY)
133 )(value => {
134 a(value, false, "#8");
135
136 // Remove root .gitignore
137 return unlink(rootFile);
138 })(
139 delay(() => {
140 a(invoked, null, "#9 event");
141 return t("git", twoFooPath);
142 }, DELAY)
143 )(value => {
144 a(value, false, "#9");
145
146 // Write root .gitignore with "/one"
147 return writeFile(rootFile, "/one");
148 })(
149 delay(() => {
150 a(invoked, true, "#10 event");
151 invoked = null;
152 return t("git", twoFooPath);
153 }, DELAY)
154 )(value => {
155 a(value, true, "#10");
156 watcher.close();
157
158 // Write root .gitignore with "one\n!one/two/foo"
159 // Remove root/one .gitignore
160 return deferred(writeFile(rootFile, "one\n!one/two/foo"), unlink(oneFile));
161 })(
162 delay(() => {
163 a(invoked, null, "#11 event");
164 invoked = null;
165 return t("git", twoFooPath);
166 }, DELAY)
167 )(value => {
168 a(value, true, "#11");
169
170 // Remove root .gitignore
171 return unlink(rootFile);
172 })(
173 delay(() => {
174 invoked = null;
175
176 watcher = t(["git", "test"], twoFooPath, { watch: true });
177 watcher.on("change", arg => {
178 a(invoked, null, "Invoked once");
179 invoked = arg;
180 });
181 watcher.done();
182 return t("git", twoFooPath);
183 }, DELAY)
184 )(value => {
185 a(value, false, "Both #1");
186
187 // Write root .gitignore with "foo"
188 return writeFile(rootFile, "foo");
189 })(
190 delay(() => {
191 a(invoked, true, "Both #2");
192 invoked = null;
193 return t(["git", "test"], twoFooPath);
194 }, DELAY * 2)
195 )(value => {
196 a(value, true, "Both #2");
197 return writeFile(twoOtherFile, "!foo");
198 })(
199 delay(() => {
200 a(invoked, false, "Both #3");
201 invoked = null;
202 return t(["git", "test"], twoFooPath);
203 }, DELAY)
204 )(value => {
205 a(value, false, "Both #3");
206 return writeFile(rootFile, "one\n");
207 })(
208 delay(() => {
209 a(invoked, true, "Both #4");
210 invoked = null;
211 return t(["git", "test"], twoFooPath);
212 }, DELAY)
213 )(value => {
214 a(value, true, "Both #4");
215 return unlink(rootFile);
216 })(
217 delay(() => {
218 a(invoked, false, "Both #5");
219 invoked = null;
220 return t(["git", "test"], twoFooPath);
221 }, DELAY)
222 )(value => {
223 a(value, false, "Both #5");
224 return writeFile(twoOtherFile, "foo");
225 })(
226 delay(() => {
227 a(invoked, true, "Both #6");
228 invoked = null;
229 return t(["git", "test"], twoFooPath);
230 }, DELAY)
231 )(value => {
232 a(value, true, "Both #6");
233 return writeFile(twoFile, "!foo");
234 })(
235 delay(() => {
236 a(invoked, null, "Both #7");
237 invoked = null;
238 return t(["git", "test"], twoFooPath);
239 }, DELAY)
240 )(value => {
241 a(value, true, "Both #7");
242 return writeFile(twoOtherFile, "!foo");
243 })(
244 delay(() => {
245 a(invoked, false, "Both #8");
246 invoked = null;
247 return t(["git", "test"], twoFooPath);
248 }, DELAY)
249 )(value => {
250 a(value, false, "Both #8");
251 return writeFile(twoFile, "foo");
252 })(
253 delay(() => {
254 a(invoked, null, "Both #9");
255 invoked = null;
256 return t(["git", "test"], twoFooPath);
257 }, DELAY)
258 )(value => {
259 a(value, false, "Both #9");
260 watcher.close();
261 })(delay(() => deferred(unlink(twoFile), unlink(twoOtherFile)), DELAY))(
262 delay(() => {
263 t(null, twoFooPath, { globalRules: "foo" })(value => {
264 a(value, true, "Global: Direct");
265 });
266 t(null, twoFooPath, { globalRules: ["one"] })(value => {
267 a(value, true, "Global: Upper");
268 });
269 t(null, twoFooPath, { globalRules: ["bar"] })(value => {
270 a(value, false, "Global: Not matching");
271 });
272 delete modes.test;
273 return deferred(rmdir(gitRoot), rmdir(twoPath)(() => rmdir(onePath)))(false);
274 }, DELAY)
275 ).done(d, d);
276 };