Repository: dolthub/dolt
File: go/libraries/doltcore/servercfg/yaml_config_test.go (Go)

Code before:
`), 0o644)
	require.NoError(t, err)

	cfg, err := YamlConfigFromFile(fs, "config.yaml")
	require.NoError(t, err)
	require.Equal(t, "127.0.0.1", cfg.Host())
}

func TestYamlConfigFromFileEnvInterpolation_Int(t *testing.T) {
	t.Setenv("DOLT_TEST_SQLSERVER_PORT", "15200")

	fs := filesys.EmptyInMemFS("/")
	err := fs.WriteFile("config.yaml", []byte(`
listener:
  port: ${DOLT_TEST_SQLSERVER_PORT}
`), 0o644)
	require.NoError(t, err)

	cfg, err := YamlConfigFromFile(fs, "config.yaml")
	require.NoError(t, err)
	require.Equal(t, 15200, cfg.Port())
}

func TestYamlConfigFromFileEnvInterpolation_Default(t *testing.T) {
	// Empty env vars are treated as missing for interpolation purposes.
	t.Setenv("DOLT_TEST_SQLSERVER_PORT", "")

	fs := filesys.EmptyInMemFS("/")
	err := fs.WriteFile("config.yaml", []byte(`
listener:
  port: ${DOLT_TEST_SQLSERVER_PORT:-15200}
`), 0o644)
	require.NoError(t, err)

	cfg, err := YamlConfigFromFile(fs, "config.yaml")
	require.NoError(t, err)
	require.Equal(t, 15200, cfg.Port())
}

func TestYamlConfigFromFileEnvInterpolation_MissingVarErrors(t *testing.T) {
	// Empty env vars are treated as missing for interpolation purposes.
	t.Setenv("DOLT_TEST_SQLSERVER_MISSING", "")

	fs := filesys.EmptyInMemFS("/")
	err := fs.WriteFile("config.yaml", []byte(`
listener:
  port: ${DOLT_TEST_SQLSERVER_MISSING}
`), 0o644)
	require.NoError(t, err)

	_, err = YamlConfigFromFile(fs, "config.yaml")

Review comment (suggestion):
Hmm. As a user I'd want this to barf. They set a variable that is garbage. I would think the default value (15200) would only be used when the env var wasn't set at all. Is there a common pattern for this behavior you're following? I asked chatgpt and it seems like every tool does this a little differently. I could see the argument either way, just saying what I'd want it to do.

Code after:
`), 0o644)
	require.NoError(t, err)

	cfg, err := YamlConfigFromFile(fs, "config.yaml")
	require.NoError(t, err)
	require.Equal(t, "127.0.0.1", cfg.Host())
}

func TestYamlConfigFromFileEnvInterpolation_Int(t *testing.T) {
	t.Setenv("DOLT_TEST_SQLSERVER_PORT", "15200")

	fs := filesys.EmptyInMemFS("/")
	err := fs.WriteFile("config.yaml", []byte(`
listener:
  port: ${DOLT_TEST_SQLSERVER_PORT}
`), 0o644)
	require.NoError(t, err)

	cfg, err := YamlConfigFromFile(fs, "config.yaml")
	require.NoError(t, err)
	require.Equal(t, 15200, cfg.Port())
}

func TestYamlConfigFromFileEnvInterpolation_MissingVarErrors(t *testing.T) {
	// Empty env vars result in an interpolation error.
	t.Setenv("DOLT_TEST_SQLSERVER_MISSING", "")

	fs := filesys.EmptyInMemFS("/")
	err := fs.WriteFile("config.yaml", []byte(`
listener:
  port: ${DOLT_TEST_SQLSERVER_MISSING}
`), 0o644)
	require.NoError(t, err)

	_, err = YamlConfigFromFile(fs, "config.yaml")
	require.Error(t, err)
	require.Contains(t, err.Error(), "DOLT_TEST_SQLSERVER_MISSING")
}

func TestYamlConfigFromFileEnvInterpolation_UnsetVarErrors(t *testing.T) {
	fs := filesys.EmptyInMemFS("/")
	err := fs.WriteFile("config.yaml", []byte(`
listener:
  port: ${DOLT_TEST_SQLSERVER_UNSET}
`), 0o644)
	require.NoError(t, err)

	_, err = YamlConfigFromFile(fs, "config.yaml")
	require.Error(t, err)
	require.Contains(t, err.Error(), "DOLT_TEST_SQLSERVER_UNSET")
}