Skip to content

Commit c1818cc

Browse files
committed
Implemented uninstall test
1 parent 8300438 commit c1818cc

File tree

2 files changed

+97
-77
lines changed

2 files changed

+97
-77
lines changed

src/cli/commands/uninstall.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function run(
3434
let step = 0;
3535

3636
async function runInstall() {
37-
let lockfile = new Lockfile(null, false);
37+
let lockfile = await Lockfile.fromDirectory(config.cwd, reporter, {});
3838
let install = new Install("uninstall", flags, [], config, new NoopReporter, lockfile);
3939
await install.init();
4040
return install;
@@ -53,7 +53,7 @@ export async function run(
5353

5454
// remove
5555
for (let name of args) {
56-
let loc = path.join("node_modules", name);
56+
let loc = path.join(config.cwd, "node_modules", name);
5757
reporter.step(++step, totalSteps, `Removing module ${name}`);
5858

5959
// check that it's there

test/commands/install.js

Lines changed: 95 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async function run(flags, args, name, checkInstalled, beforeInstall) {
6666
await install.init();
6767

6868
if (checkInstalled) {
69-
await checkInstalled(cwd);
69+
await checkInstalled(config, reporter);
7070
}
7171

7272
// clean up
@@ -95,50 +95,50 @@ test("install with arg that has binaries", () => {
9595

9696
test("install with --save and offline mirror", () => {
9797
let mirrorPath = "mirror-for-offline";
98-
return run({save: true}, ["is-array@1.0.1"], "install-with-save-offline-mirror", async (cwd) => {
98+
return run({save: true}, ["is-array@1.0.1"], "install-with-save-offline-mirror", async (config) => {
9999

100-
let allFiles = await fs.walk(cwd);
100+
let allFiles = await fs.walk(config.cwd);
101101

102102
assert(allFiles.findIndex((file) => {
103103
return file.relative === `${mirrorPath}/is-array-1.0.1.tgz`;
104104
}) !== -1);
105105

106-
let rawLockfile = await fs.readFile(path.join(cwd, constants.LOCKFILE_FILENAME));
106+
let rawLockfile = await fs.readFile(path.join(config.cwd, constants.LOCKFILE_FILENAME));
107107
let lockfile = parse(rawLockfile);
108108
assert.equal(lockfile["is-array@1.0.1"]["resolved"],
109109
"is-array-1.0.1.tgz#e9850cc2cc860c3bc0977e84ccf0dd464584279a");
110110

111-
await fs.unlink(path.join(cwd, mirrorPath));
112-
await fs.unlink(path.join(cwd, "package.json"));
111+
await fs.unlink(path.join(config.cwd, mirrorPath));
112+
await fs.unlink(path.join(config.cwd, "package.json"));
113113
return allFiles;
114114
});
115115
});
116116

117117
test("install with --save and without offline mirror", () => {
118118
let mirrorPath = "mirror-for-offline";
119-
return run({save: true}, ["is-array@1.0.1"], "install-with-save-no-offline-mirror", async (cwd) => {
119+
return run({save: true}, ["is-array@1.0.1"], "install-with-save-no-offline-mirror", async (config) => {
120120

121-
let allFiles = await fs.walk(cwd);
121+
let allFiles = await fs.walk(config.cwd);
122122

123123
assert(allFiles.findIndex((file) => {
124124
return file.relative === `${mirrorPath}/is-array-1.0.1.tgz`;
125125
}) === -1);
126126

127-
let rawLockfile = await fs.readFile(path.join(cwd, constants.LOCKFILE_FILENAME));
127+
let rawLockfile = await fs.readFile(path.join(config.cwd, constants.LOCKFILE_FILENAME));
128128
let lockfile = parse(rawLockfile);
129129
assert.equal(lockfile["is-array@1.0.1"]["resolved"],
130130
"https://registry.npmjs.org/is-array/-/is-array-1.0.1.tgz#e9850cc2cc860c3bc0977e84ccf0dd464584279a");
131131

132-
await fs.unlink(path.join(cwd, mirrorPath));
133-
await fs.unlink(path.join(cwd, "package.json"));
132+
await fs.unlink(path.join(config.cwd, mirrorPath));
133+
await fs.unlink(path.join(config.cwd, "package.json"));
134134
return allFiles;
135135
});
136136
});
137137

138138
test("install from offline mirror", () => {
139-
return run({}, [], "install-from-offline-mirror", async (cwd) => {
139+
return run({}, [], "install-from-offline-mirror", async (config) => {
140140

141-
let allFiles = await fs.walk(cwd);
141+
let allFiles = await fs.walk(config.cwd);
142142

143143
assert(allFiles.findIndex((file) => {
144144
return file.relative === "node_modules/fake-fbkpm-dependency/package.json";
@@ -152,23 +152,23 @@ test("install should dedupe dependencies avoiding conflicts 0", () => {
152152
// A@2.0.1 -> B@2.0.0
153153
// B@1.0.0
154154
// should result in B@2.0.0 not flattened
155-
return run({}, [], "install-should-dedupe-avoiding-conflicts-0", async (cwd) => {
156-
let rawDepBPackage = await fs.readFile(path.join(cwd, "node_modules/dep-b/package.json"));
155+
return run({}, [], "install-should-dedupe-avoiding-conflicts-0", async (config) => {
156+
let rawDepBPackage = await fs.readFile(path.join(config.cwd, "node_modules/dep-b/package.json"));
157157
assert.equal(JSON.parse(rawDepBPackage).version, "1.0.0");
158158

159-
rawDepBPackage = await fs.readFile(path.join(cwd, "node_modules/dep-a/node_modules/dep-b/package.json"));
159+
rawDepBPackage = await fs.readFile(path.join(config.cwd, "node_modules/dep-a/node_modules/dep-b/package.json"));
160160
assert.equal(JSON.parse(rawDepBPackage).version, "2.0.0");
161161
});
162162
});
163163

164164
test("install should dedupe dependencies avoiding conflicts 1", () => {
165165
// A@2.0.1 -> B@2.0.0
166166
// should result in B@2.0.0 flattened
167-
return run({}, [], "install-should-dedupe-avoiding-conflicts-1", async (cwd) => {
168-
let rawDepBPackage = await fs.readFile(path.join(cwd, "node_modules/dep-b/package.json"));
167+
return run({}, [], "install-should-dedupe-avoiding-conflicts-1", async (config) => {
168+
let rawDepBPackage = await fs.readFile(path.join(config.cwd, "node_modules/dep-b/package.json"));
169169
assert.equal(JSON.parse(rawDepBPackage).version, "2.0.0");
170170

171-
rawDepBPackage = await fs.readFile(path.join(cwd, "node_modules/dep-a/package.json"));
171+
rawDepBPackage = await fs.readFile(path.join(config.cwd, "node_modules/dep-a/package.json"));
172172
assert.equal(JSON.parse(rawDepBPackage).version, "2.0.1");
173173
});
174174
});
@@ -183,18 +183,18 @@ test("install should dedupe dependencies avoiding conflicts 2", () => {
183183
// B@1 -> C@1
184184
// C@2
185185

186-
return run({}, [], "install-should-dedupe-avoiding-conflicts-2", async (cwd) => {
187-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
186+
return run({}, [], "install-should-dedupe-avoiding-conflicts-2", async (config) => {
187+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
188188
"node_modules/dep-a/package.json"))).version, "2.0.0");
189-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
189+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
190190
"node_modules/dep-a/node_modules/dep-b/package.json"))).version, "2.0.0");
191-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
191+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
192192
"node_modules/dep-c/package.json"))).version, "2.0.0");
193-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
193+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
194194
"node_modules/dep-d/package.json"))).version, "1.0.0");
195-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
195+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
196196
"node_modules/dep-b/package.json"))).version, "1.0.0");
197-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
197+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
198198
"node_modules/dep-b/node_modules/dep-c/package.json"))).version, "1.0.0");
199199
});
200200
});
@@ -208,16 +208,16 @@ test("install should dedupe dependencies avoiding conflicts 3", () => {
208208
// B@2
209209
// C@2
210210
// D@1
211-
return run({}, [], "install-should-dedupe-avoiding-conflicts-3", async (cwd) => {
212-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
211+
return run({}, [], "install-should-dedupe-avoiding-conflicts-3", async (config) => {
212+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
213213
"node_modules/dep-a/package.json"))).version, "2.0.0");
214-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
214+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
215215
"node_modules/dep-c/package.json"))).version, "2.0.0");
216-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
216+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
217217
"node_modules/dep-d/package.json"))).version, "1.0.0");
218-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
218+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
219219
"node_modules/dep-b/package.json"))).version, "2.0.0");
220-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
220+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
221221
"node_modules/dep-a/node_modules/dep-c/package.json"))).version, "1.0.0");
222222
});
223223
});
@@ -233,16 +233,16 @@ test("install should dedupe dependencies avoiding conflicts 4", () => {
233233
// C@2
234234
// B@2
235235
// D@1
236-
return run({}, [], "install-should-dedupe-avoiding-conflicts-4", async (cwd) => {
237-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
236+
return run({}, [], "install-should-dedupe-avoiding-conflicts-4", async (config) => {
237+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
238238
"node_modules/dep-a/package.json"))).version, "2.0.0");
239-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
239+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
240240
"node_modules/dep-c/package.json"))).version, "2.0.0");
241-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
241+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
242242
"node_modules/dep-d/package.json"))).version, "1.0.0");
243-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
243+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
244244
"node_modules/dep-b/package.json"))).version, "2.0.0");
245-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
245+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
246246
"node_modules/dep-a/node_modules/dep-c/package.json"))).version, "1.0.0");
247247
});
248248
});
@@ -259,18 +259,18 @@ test("install should dedupe dependencies avoiding conflicts 5", () => {
259259
// D@1 -> A@2
260260
// -> B@2
261261

262-
return run({}, [], "install-should-dedupe-avoiding-conflicts-5", async (cwd) => {
263-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
262+
return run({}, [], "install-should-dedupe-avoiding-conflicts-5", async (config) => {
263+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
264264
"node_modules/dep-a/package.json"))).version, "1.0.0");
265-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
265+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
266266
"node_modules/dep-b/package.json"))).version, "1.0.0");
267-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
267+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
268268
"node_modules/dep-c/package.json"))).version, "1.0.0");
269-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
269+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
270270
"node_modules/dep-d/package.json"))).version, "1.0.0");
271-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
271+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
272272
"node_modules/dep-d/node_modules/dep-a/package.json"))).version, "2.0.0");
273-
assert.equal(JSON.parse(await fs.readFile(path.join(cwd,
273+
assert.equal(JSON.parse(await fs.readFile(path.join(config.cwd,
274274
"node_modules/dep-d/node_modules/dep-b/package.json"))).version, "2.0.0");
275275

276276
});
@@ -288,47 +288,47 @@ test("upgrade scenario", () => {
288288
await fs.unlink(path.join(cwd, "package.json"));
289289
}
290290

291-
return run({ save: true }, ["left-pad@0.0.9"], "install-upgrade-scenario", async (cwd) => {
291+
return run({ save: true }, ["left-pad@0.0.9"], "install-upgrade-scenario", async (config) => {
292292
assert.equal(
293-
JSON.parse(await fs.readFile(path.join(cwd, "node_modules/left-pad/package.json"))).version,
293+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/left-pad/package.json"))).version,
294294
"0.0.9"
295295
);
296296
assert.deepEqual(
297-
JSON.parse(await fs.readFile(path.join(cwd, "package.json"))).dependencies,
297+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies,
298298
{"left-pad": "0.0.9"}
299299
);
300300

301-
let lockFileWritten = await fs.readFile(path.join(cwd, "fbkpm.lock"));
301+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
302302
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
303303
assert.equal(lockFileLines[0], "left-pad@0.0.9:");
304304
assert.equal(lockFileLines.length, 4);
305305
assert.notEqual(lockFileLines[3].indexOf("resolved left-pad-0.0.9.tgz"), -1);
306306

307-
let mirror = await fs.walk(path.join(cwd, mirrorPath));
307+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
308308
assert.equal(mirror.length, 1);
309309
assert.equal(mirror[0].relative, "left-pad-0.0.9.tgz");
310310

311-
return run({save: true}, ["left-pad@1.1.0"], "install-upgrade-scenario", async (cwd) => {
311+
return run({save: true}, ["left-pad@1.1.0"], "install-upgrade-scenario", async (config) => {
312312
assert.equal(
313-
JSON.parse(await fs.readFile(path.join(cwd, "node_modules/left-pad/package.json"))).version,
313+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/left-pad/package.json"))).version,
314314
"1.1.0"
315315
);
316316
assert.deepEqual(
317-
JSON.parse(await fs.readFile(path.join(cwd, "package.json"))).dependencies,
317+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies,
318318
{"left-pad": "1.1.0"}
319319
);
320320

321-
let lockFileWritten = await fs.readFile(path.join(cwd, "fbkpm.lock"));
321+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
322322
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
323323
assert.equal(lockFileLines[0], "left-pad@1.1.0:");
324324
assert.equal(lockFileLines.length, 4);
325325
assert.notEqual(lockFileLines[3].indexOf("resolved left-pad-1.1.0.tgz"), -1);
326326

327-
let mirror = await fs.walk(path.join(cwd, mirrorPath));
327+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
328328
assert.equal(mirror.length, 2);
329329
assert.equal(mirror[1].relative, "left-pad-1.1.0.tgz");
330330

331-
await clean(cwd);
331+
await clean(config.cwd);
332332
});
333333
}, clean);
334334
});
@@ -337,65 +337,85 @@ test("downgrade scenario", () => {
337337
// left-pad first installed 1.1.0 then downgraded to 0.0.9
338338
// files in mirror, fbkpm.lock, package.json and node_modules should reflect that
339339

340-
return run({save: true}, ["left-pad@1.1.0"], "install-downgrade-scenario", async (cwd) => {
340+
return run({save: true}, ["left-pad@1.1.0"], "install-downgrade-scenario", async (config) => {
341341
assert.equal(
342-
JSON.parse(await fs.readFile(path.join(cwd, "node_modules/left-pad/package.json"))).version,
342+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/left-pad/package.json"))).version,
343343
"1.1.0"
344344
);
345345
assert.deepEqual(
346-
JSON.parse(await fs.readFile(path.join(cwd, "package.json"))).dependencies,
346+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies,
347347
{"left-pad": "1.1.0"}
348348
);
349349

350350
let mirrorPath = "mirror-for-offline";
351-
let lockFileWritten = await fs.readFile(path.join(cwd, "fbkpm.lock"));
351+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
352352
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
353353
assert.equal(lockFileLines[0], "left-pad@1.1.0:");
354354
assert.equal(lockFileLines.length, 4);
355355
assert.notEqual(lockFileLines[3].indexOf("resolved left-pad-1.1.0.tgz"), -1);
356356

357-
let mirror = await fs.walk(path.join(cwd, mirrorPath));
357+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
358358
assert.equal(mirror.length, 1);
359359
assert.equal(mirror[0].relative, "left-pad-1.1.0.tgz");
360360

361-
return run({save: true}, ["left-pad@0.0.9"], "install-downgrade-scenario", async (cwd) => {
361+
return run({save: true}, ["left-pad@0.0.9"], "install-downgrade-scenario", async (config) => {
362362
assert.equal(
363-
JSON.parse(await fs.readFile(path.join(cwd, "node_modules/left-pad/package.json"))).version,
363+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/left-pad/package.json"))).version,
364364
"0.0.9"
365365
);
366366
assert.deepEqual(
367-
JSON.parse(await fs.readFile(path.join(cwd, "package.json"))).dependencies,
367+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies,
368368
{"left-pad": "0.0.9"}
369369
);
370370

371-
let lockFileWritten = await fs.readFile(path.join(cwd, "fbkpm.lock"));
371+
let lockFileWritten = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
372372
let lockFileLines = lockFileWritten.split("\n").filter((line) => !!line);
373373
assert.equal(lockFileLines[0], "left-pad@0.0.9:");
374374
assert.equal(lockFileLines.length, 4);
375375
assert.notEqual(lockFileLines[3].indexOf("resolved left-pad-0.0.9.tgz"), -1);
376376

377-
let mirror = await fs.walk(path.join(cwd, mirrorPath));
377+
let mirror = await fs.walk(path.join(config.cwd, mirrorPath));
378378
assert.equal(mirror.length, 2);
379379
assert.equal(mirror[0].relative, "left-pad-0.0.9.tgz");
380380

381-
await fs.unlink(path.join(cwd, mirrorPath));
382-
await fs.unlink(path.join(cwd, "fbkpm.lock"));
383-
await fs.unlink(path.join(cwd, "package.json"));
381+
await fs.unlink(path.join(config.cwd, mirrorPath));
382+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
383+
await fs.unlink(path.join(config.cwd, "package.json"));
384384
});
385385
});
386386
});
387387

388-
test.skip("uninstall should remove dependency from package.json, fbkpm.lock and node_modules", () => {
389-
return run({}, [], "uninstall-should-clean", async (cwd) => {
388+
test("uninstall should remove dependency from package.json, fbkpm.lock and node_modules", () => {
389+
let mirrorPath = "mirror-for-offline";
390+
391+
return run({}, [], "uninstall-should-clean", async (config, reporter) => {
390392
assert.equal(
391-
JSON.parse(await fs.readFile(path.join(cwd, "node_modules/dep-a/package.json"))).version,
393+
JSON.parse(await fs.readFile(path.join(config.cwd, "node_modules/dep-a/package.json"))).version,
392394
"1.0.0"
393395
);
394396

395-
let reporter = new reporters.NoopReporter;
396-
let config = new Config(reporter, { cwd });
397-
await config.init();
397+
await fs.copy(path.join(config.cwd, "fbkpm.lock"), path.join(config.cwd, "fbkpm.lock.orig"));
398+
await fs.copy(path.join(config.cwd, "package.json"), path.join(config.cwd, "package.json.orig"));
398399

399400
await uninstall(config, reporter, {}, ["dep-a"]);
401+
402+
assert(!await fs.exists(path.join(config.cwd, "node_modules/dep-a")));
403+
assert(await fs.exists(path.join(config.cwd, `${mirrorPath}/dep-a-1.0.0.tgz`)));
404+
405+
assert.deepEqual(
406+
JSON.parse(await fs.readFile(path.join(config.cwd, "package.json"))).dependencies,
407+
{}
408+
);
409+
410+
let lockFileContent = await fs.readFile(path.join(config.cwd, "fbkpm.lock"));
411+
let lockFileLines = lockFileContent.split("\n").filter((line) => !!line);
412+
assert.equal(lockFileLines.length, 0);
413+
414+
await fs.unlink(path.join(config.cwd, "fbkpm.lock"));
415+
await fs.unlink(path.join(config.cwd, "package.json"));
416+
await fs.copy(path.join(config.cwd, "fbkpm.lock.orig"), path.join(config.cwd, "fbkpm.lock"));
417+
await fs.copy(path.join(config.cwd, "package.json.orig"), path.join(config.cwd, "package.json"));
418+
await fs.unlink(path.join(config.cwd, "fbkpm.lock.orig"));
419+
await fs.unlink(path.join(config.cwd, "package.json.orig"));
400420
});
401421
});

0 commit comments

Comments
 (0)