Skip to content

Commit

Permalink
chore: rename MustGuessRootDir to RootDir
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Nov 20, 2023
1 parent cc67ff4 commit 3bd97b5
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newStartCmd(io commands.IO) *commands.Command {
}

func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
gnoroot := gnoenv.MustGuessGnoRootDir()
gnoroot := gnoenv.RootDir()
defaultGenesisBalancesFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_balances.txt")
defaultGenesisTxsFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_txs.txt")

Expand Down
4 changes: 2 additions & 2 deletions gno.land/cmd/gnoweb/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestRoutes(t *testing.T) {
{"/404-not-found", notFound, "/404-not-found"},
}

config, _ := integration.TestingNodeConfig(t, gnoenv.MustGuessGnoRootDir())
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config)
defer node.Stop()

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestAnalytics(t *testing.T) {
"/404-not-found",
}

config, _ := integration.TestingNodeConfig(t, gnoenv.MustGuessGnoRootDir())
config, _ := integration.TestingNodeConfig(t, gnoenv.RootDir())
node, remoteAddr := integration.TestingInMemoryNode(t, log.NewNopLogger(), config)
defer node.Stop()

Expand Down
3 changes: 1 addition & 2 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gnoland
import (
"fmt"
"path/filepath"
"strings"

"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
Expand Down Expand Up @@ -34,7 +33,7 @@ func NewAppOptions() *AppOptions {
return &AppOptions{
Logger: log.NewNopLogger(),
DB: dbm.NewMemDB(),
GnoRootDir: gnoenv.MustGuessGnoRootDir(),
GnoRootDir: gnoenv.RootDir(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/integration/testing_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func setupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {

// `gnoRootDir` should point to the local location of the gno repository.
// It serves as the gno equivalent of GOROOT.
gnoRootDir := gnoenv.MustGuessGnoRootDir()
gnoRootDir := gnoenv.RootDir()

// `gnoHomeDir` should be the local directory where gnokey stores keys.
gnoHomeDir := filepath.Join(tmpdir, "gno")
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *docCfg) RegisterFlags(fs *flag.FlagSet) {
func execDoc(cfg *docCfg, args []string, io commands.IO) error {
// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = gnoenv.MustGuessGnoRootDir()
cfg.rootDir = gnoenv.RootDir()
}

wd, err := os.Getwd()
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func execEnv(cfg *envCfg, args []string, io commands.IO) error {
envs := []envVar{
// GNOROOT Should point to the local location of the GNO repository.
// It serves as the gno equivalent of `GOROOT`.
{Key: "GNOROOT", Value: gnoenv.MustGuessGnoRootDir()},
{Key: "GNOROOT", Value: gnoenv.RootDir()},
// GNOHOME Should point to the user local configuration.
// The most common place for this should be $HOME/gno.
{Key: "GNOHOME", Value: gnoenv.HomeDir()},
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func execLint(cfg *lintCfg, args []string, io commands.IO) error {
rootDir = cfg.rootDir
)
if rootDir == "" {
rootDir = gnoenv.MustGuessGnoRootDir()
rootDir = gnoenv.RootDir()
}

pkgPaths, err := gnoPackagesFromArgs(args)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func execRepl(cfg *replCfg, args []string) error {
}

if cfg.rootDir == "" {
cfg.rootDir = gnoenv.MustGuessGnoRootDir()
cfg.rootDir = gnoenv.RootDir()

Check warning on line 85 in gnovm/cmd/gno/repl.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/repl.go#L85

Added line #L85 was not covered by tests
}

if !cfg.skipUsage {
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error {
}

if cfg.rootDir == "" {
cfg.rootDir = gnoenv.MustGuessGnoRootDir()
cfg.rootDir = gnoenv.RootDir()
}

stdin := io.In()
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func execTest(cfg *testCfg, args []string, io commands.IO) error {

// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = gnoenv.MustGuessGnoRootDir()
cfg.rootDir = gnoenv.RootDir()
}

paths, err := targetsFromPatterns(args)
Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func ResolvePath(output string, path importPath) (string, error) {
if err != nil {
return "", err
}
pkgPath := strings.TrimPrefix(absPkgPath, gnoenv.MustGuessGnoRootDir())
pkgPath := strings.TrimPrefix(absPkgPath, gnoenv.RootDir())

return filepath.Join(absOutput, pkgPath), nil
}
Expand Down
18 changes: 9 additions & 9 deletions gnovm/pkg/gnoenv/gnoroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var ErrUnableToGuessGnoRoot = errors.New("gno was unable to determine GNOROOT. P
// -ldflags="-X github.com/gnolang/gno/gnovm/pkg/gnoenv._GNOROOT"
var _GNOROOT string

// MustGuessGnoRootDir guesses the Gno root directory and panics if it fails.
func MustGuessGnoRootDir() string {
root, err := GuessGnoRootDir()
// RootDir guesses the Gno root directory and panics if it fails.
func RootDir() string {
root, err := GuessRootDir()
if err != nil {
panic(err)

Check warning on line 24 in gnovm/pkg/gnoenv/gnoroot.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnoenv/gnoroot.go#L24

Added line #L24 was not covered by tests
}
Expand All @@ -29,12 +29,12 @@ func MustGuessGnoRootDir() string {

var muGnoRoot sync.Mutex

// GuessGnoRootDir attempts to determine the Gno root directory using various strategies:
// GuessRootDir attempts to determine the Gno root directory using various strategies:
// 1. First, It tries to obtain it from the `GNOROOT` environment variable.
// 2. If the env variable isn't set, It checks if `_GNOROOT` has been previously determined or set with -ldflags.
// 3. If not, it uses the `go list` command to infer from go.mod.
// 4. As a last resort, it determines `GNOROOT` based on the caller stack's file path.
func GuessGnoRootDir() (string, error) {
func GuessRootDir() (string, error) {
muGnoRoot.Lock()
defer muGnoRoot.Unlock()

Expand All @@ -46,15 +46,15 @@ func GuessGnoRootDir() (string, error) {
var err error
if _GNOROOT == "" {
// Try to guess `GNOROOT` using various strategies
_GNOROOT, err = guessGnoRootDir()
_GNOROOT, err = guessRootDir()
}

return _GNOROOT, err
}

func guessGnoRootDir() (string, error) {
func guessRootDir() (string, error) {
// Attempt to guess `GNOROOT` from go.mod by using the `go list` command.
if rootdir, err := inferGnoRootFromGoMod(); err == nil {
if rootdir, err := inferRootFromGoMod(); err == nil {
return filepath.Clean(rootdir), nil
}

Check warning on line 59 in gnovm/pkg/gnoenv/gnoroot.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnoenv/gnoroot.go#L58-L59

Added lines #L58 - L59 were not covered by tests

Expand All @@ -76,7 +76,7 @@ func guessGnoRootDir() (string, error) {
return "", ErrUnableToGuessGnoRoot

Check warning on line 76 in gnovm/pkg/gnoenv/gnoroot.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnoenv/gnoroot.go#L76

Added line #L76 was not covered by tests
}

func inferGnoRootFromGoMod() (string, error) {
func inferRootFromGoMod() (string, error) {
gobin, err := exec.LookPath("go")
if err != nil {
return "", fmt.Errorf("unable to find `go` binary: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions gnovm/pkg/gnoenv/gnoroot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGuessGnoRootDir_WithSetGnoRoot(t *testing.T) {
const testPath = "/path/to/gnoRoot"

_GNOROOT = testPath
root, err := GuessGnoRootDir()
root, err := GuessRootDir()
require.NoError(t, err)
require.Equal(t, root, testPath)
}
Expand All @@ -35,7 +35,7 @@ func TestGuessGnoRootDir_UsingCallerStack(t *testing.T) {

// gno/ .. /gnovm/ .. /pkg/ .. /gnoenv/gnoroot.go
testPath, _ := filepath.Abs(filepath.Join(".", "..", "..", ".."))
root, err := GuessGnoRootDir()
root, err := GuessRootDir()
require.NoError(t, err)
require.Equal(t, root, testPath)
}
Expand All @@ -59,7 +59,7 @@ func TestInferGnoRootFromGoMod(t *testing.T) {
testPath, _ := filepath.Abs(filepath.Join(".", "..", "..", ".."))

t.Run("go is present", func(t *testing.T) {
root, err := inferGnoRootFromGoMod()
root, err := inferRootFromGoMod()
require.NoError(t, err)
require.Equal(t, root, testPath)
})
Expand All @@ -68,7 +68,7 @@ func TestInferGnoRootFromGoMod(t *testing.T) {
// Unset PATH should prevent `inferGnoRootFromGoMod` to works
t.Setenv("PATH", "")

root, err := inferGnoRootFromGoMod()
root, err := inferRootFromGoMod()
require.Error(t, err)
require.Empty(t, root)
})
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/integration/gno.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// to correctly set up the environment variables needed for the `gno` command.
func SetupGno(p *testscript.Params, buildDir string) error {
// Try to fetch `GNOROOT` from the environment variables
gnoroot := gnoenv.MustGuessGnoRootDir()
gnoroot := gnoenv.RootDir()

if !osm.DirExists(buildDir) {
return fmt.Errorf("%q does not exist or is not a directory", buildDir)
Expand Down

0 comments on commit 3bd97b5

Please sign in to comment.