convert_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package integration_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. . "github.com/onsi/ginkgo"
  9. . "github.com/onsi/gomega"
  10. )
  11. var _ = Describe("ginkgo convert", func() {
  12. var tmpDir string
  13. readConvertedFileNamed := func(pathComponents ...string) string {
  14. pathToFile := filepath.Join(tmpDir, "convert_fixtures", filepath.Join(pathComponents...))
  15. bytes, err := ioutil.ReadFile(pathToFile)
  16. ExpectWithOffset(1, err).NotTo(HaveOccurred())
  17. return string(bytes)
  18. }
  19. readGoldMasterNamed := func(filename string) string {
  20. bytes, err := ioutil.ReadFile(filepath.Join("_fixtures", "convert_goldmasters", filename))
  21. Ω(err).ShouldNot(HaveOccurred())
  22. return string(bytes)
  23. }
  24. BeforeEach(func() {
  25. var err error
  26. tmpDir, err = ioutil.TempDir("", "ginkgo-convert")
  27. Ω(err).ShouldNot(HaveOccurred())
  28. err = exec.Command("cp", "-r", filepath.Join("_fixtures", "convert_fixtures"), tmpDir).Run()
  29. Ω(err).ShouldNot(HaveOccurred())
  30. })
  31. JustBeforeEach(func() {
  32. cwd, err := os.Getwd()
  33. Ω(err).ShouldNot(HaveOccurred())
  34. relPath, err := filepath.Rel(cwd, filepath.Join(tmpDir, "convert_fixtures"))
  35. Ω(err).ShouldNot(HaveOccurred())
  36. cmd := exec.Command(pathToGinkgo, "convert", relPath)
  37. cmd.Env = os.Environ()
  38. for i, env := range cmd.Env {
  39. if strings.HasPrefix(env, "PATH") {
  40. cmd.Env[i] = cmd.Env[i] + ":" + filepath.Dir(pathToGinkgo)
  41. break
  42. }
  43. }
  44. err = cmd.Run()
  45. Ω(err).ShouldNot(HaveOccurred())
  46. })
  47. AfterEach(func() {
  48. err := os.RemoveAll(tmpDir)
  49. Ω(err).ShouldNot(HaveOccurred())
  50. })
  51. It("rewrites xunit tests as ginkgo tests", func() {
  52. convertedFile := readConvertedFileNamed("xunit_test.go")
  53. goldMaster := readGoldMasterNamed("xunit_test.go")
  54. Ω(convertedFile).Should(Equal(goldMaster))
  55. })
  56. It("rewrites all usages of *testing.T as mr.T()", func() {
  57. convertedFile := readConvertedFileNamed("extra_functions_test.go")
  58. goldMaster := readGoldMasterNamed("extra_functions_test.go")
  59. Ω(convertedFile).Should(Equal(goldMaster))
  60. })
  61. It("rewrites tests in the package dir that belong to other packages", func() {
  62. convertedFile := readConvertedFileNamed("outside_package_test.go")
  63. goldMaster := readGoldMasterNamed("outside_package_test.go")
  64. Ω(convertedFile).Should(Equal(goldMaster))
  65. })
  66. It("rewrites tests in nested packages", func() {
  67. convertedFile := readConvertedFileNamed("nested", "nested_test.go")
  68. goldMaster := readGoldMasterNamed("nested_test.go")
  69. Ω(convertedFile).Should(Equal(goldMaster))
  70. })
  71. Context("ginkgo test suite files", func() {
  72. It("creates a ginkgo test suite file for the package you specified", func() {
  73. testsuite := readConvertedFileNamed("convert_fixtures_suite_test.go")
  74. goldMaster := readGoldMasterNamed("suite_test.go")
  75. Ω(testsuite).Should(Equal(goldMaster))
  76. })
  77. It("converts go tests in deeply nested packages (some may not contain go files)", func() {
  78. testsuite := readConvertedFileNamed("nested_without_gofiles", "subpackage", "nested_subpackage_test.go")
  79. goldMaster := readGoldMasterNamed("nested_subpackage_test.go")
  80. Ω(testsuite).Should(Equal(goldMaster))
  81. })
  82. It("creates ginkgo test suites for all nested packages", func() {
  83. testsuite := readConvertedFileNamed("nested", "nested_suite_test.go")
  84. goldMaster := readGoldMasterNamed("nested_suite_test.go")
  85. Ω(testsuite).Should(Equal(goldMaster))
  86. })
  87. })
  88. Context("with an existing test suite file", func() {
  89. BeforeEach(func() {
  90. goldMaster := readGoldMasterNamed("fixtures_suite_test.go")
  91. err := ioutil.WriteFile(filepath.Join(tmpDir, "convert_fixtures", "tmp_suite_test.go"), []byte(goldMaster), 0600)
  92. Ω(err).ShouldNot(HaveOccurred())
  93. })
  94. It("gracefully handles existing test suite files", func() {
  95. //nothing should have gone wrong!
  96. })
  97. })
  98. })