Skip to content

Commit

Permalink
fix(cli): Fix boolean flag defaults not being respected (#1405)
Browse files Browse the repository at this point in the history
Fixes #1404

Was overriding default with environment variable value, but not checking
if environment variable was actually set
  • Loading branch information
msfstef committed Jun 27, 2024
1 parent af6a7be commit cf7f316
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/blue-dots-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@electric-sql/cli": patch
---

Fix boolean flag defaults not being respected.
2 changes: 1 addition & 1 deletion components/cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function getConfigValue<K extends ConfigOptionName>(
// Then check if the option was passed as an environment variable
const envName = name.startsWith('ELECTRIC_') ? name : `ELECTRIC_${name}`
const envVal = process.env[envName]
if (configOptions[name].valueType === Boolean) {
if (envName in process.env && configOptions[name].valueType === Boolean) {
return (!!envVal &&
!['f', 'false', '0', '', 'no'].includes(
envVal?.toLocaleLowerCase()
Expand Down
33 changes: 33 additions & 0 deletions components/cli/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import test from 'ava'
import { getConfigValue, redactConfigSecrets } from '../src/config'
import { configOptions } from '../src/config-options'

const origEnv = { ...process.env }
const origConfigOptions = { ...configOptions }
test.beforeEach(() => {
// restore environment and config options
process.env = origEnv
Object.assign(configOptions, origConfigOptions)
})

test('getConfigValue respects boolean flag defaults', async (t) => {
const flagWithTrueDefault = '_MOCK_TRUE_DEFAULT'
const flagWithFalseDefault = '_MOCK_FALSE_DEFAULT'

configOptions[flagWithTrueDefault] = {
valueType: Boolean,
defaultVal: true,
}

configOptions[flagWithFalseDefault] = {
valueType: Boolean,
defaultVal: false,
}

t.is(getConfigValue(flagWithTrueDefault), true)
t.is(getConfigValue(flagWithFalseDefault), false)

// ensure environment overrides default
process.env[`ELECTRIC_${flagWithTrueDefault}`] = 'false'
process.env[`ELECTRIC_${flagWithFalseDefault}`] = 'true'
t.is(getConfigValue(flagWithTrueDefault), false)
t.is(getConfigValue(flagWithFalseDefault), true)
})

test('getConfigValue can capture `ELECTRIC_` prefixed CLI opitons', async (t) => {
const image = getConfigValue('ELECTRIC_IMAGE', { image: 'electric:test' })
Expand Down

0 comments on commit cf7f316

Please sign in to comment.