Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
fix(lib): Handle special characters and quoted strings
Browse files Browse the repository at this point in the history
close #6
  • Loading branch information
inyono committed Dec 25, 2015
1 parent 187c068 commit 2e75b73
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {spawn} from 'cross-spawn-async';
import assign from 'lodash.assign';
export default crossEnv;

const envSetterRegex = /(\w+)=(\w+)/;
const envSetterRegex = /(\w+)=('(.+)'|"(.+)"|(.+))/;

function crossEnv(args) {
const [command, commandArgs, env] = getCommandArgsAndEnvVars(args);
Expand All @@ -19,7 +19,7 @@ function getCommandArgsAndEnvVars(args) {
const shifted = commandArgs.shift();
const match = envSetterRegex.exec(shifted);
if (match) {
envVars[match[1]] = match[2];
envVars[match[1]] = match[3] || match[4] || match[5];
} else {
command = shifted;
break;
Expand Down
45 changes: 37 additions & 8 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,60 @@ describe(`cross-env`, () => {
});

it(`should set environment variables and run the remaining command`, () => {
testEnvSetting('FOO_ENV=production');
testEnvSetting({
FOO_ENV: 'production'
}, 'FOO_ENV=production');
});

it(`should handle multiple env variables`, () => {
testEnvSetting('FOO_ENV=production', 'bar_env=dev');
testEnvSetting({
FOO_ENV: 'production',
BAR_ENV: 'dev'
}, 'FOO_ENV=production', 'BAR_ENV=dev');
});

it(`should handle special characters`, () => {
testEnvSetting({
FOO_ENV: './!?'
}, 'FOO_ENV=./!?');
});

it(`should handle single-quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'bar env'
}, 'FOO_ENV=\'bar env\'');
});

it(`should handle double-quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'bar env'
}, 'FOO_ENV="bar env"');
});

it(`should handle equality signs in quoted strings`, () => {
testEnvSetting({
FOO_ENV: 'foo=bar'
}, 'FOO_ENV="foo=bar"');
});

it(`should do nothing given no command`, () => {
crossEnv([]);
expect(proxied['cross-spawn-async'].spawn).to.have.not.been.called;
});

function testEnvSetting(...envSettings) {
function testEnvSetting(expected, ...envSettings) {
const ret = crossEnv([...envSettings, 'echo', 'hello world']);
const env = {[getPathVar()]: process.env[getPathVar()]};
env.APPDATA = process.env.APPDATA;
envSettings.forEach(setting => {
const [prop, val] = setting.split('=');
env[prop] = val;
});
assign(env, expected);

expect(ret, 'returns what spawn returns').to.equal('spawn-returned');
expect(proxied['cross-spawn-async'].spawn).to.have.been.calledOnce;
expect(proxied['cross-spawn-async'].spawn).to.have.been.calledWith(
'echo', ['hello world'], {stdio: 'inherit', env: assign({}, process.env, env)}
'echo', ['hello world'], {
stdio: 'inherit',
env: assign({}, process.env, env)
}
);
}
});

0 comments on commit 2e75b73

Please sign in to comment.