From b212e54a3504bf08e504ab5935cdc7d1832ac050 Mon Sep 17 00:00:00 2001 From: aberkovsky Date: Thu, 17 Oct 2019 10:01:51 +0300 Subject: [PATCH] remove duplicate of PATH environment variable on Windows --- src/__tests__/index.js | 1 + src/__tests__/patch-path.js | 22 ++++++++++++++++++++++ src/index.js | 2 ++ src/patch-path.js | 9 +++++++++ 4 files changed, 34 insertions(+) create mode 100644 src/__tests__/patch-path.js create mode 100644 src/patch-path.js diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 0d5abae..237f031 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -2,6 +2,7 @@ const crossSpawnMock = require('cross-spawn') const isWindowsMock = require('../is-windows') jest.mock('../is-windows') +jest.mock('../patch-path') jest.mock('cross-spawn') const crossEnv = require('../') diff --git a/src/__tests__/patch-path.js b/src/__tests__/patch-path.js new file mode 100644 index 0000000..11b7b37 --- /dev/null +++ b/src/__tests__/patch-path.js @@ -0,0 +1,22 @@ +const patchPathEnv = require('../patch-path') +const isWindowsMock = require('../is-windows') + +jest.mock('../is-windows') + +beforeEach(() => { + isWindowsMock.mockReturnValue(true) +}) + +test(`remove duplicate of PATH variable if the current OS is Windows`, () => { + isWindowsMock.mockReturnValue(true) + const env = {PATH: 'path1', Path: 'path2'} + patchPathEnv(env) + expect(env).toStrictEqual({PATH: 'path1'}) +}) + +test(`do nothing if the current OS is not Windows`, () => { + isWindowsMock.mockReturnValue(false) + const env = {PATH: 'path1', Path: 'path2'} + patchPathEnv(env) + expect(env).toStrictEqual({PATH: 'path1', Path: 'path2'}) +}) diff --git a/src/index.js b/src/index.js index d9bd227..df64143 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,14 @@ const {spawn} = require('cross-spawn') const commandConvert = require('./command') const varValueConvert = require('./variable') +const patchPathEnv = require('./patch-path') module.exports = crossEnv const envSetterRegex = /(\w+)=('(.*)'|"(.*)"|(.*))/ function crossEnv(args, options = {}) { + patchPathEnv(process.env) const [envSetters, command, commandArgs] = parseCommand(args) const env = getEnvVars(envSetters) if (command) { diff --git a/src/patch-path.js b/src/patch-path.js new file mode 100644 index 0000000..a80f439 --- /dev/null +++ b/src/patch-path.js @@ -0,0 +1,9 @@ +const isWindows = require('./is-windows') + +module.exports = patchPathEnv + +function patchPathEnv(env) { + if (isWindows() && env.Path && env.PATH) { + delete env.Path + } +}