From 66ae72207e1019c702bd7a46c869de17c70773fc Mon Sep 17 00:00:00 2001 From: Tareq Sharafy Date: Sun, 18 Aug 2019 09:33:11 +0300 Subject: [PATCH] test: deletes node_modules contents but keep the dir itself PR-URL: https://github.com/npm/libcipm/pull/1 Credit: @tareqhs Close: #3 Reviewed-by: @claudiahdz --- test/specs/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/specs/index.js b/test/specs/index.js index fefc9c7..9c4583a 100644 --- a/test/specs/index.js +++ b/test/specs/index.js @@ -168,6 +168,45 @@ test('handles dependency list with only shallow subdeps', t => { }) }) +test('deletes node_modules/ contents, without deleting node_modules/ itself', t => { + const fixture = new Tacks(Dir({ + 'node_modules': Dir({ + 'stale-dependency': Dir({ + 'package.json': File({ + name: 'stale-dependency', + version: '1.0.0' + }) + }) + }), + 'package.json': File({ + name: pkgName, + version: pkgVersion + }), + 'package-lock.json': File({ + dependencies: {}, + lockfileVersion: 1 + }) + })) + fixture.create(prefix) + + let notNodeModulesDeleted = true + const nodeModulesDir = path.join(prefix, 'node_modules') + const watcher = fs.watch(nodeModulesDir, () => { + if (!fs.existsSync(nodeModulesDir)) { + notNodeModulesDeleted = false + } + }) + + return run().then(() => { + t.ok( + !fs.existsSync(path.join(nodeModulesDir, 'stale-dependency')), + 'node_modules/ contents were deleted' + ) + watcher.close() + t.ok(notNodeModulesDeleted, 'node_modules/ itself was not deleted') + }) +}) + test('handles dependency list with only deep subdeps', t => { const fixture = new Tacks(Dir({ 'package.json': File({