-
Notifications
You must be signed in to change notification settings - Fork 0
Fix getter mode baking env vars into generated code #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,53 @@ max_conns = 10 | |
| require.NotContains(t, outputStr, `":8080"`, "original server.addr should have been overridden") | ||
| } | ||
|
|
||
| func TestGenerateFromFile_GetterModeIgnoresEnvOverrides(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| inputFile := filepath.Join(tmpDir, "config.toml") | ||
| outputFile := filepath.Join(tmpDir, "config.go") | ||
|
|
||
| tomlData := []byte(` | ||
| [server] | ||
| addr = ":8080" | ||
|
|
||
| [secrets] | ||
| api_key = "set-from-env" | ||
| `) | ||
|
|
||
| err := os.WriteFile(inputFile, tomlData, 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Set env var that would override the TOML value at generation time | ||
| os.Setenv("CONFIG_SECRETS_API_KEY", "sk-real-secret-key-12345") | ||
| defer os.Unsetenv("CONFIG_SECRETS_API_KEY") | ||
|
Comment on lines
+92
to
+93
|
||
|
|
||
| opts := &GenerateOptions{ | ||
| InputFile: inputFile, | ||
| OutputFile: outputFile, | ||
| PackageName: "config", | ||
| EnableEnv: true, | ||
| Mode: "getter", | ||
| } | ||
|
|
||
| err = GenerateFromFile(opts) | ||
| require.NoError(t, err, "GenerateFromFile() should not error") | ||
|
|
||
| output, err := os.ReadFile(outputFile) | ||
| require.NoError(t, err) | ||
|
|
||
| outputStr := string(output) | ||
|
|
||
| // The generated code should use the TOML default, NOT the env var value | ||
| require.Contains(t, outputStr, `"set-from-env"`, | ||
| "getter mode should use TOML default, not env var value") | ||
| require.NotContains(t, outputStr, "sk-real-secret-key-12345", | ||
| "getter mode must not bake env var values into generated code") | ||
|
|
||
| // It should still have the os.Getenv call for runtime override | ||
| require.Contains(t, outputStr, `os.Getenv("CONFIG_SECRETS_API_KEY")`, | ||
| "getter mode should still generate runtime env var lookups") | ||
|
Comment on lines
+103
to
+119
|
||
| } | ||
|
|
||
| func TestGenerateFromFile(t *testing.T) { | ||
| // Create a temporary TOML file | ||
| tmpDir := t.TempDir() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GenerateFromFile’s behavior (including whether env overrides are applied) depends on the exact
opts.Modestring matching "getter". Consider normalizing/validatingopts.Modehere (e.g., trim + strings.ToLower and reject unknown values) so typos/casing differences don’t accidentally fall back to static-mode behavior and reintroduce secret-baking during generation.