Skip to content

Commit 2652baa

Browse files
committed
test: more affordances for Windows
PR-URL: #1509 Credit: @isaacs Close: #1509 Reviewed-by: @ruyadorno
1 parent bf2e334 commit 2652baa

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

lib/npm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ const npm = module.exports = new class extends EventEmitter {
243243
}
244244

245245
get localDir () {
246-
return resolve(this.prefix, 'node_modules')
246+
return resolve(this.localPrefix, 'node_modules')
247247
}
248248

249249
get dir () {

test/lib/install.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ test('completion to folder', (t) => {
109109
partialWord: '/ar'
110110
}, (er, res) => {
111111
t.equal(er, null)
112-
t.strictSame(res, ['/arborist'], 'package dir match')
112+
const expect = process.platform === 'win32' ? '\\arborist' : '/arborist'
113+
t.strictSame(res, [expect], 'package dir match')
113114
t.end()
114115
})
115116
})

test/lib/ls.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
const { resolve } = require('path')
22

3-
const { test } = require('tap')
3+
const t = require('tap')
44
const requireInject = require('require-inject')
55

6+
t.cleanSnapshot = str => str.split(/\r\n/).join('\n')
7+
68
const simpleNmFixture = {
79
node_modules: {
810
'foo': {
@@ -114,17 +116,16 @@ const ls = requireInject('../../lib/ls.js', {
114116
})
115117

116118
const redactCwd = res =>
117-
res && res.replace(/\\/g, '/').replace(new RegExp(__dirname.replace(/\\/g, '/'), 'gi'), '{CWD}')
119+
res && res.replace(/\\+/g, '/').replace(new RegExp(__dirname.replace(/\\+/g, '/'), 'gi'), '{CWD}')
118120

119-
const jsonParse = res =>
120-
JSON.parse(redactCwd(res))
121+
const jsonParse = res => JSON.parse(redactCwd(res))
121122

122123
const cleanUpResult = (done, t) => {
123124
result = ''
124125
done()
125126
}
126127

127-
test('ls', (t) => {
128+
t.test('ls', (t) => {
128129
t.beforeEach(cleanUpResult)
129130
_flatOptions.json = false
130131
_flatOptions.unicode = false
@@ -423,7 +424,7 @@ test('ls', (t) => {
423424
ls([], (err) => {
424425
t.equal(err.code, 'ELSPROBLEMS', 'should have error code')
425426
t.equal(
426-
redactCwd(err.message),
427+
redactCwd(err.message).replace(/\r\n/g, '\n'),
427428
'invalid: foo@1.0.0 {CWD}/ls-ls-missing-invalid-extraneous/node_modules/foo\n' +
428429
'missing: ipsum@^1.0.0, required by test-npm-ls@1.0.0\n' +
429430
'extraneous: lorem@1.0.0 {CWD}/ls-ls-missing-invalid-extraneous/node_modules/lorem',
@@ -1334,7 +1335,7 @@ test('ls', (t) => {
13341335
t.end()
13351336
})
13361337

1337-
test('ls --parseable', (t) => {
1338+
t.test('ls --parseable', (t) => {
13381339
t.beforeEach(cleanUpResult)
13391340
_flatOptions.json = false
13401341
_flatOptions.unicode = false
@@ -2128,7 +2129,7 @@ test('ls --parseable', (t) => {
21282129
t.end()
21292130
})
21302131

2131-
test('ls --json', (t) => {
2132+
t.test('ls --json', (t) => {
21322133
t.beforeEach(cleanUpResult)
21332134
_flatOptions.json = true
21342135
_flatOptions.parseable = false

test/lib/npm.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const t = require('tap')
2+
const fs = require('fs')
23

34
// delete this so that we don't have configs from the fact that it
45
// is being run by 'npm test'
@@ -8,6 +9,8 @@ for (env of Object.keys(process.env).filter(e => /^npm_/.test(e))) {
89

910
const { resolve } = require('path')
1011

12+
const actualPlatform = process.platform
13+
1114
const beWindows = () => {
1215
Object.defineProperty(process, 'platform', {
1316
value: 'win32',
@@ -126,7 +129,7 @@ t.test('npm.load', t => {
126129
])
127130
logs.length = 0
128131

129-
t.equal(npm.cache, CACHE, 'cache is cache')
132+
t.equal(resolve(npm.cache), resolve(CACHE), 'cache is cache')
130133
const newCache = t.testdir()
131134
npm.cache = newCache
132135
t.equal(npm.config.get('cache'), newCache, 'cache setter sets config')
@@ -152,10 +155,10 @@ t.test('npm.load', t => {
152155
npm.config.set('global', true)
153156
t.equal(npm.prefix, npm.globalPrefix, 'prefix is global prefix after setting global')
154157
t.notEqual(npm.prefix, npm.localPrefix, 'prefix is not local prefix after setting global')
155-
t.equal(npm.bin, npm.globalBin, 'bin is global bin after prefix setter')
156-
t.notEqual(npm.bin, npm.localBin, 'bin is not local bin after prefix setter')
157-
t.equal(npm.dir, npm.globalDir, 'dir is global dir after prefix setter')
158-
t.notEqual(npm.dir, npm.localDir, 'dir is not local dir after prefix setter')
158+
t.equal(npm.bin, npm.globalBin, 'bin is global bin after setting global')
159+
t.notEqual(npm.bin, npm.localBin, 'bin is not local bin after setting global')
160+
t.equal(npm.dir, npm.globalDir, 'dir is global dir after setting global')
161+
t.notEqual(npm.dir, npm.localDir, 'dir is not local dir after setting global')
159162

160163
npm.prefix = dir + '/new/global/prefix'
161164
t.equal(npm.prefix, npm.globalPrefix, 'prefix is global prefix after prefix setter')
@@ -214,11 +217,14 @@ t.test('npm.load', t => {
214217
})
215218

216219
t.test('node is a symlink', t => {
217-
const node = process.platform === 'win32' ? 'node.exe' : 'node'
220+
const node = actualPlatform === 'win32' ? 'node.exe' : 'node'
218221
const dir = t.testdir({
219-
[node]: t.fixture('symlink', process.execPath),
220222
'.npmrc': 'foo = bar'
221223
})
224+
225+
// create manually to set the 'file' option in windows
226+
fs.symlinkSync(process.execPath, resolve(dir, node), 'file')
227+
222228
const PATH = process.env.PATH || process.env.Path
223229
process.env.PATH = dir
224230
const { execPath } = process

test/lib/utils/error-message.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const bePosix = () => {
2626
delete require.cache[require.resolve('../../../lib/utils/is-windows.js')]
2727
}
2828

29+
const { resolve } = require('path')
2930
const npm = require('../../../lib/npm.js')
3031
const CACHE = '/some/cache/dir'
3132
npm.config = {
@@ -257,9 +258,9 @@ t.test('json parse', t => {
257258
})
258259
Object.defineProperty(npm, 'prefix', { value: dir, configurable: true })
259260
process.argv = ['arg', 'v']
260-
t.matchSnapshot(errorMessage(Object.assign(new Error('conflicted'), {
261+
const ok = t.matchSnapshot(errorMessage(Object.assign(new Error('conflicted'), {
261262
code: 'EJSONPARSE',
262-
file: `${dir}/package.json`
263+
file: resolve(dir, 'package.json')
263264
})))
264265
t.end()
265266
})

0 commit comments

Comments
 (0)