github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/pkg/gnoenv/gnoroot_test.go (about) 1 package gnoenv 2 3 import ( 4 "os/exec" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestGuessGnoRootDir_WithSetGnoRoot(t *testing.T) { 12 originalGnoRoot := _GNOROOT 13 defer func() { _GNOROOT = originalGnoRoot }() // Restore after test 14 15 t.Setenv("GNOROOT", "") 16 17 const testPath = "/path/to/gnoRoot" 18 19 _GNOROOT = testPath 20 root, err := GuessRootDir() 21 require.NoError(t, err) 22 require.Equal(t, testPath, root) 23 } 24 25 func TestGuessGnoRootDir_UsingCallerStack(t *testing.T) { 26 originalGnoRoot := _GNOROOT 27 defer func() { _GNOROOT = originalGnoRoot }() 28 29 // Unset PATH should prevent InferGnoRootFromGoMod to works 30 t.Setenv("GNOROOT", "") 31 t.Setenv("PATH", "") 32 33 _, err := exec.LookPath("go") 34 require.Error(t, err) 35 36 // gno/ .. /gnovm/ .. /pkg/ .. /gnoenv/gnoroot.go 37 testPath, _ := filepath.Abs(filepath.Join(".", "..", "..", "..")) 38 root, err := GuessRootDir() 39 require.NoError(t, err) 40 require.Equal(t, testPath, root) 41 } 42 43 func TestGuessGnoRootDir_Error(t *testing.T) { 44 // XXX: Determine a method to test the GuessGnoRoot final error. 45 // One approach might be to use `txtar` to build a test binary with -trimpath, 46 // avoiding absolute paths in the call stack. 47 t.Skip("not implemented; refer to the inline comment for more details.") 48 } 49 50 func TestGuessGnoRootDir_WithGoModList(t *testing.T) { 51 // XXX: find a way to test `go mod list` phase. 52 // One solution is to use txtar with embed go.mod file. 53 // For now only `inferGnoRootFromGoMod` is tested. 54 t.Skip("not implemented; refer to the inline comment for more details.") 55 } 56 57 func TestInferGnoRootFromGoMod(t *testing.T) { 58 // gno/ .. /gnovm/ .. /pkg/ .. /gnoenv/gnoroot.go 59 testPath, _ := filepath.Abs(filepath.Join(".", "..", "..", "..")) 60 61 t.Run("go is present", func(t *testing.T) { 62 root, err := inferRootFromGoMod() 63 require.NoError(t, err) 64 require.Equal(t, testPath, root) 65 }) 66 67 t.Run("go is not present", func(t *testing.T) { 68 // Unset PATH should prevent `inferGnoRootFromGoMod` to works 69 t.Setenv("PATH", "") 70 71 root, err := inferRootFromGoMod() 72 require.Error(t, err) 73 require.Empty(t, root) 74 }) 75 }