Skip to content

Commit

Permalink
fix: Do not throw errors until validateEnvVars is called
Browse files Browse the repository at this point in the history
Running `cmd.js` against a config outputs the `toMarkdown()`
result. This has no need to actually use or parse the env
variables, so it should not throw errors for missing
required variables. This error was introduced on the last
major refactor.

Ref: LOG-13202
  • Loading branch information
darinspivey committed Jun 9, 2022
1 parent eda8aa4 commit 8afa07d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
22 changes: 12 additions & 10 deletions lib/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,6 @@ module.exports = class Definition {
const value_is_missing = !has(process.env, this._env)
|| process.env[this._env] === EMPTY_STRING

if (this._required) {
if (value_is_missing) {
const err = new MissingEnvError({
type: this._type
, name: this._env
})
throw err
}
}

if (value_is_missing) {
if (this._default !== null && !this._allow_empty) {
this._value = this._transform(this._default)
Expand All @@ -191,6 +181,18 @@ module.exports = class Definition {
})
throw err
}
if (this._required) {
const value_is_missing = !has(process.env, this._env)
|| process.env[this._env] === EMPTY_STRING

if (value_is_missing) {
const err = new MissingEnvError({
type: this._type
, name: this._env
})
throw err
}
}
}

validate() {
Expand Down
7 changes: 1 addition & 6 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ test('bin', async (t) => {
const fixture = path.join(__dirname, 'fixtures/config.js')
const output_fp = path.join(__dirname, 'fixtures/output.md')
const output = await readFile(output_fp, 'utf8')
const {stdout} = await exec(`${bin} ${fixture}`, {
env: {
...process.env
, MY_STRING: 'my-string-value'
}
})
const {stdout} = await exec(`${bin} ${fixture}`)
t.equal(stdout, output, 'stdout is correct')
})
})

0 comments on commit 8afa07d

Please sign in to comment.