Skip to content

Commit

Permalink
test: use t.testdir() instead of manually creating test dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 29, 2019
1 parent a79846e commit ad72e94
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 60 deletions.
12 changes: 5 additions & 7 deletions test/dir.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const DirFetcher = require('../lib/dir.js')
const t = require('tap')
const { relative, resolve, basename } = require('path')
const npa = require('npm-package-arg')
const fs = require('fs')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const mkdirp = require('mkdirp')
const me = resolve(__dirname, basename(__filename, '.js'))
rimraf.sync(me)
mkdirp.sync(me)

const { relative, resolve, basename } = require('path')

const me = t.testdir()

t.cleanSnapshot = str => str.split(process.cwd()).join('${CWD}')
t.teardown(() => rimraf.sync(me))

const abbrev = resolve(__dirname, 'fixtures/abbrev')
const abbrevspec = `file:${relative(process.cwd(), abbrev)}`
Expand Down
9 changes: 2 additions & 7 deletions test/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@ if (fakeSudo) {
}

const { relative, resolve, basename } = require('path')
const me = resolve(__dirname, basename(__filename, '.js'))
const Fetcher = require('../lib/fetcher.js')
const t = require('tap')
const me = t.testdir()
const Fetcher = require('../lib/fetcher.js')
t.cleanSnapshot = s => s.split(process.cwd()).join('{CWD}')
if (!fakeSudo)
t.teardown(() => require('rimraf').sync(me))

require('rimraf').sync(me)
const npa = require('npm-package-arg')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const mkdirp = require('mkdirp')
mkdirp.sync(me)

const _tarballFromResolved = Symbol.for('pacote.Fetcher._tarballFromResolved')

Expand Down
5 changes: 1 addition & 4 deletions test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ const npa = require('npm-package-arg')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const mkdirp = require('mkdirp')
const me = resolve(__dirname, basename(__filename, '.js'))
rimraf.sync(me)
mkdirp.sync(me)
const me = t.testdir()
t.cleanSnapshot = str => str.split(process.cwd()).join('${CWD}')
t.teardown(() => rimraf.sync(me))

const abbrev = resolve(__dirname, 'fixtures/abbrev-1.1.1.tgz')
const abbrevspec = `file:${relative(process.cwd(), abbrev)}`
Expand Down
10 changes: 7 additions & 3 deletions test/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ const t = require('tap')
const fs = require('fs')
const http = require('http')

const {resolve, basename, relative} = require('path')
const {resolve} = require('path')
const rimraf = require('rimraf')
const abbrev = resolve(__dirname, 'fixtures/abbrev-1.1.1.tgz')
const npa = require('npm-package-arg')

const me = resolve(__dirname, basename(__filename, '.js'))
rimraf.sync(me)
t.saveFixture = true
const me = t.testdir({
repo: {},
cache: {},
})
const repo = resolve(me, 'repo')
const cache = resolve(me, 'cache')

const abbrevSpec = `file:${abbrev}`

const spawnGit = require('../lib/util/git/spawn.js')
Expand Down
7 changes: 2 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const t = require('tap')
const pacote = require('../lib/index.js')
const fs = require('fs')

const { resolve, basename, relative } = require('path')
const { resolve, relative } = require('path')
const abbrev = resolve(__dirname, 'fixtures/abbrev-1.1.1.tgz')
const abbrevspec = `file:${relative(process.cwd(), abbrev)}`
const abbrevMani = require('./fixtures/abbrev-manifest-min.json')
Expand All @@ -12,10 +12,7 @@ const ignore = resolve(__dirname, 'fixtures/ignore-pkg.tgz')
const ignorespec = `file:${relative(process.cwd(), ignore)}`

const mkdirp = require('mkdirp')
const me = resolve(__dirname, basename(__filename, '.js'))
mkdirp.sync(me)
const rimraf = require('rimraf')
t.teardown(() => rimraf.sync(me))
const me = t.testdir()

