From 2a78b96f830bbd834720ccc9eacccc54915ae6f7 Mon Sep 17 00:00:00 2001 From: isaacs Date: Sat, 13 Jul 2019 12:50:31 -0700 Subject: [PATCH] test: check cache for root-owned files This requires that all tests use `common.cache` as their cache folder, and fix the ownership of any cache files that the test is directly managing. It also adds a `sudotest` target which runs the tests as root, and then verifies that no root-owned files have been dropped into the cache. Lastly, several setup() and cleanup() methods have been removed, as they are largely unnecessary. common-tap does all the required setup for test package directories and cache folders, and removing the cache avoids the root-ownership test, which is an important integration requirement. --- package.json | 1 + test/common-tap.js | 56 ++++++++++++--- test/tap/404-parent.js | 3 - test/tap/404-private-registry-scoped.js | 24 +------ test/tap/404-private-registry.js | 23 +------ test/tap/404.js | 2 +- test/tap/access.js | 28 ++++---- .../all-package-metadata-cache-stream-unit.js | 27 +++++--- .../all-package-metadata-entry-stream-unit.js | 29 ++++---- ...all-package-metadata-update-stream-unit.js | 29 ++++---- .../all-package-metadata-write-stream-unit.js | 23 ++++--- test/tap/all-package-metadata.js | 2 +- test/tap/anon-cli-metrics.js | 2 +- test/tap/auto-prune.js | 2 +- .../bitbucket-https-url-with-creds-package.js | 2 +- test/tap/bitbucket-https-url-with-creds.js | 2 +- test/tap/bitbucket-shortcut-package.js | 2 +- test/tap/bitbucket-shortcut.js | 2 +- test/tap/cache-shasum-fork.js | 4 +- test/tap/check-permissions.js | 9 +-- test/tap/ci-header.js | 2 +- test/tap/circular-dep.js | 2 +- test/tap/debug-logs.js | 25 ++----- test/tap/dedupe-git-semver.js | 2 +- test/tap/doctor.js | 2 +- test/tap/false-name.js | 11 +-- test/tap/fetch-package-metadata.js | 23 +------ test/tap/gist-short-shortcut-package.js | 2 +- test/tap/gist-short-shortcut.js | 2 +- test/tap/gist-shortcut-package.js | 2 +- test/tap/gist-shortcut.js | 2 +- test/tap/git-dependency-install-link.js | 2 +- test/tap/git-npmignore.js | 3 +- test/tap/git-prepare.js | 3 +- test/tap/github-shortcut-package.js | 2 +- test/tap/github-shortcut.js | 2 +- test/tap/gitlab-shortcut-package.js | 2 +- test/tap/gitlab-shortcut.js | 2 +- test/tap/ignore-install-link.js | 2 +- test/tap/install-actions.js | 2 +- test/tap/install-dep-classification.js | 2 +- test/tap/install-duplicate-deps-warning.js | 2 +- test/tap/install-package-json-order.js | 2 +- test/tap/install-package-lock-only.js | 2 +- .../install-scoped-with-bundled-dependency.js | 2 +- test/tap/install-shrinkwrapped-git.js | 1 - test/tap/install-with-dev-dep-duplicate.js | 2 +- test/tap/invalid-dep-version-filtering.js | 2 +- test/tap/lockfile-http-deps.js | 2 +- test/tap/ls-peer.js | 2 +- .../optional-metadep-rollback-collision.js | 2 +- test/tap/outdated-git.js | 2 +- test/tap/outdated-latest.js | 2 +- test/tap/outdated-long.js | 17 +---- test/tap/outdated-remote.js | 8 +-- test/tap/outdated-symlink.js | 2 +- test/tap/outdated.js | 13 +--- test/tap/owner.js | 12 +--- test/tap/pack-scoped.js | 2 +- test/tap/pack.js | 2 +- test/tap/peer-deps.js | 2 +- test/tap/prepare.js | 2 +- test/tap/prepublish-only.js | 2 +- test/tap/prepublish.js | 2 +- test/tap/progress-config.js | 7 +- test/tap/prune-with-dev-dep-duplicate.js | 13 +--- test/tap/prune-with-only-dev-deps.js | 12 +--- test/tap/prune.js | 14 +--- test/tap/publish-access-scoped.js | 68 ++++++++----------- test/tap/publish-access-unscoped.js | 68 ++++++++----------- test/tap/publish-config.js | 4 +- test/tap/publish-invalid-semver-tag.js | 10 ++- test/tap/publish-scoped.js | 14 +--- test/tap/publish.js | 23 ++----- test/tap/retry-on-stale-cache.js | 3 +- test/tap/run-script.js | 2 +- test/tap/save-optional.js | 2 +- test/tap/scope-header.js | 2 +- test/tap/scripts-whitespace-windows.js | 2 +- test/tap/search.all-package-search.js | 28 +++----- test/tap/search.js | 48 +++++++------ test/tap/shared-linked.js | 3 +- test/tap/shrinkwrap-default-dev.js | 2 +- test/tap/shrinkwrap-dev-dependency.js | 2 +- test/tap/shrinkwrap-lifecycle-cwd.js | 2 +- test/tap/shrinkwrap-local-dependency.js | 2 +- test/tap/shrinkwrap-optional-dependency.js | 2 +- test/tap/shrinkwrap-optional-platform.js | 2 +- test/tap/shrinkwrap-optional-property.js | 2 +- test/tap/shrinkwrap-prod-dependency-also.js | 2 +- test/tap/shrinkwrap-prod-dependency.js | 2 +- test/tap/shrinkwrap-shared-dev-dependency.js | 2 +- test/tap/sorted-package-json.js | 60 +++++----------- test/tap/spec-local-specifiers.js | 2 +- .../splat-with-only-prerelease-to-latest.js | 13 +--- test/tap/tag-version-prefix.js | 19 ------ test/tap/tagged-version-matching.js | 2 +- test/tap/unpack-foreign-tarball.js | 2 +- test/tap/update-examples.js | 11 ++- test/tap/update-symlink.js | 2 +- test/tap/upgrade-lifecycles.js | 2 +- test/tap/version-allow-same-version.js | 2 +- test/tap/version-consistent-newlines.js | 2 +- test/tap/version-from-git.js | 19 ++---- test/tap/version-git-not-clean.js | 22 +----- test/tap/version-lifecycle.js | 12 +--- test/tap/version-message-config.js | 18 ----- test/tap/version-no-git.js | 13 +--- test/tap/version-no-package.js | 22 +----- test/tap/version-no-tags.js | 15 +--- test/tap/version-prerelease-id.js | 24 ------- test/tap/version-sub-directory-shrinkwrap.js | 17 +---- test/tap/version-sub-directory.js | 2 +- test/tap/version-update-shrinkwrap.js | 21 ++---- test/tap/zz-cleanup.js | 8 --- 115 files changed, 372 insertions(+), 737 deletions(-) delete mode 100644 test/tap/zz-cleanup.js diff --git a/package.json b/package.json index 34653415bb73d..8ba59ee12e3f8 100644 --- a/package.json +++ b/package.json @@ -292,6 +292,7 @@ "tap-cover": "tap -J --nyc-arg=--cache --coverage --timeout 600", "pretest": "standard", "test": "npm run test-tap --", + "sudotest": "sudo npm run tap -- \"test/tap/*.js\"", "posttest": "rimraf test/npm_cache*", "test-coverage": "npm run tap-cover -- \"test/tap/*.js\" \"test/network/*.js\" \"test/broken-under-*/*.js\"", "test-tap": "npm run tap -- \"test/tap/*.js\" \"test/network/*.js\" \"test/broken-under-*/*.js\"", diff --git a/test/common-tap.js b/test/common-tap.js index e15d5dab2487d..44b68d719de1a 100644 --- a/test/common-tap.js +++ b/test/common-tap.js @@ -22,17 +22,57 @@ if (!global.setImmediate || !require('timers').setImmediate) { } var spawn = require('child_process').spawn +const spawnSync = require('child_process').spawnSync var path = require('path') +// space these out to help prevent collisions +const testId = 3 * (+process.env.TAP_CHILD_ID || 0) + // provide a working dir unique to each test const main = require.main.filename -exports.pkg = path.resolve(path.dirname(main), path.basename(main, '.js')) +const testName = path.basename(main, '.js') +exports.pkg = path.resolve(path.dirname(main), testName) +var commonCache = path.resolve(__dirname, 'npm_cache_' + testName) +exports.cache = commonCache + const mkdirp = require('mkdirp') const rimraf = require('rimraf') +rimraf.sync(exports.pkg) +rimraf.sync(commonCache) mkdirp.sync(exports.pkg) +mkdirp.sync(commonCache) +// if we're in sudo mode, make sure that the cache is not root-owned +const isRoot = process.getuid && process.getuid() === 0 +const isSudo = isRoot && process.env.SUDO_UID && process.env.SUDO_GID +if (isSudo) { + const sudoUid = +process.env.SUDO_UID + const sudoGid = +process.env.SUDO_GID + fs.chownSync(commonCache, sudoUid, sudoGid) +} + +const returnCwd = path.dirname(__dirname) +const find = require('which').sync('find') require('tap').teardown(() => { + // work around windows folder locking + process.chdir(returnCwd) try { - rimraf.sync(exports.pkg) + if (isSudo) { + // running tests as sudo. ensure we didn't leave any root-owned + // files in the cache by mistake. + const args = [ commonCache, '-uid', '0' ] + const found = spawnSync(find, args) + const output = found && found.stdout && found.stdout.toString() + if (output.length) { + const er = new Error('Root-owned files left in cache!') + er.testName = main + er.files = output.trim().split('\n') + throw er + } + } + if (!process.env.NO_TEST_CLEANUP) { + rimraf.sync(exports.pkg) + rimraf.sync(commonCache) + } } catch (e) { if (process.platform !== 'win32') { throw e @@ -40,9 +80,6 @@ require('tap').teardown(() => { } }) -// space these out to help prevent collisions -const testId = 3 * (+process.env.TAP_CHILD_ID || 0) - var port = exports.port = 15443 + testId exports.registry = 'http://localhost:' + port @@ -59,8 +96,8 @@ ourenv.npm_config_progress = 'false' ourenv.npm_config_metrics = 'false' ourenv.npm_config_audit = 'false' -var npm_config_cache = path.resolve(__dirname, 'npm_cache_' + testId) -ourenv.npm_config_cache = exports.npm_config_cache = npm_config_cache +ourenv.npm_config_unsafe_perm = 'true' +ourenv.npm_config_cache = commonCache ourenv.npm_config_userconfig = exports.npm_config_userconfig = configCommon.userconfig ourenv.npm_config_globalconfig = exports.npm_config_globalconfig = configCommon.globalconfig ourenv.npm_config_global_style = 'false' @@ -94,7 +131,10 @@ exports.npm = function (cmd, opts, cb) { opts.env = opts.env || process.env if (opts.env._storage) opts.env = Object.assign({}, opts.env._storage) if (!opts.env.npm_config_cache) { - opts.env.npm_config_cache = npm_config_cache + opts.env.npm_config_cache = commonCache + } + if (!opts.env.npm_config_unsafe_perm) { + opts.env.npm_config_unsafe_perm = 'true' } if (!opts.env.npm_config_send_metrics) { opts.env.npm_config_send_metrics = 'false' diff --git a/test/tap/404-parent.js b/test/tap/404-parent.js index 539ead7a45e0e..306a4bc4bf254 100644 --- a/test/tap/404-parent.js +++ b/test/tap/404-parent.js @@ -5,7 +5,6 @@ var osenv = require('osenv') var path = require('path') var fs = require('fs') var rimraf = require('rimraf') -var mkdirp = require('mkdirp') const pkg = common.pkg var mr = require('npm-registry-mock') @@ -26,8 +25,6 @@ test('cleanup', function (t) { }) function setup () { - mkdirp.sync(pkg) - mkdirp.sync(path.resolve(pkg, 'cache')) fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({ author: 'Evan Lucas', name: '404-parent-test', diff --git a/test/tap/404-private-registry-scoped.js b/test/tap/404-private-registry-scoped.js index 5565f7a404e9c..0aa262f54628c 100644 --- a/test/tap/404-private-registry-scoped.js +++ b/test/tap/404-private-registry-scoped.js @@ -1,23 +1,9 @@ var test = require('tap').test -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var common = require('../common-tap.js') var mr = common.fakeRegistry.compat var server -var testdir = common.pkg - -function setup () { - cleanup() - mkdirp.sync(testdir) -} - -function cleanup () { - rimraf.sync(testdir) -} - test('setup', function (t) { - setup() mr({port: common.port, throwOnUnmatched: true}, function (err, s) { t.ifError(err, 'registry mocked successfully') server = s @@ -30,7 +16,7 @@ test('scoped package names not mangled on error with non-root registry', functio common.npm( [ '--registry=' + common.registry, - '--cache=' + testdir, + '--cache=' + common.cache, 'cache', 'add', '@scope/foo@*', @@ -43,14 +29,8 @@ test('scoped package names not mangled on error with non-root registry', functio t.match(stderr, /E404/, 'should notify the sort of error as a 404') t.match(stderr, /@scope(?:%2f|\/)foo/, 'should have package name in error') server.done() + server.close() t.end() } ) }) - -test('cleanup', function (t) { - server.close() - cleanup() - t.pass('cleaned up') - t.end() -}) diff --git a/test/tap/404-private-registry.js b/test/tap/404-private-registry.js index 869125eba18f0..0ca05105dc9f3 100644 --- a/test/tap/404-private-registry.js +++ b/test/tap/404-private-registry.js @@ -1,25 +1,12 @@ var test = require('tap').test var path = require('path') -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var common = require('../common-tap.js') var mr = common.fakeRegistry.compat var server var packageName = path.basename(__filename, '.js') -var testdir = common.pkg - -function setup () { - cleanup() - mkdirp.sync(testdir) -} - -function cleanup () { - rimraf.sync(testdir) -} test('setup', function (t) { - setup() mr({port: common.port, throwOnUnmatched: true}, function (err, s) { t.ifError(err, 'registry mocked successfully') server = s @@ -32,7 +19,7 @@ test('package names not mangled on error with non-root registry', function (t) { common.npm( [ '--registry=' + common.registry, - '--cache=' + testdir, + '--cache=' + common.cache, 'cache', 'add', packageName + '@*' @@ -43,14 +30,8 @@ test('package names not mangled on error with non-root registry', function (t) { t.equal(code, 1, 'exited with error') t.match(stderr, packageName, 'should have package name in error') server.done() + server.close() t.end() } ) }) - -test('cleanup', function (t) { - server.close() - cleanup() - t.pass('cleaned up') - t.end() -}) diff --git a/test/tap/404.js b/test/tap/404.js index 450b0c2816fba..af146371c7933 100644 --- a/test/tap/404.js +++ b/test/tap/404.js @@ -11,7 +11,7 @@ const invalidPackage = /Your package name is not valid, because[\s\S]+1\. name c const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/access.js b/test/tap/access.js index db03cbfc9e80b..2998bbf63b2d5 100644 --- a/test/tap/access.js +++ b/test/tap/access.js @@ -19,22 +19,18 @@ const scoped = { } test('setup', function (t) { - mkdirp(pkg, function (er) { - t.ifError(er, pkg + ' made successfully') - - mr({port: common.port}, function (err, s) { - t.ifError(err, 'registry mocked successfully') - server = s - - fs.writeFile( - path.join(pkg, 'package.json'), - JSON.stringify(scoped), - function (er) { - t.ifError(er, 'wrote package.json') - t.end() - } - ) - }) + mr({port: common.port}, function (err, s) { + t.ifError(err, 'registry mocked successfully') + server = s + + fs.writeFile( + path.join(pkg, 'package.json'), + JSON.stringify(scoped), + function (er) { + t.ifError(er, 'wrote package.json') + t.end() + } + ) }) }) diff --git a/test/tap/all-package-metadata-cache-stream-unit.js b/test/tap/all-package-metadata-cache-stream-unit.js index 9c1babd59c96c..66b24c5e0c710 100644 --- a/test/tap/all-package-metadata-cache-stream-unit.js +++ b/test/tap/all-package-metadata-cache-stream-unit.js @@ -5,7 +5,6 @@ const common = require('../common-tap.js') const getStream = require('get-stream') const mkdirp = require('mkdirp') const path = require('path') -const rimraf = require('rimraf') const Tacks = require('tacks') const {test} = require('tap') @@ -13,16 +12,23 @@ const {File} = Tacks const _createCacheEntryStream = require('../../lib/search/all-package-metadata.js')._createCacheEntryStream -const PKG_DIR = common.pkg -const CACHE_DIR = path.resolve(PKG_DIR, 'cache') - +// this test uses a fresh cache for each test block +// create them all in common.cache so that we can verify +// them for root-owned files in sudotest +let CACHE_DIR +let cacheCounter = 1 +const chownr = require('chownr') function setup () { + CACHE_DIR = common.cache + '/' + cacheCounter++ mkdirp.sync(CACHE_DIR) + fixOwner(CACHE_DIR) } -function cleanup () { - rimraf.sync(PKG_DIR) -} +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} test('createCacheEntryStream basic', t => { setup() @@ -39,6 +45,7 @@ test('createCacheEntryStream basic', t => { } })) fixture.create(cachePath) + fixOwner(cachePath) return _createCacheEntryStream(cachePath, {}).then(({ updateStream: stream, updatedLatest: latest @@ -53,7 +60,6 @@ test('createCacheEntryStream basic', t => { name: 'foo', version: '1.0.0' }]) - cleanup() }) }) }) @@ -63,12 +69,12 @@ test('createCacheEntryStream empty cache', t => { const cachePath = path.join(CACHE_DIR, '.cache.json') const fixture = new Tacks(File({})) fixture.create(cachePath) + fixOwner(cachePath) return _createCacheEntryStream(cachePath, {}).then( () => { throw new Error('should not succeed') }, err => { t.ok(err, 'returned an error because there was no _updated') t.match(err.message, /Empty or invalid stream/, 'useful error message') - cleanup() } ) }) @@ -80,6 +86,7 @@ test('createCacheEntryStream no entry cache', t => { '_updated': 1234 })) fixture.create(cachePath) + fixOwner(cachePath) return _createCacheEntryStream(cachePath, {}).then(({ updateStream: stream, updatedLatest: latest @@ -88,7 +95,6 @@ test('createCacheEntryStream no entry cache', t => { t.ok(stream, 'returned a stream') return getStream.array(stream).then(results => { t.deepEquals(results, [], 'no results') - cleanup() }) }) }) @@ -101,7 +107,6 @@ test('createCacheEntryStream missing cache', t => { err => { t.ok(err, 'returned an error because there was no cache') t.equals(err.code, 'ENOENT', 'useful error message') - cleanup() } ) }) diff --git a/test/tap/all-package-metadata-entry-stream-unit.js b/test/tap/all-package-metadata-entry-stream-unit.js index e693e6b33d76e..164a34f4c50c8 100644 --- a/test/tap/all-package-metadata-entry-stream-unit.js +++ b/test/tap/all-package-metadata-entry-stream-unit.js @@ -6,7 +6,6 @@ const mkdirp = require('mkdirp') const mr = require('npm-registry-mock') const npm = require('../../') const path = require('path') -const rimraf = require('rimraf') const Tacks = require('tacks') const test = require('tap').test @@ -14,21 +13,27 @@ const {File} = Tacks const _createEntryStream = require('../../lib/search/all-package-metadata.js')._createEntryStream -const PKG_DIR = common.pkg -const CACHE_DIR = path.resolve(PKG_DIR, 'cache') - let server +// this test uses a fresh cache for each test block +// create them all in common.cache so that we can verify +// them for root-owned files in sudotest +let CACHE_DIR +let cacheCounter = 1 function setup () { + CACHE_DIR = common.cache + '/' + cacheCounter++ mkdirp.sync(CACHE_DIR) + fixOwner(CACHE_DIR) } -function cleanup () { - rimraf.sync(PKG_DIR) -} +const chownr = require('chownr') +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} test('setup', t => { - cleanup() mr({port: common.port, throwOnUnmatched: true}, (err, s) => { t.ifError(err, 'registry mocked successfully') npm.load({ cache: CACHE_DIR, registry: common.registry }, err => { @@ -71,7 +76,6 @@ test('createEntryStream full request', t => { version: '1.0.0' }]) server.done() - cleanup() }) }) @@ -88,6 +92,7 @@ test('createEntryStream cache only', function (t) { other: { name: 'other', version: '1.0.0' } })) fixture.create(cachePath) + fixOwner(cachePath) return _createEntryStream(cachePath, 600, { registry: common.registry }).then(({ @@ -106,7 +111,6 @@ test('createEntryStream cache only', function (t) { 'packages deduped and sorted' ) server.done() - cleanup() }) }) @@ -130,6 +134,7 @@ test('createEntryStream merged stream', function (t) { other: { name: 'other', version: '1.0.0' } })) fixture.create(cachePath) + fixOwner(cachePath) return _createEntryStream(cachePath, 600, { registry: common.registry }).then(({ @@ -156,7 +161,6 @@ test('createEntryStream merged stream', function (t) { version: '1.0.0' }, 'update stream version wins on dedupe even when the newer one has a lower semver.') server.done() - cleanup() }) }) @@ -177,13 +181,10 @@ test('createEntryStream no sources', function (t) { t.match(err.message, /No search sources available/, 'useful error message') }).then(() => { server.done() - cleanup() }) }) test('cleanup', function (t) { - cleanup() server.close() - t.pass('all done') t.done() }) diff --git a/test/tap/all-package-metadata-update-stream-unit.js b/test/tap/all-package-metadata-update-stream-unit.js index 782bdf231c88d..126fe9d398593 100644 --- a/test/tap/all-package-metadata-update-stream-unit.js +++ b/test/tap/all-package-metadata-update-stream-unit.js @@ -5,24 +5,30 @@ const getStream = require('get-stream') const npm = require('../../') const test = require('tap').test const mkdirp = require('mkdirp') -const rimraf = require('rimraf') -const path = require('path') const mr = require('npm-registry-mock') var _createEntryUpdateStream = require('../../lib/search/all-package-metadata.js')._createEntryUpdateStream -var PKG_DIR = common.pkg -var CACHE_DIR = path.resolve(PKG_DIR, 'cache') - var server +// this test uses a fresh cache for each test block +// create them all in common.cache so that we can verify +// them for root-owned files in sudotest +let CACHE_DIR +let cacheCounter = 1 function setup () { + CACHE_DIR = common.cache + '/' + cacheCounter++ mkdirp.sync(CACHE_DIR) + fixOwner(CACHE_DIR) } -function cleanup () { - rimraf.sync(PKG_DIR) -} +const chownr = require('chownr') + +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} test('setup', function (t) { mr({port: common.port, throwOnUnmatched: true}, function (err, s) { @@ -63,7 +69,6 @@ test('createEntryUpdateStream full request', function (t) { version: '1.0.0' }]) server.done() - cleanup() }) }) @@ -94,7 +99,6 @@ test('createEntryUpdateStream partial update', function (t) { version: '1.0.0' }]) server.done() - cleanup() }) }) @@ -127,7 +131,6 @@ test('createEntryUpdateStream authed request', function (t) { version: '1.0.0' }]) server.done() - cleanup() }) }) @@ -147,7 +150,6 @@ test('createEntryUpdateStream bad auth', function (t) { t.match(err, /unauthorized/, 'failure message from request used') }).then(() => { server.done() - cleanup() }) }) @@ -164,14 +166,11 @@ test('createEntryUpdateStream not stale', function (t) { t.notOk(stream, 'no stream returned') t.notOk(latest, 'no latest returned') server.done() - cleanup() t.end() }) }) test('cleanup', function (t) { - cleanup() server.close() - t.pass('all done') t.done() }) diff --git a/test/tap/all-package-metadata-write-stream-unit.js b/test/tap/all-package-metadata-write-stream-unit.js index b258b40dfe666..8cdfe96da05e2 100644 --- a/test/tap/all-package-metadata-write-stream-unit.js +++ b/test/tap/all-package-metadata-write-stream-unit.js @@ -5,23 +5,29 @@ const getStream = require('get-stream') const npm = require('../../') const test = require('tap').test const mkdirp = require('mkdirp') -const rimraf = require('rimraf') const path = require('path') const fs = require('fs') const ms = require('mississippi') const _createCacheWriteStream = require('../../lib/search/all-package-metadata.js')._createCacheWriteStream -const PKG_DIR = common.pkg -const CACHE_DIR = path.resolve(PKG_DIR, 'cache') - +// this test uses a fresh cache for each test block +// create them all in common.cache so that we can verify +// them for root-owned files in sudotest +let CACHE_DIR +let cacheCounter = 1 function setup () { + CACHE_DIR = common.cache + '/' + cacheCounter++ mkdirp.sync(CACHE_DIR) + fixOwner(CACHE_DIR) } -function cleanup () { - rimraf.sync(PKG_DIR) -} +const chownr = require('chownr') +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} function fromArray (array) { var idx = 0 @@ -74,7 +80,6 @@ test('createCacheEntryStream basic', function (t) { version: '1.0.0' } }, 'cache contents based on what was written') - cleanup() }) }) @@ -94,7 +99,6 @@ test('createCacheEntryStream no entries', function (t) { }).then(() => { const fileData = JSON.parse(fs.readFileSync(cachePath)) t.ok(fileData, 'cache file exists and has stuff in it') - cleanup() }) }) @@ -117,6 +121,5 @@ test('createCacheEntryStream missing cache dir', function (t) { t.deepEquals(fileData, { '_updated': latest }, 'cache still contains `_updated`') - cleanup() }) }) diff --git a/test/tap/all-package-metadata.js b/test/tap/all-package-metadata.js index ec605c3486bb9..153878500acba 100644 --- a/test/tap/all-package-metadata.js +++ b/test/tap/all-package-metadata.js @@ -15,7 +15,7 @@ const File = Tacks.File const allPackageMetadata = require('../../lib/search/all-package-metadata.js') -const PKG_DIR = path.resolve(common.pkg, 'update-index') +const PKG_DIR = path.resolve(common.cache, 'update-index') const CACHE_DIR = path.resolve(PKG_DIR, 'cache', '_cacache') let cacheBase let cachePath diff --git a/test/tap/anon-cli-metrics.js b/test/tap/anon-cli-metrics.js index d021cf62bc92f..cb1f878a4cb0a 100644 --- a/test/tap/anon-cli-metrics.js +++ b/test/tap/anon-cli-metrics.js @@ -11,7 +11,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') var metricsFile = path.join(cachedir, 'anonymous-cli-metrics.json') diff --git a/test/tap/auto-prune.js b/test/tap/auto-prune.js index 8d129c4a83b21..aab3692a3b242 100644 --- a/test/tap/auto-prune.js +++ b/test/tap/auto-prune.js @@ -9,7 +9,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/bitbucket-https-url-with-creds-package.js b/test/tap/bitbucket-https-url-with-creds-package.js index 4177d5f89fcb7..4891b9886bd8b 100644 --- a/test/tap/bitbucket-https-url-with-creds-package.js +++ b/test/tap/bitbucket-https-url-with-creds-package.js @@ -50,7 +50,7 @@ test('bitbucket-https-url-with-creds-package', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/bitbucket-https-url-with-creds.js b/test/tap/bitbucket-https-url-with-creds.js index 09f7e0e8e5488..7f7e7eee4810a 100644 --- a/test/tap/bitbucket-https-url-with-creds.js +++ b/test/tap/bitbucket-https-url-with-creds.js @@ -47,7 +47,7 @@ test('bitbucket-https-url-with-creds', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/bitbucket-shortcut-package.js b/test/tap/bitbucket-shortcut-package.js index bf1a39068ec87..ef606f4aa3e0c 100644 --- a/test/tap/bitbucket-shortcut-package.js +++ b/test/tap/bitbucket-shortcut-package.js @@ -51,7 +51,7 @@ test('bitbucket-shortcut', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/bitbucket-shortcut.js b/test/tap/bitbucket-shortcut.js index c81554e2d9d20..fe1c4179755c4 100644 --- a/test/tap/bitbucket-shortcut.js +++ b/test/tap/bitbucket-shortcut.js @@ -48,7 +48,7 @@ test('bitbucket-shortcut', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/cache-shasum-fork.js b/test/tap/cache-shasum-fork.js index 3bc2a5d2a59ae..e035c78111af8 100644 --- a/test/tap/cache-shasum-fork.js +++ b/test/tap/cache-shasum-fork.js @@ -15,7 +15,7 @@ var forkPath = path.resolve( __dirname, '..', 'fixtures', 'forked-underscore-1.5.1.tgz' ) var pkg = common.pkg -var cache = path.join(pkg, 'cache') +var cache = common.cache var server test('setup', function (t) { @@ -42,7 +42,7 @@ test('npm cache - install from fork', function (t) { }, function (err, code, stdout, stderr) { t.ifErr(err, 'install finished without error') - t.notOk(stderr, 'Should not get data on stderr: ' + stderr) + t.equal(stderr, '', 'Should not get data on stderr') t.equal(code, 0, 'install finished successfully') var deps = {} diff --git a/test/tap/check-permissions.js b/test/tap/check-permissions.js index 74e49fb65fb6c..b8238891d9075 100644 --- a/test/tap/check-permissions.js +++ b/test/tap/check-permissions.js @@ -79,13 +79,14 @@ function writableTests (t, writable) { writable(writableDir, function (er) { t.error(er, 'writable dir is writable') }) - if (process.platform !== 'win32') { - // Windows folders cannot be set to be read-only. + if (process.platform === 'win32') { + t.pass('windows folders cannot be read-only') + } else if (process.getuid && process.getuid() === 0) { + t.pass('root is not blocked by read-only dirs') + } else { writable(nonWritableDir, function (er) { t.ok(er, 'non-writable dir resulted in an error') }) - } else { - t.pass('windows folders cannot be read-only') } } diff --git a/test/tap/ci-header.js b/test/tap/ci-header.js index 530da3bf3a72a..fc791c6e6710d 100644 --- a/test/tap/ci-header.js +++ b/test/tap/ci-header.js @@ -10,7 +10,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/circular-dep.js b/test/tap/circular-dep.js index a08c41bf4a9af..624ea3800a906 100644 --- a/test/tap/circular-dep.js +++ b/test/tap/circular-dep.js @@ -16,7 +16,7 @@ var minimist = path.join(pkg, 'minimist') var EXEC_OPTS = { cwd: path.join(pkg, 'minimist/node_modules'), - npm_config_cache: path.join(pkg, 'cache') + npm_config_cache: common.cache } var json = { diff --git a/test/tap/debug-logs.js b/test/tap/debug-logs.js index 0e951c4bb082b..4da52bfcee8ae 100644 --- a/test/tap/debug-logs.js +++ b/test/tap/debug-logs.js @@ -10,14 +10,13 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') var conf = { cwd: testdir, env: Object.assign({}, process.env, { - npm_config_cache: cachedir, npm_config_tmp: tmpdir, npm_config_prefix: globaldir, npm_config_registry: common.registry, @@ -26,7 +25,6 @@ var conf = { } var fixture = new Tacks(Dir({ - cache: Dir(), global: Dir(), tmp: Dir(), testdir: Dir({ @@ -41,17 +39,8 @@ var fixture = new Tacks(Dir({ }) })) -function setup () { - cleanup() - fixture.create(basedir) -} - -function cleanup () { - fixture.remove(basedir) -} - test('setup', function (t) { - setup() + fixture.create(basedir) t.done() }) @@ -59,8 +48,9 @@ test('example', function (t) { common.npm(['run', 'false'], conf, function (err, code, stdout, stderr) { if (err) throw err t.is(code, 1, 'command errored') - var matches = stderr.match(/A complete log of this run can be found in:.*\nnpm ERR! {5,5}(.*)/) - t.ok(matches, 'debug log mentioned in error message') + const re = /A complete log of this run can be found in:.*\nnpm ERR! {5,5}(.*)/ + const matches = stderr.match(re) + t.match(stderr, re, 'debug log mentioned in error message') if (matches) { var logfile = matches[1] t.matches(path.relative(cachedir, logfile), /^_logs/, 'debug log is inside the cache in _logs') @@ -105,8 +95,3 @@ test('example', function (t) { }) }) }) - -test('cleanup', function (t) { - cleanup() - t.done() -}) diff --git a/test/tap/dedupe-git-semver.js b/test/tap/dedupe-git-semver.js index 35bb2c87d80e7..b7e31b08614f0 100644 --- a/test/tap/dedupe-git-semver.js +++ b/test/tap/dedupe-git-semver.js @@ -39,7 +39,7 @@ const npm = requireInject.installGlobally('../../lib/npm.js', { const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const tmpdir = path.join(basedir, 'tmp') const cwd = process.cwd() diff --git a/test/tap/doctor.js b/test/tap/doctor.js index 6ee05746960d6..7b07e0f39b7d8 100644 --- a/test/tap/doctor.js +++ b/test/tap/doctor.js @@ -14,7 +14,7 @@ const Dir = Tacks.Dir const File = Tacks.File const ROOT = common.pkg -const CACHE = path.join(ROOT, 'cache') +const CACHE = common.cache const TMP = path.join(ROOT, 'tmp') const PREFIX = path.join(ROOT, 'global-prefix') const PKG = path.join(ROOT, 'pkg') diff --git a/test/tap/false-name.js b/test/tap/false-name.js index 301be4fc8ef94..541bacc7eda53 100644 --- a/test/tap/false-name.js +++ b/test/tap/false-name.js @@ -11,15 +11,13 @@ var fs = require('graceful-fs') var path = require('path') var existsSync = fs.existsSync || path.existsSync -var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap.js') var pkg = common.pkg -var cache = path.join(pkg, 'cache') +var cache = common.cache var server var EXEC_OPTS = { cwd: pkg } @@ -69,17 +67,10 @@ test('not every pkg.name can be required', function (t) { test('cleanup', function (t) { server.close() - cleanup() t.end() }) -function cleanup () { - rimraf.sync(pkg) -} - function setup () { - cleanup() - mkdirp.sync(cache) fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify(json, null, 2) diff --git a/test/tap/fetch-package-metadata.js b/test/tap/fetch-package-metadata.js index 9a45fb57a92de..2e666772ed204 100644 --- a/test/tap/fetch-package-metadata.js +++ b/test/tap/fetch-package-metadata.js @@ -1,11 +1,6 @@ 'use strict' -var path = require('path') -var mkdirp = require('mkdirp') - var mr = require('npm-registry-mock') var npa = require('npm-package-arg') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap.js') @@ -13,22 +8,11 @@ var npm = require('../../lib/npm.js') var pkg = common.pkg -function setup (cb) { - cleanup() - mkdirp.sync(pkg) -} - -function cleanup () { - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -} - test('setup', function (t) { - setup() process.chdir(pkg) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, registry: common.registry, // important to make sure devDependencies don't get stripped dev: true @@ -62,8 +46,3 @@ test('fetch-package-metadata provides resolved metadata', function (t) { t.end() } }) - -test('cleanup', function (t) { - cleanup() - t.end() -}) diff --git a/test/tap/gist-short-shortcut-package.js b/test/tap/gist-short-shortcut-package.js index d7c70b16b029f..e5e67f21a05dc 100644 --- a/test/tap/gist-short-shortcut-package.js +++ b/test/tap/gist-short-shortcut-package.js @@ -49,7 +49,7 @@ test('gist-short-shortcut-package', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/gist-short-shortcut.js b/test/tap/gist-short-shortcut.js index 04bf5bd42a985..2fcf63d53ede1 100644 --- a/test/tap/gist-short-shortcut.js +++ b/test/tap/gist-short-shortcut.js @@ -46,7 +46,7 @@ test('gist-shortcut', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/gist-shortcut-package.js b/test/tap/gist-shortcut-package.js index 9851e80b0e07a..06b4383583aad 100644 --- a/test/tap/gist-shortcut-package.js +++ b/test/tap/gist-shortcut-package.js @@ -49,7 +49,7 @@ test('gist-shortcut-package', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/gist-shortcut.js b/test/tap/gist-shortcut.js index 9958a996e076c..e5f200f5fdeb1 100644 --- a/test/tap/gist-shortcut.js +++ b/test/tap/gist-shortcut.js @@ -46,7 +46,7 @@ test('gist-shortcut', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/git-dependency-install-link.js b/test/tap/git-dependency-install-link.js index 462df8973fead..44438e7f95825 100644 --- a/test/tap/git-dependency-install-link.js +++ b/test/tap/git-dependency-install-link.js @@ -14,7 +14,7 @@ var common = require('../common-tap.js') var pkg = common.pkg var repo = pkg + '-repo' var prefix = pkg + '-prefix' -var cache = resolve(pkg, 'cache') +var cache = common.cache var daemon var daemonPID diff --git a/test/tap/git-npmignore.js b/test/tap/git-npmignore.js index 42c1637b18013..2ab7db7304f0f 100644 --- a/test/tap/git-npmignore.js +++ b/test/tap/git-npmignore.js @@ -14,7 +14,6 @@ var Dir = Tacks.Dir var File = Tacks.File var fixture = new Tacks(Dir({ - cache: Dir({}), deps: Dir({ gitch: Dir({ '.npmignore': File( @@ -42,7 +41,7 @@ var fixture = new Tacks(Dir({ })) var testdir = common.pkg -var cachedir = resolve(testdir, 'cache') +var cachedir = common.cache var dep = resolve(testdir, 'deps', 'gitch') var packname = 'gitch-1.0.0.tgz' var packed = resolve(testdir, packname) diff --git a/test/tap/git-prepare.js b/test/tap/git-prepare.js index 9112828ce40cd..37534fbfc430f 100644 --- a/test/tap/git-prepare.js +++ b/test/tap/git-prepare.js @@ -14,7 +14,7 @@ const common = require('../common-tap.js') const testdir = common.pkg const repo = path.join(testdir, 'repo') const prefix = path.join(testdir, 'prefix') -const cache = path.join(testdir, 'cache') +const cache = common.cache var Tacks = require('tacks') var Dir = Tacks.Dir @@ -30,7 +30,6 @@ process.env.npm_config_prefix = prefix const fixture = new Tacks(Dir({ repo: Dir({}), prefix: Dir({}), - cache: Dir({}), deps: Dir({ parent: Dir({ 'package.json': File({ diff --git a/test/tap/github-shortcut-package.js b/test/tap/github-shortcut-package.js index a084479e1791e..db153ba352e55 100644 --- a/test/tap/github-shortcut-package.js +++ b/test/tap/github-shortcut-package.js @@ -49,7 +49,7 @@ test('github-shortcut-package', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/github-shortcut.js b/test/tap/github-shortcut.js index 90f4655149bb9..0c89d428fa1af 100644 --- a/test/tap/github-shortcut.js +++ b/test/tap/github-shortcut.js @@ -48,7 +48,7 @@ test('github-shortcut', function (t) { }) const opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/gitlab-shortcut-package.js b/test/tap/gitlab-shortcut-package.js index 54632f8370403..4f5b43851d88b 100644 --- a/test/tap/gitlab-shortcut-package.js +++ b/test/tap/gitlab-shortcut-package.js @@ -48,7 +48,7 @@ test('gitlab-shortcut-package', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/gitlab-shortcut.js b/test/tap/gitlab-shortcut.js index 724178867aede..e6bd54765a089 100644 --- a/test/tap/gitlab-shortcut.js +++ b/test/tap/gitlab-shortcut.js @@ -45,7 +45,7 @@ test('gitlab-shortcut', function (t) { }) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, prefix: pkg, registry: common.registry, loglevel: 'silent' diff --git a/test/tap/ignore-install-link.js b/test/tap/ignore-install-link.js index 129df3c2717ca..7b3b1be3b9d5e 100644 --- a/test/tap/ignore-install-link.js +++ b/test/tap/ignore-install-link.js @@ -13,7 +13,7 @@ var root = common.pkg var pkg = path.resolve(root, 'pkg') var dep = path.resolve(root, 'dep') var target = path.resolve(pkg, 'node_modules', 'dep') -var cache = path.resolve(root, 'cache') +var cache = common.cache var globalPath = path.resolve(root, 'global') var pkgj = { diff --git a/test/tap/install-actions.js b/test/tap/install-actions.js index b34be3ad0dd70..071dc2cc371ed 100644 --- a/test/tap/install-actions.js +++ b/test/tap/install-actions.js @@ -10,7 +10,7 @@ var mockLog = { var actions test('setup', function (t) { - npm.load(function () { + npm.load({ 'unsafe-perm': true }, function () { log.disableProgress() actions = require('../../lib/install/actions.js').actions t.end() diff --git a/test/tap/install-dep-classification.js b/test/tap/install-dep-classification.js index 3c65459d345fb..2775c367b0002 100644 --- a/test/tap/install-dep-classification.js +++ b/test/tap/install-dep-classification.js @@ -9,7 +9,7 @@ const fs = require('fs') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') const optionaldir = path.join(testdir, 'optional') diff --git a/test/tap/install-duplicate-deps-warning.js b/test/tap/install-duplicate-deps-warning.js index 2b10d8aa31964..05eccd8e43b03 100644 --- a/test/tap/install-duplicate-deps-warning.js +++ b/test/tap/install-duplicate-deps-warning.js @@ -38,7 +38,7 @@ test('npm install with duplicate dependencies, different versions', function (t) t.plan(1) mr({ port: common.port }, function (er, s) { var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, registry: common.registry } diff --git a/test/tap/install-package-json-order.js b/test/tap/install-package-json-order.js index ae000ff6738df..2e780def2696f 100644 --- a/test/tap/install-package-json-order.js +++ b/test/tap/install-package-json-order.js @@ -9,7 +9,7 @@ const common = require('../common-tap.js') var pkg = common.pkg var workdir = path.join(pkg, 'workdir') var tmp = path.join(pkg, 'tmp') -var cache = path.join(pkg, 'cache') +var cache = common.cache var fs = require('fs') var osenv = require('osenv') diff --git a/test/tap/install-package-lock-only.js b/test/tap/install-package-lock-only.js index 6e98f27b30021..2c5191a02e7d7 100644 --- a/test/tap/install-package-lock-only.js +++ b/test/tap/install-package-lock-only.js @@ -10,7 +10,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/install-scoped-with-bundled-dependency.js b/test/tap/install-scoped-with-bundled-dependency.js index 76f440dc6c158..db126eb6426c4 100644 --- a/test/tap/install-scoped-with-bundled-dependency.js +++ b/test/tap/install-scoped-with-bundled-dependency.js @@ -8,7 +8,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/install-shrinkwrapped-git.js b/test/tap/install-shrinkwrapped-git.js index 7e50aa713d570..f74e185bfbad0 100644 --- a/test/tap/install-shrinkwrapped-git.js +++ b/test/tap/install-shrinkwrapped-git.js @@ -116,7 +116,6 @@ function setup (cb) { function cleanup () { process.chdir(osenv.tmpdir()) rimraf.sync(mockPath) - rimraf.sync(common['npm_config_cache']) } function prepareChildAndGetRefs (cb) { diff --git a/test/tap/install-with-dev-dep-duplicate.js b/test/tap/install-with-dev-dep-duplicate.js index b3a58987631b4..01ba38a1123a9 100644 --- a/test/tap/install-with-dev-dep-duplicate.js +++ b/test/tap/install-with-dev-dep-duplicate.js @@ -78,7 +78,7 @@ function setup (cb) { process.chdir(pkg) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, registry: common.registry } npm.load(opts, cb) diff --git a/test/tap/invalid-dep-version-filtering.js b/test/tap/invalid-dep-version-filtering.js index 502610f231ce4..f2f175b24ef5e 100644 --- a/test/tap/invalid-dep-version-filtering.js +++ b/test/tap/invalid-dep-version-filtering.js @@ -8,7 +8,7 @@ var File = Tacks.File var Dir = Tacks.Dir var testdir = common.pkg -var cachedir = path.join(testdir, 'cache') +var cachedir = common.cache var fixture = new Tacks(Dir({ cache: Dir(), diff --git a/test/tap/lockfile-http-deps.js b/test/tap/lockfile-http-deps.js index aaa47f5de6d3e..b5eaa10760a43 100644 --- a/test/tap/lockfile-http-deps.js +++ b/test/tap/lockfile-http-deps.js @@ -10,7 +10,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/ls-peer.js b/test/tap/ls-peer.js index fd53d37be62ec..05cc1382250c1 100644 --- a/test/tap/ls-peer.js +++ b/test/tap/ls-peer.js @@ -8,7 +8,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/optional-metadep-rollback-collision.js b/test/tap/optional-metadep-rollback-collision.js index 51cc6367dded7..d665a123bff90 100644 --- a/test/tap/optional-metadep-rollback-collision.js +++ b/test/tap/optional-metadep-rollback-collision.js @@ -13,7 +13,7 @@ var common = require('../common-tap.js') var pkg = common.pkg var deps = path.resolve(pkg, 'deps') var opdep = path.resolve(pkg, 'node_modules', 'opdep') -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var createServer = require('http').createServer var mr = require('npm-registry-mock') var serverPort = 27991 diff --git a/test/tap/outdated-git.js b/test/tap/outdated-git.js index 3932330c99f34..15db1a30c7c74 100644 --- a/test/tap/outdated-git.js +++ b/test/tap/outdated-git.js @@ -10,7 +10,7 @@ var npm = require('../../') // config var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'outdated-git', author: 'Rocko Artischocko', diff --git a/test/tap/outdated-latest.js b/test/tap/outdated-latest.js index e0b432a2105cd..073b71da91cc6 100644 --- a/test/tap/outdated-latest.js +++ b/test/tap/outdated-latest.js @@ -9,7 +9,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/outdated-long.js b/test/tap/outdated-long.js index 474df8031a50a..0a338815983eb 100644 --- a/test/tap/outdated-long.js +++ b/test/tap/outdated-long.js @@ -1,9 +1,7 @@ var fs = require('graceful-fs') var path = require('path') -var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap.js') @@ -11,7 +9,7 @@ var npm = require('../../') // config var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'outdated-long', @@ -23,8 +21,6 @@ var json = { } test('setup', function (t) { - cleanup() - mkdirp.sync(cache) fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify(json, null, 2) @@ -65,7 +61,7 @@ test('it should not throw', function (t) { mr({ port: common.port }, function (er, s) { npm.load( { - cache: 'cache', + cache: cache, loglevel: 'silent', parseable: true, registry: common.registry @@ -94,12 +90,3 @@ test('it should not throw', function (t) { ) }) }) - -test('cleanup', function (t) { - cleanup() - t.end() -}) - -function cleanup () { - rimraf.sync(pkg) -} diff --git a/test/tap/outdated-remote.js b/test/tap/outdated-remote.js index b3990a382caa2..e6cbd0e8acdde 100644 --- a/test/tap/outdated-remote.js +++ b/test/tap/outdated-remote.js @@ -8,7 +8,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') @@ -54,14 +54,9 @@ const fixture = new Tacks(Dir({ })) function setup () { - cleanup() fixture.create(basedir) } -function cleanup () { - fixture.remove(basedir) -} - test('setup', t => { setup() return common.fakeRegistry.listen() @@ -90,6 +85,5 @@ test('discovers new versions in outdated', t => { test('cleanup', t => { common.fakeRegistry.close() - cleanup() t.done() }) diff --git a/test/tap/outdated-symlink.js b/test/tap/outdated-symlink.js index 86128338a7be7..5ec089758a3ca 100644 --- a/test/tap/outdated-symlink.js +++ b/test/tap/outdated-symlink.js @@ -10,7 +10,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/outdated.js b/test/tap/outdated.js index 1acb0ed687b5a..e64b7b0f813c8 100644 --- a/test/tap/outdated.js +++ b/test/tap/outdated.js @@ -1,9 +1,7 @@ var fs = require('graceful-fs') var path = require('path') -var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var rimraf = require('rimraf') var test = require('tap').test var npm = require('../../') @@ -11,7 +9,7 @@ var common = require('../common-tap.js') // config var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var originalLog var json = { @@ -26,9 +24,7 @@ var json = { } test('setup', function (t) { - cleanup() originalLog = console.log - mkdirp.sync(cache) fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify(json, null, 2) @@ -91,7 +87,7 @@ test('it should not throw', function (t) { mr({ port: common.port }, function (er, s) { npm.load( { - cache: 'cache', + cache: cache, loglevel: 'silent', parseable: true, registry: common.registry @@ -122,11 +118,6 @@ test('it should not throw', function (t) { }) test('cleanup', function (t) { - cleanup() console.log = originalLog t.end() }) - -function cleanup () { - rimraf.sync(pkg) -} diff --git a/test/tap/owner.js b/test/tap/owner.js index b4da383ee1023..622b272852c7d 100644 --- a/test/tap/owner.js +++ b/test/tap/owner.js @@ -1,12 +1,9 @@ var mr = require('npm-registry-mock') var test = require('tap').test -var path = require('path') -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var common = require('../common-tap.js') var basedir = common.pkg -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var server @@ -74,8 +71,6 @@ function mocks (server) { } test('setup', function (t) { - cleanup() - mkdirp.sync(cachedir) mr({ port: common.port, plugin: mocks }, function (er, s) { server = s t.end() @@ -160,10 +155,5 @@ test('npm owner rm', function (t) { test('cleanup', function (t) { server.close() - cleanup() t.end() }) - -function cleanup () { - rimraf.sync(basedir) -} diff --git a/test/tap/pack-scoped.js b/test/tap/pack-scoped.js index 29d9aab478b8f..06d02297fd5d0 100644 --- a/test/tap/pack-scoped.js +++ b/test/tap/pack-scoped.js @@ -9,7 +9,7 @@ var rimraf = require('rimraf') var pkg = common.pkg var manifest = join(pkg, 'package.json') var tmp = join(pkg, 'tmp') -var cache = join(pkg, 'cache') +var cache = common.cache var data = { name: '@scope/generic-package', diff --git a/test/tap/pack.js b/test/tap/pack.js index 7aa42d48370a9..23c8296d003cd 100644 --- a/test/tap/pack.js +++ b/test/tap/pack.js @@ -14,7 +14,7 @@ const File = Tacks.File const testDir = common.pkg const tmp = path.join(testDir, 'tmp') -const cache = path.join(testDir, 'cache') +const cache = common.cache test('basic pack', (t) => { const fixture = new Tacks(new Dir({ diff --git a/test/tap/peer-deps.js b/test/tap/peer-deps.js index 20cbf5d1ded04..463a5ec47908d 100644 --- a/test/tap/peer-deps.js +++ b/test/tap/peer-deps.js @@ -57,7 +57,7 @@ function setup (cb) { ) process.chdir(pkg) - var opts = { cache: path.resolve(pkg, 'cache'), registry: common.registry } + var opts = { cache: common.cache, registry: common.registry } npm.load(opts, cb) } diff --git a/test/tap/prepare.js b/test/tap/prepare.js index 54edf7cb19bc8..1169089b17e4d 100644 --- a/test/tap/prepare.js +++ b/test/tap/prepare.js @@ -8,7 +8,7 @@ var rimraf = require('rimraf') var pkg = common.pkg var tmp = join(pkg, 'tmp') -var cache = join(pkg, 'cache') +var cache = common.cache test('setup', function (t) { var n = 0 diff --git a/test/tap/prepublish-only.js b/test/tap/prepublish-only.js index 9996479da442a..57af26038a7bd 100644 --- a/test/tap/prepublish-only.js +++ b/test/tap/prepublish-only.js @@ -11,7 +11,7 @@ var path = require('path') var common = require('../common-tap') var pkg = common.pkg -var cachedir = join(pkg, 'cache') +var cachedir = common.cache var tmpdir = join(pkg, 'tmp') var env = { diff --git a/test/tap/prepublish.js b/test/tap/prepublish.js index 5628f94db3884..e561d057f2124 100644 --- a/test/tap/prepublish.js +++ b/test/tap/prepublish.js @@ -8,7 +8,7 @@ var rimraf = require('rimraf') var pkg = common.pkg var tmp = join(pkg, 'tmp') -var cache = join(pkg, 'cache') +var cache = common.cache test('setup', function (t) { var n = 0 diff --git a/test/tap/progress-config.js b/test/tap/progress-config.js index 1f1e1a0c225ed..92468004f2c53 100644 --- a/test/tap/progress-config.js +++ b/test/tap/progress-config.js @@ -3,7 +3,7 @@ var test = require('tap').test var log = require('npmlog') var fs = require('graceful-fs') const common = require('../common-tap.js') -var configName = common.pkg + '-npmrc' +var configName = common.pkg + '/npmrc' // We use requireInject to get a fresh copy of // the npm singleton each time we require it. @@ -85,8 +85,3 @@ test('unicode-false', function (t) { t.done() }) }) - -test('cleanup', function (t) { - fs.unlinkSync(configName) - t.done() -}) diff --git a/test/tap/prune-with-dev-dep-duplicate.js b/test/tap/prune-with-dev-dep-duplicate.js index 7efea5c01e619..7e7632b489cf6 100644 --- a/test/tap/prune-with-dev-dep-duplicate.js +++ b/test/tap/prune-with-dev-dep-duplicate.js @@ -1,17 +1,14 @@ var fs = require('fs') var path = require('path') -var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap') var server var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'prune-with-dev-dep-duplicate', @@ -32,8 +29,6 @@ var EXEC_OPTS = { } test('setup', function (t) { - cleanup() - mkdirp.sync(cache) fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify(json, null, 2) @@ -103,12 +98,6 @@ test('npm prune only=prod', function (t) { test('cleanup', function (t) { server.close() - cleanup() t.pass('cleaned up') t.end() }) - -function cleanup () { - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -} diff --git a/test/tap/prune-with-only-dev-deps.js b/test/tap/prune-with-only-dev-deps.js index c8e87a53569a9..6ea1bcf64b480 100644 --- a/test/tap/prune-with-only-dev-deps.js +++ b/test/tap/prune-with-only-dev-deps.js @@ -3,15 +3,13 @@ var path = require('path') var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap') var server var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'prune-with-only-dev-deps', @@ -30,7 +28,6 @@ var EXEC_OPTS = { } test('setup', function (t) { - cleanup() mkdirp.sync(cache) fs.writeFileSync( path.join(pkg, 'package.json'), @@ -113,12 +110,5 @@ test('verify installs', function (t) { test('cleanup', function (t) { server.close() - cleanup() - t.pass('cleaned up') t.end() }) - -function cleanup () { - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -} diff --git a/test/tap/prune.js b/test/tap/prune.js index 6e033ceae00f2..936ee3a91b6b8 100644 --- a/test/tap/prune.js +++ b/test/tap/prune.js @@ -1,17 +1,14 @@ var fs = require('fs') var path = require('path') -var mkdirp = require('mkdirp') var mr = require('npm-registry-mock') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap') var server var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'prune', @@ -32,8 +29,6 @@ var EXEC_OPTS = { } test('setup', function (t) { - cleanup() - mkdirp.sync(cache) fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify(json, null, 2) @@ -122,12 +117,5 @@ test('pruduction: verify installs', function (t) { test('cleanup', function (t) { server.close() - cleanup() - t.pass('cleaned up') t.end() }) - -function cleanup () { - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -} diff --git a/test/tap/publish-access-scoped.js b/test/tap/publish-access-scoped.js index 31b1bed26c830..7b9a3ecb59b9c 100644 --- a/test/tap/publish-access-scoped.js +++ b/test/tap/publish-access-scoped.js @@ -2,8 +2,6 @@ var fs = require('fs') var path = require('path') var test = require('tap').test -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var mr = require('npm-registry-mock') var common = require('../common-tap') var server @@ -28,46 +26,40 @@ test('scoped packages pass public access if set', function (t) { return true }).put('/@bigco%2fpublish-access', true).reply(201, {ok: true}) - mkdirp(path.join(pkg, 'cache'), function () { - fs.writeFile( - path.join(pkg, 'package.json'), - JSON.stringify({ - name: '@bigco/publish-access', - version: '1.2.5', - public: true - }), - 'ascii', - function (er) { - t.ifError(er, 'package file written') - common.npm( - [ - 'publish', - '--access', 'public', - '--cache', path.join(pkg, 'cache'), - '--loglevel', 'silly', - '--registry', common.registry - ], - { - cwd: pkg - }, - function (er) { - t.ifError(er, 'published without error') + fs.writeFile( + path.join(pkg, 'package.json'), + JSON.stringify({ + name: '@bigco/publish-access', + version: '1.2.5', + public: true + }), + 'ascii', + function (er) { + t.ifError(er, 'package file written') + common.npm( + [ + 'publish', + '--access', 'public', + '--cache', common.cache, + '--loglevel', 'silly', + '--registry', common.registry + ], + { + cwd: pkg + }, + function (er) { + t.ifError(er, 'published without error') - server.done() - t.end() - } - ) - } - ) - }) + server.done() + t.end() + } + ) + } + ) }) test('cleanup', function (t) { process.chdir(__dirname) server.close() - rimraf(pkg, function (er) { - t.ifError(er) - - t.end() - }) + t.end() }) diff --git a/test/tap/publish-access-unscoped.js b/test/tap/publish-access-unscoped.js index a2489e2bcaf4b..a7ea8e6694849 100644 --- a/test/tap/publish-access-unscoped.js +++ b/test/tap/publish-access-unscoped.js @@ -2,8 +2,6 @@ var fs = require('fs') var path = require('path') var test = require('tap').test -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var mr = require('npm-registry-mock') var common = require('../common-tap') var server @@ -28,46 +26,40 @@ test('unscoped packages can be explicitly set as public', function (t) { return true }).put('/publish-access', true).reply(201, {ok: true}) - mkdirp(path.join(pkg, 'cache'), function () { - fs.writeFile( - path.join(pkg, 'package.json'), - JSON.stringify({ - name: 'publish-access', - version: '1.2.5', - public: true - }), - 'ascii', - function (er) { - t.ifError(er, 'package file written') - common.npm( - [ - 'publish', - '--access', 'public', - '--cache', path.join(pkg, 'cache'), - '--loglevel', 'silly', - '--registry', common.registry - ], - { - cwd: pkg - }, - function (er) { - t.ifError(er, 'published without error') + fs.writeFile( + path.join(pkg, 'package.json'), + JSON.stringify({ + name: 'publish-access', + version: '1.2.5', + public: true + }), + 'ascii', + function (er) { + t.ifError(er, 'package file written') + common.npm( + [ + 'publish', + '--access', 'public', + '--cache', common.cache, + '--loglevel', 'silly', + '--registry', common.registry + ], + { + cwd: pkg + }, + function (er) { + t.ifError(er, 'published without error') - server.done() - t.end() - } - ) - } - ) - }) + server.done() + t.end() + } + ) + } + ) }) test('cleanup', function (t) { process.chdir(__dirname) server.close() - rimraf(pkg, function (er) { - t.ifError(er) - - t.end() - }) + t.end() }) diff --git a/test/tap/publish-config.js b/test/tap/publish-config.js index 705ef7ae7a2ca..7d617df2c4032 100644 --- a/test/tap/publish-config.js +++ b/test/tap/publish-config.js @@ -4,9 +4,7 @@ const common = require('../common-tap.js') const test = require('tap').test const fs = require('fs') const osenv = require('osenv') -const pkg = `${process.env.npm_config_tmp || '/tmp'}/npm-test-publish-config` - -require('mkdirp').sync(pkg) +const pkg = common.pkg fs.writeFileSync(pkg + '/package.json', JSON.stringify({ name: 'npm-test-publish-config', diff --git a/test/tap/publish-invalid-semver-tag.js b/test/tap/publish-invalid-semver-tag.js index 400030908b21c..fed064bb4e0da 100644 --- a/test/tap/publish-invalid-semver-tag.js +++ b/test/tap/publish-invalid-semver-tag.js @@ -10,7 +10,7 @@ var mr = require('npm-registry-mock') var osenv = require('osenv') var PKG_DIR = common.pkg -var CACHE_DIR = path.resolve(PKG_DIR, 'cache') +var CACHE_DIR = common.cache var DEFAULT_PKG = { 'name': 'examples', @@ -19,9 +19,17 @@ var DEFAULT_PKG = { var mockServer +const chownr = require('chownr') +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} + function resetPackage (options) { rimraf.sync(CACHE_DIR) mkdirp.sync(CACHE_DIR) + fixOwner(CACHE_DIR) fs.writeFileSync(path.resolve(PKG_DIR, 'package.json'), DEFAULT_PKG) } diff --git a/test/tap/publish-scoped.js b/test/tap/publish-scoped.js index efa86c85391d0..78ca4699d3a31 100644 --- a/test/tap/publish-scoped.js +++ b/test/tap/publish-scoped.js @@ -2,8 +2,6 @@ var fs = require('fs') var path = require('path') var test = require('tap').test -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var common = require('../common-tap') var mr = require('npm-registry-mock') @@ -12,9 +10,6 @@ var pkg = common.pkg var server function setup () { - cleanup() - mkdirp.sync(path.join(pkg, 'cache')) - fs.writeFileSync( path.join(pkg, 'package.json'), JSON.stringify({ @@ -40,7 +35,7 @@ test('npm publish should honor scoping', function (t) { var configuration = [ 'progress=false', - 'cache=' + path.join(pkg, 'cache'), + 'cache=' + common.cache, 'registry=http://nonexistent.lvh.me', '//localhost:' + common.port + '/:username=username', '//localhost:' + common.port + '/:_password=' + Buffer.from('password').toString('base64'), @@ -81,11 +76,6 @@ test('npm publish should honor scoping', function (t) { test('cleanup', function (t) { server.close() + process.chdir(__dirname) t.end() - cleanup() }) - -function cleanup () { - process.chdir(__dirname) - rimraf.sync(pkg) -} diff --git a/test/tap/publish.js b/test/tap/publish.js index c507044433690..0913576a31b44 100644 --- a/test/tap/publish.js +++ b/test/tap/publish.js @@ -4,18 +4,13 @@ const BB = require('bluebird') const common = require('../common-tap') const fs = require('fs') -const mkdirp = require('mkdirp') const mr = BB.promisify(require('npm-registry-mock')) const path = require('path') -const rimraf = require('rimraf') const test = require('tap').test const testDir = common.pkg function setup () { - cleanup() - mkdirp.sync(path.join(testDir, 'cache')) - fs.writeFileSync( path.join(testDir, 'package.json'), JSON.stringify({ @@ -52,7 +47,7 @@ test('basic npm publish', (t) => { return common.npm([ 'publish', '--no-color', - '--cache', path.join(testDir, 'cache'), + '--cache', common.cache, '--registry=' + common.registry.replace(common.port, server.port), `--//localhost:${server.port}/:username=username`, `--//localhost:${server.port}/:_password=` + Buffer.from('password').toString('base64'), @@ -92,7 +87,7 @@ test('npm publish --dry-run', (t) => { 'publish', '--dry-run', '--registry=https://example.registry/fake', - '--cache', path.join(testDir, 'cache'), + '--cache', common.cache, '--loglevel=notice', '--no-color' ], {'cwd': testDir}) @@ -115,7 +110,7 @@ test('npm publish --json', (t) => { 'publish', '--json', '--registry', common.registry.replace(common.port, server.port), - '--cache', path.join(testDir, 'cache') + '--cache', common.cache ], {'cwd': testDir}) .spread((code, stdout, stderr) => { t.comment(stdout) @@ -142,7 +137,7 @@ test('npm publish --dry-run --json', (t) => { '--dry-run', '--json', '--registry=https://example.registry/fake', - '--cache', path.join(testDir, 'cache'), + '--cache', common.cache, '--loglevel=notice', '--no-color' ], {'cwd': testDir}) @@ -162,13 +157,3 @@ test('npm publish --dry-run --json', (t) => { t.equal(stderr.trim(), '', 'nothing on stderr') }) }) - -test('cleanup', (t) => { - cleanup() - t.end() -}) - -function cleanup () { - process.chdir(__dirname) - rimraf.sync(testDir) -} diff --git a/test/tap/retry-on-stale-cache.js b/test/tap/retry-on-stale-cache.js index 129c52c160d0e..7e3d291a5bdd7 100644 --- a/test/tap/retry-on-stale-cache.js +++ b/test/tap/retry-on-stale-cache.js @@ -8,7 +8,7 @@ var Dir = Tacks.Dir var File = Tacks.File var workdir = common.pkg -var cachedir = path.join(workdir, 'cache') +var cachedir = common.cache var modulesdir = path.join(workdir, 'modules') var oldModule = path.join(modulesdir, 'good-night-0.1.0.tgz') var newModule = path.join(modulesdir, 'good-night-1.0.0.tgz') @@ -20,7 +20,6 @@ var config = [ ] var fixture = new Tacks(Dir({ - 'cache': Dir(), 'modules': Dir({ 'good-night-0.1.0.tgz': File(Buffer.from( '1f8b0800000000000003ed934f4bc43010c57beea7187a59056dd36eff80' + diff --git a/test/tap/run-script.js b/test/tap/run-script.js index a937abadf2c1a..75b714002e2c8 100644 --- a/test/tap/run-script.js +++ b/test/tap/run-script.js @@ -8,7 +8,7 @@ var rimraf = require('rimraf') var common = require('../common-tap') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var tmp = path.resolve(pkg, 'tmp') var opts = { cwd: pkg } diff --git a/test/tap/save-optional.js b/test/tap/save-optional.js index c293401f527cb..9985451684d07 100644 --- a/test/tap/save-optional.js +++ b/test/tap/save-optional.js @@ -10,7 +10,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/scope-header.js b/test/tap/scope-header.js index f291b3a500964..f4eb7ae12c104 100644 --- a/test/tap/scope-header.js +++ b/test/tap/scope-header.js @@ -12,7 +12,7 @@ var testdir = path.join(basedir, 'testdir') var withScope = path.join(testdir, 'with-scope') var withoutScope = path.join(testdir, 'without-scope') var onlyProjectScope = path.join(testdir, 'only-project-scope') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/scripts-whitespace-windows.js b/test/tap/scripts-whitespace-windows.js index 9378564bb52bc..06f06e36eb2a6 100644 --- a/test/tap/scripts-whitespace-windows.js +++ b/test/tap/scripts-whitespace-windows.js @@ -10,7 +10,7 @@ var common = require('../common-tap') var pkg = common.pkg var tmp = path.resolve(pkg, 'tmp') -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var dep = path.resolve(pkg, 'dep') var EXEC_OPTS = { cwd: pkg } diff --git a/test/tap/search.all-package-search.js b/test/tap/search.all-package-search.js index 1115dcaf1ae3f..9b3b989f9c726 100644 --- a/test/tap/search.all-package-search.js +++ b/test/tap/search.all-package-search.js @@ -3,10 +3,8 @@ const cacheFile = require('npm-cache-filename') const mkdirp = require('mkdirp') const mr = require('npm-registry-mock') -const osenv = require('osenv') const path = require('path') const qs = require('querystring') -const rimraf = require('rimraf') const Tacks = require('tacks') const test = require('tap').test @@ -14,14 +12,21 @@ const {File} = Tacks const common = require('../common-tap.js') -const PKG_DIR = common.pkg -const CACHE_DIR = path.resolve(PKG_DIR, 'cache') +const CACHE_DIR = common.cache const cacheBase = cacheFile(CACHE_DIR)(common.registry + '/-/all') const cachePath = path.join(cacheBase, '.cache.json') +const chownr = require('chownr') +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} let server test('setup', function (t) { + mkdirp.sync(cacheBase) + fixOwner(CACHE_DIR) mr({port: common.port, throwOnUnmatched: true}, function (err, s) { t.ifError(err, 'registry mocked successfully') server = s @@ -142,7 +147,6 @@ const searches = [ // These test classic hand-matched searches searches.forEach(function (search) { test(search.description, function (t) { - setup() const query = qs.stringify({ text: search.term, size: 20, @@ -165,6 +169,7 @@ searches.forEach(function (search) { } const fixture = new Tacks(File(cacheContents)) fixture.create(cachePath) + fixOwner(cachePath) common.npm([ 'search', search.term, '--registry', common.registry, @@ -196,18 +201,7 @@ searches.forEach(function (search) { }) test('cleanup', function (t) { - cleanup() + server.done() server.close() t.end() }) - -function setup () { - cleanup() - mkdirp.sync(cacheBase) -} - -function cleanup () { - server.done() - process.chdir(osenv.tmpdir()) - rimraf.sync(PKG_DIR) -} diff --git a/test/tap/search.js b/test/tap/search.js index a7e269df7abd0..049706cc0892d 100644 --- a/test/tap/search.js +++ b/test/tap/search.js @@ -3,10 +3,8 @@ const cacheFile = require('npm-cache-filename') const mkdirp = require('mkdirp') const mr = require('npm-registry-mock') -const osenv = require('osenv') const path = require('path') const qs = require('querystring') -const rimraf = require('rimraf') const test = require('tap').test const Tacks = require('tacks') @@ -14,10 +12,27 @@ const File = Tacks.File const common = require('../common-tap.js') -const PKG_DIR = common.pkg -const CACHE_DIR = path.resolve(PKG_DIR, 'cache') -const cacheBase = cacheFile(CACHE_DIR)(common.registry + '/-/all') -const cachePath = path.join(cacheBase, '.cache.json') +// this test uses a fresh cache for each test block +// create them all in common.cache so that we can verify +// them for root-owned files in sudotest +let CACHE_DIR +let cacheBase +let cachePath +let cacheCounter = 1 +function setup () { + CACHE_DIR = common.cache + '/' + cacheCounter++ + cacheBase = cacheFile(CACHE_DIR)(common.registry + '/-/all') + cachePath = path.join(cacheBase, '.cache.json') + mkdirp.sync(cacheBase) + fixOwner(CACHE_DIR) +} + +const chownr = require('chownr') +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} let server @@ -46,7 +61,8 @@ test('notifies when there are no results', function (t) { common.npm([ 'search', 'none', '--registry', common.registry, - '--loglevel', 'error' + '--loglevel', 'error', + '--cache', CACHE_DIR ], {}, function (err, code, stdout, stderr) { if (err) throw err t.equal(stderr, '', 'no error output') @@ -71,6 +87,7 @@ test('spits out a useful error when no cache nor network', function (t) { const cacheContents = {} const fixture = new Tacks(File(cacheContents)) fixture.create(cachePath) + fixOwner(cachePath) common.npm([ 'search', 'foo', '--registry', common.registry, @@ -216,8 +233,8 @@ test('tab mode does not notify on empty', function (t) { }) test('no arguments provided should error', function (t) { - cleanup() - common.npm(['search'], {}, function (err, code, stdout, stderr) { + setup() + common.npm(['search', '--cache', CACHE_DIR], {}, function (err, code, stdout, stderr) { if (err) throw err t.equal(code, 1, 'search finished unsuccessfully') @@ -231,18 +248,7 @@ test('no arguments provided should error', function (t) { }) test('cleanup', function (t) { - cleanup() + server.done() server.close() t.end() }) - -function setup () { - cleanup() - mkdirp.sync(cacheBase) -} - -function cleanup () { - server.done() - process.chdir(osenv.tmpdir()) - rimraf.sync(PKG_DIR) -} diff --git a/test/tap/shared-linked.js b/test/tap/shared-linked.js index bb2d9bd459aa3..cbdbcf66c1612 100644 --- a/test/tap/shared-linked.js +++ b/test/tap/shared-linked.js @@ -44,7 +44,6 @@ var optimist = Dir({ var fixture = new Tacks( Dir({ - cache: Dir({}), global: Dir({ lib: Dir({ node_modules: Dir({ @@ -126,7 +125,7 @@ test('shared-linked', function (t) { }) } var config = [ - '--cache', path.join(testdir, 'cache'), + '--cache', common.cache, '--registry', common.registry, '--unicode', 'false' ] diff --git a/test/tap/shrinkwrap-default-dev.js b/test/tap/shrinkwrap-default-dev.js index 9ff998fa4846f..cd101092d4e92 100644 --- a/test/tap/shrinkwrap-default-dev.js +++ b/test/tap/shrinkwrap-default-dev.js @@ -9,7 +9,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/shrinkwrap-dev-dependency.js b/test/tap/shrinkwrap-dev-dependency.js index 8006aa9ea8a81..066372e617105 100644 --- a/test/tap/shrinkwrap-dev-dependency.js +++ b/test/tap/shrinkwrap-dev-dependency.js @@ -12,7 +12,7 @@ var common = require('../common-tap.js') var pkg = common.pkg var opts = [ - '--cache', path.resolve(pkg, 'cache'), + '--cache', common.cache, '--registry', common.registry ] diff --git a/test/tap/shrinkwrap-lifecycle-cwd.js b/test/tap/shrinkwrap-lifecycle-cwd.js index 203c2165ebe60..f8927df5a2969 100644 --- a/test/tap/shrinkwrap-lifecycle-cwd.js +++ b/test/tap/shrinkwrap-lifecycle-cwd.js @@ -9,7 +9,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') var escapeArg = require('../../lib/utils/escape-arg.js') diff --git a/test/tap/shrinkwrap-local-dependency.js b/test/tap/shrinkwrap-local-dependency.js index d737b10bb6b97..5e6d278d30c25 100644 --- a/test/tap/shrinkwrap-local-dependency.js +++ b/test/tap/shrinkwrap-local-dependency.js @@ -9,7 +9,7 @@ var File = Tacks.File var Dir = Tacks.Dir var testdir = common.pkg -var cachedir = path.resolve(testdir, 'cache') +var cachedir = common.cache var config = ['--cache=' + cachedir, '--loglevel=error'] var shrinkwrap = { diff --git a/test/tap/shrinkwrap-optional-dependency.js b/test/tap/shrinkwrap-optional-dependency.js index f2c853ac4b9a7..621e6c4c595e1 100644 --- a/test/tap/shrinkwrap-optional-dependency.js +++ b/test/tap/shrinkwrap-optional-dependency.js @@ -89,7 +89,7 @@ function setup (cb) { process.chdir(pkg) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, registry: common.registry } npm.load(opts, cb) diff --git a/test/tap/shrinkwrap-optional-platform.js b/test/tap/shrinkwrap-optional-platform.js index d6a3c41b516f1..f5b87d2f8d445 100644 --- a/test/tap/shrinkwrap-optional-platform.js +++ b/test/tap/shrinkwrap-optional-platform.js @@ -8,7 +8,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/shrinkwrap-optional-property.js b/test/tap/shrinkwrap-optional-property.js index 84bc5cb258281..f9b37f2f30e19 100644 --- a/test/tap/shrinkwrap-optional-property.js +++ b/test/tap/shrinkwrap-optional-property.js @@ -88,7 +88,7 @@ function setup (cb) { process.chdir(pkg) var opts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, registry: common.registry } npm.load(opts, cb) diff --git a/test/tap/shrinkwrap-prod-dependency-also.js b/test/tap/shrinkwrap-prod-dependency-also.js index 94f3c4ac9012a..96ebe2249e1c7 100644 --- a/test/tap/shrinkwrap-prod-dependency-also.js +++ b/test/tap/shrinkwrap-prod-dependency-also.js @@ -10,7 +10,7 @@ var test = require('tap').test var common = require('../common-tap.js') var pkg = common.pkg var opts = [ - '--cache=' + path.resolve(pkg, 'cache'), + '--cache=' + common.cache, '--registry=' + common.registry ] diff --git a/test/tap/shrinkwrap-prod-dependency.js b/test/tap/shrinkwrap-prod-dependency.js index eb04db9ca138c..6c175af819bcd 100644 --- a/test/tap/shrinkwrap-prod-dependency.js +++ b/test/tap/shrinkwrap-prod-dependency.js @@ -77,7 +77,7 @@ function setup (opts, cb) { process.chdir(pkg) var allOpts = { - cache: path.resolve(pkg, 'cache'), + cache: common.cache, registry: common.registry } diff --git a/test/tap/shrinkwrap-shared-dev-dependency.js b/test/tap/shrinkwrap-shared-dev-dependency.js index 6a4ceb1970ece..5aa331f34cc46 100644 --- a/test/tap/shrinkwrap-shared-dev-dependency.js +++ b/test/tap/shrinkwrap-shared-dev-dependency.js @@ -12,7 +12,7 @@ var pkg = common.pkg var opts = { env: common.newEnv().extend({ - npm_config_cache: path.resolve(pkg, 'cache'), + npm_config_cache: common.cache, npm_config_registry: common.registry }), stdio: [0, 1, 2], diff --git a/test/tap/sorted-package-json.js b/test/tap/sorted-package-json.js index d67b729f683e9..f802bfd9e5605 100644 --- a/test/tap/sorted-package-json.js +++ b/test/tap/sorted-package-json.js @@ -1,21 +1,28 @@ var test = require('tap').test var path = require('path') -var rimraf = require('rimraf') -var mkdirp = require('mkdirp') var common = require('../common-tap.js') var pkg = common.pkg var tmp = path.join(pkg, 'tmp') -var cache = path.join(pkg, 'cache') +var cache = common.cache var fs = require('fs') var mr = require('npm-registry-mock') -var osenv = require('osenv') var packageJson = path.resolve(pkg, 'package.json') -test('setup', function (t) { - setup() - t.pass('setup success') - t.done() -}) +fs.writeFileSync(packageJson, JSON.stringify({ + 'name': 'sorted-package-json', + 'version': '0.0.0', + 'description': '', + 'main': 'index.js', + 'scripts': { + 'test': 'echo \'Error: no test specified\' && exit 1' + }, + 'author': 'Rocko Artischocko', + 'license': 'ISC', + 'dependencies': { + 'underscore': '^1.3.3', + 'request': '^0.9.0' + } +}, null, 2), 'utf8') test('sorting dependencies', function (t) { var before = JSON.parse(fs.readFileSync(packageJson).toString()) @@ -52,38 +59,3 @@ test('sorting dependencies', function (t) { }) }) }) - -test('cleanup', function (t) { - cleanup() - t.pass('cleaned up') - t.end() -}) - -function setup () { - cleanup() - mkdirp.sync(pkg) - - fs.writeFileSync(packageJson, JSON.stringify({ - 'name': 'sorted-package-json', - 'version': '0.0.0', - 'description': '', - 'main': 'index.js', - 'scripts': { - 'test': 'echo \'Error: no test specified\' && exit 1' - }, - 'author': 'Rocko Artischocko', - 'license': 'ISC', - 'dependencies': { - 'underscore': '^1.3.3', - 'request': '^0.9.0' - } - }, null, 2), 'utf8') -} - -function cleanup () { - process.chdir(osenv.tmpdir()) - rimraf.sync(cache) - rimraf.sync(pkg) - mkdirp.sync(cache) - mkdirp.sync(tmp) -} diff --git a/test/tap/spec-local-specifiers.js b/test/tap/spec-local-specifiers.js index 7b6cacf12cb69..e1aa29cee4d3b 100644 --- a/test/tap/spec-local-specifiers.js +++ b/test/tap/spec-local-specifiers.js @@ -13,7 +13,7 @@ var isWindows = require('../../lib/utils/is-windows.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/splat-with-only-prerelease-to-latest.js b/test/tap/splat-with-only-prerelease-to-latest.js index 5720a2cdcedf4..ac2c58b5b9de9 100644 --- a/test/tap/splat-with-only-prerelease-to-latest.js +++ b/test/tap/splat-with-only-prerelease-to-latest.js @@ -3,9 +3,6 @@ const common = require('../common-tap.js') const mr = require('npm-registry-mock') const npm = require('../../lib/npm') -const osenv = require('osenv') -const path = require('path') -const rimraf = require('rimraf') const test = require('tap').test const testdir = common.pkg @@ -62,7 +59,7 @@ test('setup', (t) => { npm.load({ loglevel: 'silent', registry: common.registry, - cache: path.join(testdir, 'cache') + cache: common.cache }, (err) => { if (err) { throw err } t.ok(true, 'npm loaded') @@ -96,11 +93,3 @@ test('splat', (t) => { server.close() }) }) - -test('cleanup', (t) => { - process.chdir(osenv.tmpdir()) - rimraf(testdir, () => { - t.ok(true, 'cleaned up test dir') - t.done() - }) -}) diff --git a/test/tap/tag-version-prefix.js b/test/tap/tag-version-prefix.js index b72255a226152..70c968705ab07 100644 --- a/test/tap/tag-version-prefix.js +++ b/test/tap/tag-version-prefix.js @@ -2,15 +2,11 @@ var common = require('../common-tap.js') var fs = require('fs') var path = require('path') -var mkdirp = require('mkdirp') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var npm = require('../../lib/npm.js') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') var npmrc = path.resolve(pkg, '.npmrc') var packagePath = path.resolve(pkg, 'package.json') @@ -70,23 +66,8 @@ test('npm version with message config', function (t) { }) }) -test('cleanup', function (t) { - cleanup() - t.end() -}) - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - - rimraf.sync(pkg) -} - function setup () { - cleanup() - mkdirp.sync(cache) process.chdir(pkg) - fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8') fs.writeFileSync(npmrc, configContents, 'ascii') } diff --git a/test/tap/tagged-version-matching.js b/test/tap/tagged-version-matching.js index 64522745c80e2..a939c21c0d749 100644 --- a/test/tap/tagged-version-matching.js +++ b/test/tap/tagged-version-matching.js @@ -9,7 +9,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/unpack-foreign-tarball.js b/test/tap/unpack-foreign-tarball.js index a1649c24bd23f..7e965d9f08acc 100644 --- a/test/tap/unpack-foreign-tarball.js +++ b/test/tap/unpack-foreign-tarball.js @@ -13,7 +13,7 @@ var fixtures = path.resolve(__dirname, '..', 'fixtures') var pkg = common.pkg var nm = path.resolve(pkg, 'node_modules') var target = path.resolve(nm, 'npm-test-gitignore') -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var tmp = path.resolve(pkg, 'tmp') var EXEC_OPTS = { diff --git a/test/tap/update-examples.js b/test/tap/update-examples.js index f0a7a6271dae9..8b6323ff1c71a 100644 --- a/test/tap/update-examples.js +++ b/test/tap/update-examples.js @@ -2,7 +2,6 @@ var common = require('../common-tap.js') var test = require('tap').test var mkdirp = require('mkdirp') var rimraf = require('rimraf') -var path = require('path') var mr = require('npm-registry-mock') var osenv = require('osenv') @@ -10,7 +9,7 @@ var osenv = require('osenv') var requireInject = require('require-inject') var PKG_DIR = common.pkg -var CACHE_DIR = path.resolve(PKG_DIR, 'cache') +var CACHE_DIR = common.cache // ** constant templates for mocks ** var DEFAULT_PKG = { @@ -84,9 +83,17 @@ function extend (a, b) { return a } +const chownr = require('chownr') +const fixOwner = ( + process.getuid && process.getuid() === 0 && + process.env.SUDO_UID && process.env.SUDO_GID +) ? (path) => chownr.sync(path, +process.env.SUDO_UID, +process.env.SUDO_GID) + : () => {} + function resetPackage (options) { rimraf.sync(CACHE_DIR) mkdirp.sync(CACHE_DIR) + fixOwner(CACHE_DIR) installAskedFor = undefined diff --git a/test/tap/update-symlink.js b/test/tap/update-symlink.js index a21a68267b542..eda07c56e1f69 100644 --- a/test/tap/update-symlink.js +++ b/test/tap/update-symlink.js @@ -10,7 +10,7 @@ const common = require('../common-tap.js') const basedir = common.pkg const testdir = path.join(basedir, 'testdir') -const cachedir = path.join(basedir, 'cache') +const cachedir = common.cache const globaldir = path.join(basedir, 'global') const tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/upgrade-lifecycles.js b/test/tap/upgrade-lifecycles.js index 3adea06d71375..0821cacba60cd 100644 --- a/test/tap/upgrade-lifecycles.js +++ b/test/tap/upgrade-lifecycles.js @@ -8,7 +8,7 @@ var common = require('../common-tap.js') var basedir = common.pkg var testdir = path.join(basedir, 'testdir') -var cachedir = path.join(basedir, 'cache') +var cachedir = common.cache var globaldir = path.join(basedir, 'global') var tmpdir = path.join(basedir, 'tmp') diff --git a/test/tap/version-allow-same-version.js b/test/tap/version-allow-same-version.js index fdd68032bc6b9..6b7978edebece 100644 --- a/test/tap/version-allow-same-version.js +++ b/test/tap/version-allow-same-version.js @@ -9,7 +9,7 @@ var test = require('tap').test var common = require('../common-tap.js') var npm = require('../../') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var npmrc = path.resolve(pkg, './.npmrc') var configContents = 'sign-git-tag=false\n' diff --git a/test/tap/version-consistent-newlines.js b/test/tap/version-consistent-newlines.js index 56b16afcf4b48..4387c489e2c77 100644 --- a/test/tap/version-consistent-newlines.js +++ b/test/tap/version-consistent-newlines.js @@ -11,7 +11,7 @@ const rimraf = require('rimraf') const requireInject = require('require-inject') const pkg = common.pkg -const cache = path.resolve(pkg, 'cache') +const cache = common.cache const gitDir = path.resolve(pkg, '.git') test('npm version does not alter the line endings in package.json (LF)', function (t) { diff --git a/test/tap/version-from-git.js b/test/tap/version-from-git.js index 759396a782191..e63865a73378a 100644 --- a/test/tap/version-from-git.js +++ b/test/tap/version-from-git.js @@ -3,7 +3,6 @@ var fs = require('fs') var path = require('path') var mkdirp = require('mkdirp') -var osenv = require('osenv') var rimraf = require('rimraf') var test = require('tap').test @@ -11,7 +10,7 @@ var npm = require('../../lib/npm.js') var pkg = common.pkg var packagePath = path.resolve(pkg, 'package.json') -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'cat', version: '0.1.2' } @@ -187,20 +186,10 @@ test('npm version from-git without any versions', function (t) { } }) -test('cleanup', function (t) { - cleanup() - t.end() -}) - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -} - function setup () { - cleanup() - mkdirp.sync(cache) + process.chdir(__dirname) + rimraf.sync(pkg) + mkdirp.sync(pkg) process.chdir(pkg) fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8') } diff --git a/test/tap/version-git-not-clean.js b/test/tap/version-git-not-clean.js index b387c00e64de3..486e2e0766181 100644 --- a/test/tap/version-git-not-clean.js +++ b/test/tap/version-git-not-clean.js @@ -1,19 +1,15 @@ var common = require('../common-tap.js') var test = require('tap').test var npm = require('../../') -var osenv = require('osenv') -var path = require('path') var fs = require('fs') -var rimraf = require('rimraf') -var mkdirp = require('mkdirp') var which = require('which') var spawn = require('child_process').spawn var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache test('npm version with working directory not clean', function (t) { - setup() + process.chdir(pkg) npm.load({ cache: cache, registry: common.registry, prefix: pkg }, function () { which('git', function (err, git) { t.ifError(err, 'git found') @@ -81,17 +77,3 @@ test('npm version --force with working directory not clean', function ( t.end() }) }) - -test('cleanup', function (t) { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - - rimraf.sync(pkg) - t.end() -}) - -function setup () { - mkdirp.sync(pkg) - mkdirp.sync(cache) - process.chdir(pkg) -} diff --git a/test/tap/version-lifecycle.js b/test/tap/version-lifecycle.js index 98c96b6826d4d..590ae86aa4b02 100644 --- a/test/tap/version-lifecycle.js +++ b/test/tap/version-lifecycle.js @@ -2,14 +2,13 @@ var fs = require('graceful-fs') var path = require('path') var mkdirp = require('mkdirp') -var osenv = require('osenv') var rimraf = require('rimraf') var test = require('tap').test var common = require('../common-tap.js') var npm = require('../../') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var npmrc = path.resolve(pkg, './.npmrc') var configContents = 'sign-git-commit=false\nsign-git-tag=false\n' @@ -145,16 +144,11 @@ test('npm version execution order', function (t) { }) }) -test('cleanup', function (t) { - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) - t.end() -}) - function setup () { + process.chdir(__dirname) + rimraf.sync(pkg) mkdirp.sync(pkg) mkdirp.sync(path.join(pkg, 'node_modules')) - mkdirp.sync(cache) fs.writeFileSync(npmrc, configContents, 'ascii') process.chdir(pkg) } diff --git a/test/tap/version-message-config.js b/test/tap/version-message-config.js index db45809879733..94e9e951e776f 100644 --- a/test/tap/version-message-config.js +++ b/test/tap/version-message-config.js @@ -2,15 +2,11 @@ var common = require('../common-tap.js') var fs = require('fs') var path = require('path') -var mkdirp = require('mkdirp') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var npm = require('../../lib/npm.js') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') var npmrc = path.resolve(pkg, '.npmrc') var packagePath = path.resolve(pkg, 'package.json') @@ -54,21 +50,7 @@ test('npm version with message config', function (t) { }) }) -test('cleanup', function (t) { - cleanup() - t.end() -}) - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - - rimraf.sync(pkg) -} - function setup () { - cleanup() - mkdirp.sync(cache) process.chdir(pkg) fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8') diff --git a/test/tap/version-no-git.js b/test/tap/version-no-git.js index dc1ee05662ff6..cea8b55ddcd8c 100644 --- a/test/tap/version-no-git.js +++ b/test/tap/version-no-git.js @@ -1,15 +1,13 @@ var common = require('../common-tap.js') var test = require('tap').test var npm = require('../../') -var osenv = require('osenv') var path = require('path') var fs = require('fs') var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var requireInject = require('require-inject') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var gitDir = path.resolve(pkg, '.git') test('npm version in a git repo without the git binary', function (t) { @@ -33,16 +31,7 @@ test('npm version in a git repo without the git binary', function (t) { }) }) -test('cleanup', function (t) { - process.chdir(osenv.tmpdir()) - - rimraf.sync(pkg) - t.end() -}) - function setup () { - mkdirp.sync(pkg) - mkdirp.sync(cache) mkdirp.sync(gitDir) fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({ author: 'Terin Stock', diff --git a/test/tap/version-no-package.js b/test/tap/version-no-package.js index 1b16c9f758c8d..aa553b080148a 100644 --- a/test/tap/version-no-package.js +++ b/test/tap/version-no-package.js @@ -1,18 +1,10 @@ var common = require('../common-tap.js') var test = require('tap').test -var osenv = require('osenv') -var mkdirp = require('mkdirp') -var rimraf = require('rimraf') var pkg = common.pkg -test('setup', function (t) { - setup() - t.end() -}) - test('npm version in a prefix with no package.json', function (t) { - setup() + process.chdir(pkg) common.npm( ['version', '--json', '--prefix', pkg], { cwd: pkg }, @@ -29,15 +21,3 @@ test('npm version in a prefix with no package.json', function (t) { } ) }) - -test('cleanup', function (t) { - process.chdir(osenv.tmpdir()) - - rimraf.sync(pkg) - t.end() -}) - -function setup () { - mkdirp.sync(pkg) - process.chdir(pkg) -} diff --git a/test/tap/version-no-tags.js b/test/tap/version-no-tags.js index 9e7b08c61fa12..c2c11d875b55a 100644 --- a/test/tap/version-no-tags.js +++ b/test/tap/version-no-tags.js @@ -1,16 +1,13 @@ var common = require('../common-tap.js') var test = require('tap').test var npm = require('../../') -var osenv = require('osenv') var path = require('path') var fs = require('fs') -var rimraf = require('rimraf') -var mkdirp = require('mkdirp') var which = require('which') var spawn = require('child_process').spawn var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache test('npm version without git tag', function (t) { setup() @@ -50,17 +47,7 @@ test('npm version without git tag', function (t) { }) }) -test('cleanup', function (t) { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - - rimraf.sync(pkg) - t.end() -}) - function setup () { - mkdirp.sync(pkg) - mkdirp.sync(cache) fs.writeFileSync(path.resolve(pkg, 'package.json'), JSON.stringify({ author: 'Evan Lucas', name: 'version-no-tags-test', diff --git a/test/tap/version-prerelease-id.js b/test/tap/version-prerelease-id.js index 5e342667cdba2..0e248423cd052 100644 --- a/test/tap/version-prerelease-id.js +++ b/test/tap/version-prerelease-id.js @@ -1,24 +1,15 @@ var fs = require('fs') var path = require('path') -var mkdirp = require('mkdirp') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var npm = require('../../') var common = require('../common-tap.js') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') var EXEC_OPTS = { cwd: pkg } -test('setup', function (t) { - setup() - t.end() -}) - test('npm version --preid=rc uses prerelease id', function (t) { setup() @@ -32,15 +23,7 @@ test('npm version --preid=rc uses prerelease id', function (t) { }) }) -test('cleanup', function (t) { - cleanup() - t.end() -}) - function setup () { - cleanup() - mkdirp.sync(pkg) - mkdirp.sync(cache) var contents = { author: 'Daniel Wilches', name: 'version-prerelease-id', @@ -52,10 +35,3 @@ function setup () { fs.writeFileSync(path.resolve(pkg, 'npm-shrinkwrap.json'), JSON.stringify(contents), 'utf8') process.chdir(pkg) } - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - rimraf.sync(cache) - rimraf.sync(pkg) -} diff --git a/test/tap/version-sub-directory-shrinkwrap.js b/test/tap/version-sub-directory-shrinkwrap.js index c6e607d7c5767..5f2d688f42ba5 100644 --- a/test/tap/version-sub-directory-shrinkwrap.js +++ b/test/tap/version-sub-directory-shrinkwrap.js @@ -3,8 +3,6 @@ var fs = require('fs') var path = require('path') var mkdirp = require('mkdirp') -var osenv = require('osenv') -var rimraf = require('rimraf') var test = require('tap').test var npm = require('../../lib/npm.js') @@ -13,7 +11,7 @@ var pkg = common.pkg var subDirectory = path.resolve(pkg, 'sub-directory') var packagePath = path.resolve(pkg, 'package.json') var shrinkwrapPath = path.resolve(pkg, 'npm-shrinkwrap.json') -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'cat', version: '0.1.2' } @@ -59,20 +57,7 @@ test('npm version from a subdirectory', function (t) { } }) -test('cleanup', function (t) { - cleanup() - t.end() -}) - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - rimraf.sync(pkg) -} - function setup () { - cleanup() - mkdirp.sync(cache) mkdirp.sync(subDirectory) process.chdir(subDirectory) fs.writeFileSync(packagePath, JSON.stringify(json), 'utf8') diff --git a/test/tap/version-sub-directory.js b/test/tap/version-sub-directory.js index a7eb8f1042089..809f24e79ffa5 100644 --- a/test/tap/version-sub-directory.js +++ b/test/tap/version-sub-directory.js @@ -12,7 +12,7 @@ var npm = require('../../lib/npm.js') var pkg = common.pkg var subDirectory = path.resolve(pkg, 'sub-directory') var packagePath = path.resolve(pkg, 'package.json') -var cache = path.resolve(pkg, 'cache') +var cache = common.cache var json = { name: 'cat', version: '0.1.2' } diff --git a/test/tap/version-update-shrinkwrap.js b/test/tap/version-update-shrinkwrap.js index 47822f646ec7f..e6ba5ee6182ba 100644 --- a/test/tap/version-update-shrinkwrap.js +++ b/test/tap/version-update-shrinkwrap.js @@ -2,7 +2,6 @@ var fs = require('fs') var path = require('path') var mkdirp = require('mkdirp') -var osenv = require('osenv') var rimraf = require('rimraf') var test = require('tap').test @@ -10,11 +9,11 @@ var npm = require('../../') var common = require('../common-tap.js') var pkg = common.pkg -var cache = path.resolve(pkg, 'cache') +var cache = common.cache test('npm version updates shrinkwrap - no git', function (t) { setup() - npm.load({ cache: pkg + '/cache', registry: common.registry }, function () { + npm.load({ cache: cache, registry: common.registry }, function () { npm.commands.version(['patch'], function (err) { if (err) return t.fail('Error perform version patch') var shrinkwrap = require(path.resolve(pkg, 'npm-shrinkwrap.json')) @@ -111,15 +110,10 @@ test('npm version updates shrinkwrap and updates git', function (t) { } }) -test('cleanup', function (t) { - cleanup() - t.end() -}) - function setup () { - cleanup() + process.chdir(__dirname) + rimraf.sync(pkg) mkdirp.sync(pkg) - mkdirp.sync(cache) var contents = { author: 'Nathan Bowser && Faiq Raza', name: 'version-with-shrinkwrap-test', @@ -131,10 +125,3 @@ function setup () { fs.writeFileSync(path.resolve(pkg, 'npm-shrinkwrap.json'), JSON.stringify(contents), 'utf8') process.chdir(pkg) } - -function cleanup () { - // windows fix for locked files - process.chdir(osenv.tmpdir()) - rimraf.sync(cache) - rimraf.sync(pkg) -} diff --git a/test/tap/zz-cleanup.js b/test/tap/zz-cleanup.js deleted file mode 100644 index e1020aa3b11b5..0000000000000 --- a/test/tap/zz-cleanup.js +++ /dev/null @@ -1,8 +0,0 @@ -var common = require('../common-tap') -var test = require('tap').test -var rimraf = require('rimraf') - -test('cleanup', function (t) { - rimraf.sync(common.npm_config_cache) - t.end() -})