| 1 | // Copyright (c) Bartłomiej Płotka @bwplotka |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package main_test |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "github.com/efficientgo/core/errors" |
| 16 | "github.com/efficientgo/core/testutil" |
| 17 | ) |
| 18 | |
| 19 | type testProject struct { |
| 20 | pwd, root string |
| 21 | isGoProject bool |
| 22 | } |
| 23 | |
| 24 | func newTestProject(t testing.TB, base string, target string, isGoProject bool) *testProject { |
| 25 | t.Helper() |
| 26 | |
| 27 | wd, err := os.Getwd() |
| 28 | testutil.Ok(t, err) |
| 29 | |
| 30 | _, err = execCmd(wd, nil, "cp", "-r", base, target) |
| 31 | testutil.Ok(t, err) |
| 32 | |
| 33 | if isGoProject { |
| 34 | _, err = execCmd(wd, nil, "cp", filepath.Join(wd, "testdata", "main.go"), target) |
| 35 | testutil.Ok(t, err) |
| 36 | _, err = execCmd(wd, nil, "cp", filepath.Join(wd, "testdata", "go.mod"), target) |
| 37 | testutil.Ok(t, err) |
| 38 | _, err = execCmd(wd, nil, "cp", filepath.Join(wd, "testdata", "go.sum"), target) |
| 39 | testutil.Ok(t, err) |
| 40 | } |
| 41 | |
| 42 | _, err = execCmd(wd, nil, "cp", filepath.Join(wd, "testdata", "Makefile"), target) |
| 43 | testutil.Ok(t, err) |
| 44 | return &testProject{ |
| 45 | pwd: wd, |
| 46 | root: target, |
| 47 | isGoProject: isGoProject, |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (g *testProject) assertNotChanged(t testing.TB, except ...string) { |
| 52 | t.Helper() |
| 53 | |
| 54 | if g.isGoProject { |
| 55 | g.assertGoModDidNotChange(t).assertGoSumDidNotChange(t) |
| 56 | except = append(except, "main.go", "go.sum", "go.mod") |
| 57 | } |
| 58 | g.assertProjectRootIsClean(t, except...) |
| 59 | } |
| 60 | |
| 61 | func (g *testProject) assertGoModDidNotChange(t testing.TB) *testProject { |
| 62 | t.Helper() |
| 63 | |
| 64 | a, err := os.ReadFile(filepath.Join(g.root, "go.mod")) |
| 65 | testutil.Ok(t, err) |
| 66 | |
| 67 | b, err := os.ReadFile(filepath.Join(g.pwd, "testdata", "go.mod")) |
| 68 | testutil.Ok(t, err) |
| 69 | |
| 70 | testutil.Equals(t, string(b), string(a)) |
| 71 | |
| 72 | return g |
| 73 | } |
| 74 | |
| 75 | func (g *testProject) assertGoSumDidNotChange(t testing.TB) *testProject { |
| 76 | t.Helper() |
| 77 | |
| 78 | a, err := os.ReadFile(filepath.Join(g.root, "go.sum")) |
| 79 | testutil.Ok(t, err) |
| 80 | |
| 81 | b, err := os.ReadFile(filepath.Join(g.pwd, "testdata", "go.sum")) |
| 82 | testutil.Ok(t, err) |
| 83 | |
| 84 | testutil.Equals(t, string(b), string(a)) |
| 85 | return g |
| 86 | } |
| 87 | |
| 88 | func (g *testProject) assertProjectRootIsClean(t testing.TB, extra ...string) *testProject { |
| 89 | t.Helper() |
| 90 | |
| 91 | expected := map[string]struct{}{ |
| 92 | "Makefile": {}, |
| 93 | } |
| 94 | for _, e := range extra { |
| 95 | expected[e] = struct{}{} |
| 96 | } |
| 97 | if g.isGoProject { |
| 98 | expected["go.mod"] = struct{}{} |
| 99 | expected["go.sum"] = struct{}{} |
| 100 | expected["main.go"] = struct{}{} |
| 101 | } |
| 102 | |
| 103 | i, err := os.ReadDir(g.root) |
| 104 | testutil.Ok(t, err) |
| 105 | got := map[string]struct{}{} |
| 106 | for _, f := range i { |
| 107 | got[f.Name()] = struct{}{} |
| 108 | } |
| 109 | testutil.Equals(t, expected, got) |
| 110 | |
| 111 | return g |
| 112 | } |
| 113 | |
| 114 | type goEnv struct { |
| 115 | goroot, gopath, goproxy, gobin, gocache, tmpDir string |
| 116 | } |
| 117 | |
| 118 | func execCmd(dir string, env []string, command string, args ...string) (string, error) { |
| 119 | var cmd *exec.Cmd |
| 120 | if env == nil { |
| 121 | cmd = exec.Command(command, args...) |
| 122 | } else { |
| 123 | // Since we want to have synthetic PATH, do not allows unspecified paths. |
| 124 | // Otherwise unit test environment PATH will be used for lookup as exec.LookPath is not parametrized. |
| 125 | // TL;DR: command has to have path separator. |
| 126 | cmd = &exec.Cmd{ |
| 127 | Env: env, |
| 128 | Path: command, |
| 129 | Args: append([]string{command}, args...), |
| 130 | } |
| 131 | } |
| 132 | cmd.Dir = dir |
| 133 | var b bytes.Buffer |
| 134 | cmd.Stdout = &b |
| 135 | cmd.Stderr = &b |
| 136 | if err := cmd.Run(); err != nil { |
| 137 | if _, ok := err.(*exec.ExitError); ok { |
| 138 | return "", errors.Newf("error while running command %q; out: %s; err: %v", cmd.String(), b.String(), err) |
| 139 | |
| 140 | } |
| 141 | return "", errors.Newf("error while running command %q; out: %s; err: %v", cmd.String(), b.String(), err) |
| 142 | } |
| 143 | return b.String(), nil |
| 144 | } |
| 145 | |
| 146 | func buildInitialGobin(t *testing.T, targetDir string) { |
| 147 | t.Helper() |
| 148 | |
| 149 | wd, err := os.Getwd() |
| 150 | testutil.Ok(t, err) |
| 151 | |
| 152 | testutil.Ok(t, os.Setenv("GOBIN", filepath.Join(wd, ".bin"))) |
| 153 | |
| 154 | _, err = execCmd(wd, nil, "make", "build") |
| 155 | testutil.Ok(t, err) |
| 156 | _, err = execCmd(wd, nil, "cp", filepath.Join(os.Getenv("GOBIN"), bingoBin), targetDir) |
| 157 | testutil.Ok(t, err) |
| 158 | } |
| 159 | |
| 160 | func makePath(t *testing.T) string { |
| 161 | t.Helper() |
| 162 | |
| 163 | makePath, err := execCmd("", nil, "which", "make") |
| 164 | testutil.Ok(t, err) |
| 165 | return strings.TrimSuffix(makePath, "\n") |
| 166 | } |
| 167 | |
| 168 | func newIsolatedGoEnv(t testing.TB, goproxy string) *goEnv { |
| 169 | tmpDir, err := os.MkdirTemp("", "bingo-tmpgoenv") |
| 170 | testutil.Ok(t, err) |
| 171 | |
| 172 | tmpDir, err = filepath.Abs(tmpDir) |
| 173 | testutil.Ok(t, err) |
| 174 | |
| 175 | goRoot, err := execCmd("", nil, "which", "go") |
| 176 | testutil.Ok(t, err) |
| 177 | |
| 178 | gopath := filepath.Join(tmpDir, "gopath") |
| 179 | return &goEnv{ |
| 180 | tmpDir: tmpDir, |
| 181 | goroot: filepath.Dir(goRoot), |
| 182 | gopath: gopath, |
| 183 | // Making sure $GOBIN is actually different than standard one to test advanced stuff. |
| 184 | gobin: filepath.Join(tmpDir, "bin"), |
| 185 | gocache: filepath.Join(tmpDir, "gocache"), |
| 186 | goproxy: goproxy, |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Clear clears all go env dirs but not goroot, gopath and gocache. |
| 191 | func (g *goEnv) Clear(t testing.TB) { |
| 192 | t.Helper() |
| 193 | |
| 194 | _, err := execCmd("", nil, "chmod", "-R", "777", g.tmpDir) |
| 195 | testutil.Ok(t, err) |
| 196 | |
| 197 | dirs, err := os.ReadDir(g.tmpDir) |
| 198 | testutil.Ok(t, err) |
| 199 | |
| 200 | for _, d := range dirs { |
| 201 | switch filepath.Join(g.tmpDir, d.Name()) { |
| 202 | case g.gocache, g.goroot, g.gopath: |
| 203 | default: |
| 204 | testutil.Ok(t, os.RemoveAll(filepath.Join(g.tmpDir, d.Name()))) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func (g *goEnv) TmpDir() string { |
| 210 | return g.tmpDir |
| 211 | } |
| 212 | |
| 213 | func (g *goEnv) syntheticEnv() []string { |
| 214 | return []string{ |
| 215 | // Make sure we don't require clang to build etc. |
| 216 | "CGO_ENABLED=0", |
| 217 | fmt.Sprintf("PATH=%s:%s:%s", g.goroot, g.tmpDir, g.gobin), |
| 218 | fmt.Sprintf("GO=%s", filepath.Join(g.goroot, "go")), |
| 219 | fmt.Sprintf("GOBIN=%s", g.gobin), |
| 220 | fmt.Sprintf("GOPATH=%s", g.gopath), |
| 221 | fmt.Sprintf("GOCACHE=%s", g.gocache), |
| 222 | fmt.Sprintf("GOPROXY=%s", g.goproxy), |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func (g *goEnv) ExecOutput(t testing.TB, dir string, command string, args ...string) string { |
| 227 | t.Helper() |
| 228 | |
| 229 | b, err := execCmd(dir, g.syntheticEnv(), command, args...) |
| 230 | testutil.Ok(t, err) |
| 231 | return b |
| 232 | } |
| 233 | |
| 234 | func (g *goEnv) ExpectErr(dir string, command string, args ...string) error { |
| 235 | _, err := execCmd(dir, g.syntheticEnv(), command, args...) |
| 236 | return err |
| 237 | } |
| 238 | |
| 239 | func (g *goEnv) existingBinaries(t *testing.T) []string { |
| 240 | t.Helper() |
| 241 | |
| 242 | var filenames []string |
| 243 | files, err := os.ReadDir(g.gobin) |
| 244 | if os.IsNotExist(err) { |
| 245 | return []string{} |
| 246 | } |
| 247 | testutil.Ok(t, err) |
| 248 | |
| 249 | for _, f := range files { |
| 250 | if f.IsDir() { |
| 251 | t.Fatal("Did not expect directory in gobin", g.gobin, "got", f.Name()) |
| 252 | } |
| 253 | filenames = append(filenames, f.Name()) |
| 254 | } |
| 255 | return filenames |
| 256 | } |
| 257 | |
| 258 | func (g *goEnv) Close(t testing.TB) { |
| 259 | t.Helper() |
| 260 | |
| 261 | _, err := execCmd("", nil, "chmod", "-R", "777", g.tmpDir) |
| 262 | testutil.Ok(t, err) |
| 263 | testutil.Ok(t, os.RemoveAll(g.tmpDir)) |
| 264 | } |