t.cleanSnapshot = str => str
.split(process.cwd()).join('${CWD}')
Expand Down
7 changes: 1 addition & 6 deletions test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ const RegistryFetcher = require('../lib/registry.js')
const t = require('tap')
const mr = require('npm-registry-mock')
const port = 18000 + (+process.env.TAP_CHILD_ID || 0)
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const {resolve, basename} = require('path')
const me = resolve(__dirname, basename(__filename, '.js'))
rimraf.sync(me)
mkdirp.sync(me)
t.teardown(() => rimraf.sync(me))
const me = t.testdir()

t.test('start mock registry', { bail: true }, t => {
mr({
Expand Down
9 changes: 2 additions & 7 deletions test/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ const RemoteFetcher = require('../lib/remote.js')
const http = require('http')
const t = require('tap')

const { relative, resolve, basename } = require('path')
const me = resolve(__dirname, basename(__filename, '.js'))
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
rimraf.sync(me)
mkdirp.sync(me)
const { relative, resolve } = require('path')
const me = t.testdir()
const cache = resolve(me, 'cache')
t.teardown(() => rimraf.sync(me))

t.cleanSnapshot = str => str.split(''+port).join('{PORT}')

Expand Down
12 changes: 6 additions & 6 deletions test/util/git/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ const clone = require('../../../lib/util/git/clone.js')
const t = require('tap')
const fs = require('fs')
const {spawn} = require('child_process')
const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const { promisify } = require('util')
const { resolve, basename } = require('path')
const { resolve } = require('path')

const port = 12345 + (+process.env.TAP_CHILD_ID || 0)
const spawnGit = require('../../../lib/util/git/spawn.js')
const me = resolve(__dirname, basename(__filename, '.js'))
rimraf.sync(me)
t.saveFixture = true
const me = t.testdir({
'submodule-repo': {},
repo: {},
})
const remote = `git://localhost:${port}/repo`
const submodsRemote = `git://localhost:${port}/submodule-repo`
const repo = resolve(me, 'repo')

t.test('create repo', { bail: true }, t => {
const git = (...cmd) => spawnGit(cmd, { cwd: repo })
const write = (f, c) => fs.writeFileSync(`${repo}/${f}`, c)
mkdirp.sync(repo)
return git('init')
.then(() => write('foo', 'bar'))
.then(() => git('add', 'foo'))
Expand Down Expand Up @@ -81,7 +82,6 @@ t.test('create a repo with a submodule', { bail: true }, t => {
const repo = resolve(me, 'submodule-repo')
const git = (...cmd) => spawnGit(cmd, { cwd: repo })
const write = (f, c) => fs.writeFileSync(`${repo}/${f}`, c)
mkdirp.sync(repo)
return git('init')
.then(() => write('file', 'data'))
.then(() => git('add', 'file'))
Expand Down
16 changes: 2 additions & 14 deletions test/util/git/revs.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
const revs = require('../../../lib/util/git/revs.js')
const spawn = require('../../../lib/util/git/spawn.js')

const { resolve, basename } = require('path')
const repo = resolve(__dirname, basename(__filename, '.js'))

const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const { promisify } = require('util')

const t = require('tap')

t.teardown(() => rimraf.sync(repo))

const repo = t.testdir()
const fs = require('fs')

const git = (...cmd) => spawn(cmd, {cwd: repo})
const write = (f, c) => fs.writeFileSync(`${repo}/${f}`, c)
t.test('setup', t =>
promisify(rimraf)(repo)
.then(() => mkdirp.sync(repo))
.then(() => git('init'))
git('init')
.then(() => write('foo', 'bar'))
.then(() => git('add', 'foo'))
.then(() => git('commit', '-m', 'foobar'))
Expand Down
1 change: 0 additions & 1 deletion test/util/spawn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const main = () => {
const t = require('tap')
const spawn = require('../../lib/util/spawn.js')
Expand Down

0 comments on commit ad72e94

Please sign in to comment.