Skip to content

Commit 2d6ab4f

Browse files
fix: prevent TestVSCode from writing to real settings.json
TestVSCode runs the full command via TestCmd which calls createConnectionProfile -> getVSCodeSettingsPath, resolving to the real %APPDATA%\Code\User\settings.json. This silently mutates the developer's actual VS Code settings on every test run. Add testSettingsPathOverride hook so TestVSCode redirects writes to t.TempDir(). The hook is cleared in t.Cleanup so other tests like TestVSCodeGetSettingsPath still exercise the real path resolution.
1 parent 2f099de commit 2d6ab4f

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

cmd/modern/root/open/vscode.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ import (
2020
"github.com/microsoft/go-sqlcmd/internal/tools/tool"
2121
)
2222

23+
// testSettingsPathOverride, when non-empty, overrides getVSCodeSettingsPath
24+
// so tests never touch the real VS Code settings.json.
25+
var testSettingsPathOverride string
26+
2327
// VSCode implements the `sqlcmd open vscode` command. It opens
2428
// Visual Studio Code and configures a connection profile for the
2529
// current context using the MSSQL extension.
@@ -281,6 +285,10 @@ func (c *VSCode) updateOrAddProfile(connections []interface{}, newProfile map[st
281285
}
282286

283287
func (c *VSCode) getVSCodeSettingsPath() string {
288+
if testSettingsPathOverride != "" {
289+
return testSettingsPathOverride
290+
}
291+
284292
var stableDir string
285293
var insidersDir string
286294

cmd/modern/root/open/vscode_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func TestVSCode(t *testing.T) {
2828
t.Skip("VS Code is not installed")
2929
}
3030

31+
// Redirect settings writes to a temp directory so the test never
32+
// touches the real VS Code settings.json.
33+
testSettingsPathOverride = filepath.Join(t.TempDir(), "settings.json")
34+
t.Cleanup(func() { testSettingsPathOverride = "" })
35+
3136
cmdparser.TestSetup(t)
3237
config.AddEndpoint(sqlconfig.Endpoint{
3338
AssetDetails: nil,

0 commit comments

Comments
 (0)