| 1 |
// Copyright 2017 The Go 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 pipeline |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"encoding/json" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"go/build" |
| 14 |
"os" |
| 15 |
"os/exec" |
| 16 |
"path" |
| 17 |
"path/filepath" |
| 18 |
"reflect" |
| 19 |
"runtime" |
| 20 |
"strings" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"golang.org/x/text/language" |
| 24 |
) |
| 25 |
|
| 26 |
var genFiles = flag.Bool("gen", false, "generate output files instead of comparing") |
| 27 |
|
| 28 |
func TestFullCycle(t *testing.T) { |
| 29 |
if runtime.GOOS == "android" { |
| 30 |
t.Skip("cannot load outside packages on android") |
| 31 |
} |
| 32 |
if b := os.Getenv("GO_BUILDER_NAME"); b == "plan9-arm" { |
| 33 |
t.Skipf("skipping: test frequently times out on %s", b) |
| 34 |
} |
| 35 |
if _, err := exec.LookPath("go"); err != nil { |
| 36 |
t.Skipf("skipping because 'go' command is unavailable: %v", err) |
| 37 |
} |
| 38 |
|
| 39 |
GOPATH, err := os.MkdirTemp("", "pipeline_test") |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
defer os.RemoveAll(GOPATH) |
| 44 |
testdata := filepath.Join(GOPATH, "src", "testdata") |
| 45 |
|
| 46 |
// Copy the testdata contents into a new module. |
| 47 |
copyTestdata(t, testdata) |
| 48 |
initTestdataModule(t, testdata) |
| 49 |
|
| 50 |
// Several places hard-code the use of build.Default. |
| 51 |
// Adjust it to match the test's temporary GOPATH. |
| 52 |
defer func(prev string) { build.Default.GOPATH = prev }(build.Default.GOPATH) |
| 53 |
build.Default.GOPATH = GOPATH + string(filepath.ListSeparator) + build.Default.GOPATH |
| 54 |
if wd := reflect.ValueOf(&build.Default).Elem().FieldByName("WorkingDir"); wd.IsValid() { |
| 55 |
defer func(prev string) { wd.SetString(prev) }(wd.String()) |
| 56 |
wd.SetString(testdata) |
| 57 |
} |
| 58 |
|
| 59 |
// To work around https://golang.org/issue/34860, execute the commands |
| 60 |
// that (transitively) use go/build in the working directory of the |
| 61 |
// corresponding module. |
| 62 |
wd, _ := os.Getwd() |
| 63 |
defer os.Chdir(wd) |
| 64 |
|
| 65 |
dirs, err := os.ReadDir(testdata) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
for _, f := range dirs { |
| 70 |
if !f.IsDir() { |
| 71 |
continue |
| 72 |
} |
| 73 |
t.Run(f.Name(), func(t *testing.T) { |
| 74 |
chk := func(t *testing.T, err error) { |
| 75 |
t.Helper() |
| 76 |
if err != nil { |
| 77 |
t.Fatal(err) |
| 78 |
} |
| 79 |
} |
| 80 |
dir := filepath.Join(testdata, f.Name()) |
| 81 |
pkgPath := "testdata/" + f.Name() |
| 82 |
config := Config{ |
| 83 |
SourceLanguage: language.AmericanEnglish, |
| 84 |
Packages: []string{pkgPath}, |
| 85 |
Dir: filepath.Join(dir, "locales"), |
| 86 |
GenFile: "catalog_gen.go", |
| 87 |
GenPackage: pkgPath, |
| 88 |
} |
| 89 |
|
| 90 |
os.Chdir(dir) |
| 91 |
|
| 92 |
// TODO: load config if available. |
| 93 |
s, err := Extract(&config) |
| 94 |
chk(t, err) |
| 95 |
chk(t, s.Import()) |
| 96 |
chk(t, s.Merge()) |
| 97 |
// TODO: |
| 98 |
// for range s.Config.Actions { |
| 99 |
// // TODO: do the actions. |
| 100 |
// } |
| 101 |
chk(t, s.Export()) |
| 102 |
chk(t, s.Generate()) |
| 103 |
|
| 104 |
os.Chdir(wd) |
| 105 |
|
| 106 |
writeJSON(t, filepath.Join(dir, "extracted.gotext.json"), s.Extracted) |
| 107 |
checkOutput(t, dir, f.Name()) |
| 108 |
}) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func copyTestdata(t *testing.T, dst string) { |
| 113 |
err := filepath.Walk("testdata", func(p string, f os.FileInfo, err error) error { |
| 114 |
if p == "testdata" || strings.HasSuffix(p, ".want") { |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
rel := strings.TrimPrefix(p, "testdata"+string(filepath.Separator)) |
| 119 |
if f.IsDir() { |
| 120 |
return os.MkdirAll(filepath.Join(dst, rel), 0755) |
| 121 |
} |
| 122 |
|
| 123 |
data, err := os.ReadFile(p) |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
return os.WriteFile(filepath.Join(dst, rel), data, 0644) |
| 128 |
}) |
| 129 |
if err != nil { |
| 130 |
t.Fatal(err) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func initTestdataModule(t *testing.T, dst string) { |
| 135 |
xTextDir, err := filepath.Abs("../..") |
| 136 |
if err != nil { |
| 137 |
t.Fatal(err) |
| 138 |
} |
| 139 |
|
| 140 |
goMod := fmt.Sprintf(`module testdata |
| 141 |
|
| 142 |
replace golang.org/x/text => %s |
| 143 |
`, xTextDir) |
| 144 |
if err := os.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil { |
| 145 |
t.Fatal(err) |
| 146 |
} |
| 147 |
|
| 148 |
// Copy in the checksums from the parent module so that we won't |
| 149 |
// need to re-fetch them from the checksum database. |
| 150 |
data, err := os.ReadFile(filepath.Join(xTextDir, "go.sum")) |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
if err := os.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil { |
| 155 |
t.Fatal(err) |
| 156 |
} |
| 157 |
|
| 158 |
// We've added a replacement for the parent version of x/text, |
| 159 |
// but now we need to populate the correct version. |
| 160 |
// (We can't just replace the zero-version because x/text |
| 161 |
// may indirectly depend on some nonzero version of itself.) |
| 162 |
// |
| 163 |
// We use 'go get' instead of 'go mod tidy' to avoid the old-release |
| 164 |
// compatibility check when graph pruning is enabled, and to avoid doing |
| 165 |
// more work than necessary for test dependencies of imported packages |
| 166 |
// (we're not going to run those tests here anyway). |
| 167 |
// |
| 168 |
// We 'go get' the packages in the testdata module — not specific dependencies |
| 169 |
// of those packages — so that they will resolve to whatever version is |
| 170 |
// already required in the (replaced) x/text go.mod file. |
| 171 |
|
| 172 |
getCmd := exec.Command("go", "get", "-d", "./...") |
| 173 |
getCmd.Dir = dst |
| 174 |
getCmd.Env = append(os.Environ(), "PWD="+dst, "GOPROXY=off", "GOCACHE=off") |
| 175 |
if out, err := getCmd.CombinedOutput(); err != nil { |
| 176 |
t.Logf("%s", out) |
| 177 |
t.Fatal(err) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func checkOutput(t *testing.T, gen string, testdataDir string) { |
| 182 |
err := filepath.Walk(gen, func(gotFile string, f os.FileInfo, err error) error { |
| 183 |
if f.IsDir() { |
| 184 |
return nil |
| 185 |
} |
| 186 |
rel := strings.TrimPrefix(gotFile, gen+string(filepath.Separator)) |
| 187 |
|
| 188 |
wantFile := filepath.Join("testdata", testdataDir, rel+".want") |
| 189 |
if _, err := os.Stat(wantFile); os.IsNotExist(err) { |
| 190 |
return nil |
| 191 |
} |
| 192 |
|
| 193 |
got, err := os.ReadFile(gotFile) |
| 194 |
if err != nil { |
| 195 |
t.Errorf("failed to read %q", gotFile) |
| 196 |
return nil |
| 197 |
} |
| 198 |
if *genFiles { |
| 199 |
if err := os.WriteFile(wantFile, got, 0644); err != nil { |
| 200 |
t.Fatal(err) |
| 201 |
} |
| 202 |
} |
| 203 |
want, err := os.ReadFile(wantFile) |
| 204 |
if err != nil { |
| 205 |
t.Errorf("failed to read %q", wantFile) |
| 206 |
} else { |
| 207 |
scanGot := bufio.NewScanner(bytes.NewReader(got)) |
| 208 |
scanWant := bufio.NewScanner(bytes.NewReader(want)) |
| 209 |
line := 0 |
| 210 |
clean := func(s string) string { |
| 211 |
if i := strings.LastIndex(s, "//"); i != -1 { |
| 212 |
s = s[:i] |
| 213 |
} |
| 214 |
return path.Clean(filepath.ToSlash(s)) |
| 215 |
} |
| 216 |
for scanGot.Scan() && scanWant.Scan() { |
| 217 |
got := clean(scanGot.Text()) |
| 218 |
want := clean(scanWant.Text()) |
| 219 |
if got != want { |
| 220 |
t.Errorf("file %q differs from .want file at line %d:\n\t%s\n\t%s", gotFile, line, got, want) |
| 221 |
break |
| 222 |
} |
| 223 |
line++ |
| 224 |
} |
| 225 |
if scanGot.Scan() || scanWant.Scan() { |
| 226 |
t.Errorf("file %q differs from .want file at line %d.", gotFile, line) |
| 227 |
} |
| 228 |
} |
| 229 |
return nil |
| 230 |
}) |
| 231 |
if err != nil { |
| 232 |
t.Fatal(err) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func writeJSON(t *testing.T, path string, x interface{}) { |
| 237 |
data, err := json.MarshalIndent(x, "", " ") |
| 238 |
if err != nil { |
| 239 |
t.Fatal(err) |
| 240 |
} |
| 241 |
if err := os.WriteFile(path, data, 0644); err != nil { |
| 242 |
t.Fatal(err) |
| 243 |
} |
| 244 |
} |