| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestEnvironmentIdentitySurvivesProjectSelectionAndLegacyUpgrade(t *testing.T) { |
| 11 | profile := t.TempDir() |
| 12 | legacy, _ := filepath.EvalSymlinks(t.TempDir()) |
| 13 | prefs, _ := json.Marshal(map[string]string{"workspace": legacy}) |
| 14 | if err := os.WriteFile(filepath.Join(profile, "workbench.json"), prefs, 0600); err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | if EnvironmentDir(profile) == legacy { |
| 18 | t.Fatal("selected project without an existing VM became the environment") |
| 19 | } |
| 20 | vmDir := filepath.Join(profile, "state", "lima", InstanceName(profile, legacy)) |
| 21 | if err := os.MkdirAll(vmDir, 0700); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if err := os.WriteFile(filepath.Join(vmDir, "lima.yaml"), []byte("{}"), 0600); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if EnvironmentDir(profile) != legacy { |
| 28 | t.Fatal("existing VM disk was not retained during upgrade") |
| 29 | } |
| 30 | saved, _ := json.Marshal(map[string]string{"directory": legacy}) |
| 31 | if err := os.WriteFile(filepath.Join(profile, "environment.json"), saved, 0600); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if err := os.WriteFile(filepath.Join(profile, "workbench.json"), []byte(`{"workspace":"/different-project"}`), 0600); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if EnvironmentDir(profile) != legacy { |
| 38 | t.Fatal("changing project changed the environment VM") |
| 39 | } |
| 40 | } |