mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
This pull request adds support for expanding environment variable expansion support. Closes #91 Since the user controls the call site of cmd-env, it only supports basic UNIX style $var format. `%var%` (windows) or `${var}` is not supported. However, you can escape variable expansion by escaping the dollar sign like so `\$`. Signed-off-by: omeid matten <public@omeid.me>
186 lines
5.3 KiB
TypeScript
186 lines
5.3 KiB
TypeScript
import * as sinon from 'sinon'
|
|
import { assert } from 'chai'
|
|
import * as parseArgsLib from '../src/parse-args'
|
|
import * as getEnvVarsLib from '../src/get-env-vars'
|
|
import * as expandEnvsLib from '../src/expand-envs'
|
|
import * as spawnLib from '../src/spawn'
|
|
import * as envCmdLib from '../src/env-cmd'
|
|
|
|
describe('CLI', (): void => {
|
|
let parseArgsStub: sinon.SinonStub<any, any>
|
|
let envCmdStub: sinon.SinonStub<any, any>
|
|
let processExitStub: sinon.SinonStub<any, any>
|
|
before((): void => {
|
|
parseArgsStub = sinon.stub(parseArgsLib, 'parseArgs')
|
|
envCmdStub = sinon.stub(envCmdLib, 'EnvCmd')
|
|
processExitStub = sinon.stub(process, 'exit')
|
|
})
|
|
|
|
after((): void => {
|
|
sinon.restore()
|
|
})
|
|
|
|
afterEach((): void => {
|
|
sinon.resetHistory()
|
|
sinon.resetBehavior()
|
|
})
|
|
|
|
it('should parse the provided args and execute the EnvCmd', async (): Promise<void> => {
|
|
parseArgsStub.returns({})
|
|
await envCmdLib.CLI(['node', './env-cmd', '-v'])
|
|
assert.equal(parseArgsStub.callCount, 1)
|
|
assert.equal(envCmdStub.callCount, 1)
|
|
assert.equal(processExitStub.callCount, 0)
|
|
})
|
|
|
|
it('should catch exception if EnvCmd throws an exception', async (): Promise<void> => {
|
|
parseArgsStub.returns({})
|
|
envCmdStub.throwsException('Error')
|
|
await envCmdLib.CLI(['node', './env-cmd', '-v'])
|
|
assert.equal(parseArgsStub.callCount, 1)
|
|
assert.equal(envCmdStub.callCount, 1)
|
|
assert.equal(processExitStub.callCount, 1)
|
|
assert.equal(processExitStub.args[0][0], 1)
|
|
})
|
|
})
|
|
|
|
describe('EnvCmd', (): void => {
|
|
let getEnvVarsStub: sinon.SinonStub<any, any>
|
|
let spawnStub: sinon.SinonStub<any, any>
|
|
let expandEnvsSpy: sinon.SinonSpy<any, any>
|
|
before((): void => {
|
|
getEnvVarsStub = sinon.stub(getEnvVarsLib, 'getEnvVars')
|
|
spawnStub = sinon.stub(spawnLib, 'spawn')
|
|
spawnStub.returns({ on: (): void => {}, kill: (): void => {} })
|
|
expandEnvsSpy = sinon.spy(expandEnvsLib, 'expandEnvs')
|
|
})
|
|
|
|
after((): void => {
|
|
sinon.restore()
|
|
})
|
|
|
|
afterEach((): void => {
|
|
sinon.resetHistory()
|
|
})
|
|
|
|
it('should parse the provided args and execute the EnvCmd', async (): Promise<void> => {
|
|
getEnvVarsStub.returns({ BOB: 'test' })
|
|
await envCmdLib.EnvCmd({
|
|
command: 'node',
|
|
commandArgs: ['-v'],
|
|
envFile: {
|
|
filePath: './.env',
|
|
fallback: true
|
|
},
|
|
rc: {
|
|
environments: ['dev'],
|
|
filePath: './.rc'
|
|
}
|
|
})
|
|
assert.equal(getEnvVarsStub.callCount, 1)
|
|
assert.equal(spawnStub.callCount, 1)
|
|
})
|
|
|
|
it('should should override existing env vars if noOverride option is false/missing',
|
|
async (): Promise<void> => {
|
|
process.env.BOB = 'cool'
|
|
getEnvVarsStub.returns({ BOB: 'test' })
|
|
await envCmdLib.EnvCmd({
|
|
command: 'node',
|
|
commandArgs: ['-v'],
|
|
envFile: {
|
|
filePath: './.env',
|
|
fallback: true
|
|
},
|
|
rc: {
|
|
environments: ['dev'],
|
|
filePath: './.rc'
|
|
}
|
|
})
|
|
assert.equal(getEnvVarsStub.callCount, 1)
|
|
assert.equal(spawnStub.callCount, 1)
|
|
assert.equal(spawnStub.args[0][2].env.BOB, 'test')
|
|
}
|
|
)
|
|
|
|
it('should should not override existing env vars if noOverride option is true',
|
|
async (): Promise<void> => {
|
|
process.env.BOB = 'cool'
|
|
getEnvVarsStub.returns({ BOB: 'test' })
|
|
await envCmdLib.EnvCmd({
|
|
command: 'node',
|
|
commandArgs: ['-v'],
|
|
envFile: {
|
|
filePath: './.env',
|
|
fallback: true
|
|
},
|
|
rc: {
|
|
environments: ['dev'],
|
|
filePath: './.rc'
|
|
},
|
|
options: {
|
|
noOverride: true
|
|
}
|
|
})
|
|
assert.equal(getEnvVarsStub.callCount, 1)
|
|
assert.equal(spawnStub.callCount, 1)
|
|
assert.equal(spawnStub.args[0][2].env.BOB, 'cool')
|
|
}
|
|
)
|
|
|
|
it('should should spawn process with shell option if useShell option is true',
|
|
async (): Promise<void> => {
|
|
process.env.BOB = 'cool'
|
|
getEnvVarsStub.returns({ BOB: 'test' })
|
|
await envCmdLib.EnvCmd({
|
|
command: 'node',
|
|
commandArgs: ['-v'],
|
|
envFile: {
|
|
filePath: './.env',
|
|
fallback: true
|
|
},
|
|
rc: {
|
|
environments: ['dev'],
|
|
filePath: './.rc'
|
|
},
|
|
options: {
|
|
useShell: true
|
|
}
|
|
})
|
|
assert.equal(getEnvVarsStub.callCount, 1)
|
|
assert.equal(spawnStub.callCount, 1)
|
|
assert.equal(spawnStub.args[0][2].shell, true)
|
|
}
|
|
)
|
|
|
|
it('should should spawn process with command and args expanded if expandEnvs option is true',
|
|
async (): Promise<void> => {
|
|
getEnvVarsStub.returns({ PING: 'PONG', CMD: 'node' })
|
|
await envCmdLib.EnvCmd({
|
|
command: '$CMD',
|
|
commandArgs: ['$PING', '\\$IP'],
|
|
envFile: {
|
|
filePath: './.env',
|
|
fallback: true
|
|
},
|
|
rc: {
|
|
environments: ['dev'],
|
|
filePath: './.rc'
|
|
},
|
|
options: {
|
|
expandEnvs: true
|
|
}
|
|
})
|
|
|
|
const spawnArgs = spawnStub.args[0]
|
|
|
|
assert.equal(getEnvVarsStub.callCount, 1, 'getEnvVars must be called once')
|
|
assert.equal(spawnStub.callCount, 1)
|
|
assert.equal(expandEnvsSpy.callCount, 3, 'command + number of args')
|
|
assert.equal(spawnArgs[0], 'node')
|
|
assert.sameOrderedMembers(spawnArgs[1], ['PONG', '\\$IP'])
|
|
assert.equal(spawnArgs[2].env.PING, 'PONG')
|
|
}
|
|
)
|
|
})
|