Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions internal/sandbox/tools/mise_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//go:build integration

package tools

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

// isolateMiseConfig points mise at empty config/data/state directories and unsets
// trust-related env vars so tests are not affected by host settings.
func isolateMiseConfig(t *testing.T) {
t.Helper()
t.Setenv("MISE_CONFIG_DIR", t.TempDir())
t.Setenv("MISE_DATA_DIR", t.TempDir())
t.Setenv("MISE_STATE_DIR", t.TempDir())
// Must fully unset (not set to empty) — mise checks var presence, not value.
unsetForTest(t, "MISE_TRUSTED_CONFIG_PATHS")
unsetForTest(t, "MISE_YES")
}

// unsetForTest removes an env var for the duration of the test, restoring it on cleanup.
func unsetForTest(t *testing.T, key string) {
t.Helper()
if orig, ok := os.LookupEnv(key); ok {
if err := os.Unsetenv(key); err != nil {
t.Fatalf("failed to unset %s: %v", key, err)
}
t.Cleanup(func() {
if err := os.Setenv(key, orig); err != nil {
t.Errorf("failed to restore %s: %v", key, err)
}
})
} else {
if err := os.Unsetenv(key); err != nil {
t.Fatalf("failed to unset %s: %v", key, err)
}
}
}

func TestCheckMiseTrust_UntrustedConfig(t *testing.T) {
if _, err := exec.LookPath("mise"); err != nil {
t.Skip("mise not installed")
}

isolateMiseConfig(t)

dir := t.TempDir()
configPath := filepath.Join(dir, ".mise.toml")
if err := os.WriteFile(configPath, []byte("[tools]\n"), 0o644); err != nil {
t.Fatal(err)
}

statuses, err := CheckMiseTrust(dir)
if err != nil {
t.Fatalf("CheckMiseTrust() error = %v", err)
}

foundUntrusted := false
for _, s := range statuses {
if !s.Trusted && strings.Contains(s.Path, dir) {
foundUntrusted = true
}
}
if !foundUntrusted {
t.Errorf("expected untrusted status for %s, got: %v", dir, statuses)
}
}

func TestTrustMiseConfig(t *testing.T) {
if _, err := exec.LookPath("mise"); err != nil {
t.Skip("mise not installed")
}

isolateMiseConfig(t)

dir := t.TempDir()
configPath := filepath.Join(dir, ".mise.toml")
if err := os.WriteFile(configPath, []byte("[tools]\n"), 0o644); err != nil {
t.Fatal(err)
}

if err := TrustMiseConfig(dir); err != nil {
t.Fatalf("TrustMiseConfig() error = %v", err)
}

statuses, err := CheckMiseTrust(dir)
if err != nil {
t.Fatalf("CheckMiseTrust() error = %v", err)
}

for _, s := range statuses {
if strings.Contains(s.Path, dir) && !s.Trusted {
t.Errorf("expected trusted status for %s after TrustMiseConfig()", s.Path)
}
}
}
76 changes: 0 additions & 76 deletions internal/sandbox/tools/mise_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package tools

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
Expand Down Expand Up @@ -73,16 +71,6 @@ func TestMise_DockerBindings_NoCacheDirs(t *testing.T) {
}
}

// isolateMiseConfig points mise at empty config/data directories so tests
// are not affected by host or CI mise settings (e.g. trusted_config_paths).
func isolateMiseConfig(t *testing.T) {
t.Helper()
t.Setenv("MISE_CONFIG_DIR", t.TempDir())
t.Setenv("MISE_DATA_DIR", t.TempDir())
t.Setenv("MISE_TRUSTED_CONFIG_PATHS", "")
t.Setenv("MISE_YES", "")
}

func TestCheckMiseTrust_NoMise(t *testing.T) {
// If mise is not installed, CheckMiseTrust should return nil
if _, err := exec.LookPath("mise"); err != nil {
Expand Down Expand Up @@ -113,67 +101,3 @@ func TestCheckMiseTrust_NoConfig(t *testing.T) {
}
}
}

func TestCheckMiseTrust_UntrustedConfig(t *testing.T) {
if _, err := exec.LookPath("mise"); err != nil {
t.Skip("mise not installed")
}

// Isolate mise from host config to get consistent trust behavior in CI.
// jdx/mise-action may configure trusted_config_paths in mise's settings file.
isolateMiseConfig(t)

dir := t.TempDir()
configPath := filepath.Join(dir, ".mise.toml")
if err := os.WriteFile(configPath, []byte("[tools]\n"), 0o644); err != nil {
t.Fatal(err)
}

statuses, err := CheckMiseTrust(dir)
if err != nil {
t.Fatalf("CheckMiseTrust() error = %v", err)
}

// Should have at least one untrusted entry for the temp dir
foundUntrusted := false
for _, s := range statuses {
if !s.Trusted && strings.Contains(s.Path, dir) {
foundUntrusted = true
}
}
if !foundUntrusted {
t.Errorf("expected untrusted status for %s, got: %v", dir, statuses)
}
}

func TestTrustMiseConfig(t *testing.T) {
if _, err := exec.LookPath("mise"); err != nil {
t.Skip("mise not installed")
}

// Isolate mise from host config to get consistent trust behavior in CI.
isolateMiseConfig(t)

dir := t.TempDir()
configPath := filepath.Join(dir, ".mise.toml")
if err := os.WriteFile(configPath, []byte("[tools]\n"), 0o644); err != nil {
t.Fatal(err)
}

// Trust the config
if err := TrustMiseConfig(dir); err != nil {
t.Fatalf("TrustMiseConfig() error = %v", err)
}

// Verify it's now trusted
statuses, err := CheckMiseTrust(dir)
if err != nil {
t.Fatalf("CheckMiseTrust() error = %v", err)
}

for _, s := range statuses {
if strings.Contains(s.Path, dir) && !s.Trusted {
t.Errorf("expected trusted status for %s after TrustMiseConfig()", s.Path)
}
}
}