Files
multica/server/internal/daemon/testmain_test.go
c8d6a1fad0 MUL-7923: fix(test): isolate test home on Windows so tests stop overwriting ~/.multica/config.json (#9128)
* fix(test): isolate test home on Windows across config-writing packages

Tests that only redirect HOME still resolve ~/.multica against the real
home on Windows: os.UserHomeDir reads USERPROFILE, not HOME. SaveCLIConfig
then writes test fixtures over a real default-profile config.json.

Reproduced on a Windows host by running `go test ./internal/cli ./cmd/multica`
on an unmodified tree: the real ~/.multica/config.json was replaced with the
test fixture {"token": "mul_owner_pat"}, dropping server_url, app_url and
workspace_id. The running daemon keeps its in-memory config, so the damage
only surfaces on the next restart as an auth failure.

Redirect both home variables process-wide in TestMain for the packages that
resolve ~/.multica (cmd/multica, internal/cli, internal/daemon), and route
per-test home redirects through a redirectTestHome helper that sets HOME and
USERPROFILE together. Per-test t.Setenv overrides still win.

Comparison on the same host, same packages:

  without this change: 6 internal/cli + 10 cmd/multica failures, real config overwritten
  with this change:    1 + 5 failures (all pre-existing Windows cases), config intact

Co-authored-by: multica-agent <[email protected]>

* fix(test): fail closed when isolating test homes

Co-authored-by: multica-agent <[email protected]>

* test(ghsnapshot): isolate cursor fixture from other packages

Co-authored-by: multica-agent <[email protected]>

---------

Co-authored-by: ZhangRui <[email protected]>
Co-authored-by: multica-agent <[email protected]>
Co-authored-by: Yushen <[email protected]>
2026-10-10 17:03:32 +08:00

49 lines
1.3 KiB
Go

// Copyright 2026 Multica. All rights reserved.
package daemon
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/multica-ai/multica/server/internal/testutil"
)
// TestMain redirects the home environment variables to one scratch directory
// for the whole binary before any test runs. Config paths resolve through
// os.UserHomeDir — USERPROFILE on Windows, HOME elsewhere — while Hermes uses
// LOCALAPPDATA on Windows. Process-wide redirection isolates tests that forget
// their own redirect; per-test t.Setenv overrides still take precedence.
func TestMain(m *testing.M) {
for _, key := range []string{
"MULTICA_AGENT_ID",
"MULTICA_TASK_ID",
"MULTICA_TOKEN",
"MULTICA_DAEMON_PORT",
"MULTICA_WORKSPACE_ID",
"MULTICA_SERVER_URL",
"MULTICA_TASK_CONFIG_ROOT",
} {
os.Unsetenv(key)
}
scratchHome, err := testutil.IsolateUserHome("multica-daemon-tests-home-")
if err != nil {
fmt.Fprintf(os.Stderr, "isolate internal/daemon test home: %v\n", err)
os.Exit(1)
}
if err := os.Setenv("LOCALAPPDATA", filepath.Join(scratchHome, "AppData", "Local")); err != nil {
fmt.Fprintf(os.Stderr, "isolate internal/daemon LOCALAPPDATA: %v\n", err)
_ = os.RemoveAll(scratchHome)
os.Exit(1)
}
code := m.Run()
_ = os.RemoveAll(scratchHome)
os.Exit(code)
}
var redirectTestHome = testutil.RedirectUserHome