]> Cypherpunks repositories - gostls13.git/commitdiff
clean: clean mod cache should respect "-n" option.
authorTim Xu <xiaoxubeii@gmail.com>
Wed, 19 Sep 2018 03:39:46 +0000 (03:39 +0000)
committerBryan C. Mills <bcmills@google.com>
Wed, 19 Sep 2018 13:39:09 +0000 (13:39 +0000)
Clean mod cache should print remove commands and not run them when with set "-n" option.
Fixes #27458.

Change-Id: I97242cb40c062b347784cdb61653c84a3a7eab44
GitHub-Last-Rev: 5a6f10cad8c5f2c3916a74ca5eea27b1fdd1dc38
GitHub-Pull-Request: golang/go#27710
Reviewed-on: https://go-review.googlesource.com/135695
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/clean/clean.go
src/cmd/go/testdata/script/mod_clean_cache.txt [new file with mode: 0644]

index d023592eedceac42b605ce11c5fe3304866e65b1..b12bd981a70a038e62299549580147e731dbdcd5 100644 (file)
@@ -112,9 +112,10 @@ func runClean(cmd *base.Command, args []string) {
                }
        }
 
+       var b work.Builder
+       b.Print = fmt.Print
+
        if cleanCache {
-               var b work.Builder
-               b.Print = fmt.Print
                dir := cache.DefaultDir()
                if dir != "off" {
                        // Remove the cache subdirectories but not the top cache directory.
@@ -156,8 +157,13 @@ func runClean(cmd *base.Command, args []string) {
                if modfetch.PkgMod == "" {
                        base.Fatalf("go clean -modcache: no module cache")
                }
-               if err := removeAll(modfetch.PkgMod); err != nil {
-                       base.Errorf("go clean -modcache: %v", err)
+               if cfg.BuildN || cfg.BuildX {
+                       b.Showcmd("", "rm -rf %s", modfetch.PkgMod)
+               }
+               if !cfg.BuildN {
+                       if err := removeAll(modfetch.PkgMod); err != nil {
+                               base.Errorf("go clean -modcache: %v", err)
+                       }
                }
        }
 }
diff --git a/src/cmd/go/testdata/script/mod_clean_cache.txt b/src/cmd/go/testdata/script/mod_clean_cache.txt
new file mode 100644 (file)
index 0000000..66a0e9e
--- /dev/null
@@ -0,0 +1,23 @@
+env GO111MODULE=on
+
+go mod download rsc.io/quote@v1.5.0
+exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.info
+exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.mod
+exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.zip
+
+go clean -modcache -n
+stdout '^rm -rf .*pkg.mod$'
+exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.info
+exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.mod
+exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.zip
+
+go clean -modcache
+! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.info
+! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.mod
+! exists $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.0.zip
+
+-- go.mod --
+module m
+
+-- m.go --
+package m
\ No newline at end of file