github.com/rgonomic/rgo@v0.2.2-0.20220708095818-4747f0905d6e/internal/mod/module_test.go (about) 1 // Copyright ©2020 The rgonomic Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mod 6 7 import ( 8 "io/ioutil" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "testing" 13 ) 14 15 func TestModule(t *testing.T) { 16 oldwd, err := os.Getwd() 17 if err != nil { 18 t.Fatalf("failed to get cwd: %v", err) 19 } 20 defer func() { 21 err := os.Chdir(oldwd) 22 if err != nil { 23 t.Errorf("failed to restore working directory: %v", err) 24 } 25 }() 26 27 mod, err := ioutil.TempDir("", "rgotest") 28 if err != nil { 29 t.Fatalf("unexpected error creating mod test directory: %v", err) 30 } 31 defer os.RemoveAll(mod) 32 cmd := exec.Command("go", "mod", "init", "rgotest") 33 cmd.Dir = mod 34 err = cmd.Run() 35 if err != nil { 36 t.Fatalf("unexpected error creating mod file: %v", err) 37 } 38 err = ioutil.WriteFile(filepath.Join(mod, "src.go"), []byte("package rgotest"), 0o664) 39 if err != nil { 40 t.Fatalf("unexpected error creating mod-case source file: %v", err) 41 } 42 43 nomod, err := ioutil.TempDir("", "rgotest") 44 if err != nil { 45 t.Fatalf("unexpected error creating non-mod test directory: %v", err) 46 } 47 defer os.RemoveAll(nomod) 48 err = ioutil.WriteFile(filepath.Join(nomod, "src.go"), []byte("package rgotest"), 0o664) 49 if err != nil { 50 t.Fatalf("unexpected error creating non-mod-case source file: %v", err) 51 } 52 53 oldmode := os.Getenv("GO111MODULE") 54 defer os.Setenv("GO111MODULE", oldmode) 55 for _, path := range []string{mod, nomod} { 56 for _, mode := range []string{"on", "off"} { 57 os.Setenv("GO111MODULE", mode) 58 err := os.Chdir(path) 59 if err != nil { 60 t.Errorf("failed to change directory: %v", err) 61 continue 62 } 63 got, err := Module("rgotest") 64 if path == mod && mode == "on" { 65 want := Info{ 66 Path: "rgotest", 67 Dir: path, 68 GoMod: filepath.Join(path, "go.mod"), 69 } 70 if got != want { 71 t.Errorf("unexpected module info: got:%+v want:%+v", got, want) 72 } 73 } else { 74 var empty Info 75 if got != empty { 76 t.Errorf("expected empty module info for go.mod!=%t/GO111MODULE=%s", path != mod, mode) 77 } 78 if mode == "on" { 79 // No mod file and GO111MODULE=on outside 80 // $GOPATH results in an error: 81 // go: cannot find main module; see 'go help modules' 82 continue 83 } 84 85 } 86 if err != nil && mode == "on" { 87 t.Errorf("unexpected error getting module information: %v", err) 88 } 89 } 90 } 91 } 92 93 func TestRoot(t *testing.T) { 94 mod, err := ioutil.TempDir("", "rgotest") 95 if err != nil { 96 t.Fatalf("unexpected error creating mod test directory: %v", err) 97 } 98 defer os.RemoveAll(mod) 99 err = os.Mkdir(filepath.Join(mod, "a"), 0o775) 100 if err != nil { 101 t.Fatalf("unexpected error creating mod test sub-directory: %v", err) 102 } 103 cmd := exec.Command("go", "mod", "init", "rgotest") 104 cmd.Dir = mod 105 err = cmd.Run() 106 if err != nil { 107 t.Fatalf("unexpected error creating mod file: %v", err) 108 } 109 110 nomod, err := ioutil.TempDir("", "rgotest") 111 if err != nil { 112 t.Fatalf("unexpected error creating non-mod test directory: %v", err) 113 } 114 defer os.RemoveAll(nomod) 115 err = os.Mkdir(filepath.Join(nomod, "a"), 0o775) 116 if err != nil { 117 t.Fatalf("unexpected error creating non-mod test sub-directory: %v", err) 118 } 119 120 oldmode := os.Getenv("GO111MODULE") 121 defer os.Setenv("GO111MODULE", oldmode) 122 for _, path := range []string{mod, nomod} { 123 for _, subdir := range []string{"", "a"} { 124 for _, mode := range []string{"on", "off"} { 125 os.Setenv("GO111MODULE", mode) 126 got, ok, err := Root(filepath.Join(path, subdir)) 127 switch { 128 case mode == "on" && path == mod: 129 want := path 130 if got != want { 131 t.Errorf("unexpected root for go.mod=true/GO111MODULE=on in %s: got:%q want:%q", 132 filepath.Join(path, subdir), got, want) 133 } 134 if !ok { 135 t.Error("expected ok==true for go.mod=true/GO111MODULE=on") 136 } 137 if err != nil { 138 t.Errorf("unexpected error for go.mod=true/GO111MODULE=on: %v", err) 139 } 140 case mode == "on" && path == nomod: 141 want := filepath.Join(path, subdir) 142 if got != want { 143 t.Errorf("unexpected root for go.mod=false/GO111MODULE=on in %s: got:%q want:%q", 144 filepath.Join(path, subdir), got, want) 145 } 146 if ok { 147 t.Error("expected ok==false for go.mod=false/GO111MODULE=on") 148 } 149 if err != nil { 150 t.Errorf("unexpected error for go.mod=false/GO111MODULE=on: %v", err) 151 } 152 case mode == "off": 153 want := "" 154 if got != want { 155 t.Errorf("unexpected root for go.mod=%t/GO111MODULE=off in %s: got:%q want:%q", 156 path == mod, filepath.Join(path, subdir), got, want) 157 } 158 if ok { 159 t.Errorf("expected ok==false for go.mod=%t/GO111MODULE=off", path == mod) 160 } 161 if err == nil { 162 t.Errorf("expected error for go.mod=%t/GO111MODULE=off: %v", path == mod, err) 163 } 164 } 165 } 166 } 167 } 168 }