Skip to content

Commit

Permalink
fix: consolidate is-windows code
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar authored and lukekarrys committed Mar 28, 2022
1 parent d8dcc02 commit f76d4f2
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const nopt = require('nopt')
const configNames = Object.keys(definitions)
const shorthandNames = Object.keys(shorthands)
const allConfs = configNames.concat(shorthandNames)
const isWindowsShell = require('../utils/is-windows-shell.js')
const { isWindowsShell } = require('../utils/is-windows.js')
const fileExists = require('../utils/file-exists.js')

const { promisify } = require('util')
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { isServerPackage } = runScript
const rpj = require('read-package-json-fast')
const log = require('../utils/log-shim.js')
const didYouMean = require('../utils/did-you-mean.js')
const isWindowsShell = require('../utils/is-windows-shell.js')
const { isWindowsShell } = require('../utils/is-windows.js')

const cmdList = [
'publish',
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { version: npmVersion } = require('../../../package.json')
const ciDetect = require('@npmcli/ci-detect')
const ciName = ciDetect()
const querystring = require('querystring')
const isWindows = require('../is-windows.js')
const { isWindows } = require('../is-windows.js')
const { join } = require('path')

// used by cafile flattening to flatOptions.ca
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = (er, npm) => {
npm.config.loaded &&
er.dest.startsWith(npm.config.get('cache'))

const isWindows = require('./is-windows.js')
const { isWindows } = require('./is-windows.js')

if (!isWindows && (isCachePath || isCacheDest)) {
// user probably doesn't need this, but still add it to the debug log
Expand Down
3 changes: 0 additions & 3 deletions lib/utils/is-windows-bash.js

This file was deleted.

3 changes: 0 additions & 3 deletions lib/utils/is-windows-shell.js

This file was deleted.

7 changes: 6 additions & 1 deletion lib/utils/is-windows.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module.exports = process.platform === 'win32'
const isWindows = process.platform === 'win32'
const isWindowsShell = isWindows &&
!/^MINGW(32|64)$/.test(process.env.MSYSTEM) && process.env.TERM !== 'cygwin'

exports.isWindows = isWindows
exports.isWindowsShell = isWindowsShell
1 change: 1 addition & 0 deletions lib/utils/path.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// return the PATH array in a cross-platform way
// TODO this is only used in a single place
const PATH = process.env.PATH || process.env.Path || process.env.path
const { delimiter } = require('path')
module.exports = PATH.split(delimiter)
2 changes: 1 addition & 1 deletion test/lib/commands/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const loadMockCompletion = async (t, o = {}) => {
}
const res = await _loadMockNpm(t, {
mocks: {
'../../lib/utils/is-windows-shell.js': !!windows,
'../../lib/utils/is-windows.js': { isWindowsShell: !!windows },
...options.mocks,
},
...options,
Expand Down
1 change: 0 additions & 1 deletion test/lib/commands/explore.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const output = []
const logs = []
const getExplore = (windows) => {
const Explore = t.mock('../../../lib/commands/explore.js', {
'../../../lib/utils/is-windows.js': windows,
path: require('path')[windows ? 'win32' : 'posix'],
'read-package-json-fast': mockRPJ,
'@npmcli/run-script': mockRunScript,
Expand Down
6 changes: 3 additions & 3 deletions test/lib/commands/run-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const getRS = windows => {
}
),
'proc-log': log,
'../../../lib/utils/is-windows-shell.js': windows,
'../../../lib/utils/is-windows.js': { isWindowsShell: windows },
})
return new RunScript(npm)
}
Expand Down Expand Up @@ -859,7 +859,7 @@ t.test('workspaces', t => {
throw new Error('err')
},
'proc-log': log,
'../../../lib/utils/is-windows-shell.js': false,
'../../../lib/utils/is-windows.js': { isWindowsShell: false },
})
const runScript = new RunScript(npm)

Expand All @@ -877,7 +877,7 @@ t.test('workspaces', t => {
RUN_SCRIPTS.push(opts)
},
'proc-log': log,
'../../../lib/utils/is-windows-shell.js': false,
'../../../lib/utils/is-windows.js': { isWindowsShell: false },
})
const runScript = new RunScript(npm)

Expand Down
18 changes: 9 additions & 9 deletions test/lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ t.test('editor', t => {
t.test('has neither EDITOR nor VISUAL, system specific', t => {
mockGlobals(t, { 'process.env': { EDITOR: undefined, VISUAL: undefined } })
const defsWin = t.mock(defpath, {
[isWin]: true,
[isWin]: { isWindows: true },
})
t.equal(defsWin.editor.default, 'notepad.exe')
const defsNix = t.mock(defpath, {
[isWin]: false,
[isWin]: { isWindows: false },
})
t.equal(defsNix.editor.default, 'vi')
t.end()
Expand All @@ -69,12 +69,12 @@ t.test('shell', t => {
t.test('windows, env.ComSpec then cmd.exe', t => {
mockGlobals(t, { 'process.env.ComSpec': 'command.com' })
const defsComSpec = t.mock(defpath, {
[isWin]: true,
[isWin]: { isWindows: true },
})
t.equal(defsComSpec.shell.default, 'command.com')
mockGlobals(t, { 'process.env.ComSpec': undefined })
const defsNoComSpec = t.mock(defpath, {
[isWin]: true,
[isWin]: { isWindows: true },
})
t.equal(defsNoComSpec.shell.default, 'cmd')
t.end()
Expand All @@ -83,12 +83,12 @@ t.test('shell', t => {
t.test('nix, SHELL then sh', t => {
mockGlobals(t, { 'process.env.SHELL': '/usr/local/bin/bash' })
const defsShell = t.mock(defpath, {
[isWin]: false,
[isWin]: { isWindows: false },
})
t.equal(defsShell.shell.default, '/usr/local/bin/bash')
mockGlobals(t, { 'process.env.SHELL': undefined })
const defsNoShell = t.mock(defpath, {
[isWin]: false,
[isWin]: { isWindows: false },
})
t.equal(defsNoShell.shell.default, 'sh')
t.end()
Expand Down Expand Up @@ -158,18 +158,18 @@ t.test('unicode allowed?', t => {
t.test('cache', t => {
mockGlobals(t, { 'process.env.LOCALAPPDATA': 'app/data/local' })
const defsWinLocalAppData = t.mock(defpath, {
[isWin]: true,
[isWin]: { isWindows: true },
})
t.equal(defsWinLocalAppData.cache.default, 'app/data/local/npm-cache')

mockGlobals(t, { 'process.env.LOCALAPPDATA': undefined })
const defsWinNoLocalAppData = t.mock(defpath, {
[isWin]: true,
[isWin]: { isWindows: true },
})
t.equal(defsWinNoLocalAppData.cache.default, '~/npm-cache')

const defsNix = t.mock(defpath, {
[isWin]: false,
[isWin]: { isWindows: false },
})
t.equal(defsNix.cache.default, '~/.npm')

Expand Down
30 changes: 0 additions & 30 deletions test/lib/utils/is-windows-bash.js

This file was deleted.

8 changes: 0 additions & 8 deletions test/lib/utils/is-windows-shell.js

This file was deleted.

43 changes: 37 additions & 6 deletions test/lib/utils/is-windows.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
const t = require('tap')
const actuallyWindows = process.platform === 'win32'
t.equal(actuallyWindows, require('../../../lib/utils/is-windows.js'))
Object.defineProperty(process, 'platform', {
value: actuallyWindows ? 'posix' : 'win32',

const mockGlobals = require('../../fixtures/mock-globals')

t.test('is not windows', async t => {
mockGlobals(t, { 'process.platform': 'posix' })
t.match({
isWindows: false,
isWindowsShell: false,
}, t.mock('../../../lib/utils/is-windows.js'))
})

t.test('is windows, shell', async t => {
mockGlobals(t, {
'process.platform': 'win32',
'process.env': {
MSYSTEM: 'notmingw',
TERM: 'notcygwin',
},
})
t.match({
isWindows: true,
isWindowsShell: true,
}, t.mock('../../../lib/utils/is-windows.js'))
})

t.test('is windows, not shell', async t => {
mockGlobals(t, {
'process.platform': 'win32',
'process.env': {
MSYSTEM: 'MINGW32',
TERM: 'cygwin',
},
})
t.match({
isWindows: true,
isWindowsShell: false,
}, t.mock('../../../lib/utils/is-windows.js'))
})
delete require.cache[require.resolve('../../../lib/utils/is-windows.js')]
t.equal(!actuallyWindows, require('../../../lib/utils/is-windows.js'))
3 changes: 2 additions & 1 deletion test/lib/utils/path.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const t = require('tap')
const mod = '../../../lib/utils/path.js'
const delim = require('../../../lib/utils/is-windows.js') ? ';' : ':'
const { isWindows } = require('../../../lib/utils/is-windows.js')
const delim = isWindows ? ';' : ':'
Object.defineProperty(process, 'env', {
value: {},
})
Expand Down

0 comments on commit f76d4f2

Please sign in to comment.