Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd/gno/clean): allow to run gno clean -modcache from anywhere + rename and use gnomod.ModCachePath + tmp GNOHOME in main tests #3083

Merged
merged 10 commits into from
Nov 8, 2024
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 @@
"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 @@
&c.modCache,
"modcache",
false,
"remove the entire module download cache",
"remove the entire module download cache and exit",
)
}

Expand All @@ -64,6 +63,19 @@
return flag.ErrHelp
}

if cfg.modCache {
modCacheDir := gnomod.ModCachePath()
if !cfg.dryRun {
if err := os.RemoveAll(modCacheDir); err != nil {
return err
}

Check warning on line 71 in gnovm/cmd/gno/clean.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/clean.go#L70-L71

Added lines #L70 - L71 were not covered by tests
}
if cfg.dryRun || cfg.verbose {
io.Println("rm -rf", modCacheDir)
}
return nil
thehowl marked this conversation as resolved.
Show resolved Hide resolved
}

path, err := os.Getwd()
if err != nil {
return err
Expand All @@ -81,17 +93,6 @@
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 @@

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()

Check warning on line 35 in gnovm/pkg/gnomod/gnomod.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnomod/gnomod.go#L35

Added line #L35 was not covered by tests
}
return filepath.Join(root, v.Path)
}
Expand Down Expand Up @@ -89,7 +89,7 @@
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
Loading