如何配置VS Code在执行Go测试时自动使用Doppler注入密钥?
Here's how to make VS Code automatically inject Doppler secrets when running or debugging Go tests:
1. Set Up "Run Test" (Non-Debug)
Open your workspace's settings.json (File > Preferences > Settings > Workspace tab, then search for "Go Test Explorer Test Command") and set it to wrap go test with doppler run:
{ "go.testExplorer.testCommand": "doppler run -- go test ${pkg} ${args}" }
This replaces the default test command with one that fetches your Doppler secrets first. The ${pkg} and ${args} variables preserve the package and flags that VS Code would normally pass to go test.
2. Configure "Debug Test"
To debug tests with Doppler secrets, update your .vscode/launch.json to wrap the Delve debugger invocation with doppler run:
{ "version": "0.2.0", "configurations": [ { "name": "Debug Test (Doppler)", "type": "go", "request": "launch", "mode": "test", "program": "doppler", "args": [ "run", "--", "dlv", "test", "${package}", "--listen=127.0.0.1:${port}", "--headless=true", "--api-version=2", "--", "-test.run", "${testName}" ], "console": "integratedTerminal", "debugAdapter": "dlv-dap" } ] }
You can set this as your default debug configuration for tests by clicking the "Set as default" button next to the configuration name in the debug panel. Now when you click "Debug Test" in the code gutter, it will run the debugger through doppler run, ensuring secrets are available.
Optional: Target Specific Doppler Configs
If you need to use a specific Doppler config (like dev or staging), add the --config flag to the doppler run command:
doppler run --config dev -- go test ${pkg} ${args}
Verify the Setup
Run a test that accesses one of your Doppler-managed environment variables. It should resolve correctly without needing to manually prefix the command with doppler run.
内容的提问来源于stack exchange,提问作者Sammy Roberts

