Skip to content

Commit

Permalink
fix(cmd/gno/clean): allow to run gno clean -modcache from anywhere …
Browse files Browse the repository at this point in the history
…+ rename and use `gnomod.ModCachePath` + tmp `GNOHOME` in main tests (#3083)

- In `gno clean`, run the `-modcache` case before checking for presence
of a `gno.mod` to allow to run `gno clean -modcache` from anywhere (like
go)
- Refactor `gno clean -modcache` to use the `gnomod.GetGnoModPath`
helper to get the modcache path
- Rename `gnomod.GetGnoModPath` -> `gnomod.ModCachePath`
- Improve `gno` cmd tests by using a tmp `GNOHOME` instead of the system
one

<details><summary>Contributors' checklist...</summary>

- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
</details>

---------

Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz authored Nov 8, 2024
1 parent da79c84 commit 4f27a57
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
27 changes: 14 additions & 13 deletions gnovm/cmd/gno/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"strings"

"github.com/gnolang/gno/gnovm/pkg/gnoenv"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/tm2/pkg/commands"
)
Expand Down Expand Up @@ -55,7 +54,7 @@ func (c *cleanCfg) RegisterFlags(fs *flag.FlagSet) {
&c.modCache,
"modcache",
false,
"remove the entire module download cache",
"remove the entire module download cache and exit",
)
}

Expand All @@ -64,6 +63,19 @@ func execClean(cfg *cleanCfg, args []string, io commands.IO) error {
return flag.ErrHelp
}

if cfg.modCache {
modCacheDir := gnomod.ModCachePath()
if !cfg.dryRun {
if err := os.RemoveAll(modCacheDir); err != nil {
return err
}
}
if cfg.dryRun || cfg.verbose {
io.Println("rm -rf", modCacheDir)
}
return nil
}

path, err := os.Getwd()
if err != nil {
return err
Expand All @@ -81,17 +93,6 @@ func execClean(cfg *cleanCfg, args []string, io commands.IO) error {
return err
}

if cfg.modCache {
modCacheDir := filepath.Join(gnoenv.HomeDir(), "pkg", "mod")
if !cfg.dryRun {
if err := os.RemoveAll(modCacheDir); err != nil {
return err
}
}
if cfg.dryRun || cfg.verbose {
io.Println("rm -rf", modCacheDir)
}
}
return nil
}

Expand Down
11 changes: 11 additions & 0 deletions gnovm/cmd/gno/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ func TestCleanApp(t *testing.T) {
testDir: "../../tests/integ/minimalist_gnomod",
simulateExternalRepo: true,
},
{
args: []string{"clean", "-modcache"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
},
{
args: []string{"clean", "-modcache", "-n"},
testDir: "../../tests/integ/empty_dir",
simulateExternalRepo: true,
stdoutShouldContain: "rm -rf ",
},
}
testMainCaseRun(t, tc)

Expand Down
6 changes: 3 additions & 3 deletions gnovm/cmd/gno/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func TestEnvApp(t *testing.T) {
{args: []string{"env", "foo"}, stdoutShouldBe: "\n"},
{args: []string{"env", "foo", "bar"}, stdoutShouldBe: "\n\n"},
{args: []string{"env", "GNOROOT"}, stdoutShouldBe: testGnoRootEnv + "\n"},
{args: []string{"env", "GNOHOME", "storm"}, stdoutShouldBe: testGnoHomeEnv + "\n\n"},
{args: []string{"env", "GNOHOME", "storm"}, stdoutShouldBe: testGnoHomeEnv + "\n\n", noTmpGnohome: true},
{args: []string{"env"}, stdoutShouldContain: fmt.Sprintf("GNOROOT=%q", testGnoRootEnv)},
{args: []string{"env"}, stdoutShouldContain: fmt.Sprintf("GNOHOME=%q", testGnoHomeEnv)},
{args: []string{"env"}, stdoutShouldContain: fmt.Sprintf("GNOHOME=%q", testGnoHomeEnv), noTmpGnohome: true},

// json
{args: []string{"env", "-json"}, stdoutShouldContain: fmt.Sprintf("\"GNOROOT\": %q", testGnoRootEnv)},
{args: []string{"env", "-json"}, stdoutShouldContain: fmt.Sprintf("\"GNOHOME\": %q", testGnoHomeEnv)},
{args: []string{"env", "-json"}, stdoutShouldContain: fmt.Sprintf("\"GNOHOME\": %q", testGnoHomeEnv), noTmpGnohome: true},
{
args: []string{"env", "-json", "GNOROOT"},
stdoutShouldBe: fmt.Sprintf("{\n\t\"GNOROOT\": %q\n}\n", testGnoRootEnv),
Expand Down
8 changes: 8 additions & 0 deletions gnovm/cmd/gno/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type testMainCase struct {
args []string
testDir string
simulateExternalRepo bool
noTmpGnohome bool

// for the following FooContain+FooBe expected couples, if both are empty,
// then the test suite will require that the "got" is not empty.
Expand Down Expand Up @@ -58,6 +59,13 @@ func testMainCaseRun(t *testing.T, tc []testMainCase) {
mockOut := bytes.NewBufferString("")
mockErr := bytes.NewBufferString("")

if !test.noTmpGnohome {
tmpGnoHome, err := os.MkdirTemp(os.TempDir(), "gnotesthome_")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(tmpGnoHome) })
t.Setenv("GNOHOME", tmpGnoHome)
}

checkOutputs := func(t *testing.T) {
t.Helper()

Expand Down
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func execModDownload(cfg *modDownloadCfg, args []string, io commands.IO) error {
}

// fetch dependencies
if err := gnoMod.FetchDeps(gnomod.GetGnoModPath(), cfg.remote, cfg.verbose); err != nil {
if err := gnoMod.FetchDeps(gnomod.ModCachePath(), cfg.remote, cfg.verbose); err != nil {
return fmt.Errorf("fetch: %w", err)
}

Expand Down
10 changes: 5 additions & 5 deletions gnovm/pkg/gnomod/gnomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ import (

const queryPathFile = "vm/qfile"

// GetGnoModPath returns the path for gno modules
func GetGnoModPath() string {
// ModCachePath returns the path for gno modules
func ModCachePath() string {
return filepath.Join(gnoenv.HomeDir(), "pkg", "mod")
}

// PackageDir resolves a given module.Version to the path on the filesystem.
// If root is dir, it is defaulted to the value of [GetGnoModPath].
// If root is dir, it is defaulted to the value of [ModCachePath].
func PackageDir(root string, v module.Version) string {
// This is also used internally exactly like filepath.Join; but we'll keep
// the calls centralized to make sure we can change the path centrally should
// we start including the module version in the path.

if root == "" {
root = GetGnoModPath()
root = ModCachePath()
}
return filepath.Join(root, v.Path)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func writePackage(remote, basePath, pkgPath string) (requirements []string, err
func GnoToGoMod(f File) (*File, error) {
// TODO(morgan): good candidate to move to pkg/transpiler.

gnoModPath := GetGnoModPath()
gnoModPath := ModCachePath()

if !gnolang.IsStdlib(f.Module.Mod.Path) {
f.AddModuleStmt(transpiler.TranspileImportPath(f.Module.Mod.Path))
Expand Down

0 comments on commit 4f27a57

Please sign in to comment.