github.com/goplus/gop@v1.2.6/cmd/make_test.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package make_test 5 6 import ( 7 "bytes" 8 "errors" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "runtime" 13 "strings" 14 "testing" 15 ) 16 17 const ( 18 inWindows = (runtime.GOOS == "windows") 19 ) 20 21 var script = "all.bash" 22 var gopRoot = "" 23 var gopBinFiles = []string{"gop", "gopfmt"} 24 var installer = "cmd/make.go" 25 var versionFile = "VERSION" 26 var mainVersionFile = "env/version.go" 27 28 func checkPathExist(path string, isDir bool) bool { 29 stat, err := os.Stat(path) 30 isExists := !os.IsNotExist(err) 31 if isDir { 32 return isExists && stat.IsDir() 33 } 34 return isExists && !stat.IsDir() 35 } 36 37 func trimRight(s string) string { 38 return strings.TrimRight(s, " \t\r\n") 39 } 40 41 func execCommand(command string, arg ...string) (string, error) { 42 var stdout, stderr bytes.Buffer 43 cmd := exec.Command(command, arg...) 44 cmd.Stdout = &stdout 45 cmd.Stderr = &stderr 46 err := cmd.Run() 47 if err != nil { 48 if stderr.Len() > 0 { 49 err = errors.New(string(stderr.String())) 50 } 51 } 52 return stdout.String(), err 53 } 54 55 func getBranch() string { 56 stdout, err := execCommand("git", "rev-parse", "--abbrev-ref", "HEAD") 57 if err != nil { 58 return "" 59 } 60 return trimRight(stdout) 61 } 62 63 func detectGoBinPath() string { 64 goBin, ok := os.LookupEnv("GOBIN") 65 if ok { 66 return goBin 67 } 68 goPath, ok := os.LookupEnv("GOPATH") 69 if ok { 70 list := filepath.SplitList(goPath) 71 if len(list) > 0 { 72 // Put in first directory of $GOPATH. 73 return filepath.Join(list[0], "bin") 74 } 75 } 76 homeDir, _ := os.UserHomeDir() 77 return filepath.Join(homeDir, "go", "bin") 78 } 79 80 func init() { 81 pwd, _ := os.Getwd() 82 gopRoot = filepath.Join(pwd, "..") 83 installer = filepath.Join(gopRoot, installer) 84 versionFile = filepath.Join(gopRoot, versionFile) 85 mainVersionFile = filepath.Join(gopRoot, mainVersionFile) 86 87 if inWindows { 88 script = "all.bat" 89 for index, file := range gopBinFiles { 90 file += ".exe" 91 gopBinFiles[index] = file 92 } 93 } 94 } 95 96 func cleanGopRunCacheFiles(t *testing.T) { 97 homeDir, _ := os.UserHomeDir() 98 runCacheDir := filepath.Join(homeDir, ".gop", "run") 99 files := []string{"go.mod", "go.sum"} 100 for _, file := range files { 101 fullPath := filepath.Join(runCacheDir, file) 102 if checkPathExist(fullPath, false) { 103 t.Fatalf("Failed: %s found in %s directory\n", file, runCacheDir) 104 } 105 } 106 } 107 108 func TestAllScript(t *testing.T) { 109 os.Chdir(gopRoot) 110 cmd := exec.Command(filepath.Join(gopRoot, script)) 111 if output, err := cmd.CombinedOutput(); err != nil { 112 t.Fatalf("Failed: %v:\nOut: %s\n", err, output) 113 } 114 115 goBinPath := detectGoBinPath() 116 117 for _, file := range gopBinFiles { 118 if !checkPathExist(filepath.Join(gopRoot, "bin", file), false) { 119 t.Fatalf("Failed: %s not found in ./bin directory\n", file) 120 } 121 if !checkPathExist(filepath.Join(goBinPath, file), false) { 122 t.Fatalf("Failed: %s not found in %s/bin directory\n", file, goBinPath) 123 } 124 } 125 126 cleanGopRunCacheFiles(t) 127 128 cmd = exec.Command(filepath.Join(gopRoot, "bin", gopBinFiles[0]), "version") 129 if output, err := cmd.CombinedOutput(); err != nil { 130 t.Fatalf("Failed: %v:\nOut: %s\n", err, output) 131 } 132 } 133 134 func TestTagFlagInGitRepo(t *testing.T) { 135 os.Chdir(gopRoot) 136 137 // Setup 138 tag := "v1.0.90" 139 tag2 := "v1.0.91" 140 bigtag := "v1.999.12" 141 releaseBranch := "v1.0" 142 nonExistBranch := "non-exist-branch" 143 sourceBranch := getBranch() 144 145 // Teardown 146 t.Cleanup(func() { 147 gitCmd := exec.Command("git", "tag", "-d", tag) 148 gitCmd.CombinedOutput() 149 gitCmd = exec.Command("git", "tag", "-d", tag2) 150 gitCmd.CombinedOutput() 151 execCommand("git", "checkout", sourceBranch) 152 execCommand("git", "branch", "-D", nonExistBranch) 153 execCommand("git", "branch", "-D", releaseBranch) 154 }) 155 156 t.Run("release new version with bad tag", func(t *testing.T) { 157 cmd := exec.Command("go", "run", installer, "--nopush", "--tag", "xyz") 158 if _, err := cmd.CombinedOutput(); err == nil { 159 t.Fatal("Failed: release a bad tag should be failed.") 160 } 161 }) 162 163 t.Run("empty tag should failed", func(t *testing.T) { 164 cmd := exec.Command("go", "run", installer, "--nopush", "--tag", "") 165 if out, err := cmd.CombinedOutput(); err != nil || !strings.Contains(string(out), "Usage") { 166 t.Fatalf("Failed: %v, out: %s\n", err, out) 167 } 168 }) 169 170 t.Run("failed when release branch corresponding to tag does not exists", func(t *testing.T) { 171 cmd := exec.Command("go", "run", installer, "--nopush", "--tag", bigtag) 172 if _, err := cmd.CombinedOutput(); err == nil { 173 t.Fatal("Failed: a corresponding release branch does not exists should be failed.") 174 } 175 }) 176 177 t.Run("release new version on release branch", func(t *testing.T) { 178 execCommand("git", "checkout", sourceBranch) 179 execCommand("git", "branch", "-D", releaseBranch) 180 _, err := execCommand("git", "checkout", "-b", releaseBranch) 181 if err != nil { 182 t.Fatal(err) 183 } 184 185 cmd := exec.Command("go", "run", installer, "--nopush", "--tag", tag) 186 if out, err := cmd.CombinedOutput(); err != nil { 187 t.Log(string(out)) 188 t.Fatalf("Failed: release tag: %s on branch: %s should not be failed", tag, releaseBranch) 189 } 190 191 if !checkPathExist(versionFile, false) { 192 t.Fatal("Failed: a VERSION file not found.") 193 } 194 195 if data, _ := os.ReadFile(versionFile); string(data) != tag { 196 t.Fatalf("Failed: content of VERSION file: '%s' not match tag: %s", data, tag) 197 } 198 199 // Make sure tag exists. 200 gitCmd := exec.Command("git", "tag") 201 if allTags, _ := gitCmd.CombinedOutput(); !strings.Contains(string(allTags), tag) { 202 t.Fatalf("Failed: %s tag not found in tag list of this git repo.\n", tag) 203 } 204 }) 205 206 t.Run("release new version on non-release branch", func(t *testing.T) { 207 _, err := execCommand("git", "checkout", "-b", nonExistBranch) 208 if err != nil { 209 t.Fatal(err) 210 } 211 212 execCommand("git", "branch", "-D", releaseBranch) 213 _, err = execCommand("git", "checkout", "-b", releaseBranch) 214 if err != nil { 215 t.Log("current branch:", getBranch()) 216 t.Fatal(err) 217 } 218 219 execCommand("git", "checkout", nonExistBranch) 220 221 cmd := exec.Command("go", "run", installer, "--nopush", "--tag", tag2) 222 if out, err := cmd.CombinedOutput(); err != nil { 223 t.Log(string(out)) 224 t.Fatalf("Failed: release tag: %s on branch: %s should not be failed", tag2, nonExistBranch) 225 } 226 227 if getBranch() != nonExistBranch { 228 t.Fatal("Failed: getBranch() != nonExistBranch") 229 } 230 execCommand("git", "checkout", releaseBranch) 231 232 if !checkPathExist(versionFile, false) { 233 t.Fatal("Failed: a VERSION file not found.") 234 } 235 236 if data, _ := os.ReadFile(versionFile); string(data) != tag2 { 237 t.Fatalf("Failed: content of VERSION file: '%s' not match tag: %s", data, tag2) 238 } 239 240 gitCmd := exec.Command("git", "tag") 241 if allTags, _ := gitCmd.CombinedOutput(); !strings.Contains(string(allTags), tag2) { 242 t.Fatalf("Failed: %s tag not found in tag list of this git repo.\n", tag) 243 } 244 }) 245 } 246 247 func TestTagFlagInNonGitRepo(t *testing.T) { 248 os.Chdir(gopRoot) 249 250 // Setup 251 gitDir := filepath.Join(gopRoot, ".git") 252 gitBackupDir := filepath.Join(gopRoot, ".gitBackup") 253 // Rename .git dir 254 if checkPathExist(gitDir, true) { 255 os.Rename(gitDir, gitBackupDir) 256 } 257 258 // Teardown 259 t.Cleanup(func() { 260 if checkPathExist(gitBackupDir, true) { 261 os.Rename(gitBackupDir, gitDir) 262 } 263 }) 264 265 t.Run("specify new tag", func(t *testing.T) { 266 cmd := exec.Command("go", "run", installer, "--tag", "v1.0.98") 267 output, err := cmd.CombinedOutput() 268 if err == nil || !strings.Contains(string(output), "Error") { 269 t.Fatal("Failed: release on a non-git repo should be failed.") 270 } 271 }) 272 } 273 274 func TestInstallInNonGitRepo(t *testing.T) { 275 os.Chdir(gopRoot) 276 277 // Setup 278 gitDir := filepath.Join(gopRoot, ".git") 279 gitBackupDir := filepath.Join(gopRoot, ".gitBackup") 280 // Rename .git dir 281 if checkPathExist(gitDir, true) { 282 os.Rename(gitDir, gitBackupDir) 283 } 284 285 // Teardown 286 t.Cleanup(func() { 287 if checkPathExist(gitBackupDir, true) { 288 os.Rename(gitBackupDir, gitDir) 289 } 290 if checkPathExist(versionFile, false) { 291 os.Remove(versionFile) 292 } 293 }) 294 295 installCmd := func() *exec.Cmd { 296 return exec.Command("go", "run", installer, "--install") 297 } 298 299 t.Run("failed build operation", func(t *testing.T) { 300 os.Remove(versionFile) 301 cmd := installCmd() 302 output, err := cmd.CombinedOutput() 303 if err == nil || !strings.Contains(string(output), "Error") { 304 t.Log(string(output)) 305 t.Fatal("Failed: build Go+ in a non-git repo and no VERSION file should be failed.") 306 } 307 }) 308 309 t.Run("install with VERSION file", func(t *testing.T) { 310 version := "v1.2.98" 311 // Create VERSION file 312 if err := os.WriteFile(versionFile, []byte(version), 0644); err != nil { 313 t.Fatal(err) 314 } 315 316 cmd := installCmd() 317 if output, err := cmd.CombinedOutput(); err != nil { 318 t.Fatalf("Failed: %v, output: %s\n", err, output) 319 } 320 321 cmd = exec.Command(filepath.Join(gopRoot, "bin", gopBinFiles[0]), "version") 322 output, err := cmd.CombinedOutput() 323 if err != nil || !strings.Contains(string(output), version) { 324 t.Fatalf("Failed: %v, output: %s\n", err, output) 325 } 326 }) 327 } 328 329 func TestHandleMultiFlags(t *testing.T) { 330 os.Chdir(gopRoot) 331 332 cmd := exec.Command("go", "run", installer, "--install", "--test", "--uninstall") 333 if output, err := cmd.CombinedOutput(); err != nil { 334 t.Fatalf("Failed: %v:\nOut: %s\n", err, output) 335 } 336 337 goBinPath := detectGoBinPath() 338 339 // Uninstall will be the last action to run, test if all build assets being cleared. 340 for _, file := range gopBinFiles { 341 if checkPathExist(filepath.Join(gopRoot, "bin", file), false) { 342 t.Fatalf("Failed: %s found in ./bin directory\n", file) 343 } 344 if checkPathExist(filepath.Join(goBinPath, file), false) { 345 t.Fatalf("Failed: %s found in %s/bin directory\n", file, goBinPath) 346 } 347 } 348 }