From 0dfe73fd48ebb39999e5fa9ecf4907186326a72a Mon Sep 17 00:00:00 2001 From: zkochan Date: Mon, 26 Sep 2016 22:08:22 +0300 Subject: [PATCH] refactor(tests): use async exists checks PR #372 --- test/install.ts | 10 +++++----- test/prune.ts | 40 ++++++++++++++++++++-------------------- test/support/exists.ts | 10 +++++----- test/uninstall.ts | 42 +++++++++++++++++++++--------------------- 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/test/install.ts b/test/install.ts index e6adcd0fa5..acfeb5c6ed 100644 --- a/test/install.ts +++ b/test/install.ts @@ -316,7 +316,7 @@ test('flattening symlinks (minimatch@3.0.0)', async function (t) { const stat = fs.lstatSync(path.join(process.cwd(), 'node_modules', '.store', 'node_modules', 'balanced-match')) t.ok(stat.isSymbolicLink(), 'balanced-match is linked into store node_modules') - const _ = exists(path.join(process.cwd(), 'node_modules', 'balanced-match')) + const _ = await exists(path.join(process.cwd(), 'node_modules', 'balanced-match')) t.ok(!_, 'balanced-match is not linked into main node_modules') }) @@ -325,10 +325,10 @@ test('flattening symlinks (minimatch + balanced-match)', async function (t) { await install(['minimatch@3.0.0'], { quiet: true }) await install(['balanced-match@^0.3.0'], { quiet: true }) - let _ = exists(path.join(process.cwd(), 'node_modules', '.store', 'node_modules', 'balanced-match')) + let _ = await exists(path.join(process.cwd(), 'node_modules', '.store', 'node_modules', 'balanced-match')) t.ok(!_, 'balanced-match is removed from store node_modules') - _ = exists(path.join(process.cwd(), 'node_modules', 'balanced-match')) + _ = await exists(path.join(process.cwd(), 'node_modules', 'balanced-match')) t.ok(_, 'balanced-match now in main node_modules') }) @@ -497,14 +497,14 @@ test("don't fail when peer dependency is fetched from GitHub", t => { return install([local('test-pnpm-peer-deps')], { quiet: true }) }) -test('create a pnpm-debug.log file when the command fails', t => { +test('create a pnpm-debug.log file when the command fails', async function (t) { prepare() const result = spawnSync('ts-node', [pnpmBin, 'install', '@zkochan/i-do-not-exist']) t.equal(result.status, 1, 'install failed') - exists('pnpm-debug.log') + t.ok(await exists('pnpm-debug.log'), 'log file created') t.end() }) diff --git a/test/prune.ts b/test/prune.ts index 88d7d6845b..bb9b83b164 100644 --- a/test/prune.ts +++ b/test/prune.ts @@ -19,34 +19,34 @@ test('prune removes extraneous packages', async function (t) { const store = path.join(process.cwd(), 'node_modules', '.store') const modules = path.join(process.cwd(), 'node_modules') - let stat = exists(path.join(store, 'is-positive@2.0.0')) + let stat = await exists(path.join(store, 'is-positive@2.0.0')) t.ok(!stat, 'extraneous package is removed from store') - stat = existsSymlink(path.join(modules, 'is-positive')) + stat = await existsSymlink(path.join(modules, 'is-positive')) t.ok(!stat, 'extraneous package is removed from node_modules') - stat = exists(path.join(store, '@zkochan+logger@0.1.0')) + stat = await exists(path.join(store, '@zkochan+logger@0.1.0')) t.ok(!stat, 'scoped extraneous package is removed from store') - stat = existsSymlink(path.join(modules, '@zkochan/logger')) + stat = await existsSymlink(path.join(modules, '@zkochan/logger')) t.ok(!stat, 'scoped extraneous package is removed from node_modules') - stat = exists(path.join(store, 'is-negative@2.1.0')) + stat = await exists(path.join(store, 'is-negative@2.1.0')) t.ok(stat, 'dependency package is not removed from store') - stat = existsSymlink(path.join(modules, 'is-negative')) + stat = await existsSymlink(path.join(modules, 'is-negative')) t.ok(stat, 'dependency package is not removed from node_modules') - stat = exists(path.join(store, 'applyq@0.2.1')) + stat = await exists(path.join(store, 'applyq@0.2.1')) t.ok(stat, 'dev dependency package is not removed from store') - stat = existsSymlink(path.join(modules, 'applyq')) + stat = await existsSymlink(path.join(modules, 'applyq')) t.ok(stat, 'dev dependency package is not removed from node_modules') - stat = exists(path.join(store, 'fnumber@0.1.0')) + stat = await exists(path.join(store, 'fnumber@0.1.0')) t.ok(stat, 'optional dependency package is not removed from store') - stat = existsSymlink(path.join(modules, 'fnumber')) + stat = await existsSymlink(path.join(modules, 'fnumber')) t.ok(stat, 'optional dependency package is not removed from node_modules') }) @@ -59,16 +59,16 @@ test('prune removes only the specified extraneous packages', async function (t) const store = path.join(process.cwd(), 'node_modules', '.store') const modules = path.join(process.cwd(), 'node_modules') - let stat = exists(path.join(store, 'is-positive@2.0.0')) + let stat = await exists(path.join(store, 'is-positive@2.0.0')) t.ok(!stat, 'extraneous package is removed from store') - stat = existsSymlink(path.join(modules, 'is-positive')) + stat = await existsSymlink(path.join(modules, 'is-positive')) t.ok(!stat, 'extraneous package is removed from node_modules') - stat = exists(path.join(store, 'is-negative@2.1.0')) + stat = await exists(path.join(store, 'is-negative@2.1.0')) t.ok(stat, 'dependency package is not removed from store') - stat = existsSymlink(path.join(modules, 'is-negative')) + stat = await existsSymlink(path.join(modules, 'is-negative')) t.ok(stat, 'dependency package is not removed from node_modules') }) @@ -96,21 +96,21 @@ test('prune removes dev dependencies in production', async function (t) { const store = path.join(process.cwd(), 'node_modules', '.store') const modules = path.join(process.cwd(), 'node_modules') - let stat = exists(path.join(store, 'is-positive@2.0.0')) + let stat = await exists(path.join(store, 'is-positive@2.0.0')) t.ok(!stat, 'dev dependency package is removed from store') - stat = existsSymlink(path.join(modules, 'is-positive')) + stat = await existsSymlink(path.join(modules, 'is-positive')) t.ok(!stat, 'dev dependency package is removed from node_modules') - stat = exists(path.join(store, 'is-negative@2.1.0')) + stat = await exists(path.join(store, 'is-negative@2.1.0')) t.ok(stat, 'dependency package is not removed from store') - stat = existsSymlink(path.join(modules, 'is-negative')) + stat = await existsSymlink(path.join(modules, 'is-negative')) t.ok(stat, 'dependency package is not removed from node_modules') - stat = exists(path.join(store, 'fnumber@0.1.0')) + stat = await exists(path.join(store, 'fnumber@0.1.0')) t.ok(stat, 'optional dependency package is not removed from store') - stat = existsSymlink(path.join(modules, 'fnumber')) + stat = await existsSymlink(path.join(modules, 'fnumber')) t.ok(stat, 'optional dependency package is not removed from node_modules') }) diff --git a/test/support/exists.ts b/test/support/exists.ts index d1ed3a5b77..fffae28a19 100644 --- a/test/support/exists.ts +++ b/test/support/exists.ts @@ -1,17 +1,17 @@ -import fs = require('fs') +import fs = require('mz/fs') -export default function exists (path: string) { +export default async function exists (path: string) { try { - return fs.statSync(path) + return await fs.stat(path) } catch (err) { if (err.code !== 'ENOENT') throw err } return null } -export function existsSymlink (path: string) { +export async function existsSymlink (path: string) { try { - return fs.lstatSync(path) + return await fs.lstat(path) } catch (err) { if (err.code !== 'ENOENT') throw err } diff --git a/test/uninstall.ts b/test/uninstall.ts index 844ee430bd..ea7bb71429 100644 --- a/test/uninstall.ts +++ b/test/uninstall.ts @@ -13,10 +13,10 @@ test('uninstall package with no dependencies', async function (t) { await install(['is-negative@2.1.0'], { quiet: true, save: true }) await uninstall(['is-negative'], { save: true }) - let stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'is-negative@2.1.0')) + let stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'is-negative@2.1.0')) t.ok(!stat, 'is-negative is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'is-negative')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'is-negative')) t.ok(!stat, 'is-negative is removed from node_modules') const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8') @@ -30,10 +30,10 @@ test('uninstall scoped package', async function (t) { await install(['@zkochan/logger@0.1.0'], { quiet: true, save: true }) await uninstall(['@zkochan/logger'], { save: true }) - let stat = exists(path.join(process.cwd(), 'node_modules', '.store', '@zkochan+logger@0.1.0')) + let stat = await exists(path.join(process.cwd(), 'node_modules', '.store', '@zkochan+logger@0.1.0')) t.ok(!stat, '@zkochan/logger is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', '@zkochan/logger')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', '@zkochan/logger')) t.ok(!stat, '@zkochan/logger is removed from node_modules') const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8') @@ -47,10 +47,10 @@ test('uninstall tarball dependency', async function (t) { await install(['http://registry.npmjs.org/is-array/-/is-array-1.0.1.tgz'], { quiet: true, save: true }) await uninstall(['is-array'], { save: true }) - let stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'is-array-1.0.1#a83102a9c117983e6ff4d85311fb322231abe3d6')) + let stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'is-array-1.0.1#a83102a9c117983e6ff4d85311fb322231abe3d6')) t.ok(!stat, 'is-array is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'is-array')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'is-array')) t.ok(!stat, 'is-array is removed from node_modules') const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8') @@ -64,28 +64,28 @@ test('uninstall package with dependencies and do not touch other deps', async fu await install(['is-negative@2.1.0', 'camelcase-keys@3.0.0'], { quiet: true, save: true }) await uninstall(['camelcase-keys'], { save: true }) - let stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase-keys@2.1.0')) + let stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase-keys@2.1.0')) t.ok(!stat, 'camelcase-keys is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase-keys')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase-keys')) t.ok(!stat, 'camelcase-keys is removed from node_modules') - stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase@3.0.0')) + stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase@3.0.0')) t.ok(!stat, 'camelcase is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase')) t.ok(!stat, 'camelcase is removed from node_modules') - stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'map-obj@1.0.1')) + stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'map-obj@1.0.1')) t.ok(!stat, 'map-obj is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'map-obj')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'map-obj')) t.ok(!stat, 'map-obj is removed from node_modules') - stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'is-negative@2.1.0')) + stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'is-negative@2.1.0')) t.ok(stat, 'is-negative is not removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'is-negative')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'is-negative')) t.ok(stat, 'is-negative is not removed from node_modules') const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8') @@ -102,10 +102,10 @@ test('uninstall package with its bin files', async function (t) { await uninstall(['sh-hello-world'], { save: true }) // check for both a symlink and a file because in some cases the file will be a proxied not symlinked - let stat = existsSymlink(path.join(process.cwd(), 'node_modules', '.bin', 'sh-hello-world')) + let stat = await existsSymlink(path.join(process.cwd(), 'node_modules', '.bin', 'sh-hello-world')) t.ok(!stat, 'sh-hello-world is removed from .bin') - stat = exists(path.join(process.cwd(), 'node_modules', '.bin', 'sh-hello-world')) + stat = await exists(path.join(process.cwd(), 'node_modules', '.bin', 'sh-hello-world')) t.ok(!stat, 'sh-hello-world is removed from .bin') }) @@ -114,19 +114,19 @@ test('keep dependencies used by others', async function (t) { await install(['hastscript@3.0.0', 'camelcase-keys@3.0.0'], { quiet: true, save: true }) await uninstall(['camelcase-keys'], { save: true }) - let stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase-keys@2.1.0')) + let stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase-keys@2.1.0')) t.ok(!stat, 'camelcase-keys is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase-keys')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'camelcase-keys')) t.ok(!stat, 'camelcase-keys is removed from node_modules') - stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase@3.0.0')) + stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'camelcase@3.0.0')) t.ok(stat, 'camelcase is not removed from store') - stat = exists(path.join(process.cwd(), 'node_modules', '.store', 'map-obj@1.0.1')) + stat = await exists(path.join(process.cwd(), 'node_modules', '.store', 'map-obj@1.0.1')) t.ok(!stat, 'map-obj is removed from store') - stat = existsSymlink(path.join(process.cwd(), 'node_modules', 'map-obj')) + stat = await existsSymlink(path.join(process.cwd(), 'node_modules', 'map-obj')) t.ok(!stat, 'map-obj is removed from node_modules') const pkgJson = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')