pipeline_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. package pipeline
  5. import (
  6. "bufio"
  7. "bytes"
  8. "encoding/json"
  9. "flag"
  10. "fmt"
  11. "go/build"
  12. "io/ioutil"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "path/filepath"
  17. "reflect"
  18. "runtime"
  19. "strings"
  20. "testing"
  21. "golang.org/x/text/language"
  22. )
  23. var genFiles = flag.Bool("gen", false, "generate output files instead of comparing")
  24. // setHelper is testing.T.Helper on Go 1.9+, overridden by go19_test.go.
  25. var setHelper = func(t *testing.T) {}
  26. func TestFullCycle(t *testing.T) {
  27. if runtime.GOOS == "android" {
  28. t.Skip("cannot load outside packages on android")
  29. }
  30. if _, err := exec.LookPath("go"); err != nil {
  31. t.Skipf("skipping because 'go' command is unavailable: %v", err)
  32. }
  33. GOPATH, err := ioutil.TempDir("", "pipeline_test")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. defer os.RemoveAll(GOPATH)
  38. testdata := filepath.Join(GOPATH, "src", "testdata")
  39. // Copy the testdata contents into a new module.
  40. copyTestdata(t, testdata)
  41. initTestdataModule(t, testdata)
  42. // Several places hard-code the use of build.Default.
  43. // Adjust it to match the test's temporary GOPATH.
  44. defer func(prev string) { build.Default.GOPATH = prev }(build.Default.GOPATH)
  45. build.Default.GOPATH = GOPATH + string(filepath.ListSeparator) + build.Default.GOPATH
  46. if wd := reflect.ValueOf(&build.Default).Elem().FieldByName("WorkingDir"); wd.IsValid() {
  47. defer func(prev string) { wd.SetString(prev) }(wd.String())
  48. wd.SetString(testdata)
  49. }
  50. // To work around https://golang.org/issue/34860, execute the commands
  51. // that (transitively) use go/build in the working directory of the
  52. // corresponding module.
  53. wd, _ := os.Getwd()
  54. defer os.Chdir(wd)
  55. dirs, err := ioutil.ReadDir(testdata)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. for _, f := range dirs {
  60. if !f.IsDir() {
  61. continue
  62. }
  63. t.Run(f.Name(), func(t *testing.T) {
  64. chk := func(t *testing.T, err error) {
  65. setHelper(t)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. dir := filepath.Join(testdata, f.Name())
  71. pkgPath := "testdata/" + f.Name()
  72. config := Config{
  73. SourceLanguage: language.AmericanEnglish,
  74. Packages: []string{pkgPath},
  75. Dir: filepath.Join(dir, "locales"),
  76. GenFile: "catalog_gen.go",
  77. GenPackage: pkgPath,
  78. }
  79. os.Chdir(dir)
  80. // TODO: load config if available.
  81. s, err := Extract(&config)
  82. chk(t, err)
  83. chk(t, s.Import())
  84. chk(t, s.Merge())
  85. // TODO:
  86. // for range s.Config.Actions {
  87. // // TODO: do the actions.
  88. // }
  89. chk(t, s.Export())
  90. chk(t, s.Generate())
  91. os.Chdir(wd)
  92. writeJSON(t, filepath.Join(dir, "extracted.gotext.json"), s.Extracted)
  93. checkOutput(t, dir, f.Name())
  94. })
  95. }
  96. }
  97. func copyTestdata(t *testing.T, dst string) {
  98. err := filepath.Walk("testdata", func(p string, f os.FileInfo, err error) error {
  99. if p == "testdata" || strings.HasSuffix(p, ".want") {
  100. return nil
  101. }
  102. rel := strings.TrimPrefix(p, "testdata"+string(filepath.Separator))
  103. if f.IsDir() {
  104. return os.MkdirAll(filepath.Join(dst, rel), 0755)
  105. }
  106. data, err := ioutil.ReadFile(p)
  107. if err != nil {
  108. return err
  109. }
  110. return ioutil.WriteFile(filepath.Join(dst, rel), data, 0644)
  111. })
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. }
  116. func initTestdataModule(t *testing.T, dst string) {
  117. xTextDir, err := filepath.Abs("../..")
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. goMod := fmt.Sprintf(`module testdata
  122. go 1.11
  123. require golang.org/x/text v0.0.0-00010101000000-000000000000
  124. replace golang.org/x/text v0.0.0-00010101000000-000000000000 => %s
  125. `, xTextDir)
  126. if err := ioutil.WriteFile(filepath.Join(dst, "go.mod"), []byte(goMod), 0644); err != nil {
  127. t.Fatal(err)
  128. }
  129. data, err := ioutil.ReadFile(filepath.Join(xTextDir, "go.sum"))
  130. if err := ioutil.WriteFile(filepath.Join(dst, "go.sum"), data, 0644); err != nil {
  131. t.Fatal(err)
  132. }
  133. }
  134. func checkOutput(t *testing.T, gen string, testdataDir string) {
  135. err := filepath.Walk(gen, func(gotFile string, f os.FileInfo, err error) error {
  136. if f.IsDir() {
  137. return nil
  138. }
  139. rel := strings.TrimPrefix(gotFile, gen+string(filepath.Separator))
  140. wantFile := filepath.Join("testdata", testdataDir, rel+".want")
  141. if _, err := os.Stat(wantFile); os.IsNotExist(err) {
  142. return nil
  143. }
  144. got, err := ioutil.ReadFile(gotFile)
  145. if err != nil {
  146. t.Errorf("failed to read %q", gotFile)
  147. return nil
  148. }
  149. if *genFiles {
  150. if err := ioutil.WriteFile(wantFile, got, 0644); err != nil {
  151. t.Fatal(err)
  152. }
  153. }
  154. want, err := ioutil.ReadFile(wantFile)
  155. if err != nil {
  156. t.Errorf("failed to read %q", wantFile)
  157. } else {
  158. scanGot := bufio.NewScanner(bytes.NewReader(got))
  159. scanWant := bufio.NewScanner(bytes.NewReader(want))
  160. line := 0
  161. clean := func(s string) string {
  162. if i := strings.LastIndex(s, "//"); i != -1 {
  163. s = s[:i]
  164. }
  165. return path.Clean(filepath.ToSlash(s))
  166. }
  167. for scanGot.Scan() && scanWant.Scan() {
  168. got := clean(scanGot.Text())
  169. want := clean(scanWant.Text())
  170. if got != want {
  171. t.Errorf("file %q differs from .want file at line %d:\n\t%s\n\t%s", gotFile, line, got, want)
  172. break
  173. }
  174. line++
  175. }
  176. if scanGot.Scan() || scanWant.Scan() {
  177. t.Errorf("file %q differs from .want file at line %d.", gotFile, line)
  178. }
  179. }
  180. return nil
  181. })
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. }
  186. func writeJSON(t *testing.T, path string, x interface{}) {
  187. data, err := json.MarshalIndent(x, "", " ")
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if err := ioutil.WriteFile(path, data, 0644); err != nil {
  192. t.Fatal(err)
  193. }
  194. }