Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor `ginkgo convert` tests

  • Loading branch information...
commit c8efbb97763156a10d90fe4759b0d608a00b56de 1 parent 2c5f638
Onsi Fakhouri authored
Showing with 146 additions and 208 deletions.
  1. +0 −13 convert_goldmasters/suite_test.go
  2. +0 −195 convert_test.go
  3. 0  { → ginkgo/integration/_fixtures}/convert_fixtures/extra_functions_test.go
  4. 0  { → ginkgo/integration/_fixtures}/convert_fixtures/ginkgo_test_goldmaster.go
  5. 0  { → ginkgo/integration/_fixtures}/convert_fixtures/nested/nested_test.go
  6. 0  ... ginkgo/integration/_fixtures}/convert_fixtures/nested_without_gofiles/subpackage/nested_subpackage_test.go
  7. 0  { → ginkgo/integration/_fixtures}/convert_fixtures/outside_package_test.go
  8. 0  { → ginkgo/integration/_fixtures}/convert_fixtures/xunit_test.go
  9. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/extra_functions_test.go
  10. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/fixtures_suite_test.go
  11. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/nested_subpackage_test.go
  12. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/nested_suite_test.go
  13. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/nested_test.go
  14. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/outside_package_test.go
  15. +13 −0 ginkgo/integration/_fixtures/convert_goldmasters/suite_test.go
  16. 0  { → ginkgo/integration/_fixtures}/convert_goldmasters/xunit_test.go
  17. +111 −0 ginkgo/integration/convert_test.go
  18. +22 −0 ginkgo/integration/integration_suite_test.go
13 convert_goldmasters/suite_test.go
View
@@ -1,13 +0,0 @@
-package tmp_test
-
-import (
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
-
- "testing"
-)
-
-func TestTmp(t *testing.T) {
- RegisterFailHandler(Fail)
- RunSpecs(t, "Tmp Suite")
-}
195 convert_test.go
View
@@ -1,195 +0,0 @@
-package ginkgo
-
-import (
- . "github.com/onsi/gomega"
- "io/ioutil"
- "os"
- "os/exec"
- "path/filepath"
-)
-
-func init() {
- Describe("using ginkgo convert", func() {
- BeforeEach(deleteTmpFiles)
- BeforeEach(buildGinkgo)
-
- It("rewrites xunit tests as ginkgo tests", func() {
- withTempDir(func(tempDir string) {
- runGinkgoConvert()
-
- convertedFile := readConvertedFileNamed(tempDir, "xunit_test.go")
- goldmaster := readGoldMasterNamed("xunit_test.go")
- Expect(convertedFile).To(Equal(goldmaster))
- })
- })
-
- It("rewrites all usages of *testing.T as mr.T()", func() {
- withTempDir(func(tempDir string) {
- runGinkgoConvert()
-
- convertedFile := readConvertedFileNamed(tempDir, "extra_functions_test.go")
- goldmaster := readGoldMasterNamed("extra_functions_test.go")
- Expect(convertedFile).To(Equal(goldmaster))
- })
- })
-
- It("rewrites tests in the package dir that belong to other packages", func() {
- withTempDir(func(tempDir string) {
- runGinkgoConvert()
-
- convertedFile := readConvertedFileNamed(tempDir, "outside_package_test.go")
- goldMaster := readGoldMasterNamed("outside_package_test.go")
- Expect(convertedFile).To(Equal(goldMaster))
- })
- })
-
- It("rewrites tests in nested packages", func() {
- withTempDir(func(dir string) {
- runGinkgoConvert()
-
- convertedFile := readConvertedFileNamed(dir, filepath.Join("nested", "nested_test.go"))
- goldMaster := readGoldMasterNamed("nested_test.go")
- Expect(convertedFile).To(Equal(goldMaster))
- })
- })
-
- Context("ginkgo test suite files", func() {
- It("creates a ginkgo test suite file for the package you specified", func() {
- withTempDir(func(dir string) {
- runGinkgoConvert()
-
- testsuite := readConvertedFileNamed(dir, "tmp_suite_test.go")
- goldmaster := readGoldMasterNamed("suite_test.go")
- Expect(testsuite).To(Equal(goldmaster))
- })
- })
-
- It("converts go tests in deeply nested packages (some may not contain go files)", func() {
- withTempDir(func(dir string) {
- runGinkgoConvert()
-
- testsuite := readConvertedFileNamed(dir, "nested_without_gofiles", "subpackage", "nested_subpackage_test.go")
- goldmaster := readGoldMasterNamed("nested_subpackage_test.go")
- Expect(testsuite).To(Equal(goldmaster))
- })
- })
-
- It("creates ginkgo test suites for all nested packages", func() {
- withTempDir(func(dir string) {
- runGinkgoConvert()
-
- testsuite := readConvertedFileNamed(dir, "nested", "nested_suite_test.go")
- goldmaster := readGoldMasterNamed("nested_suite_test.go")
- Expect(testsuite).To(Equal(goldmaster))
- })
- })
- })
-
- It("gracefully handles existing test suite files", func() {
- withTempDir(func(dir string) {
- cwd, err := os.Getwd()
- bytes, err := ioutil.ReadFile(filepath.Join(cwd, "convert_goldmasters", "fixtures_suite_test.go"))
- Expect(err).NotTo(HaveOccurred())
- err = ioutil.WriteFile(filepath.Join(cwd, "tmp", "tmp_suite_test.go"), bytes, 0600)
- Expect(err).NotTo(HaveOccurred())
-
- runGinkgoConvert()
- })
- })
- })
-}
-
-func withTempDir(cb func(tempDir string)) {
- cwd, err := os.Getwd()
- Expect(err).NotTo(HaveOccurred())
-
- tempDir := filepath.Join(cwd, "tmp")
- err = os.MkdirAll(tempDir, os.ModeDir|os.ModeTemporary|os.ModePerm)
- Expect(err).NotTo(HaveOccurred())
-
- copyFixturesIntoTempDir("convert_fixtures", tempDir)
-
- cb(tempDir)
-}
-
-func copyFixturesIntoTempDir(relativePathToFixtures, tempDir string) {
- _, err := os.Stat(relativePathToFixtures)
- if err != nil {
- os.Mkdir(relativePathToFixtures, os.ModeDir|os.ModeTemporary|os.ModePerm)
- }
-
- cwd, err := os.Getwd()
- Expect(err).NotTo(HaveOccurred())
-
- files, err := ioutil.ReadDir(filepath.Join(cwd, relativePathToFixtures))
- Expect(err).NotTo(HaveOccurred())
-
- for _, f := range files {
- if f.IsDir() {
- nestedFixturesDir := filepath.Join(relativePathToFixtures, f.Name())
- nestedTempDir := filepath.Join(tempDir, f.Name())
- err = os.MkdirAll(nestedTempDir, os.ModeDir|os.ModeTemporary|os.ModePerm)
- Expect(err).NotTo(HaveOccurred())
-
- copyFixturesIntoTempDir(nestedFixturesDir, nestedTempDir)
- continue
- }
-
- bytes, err := ioutil.ReadFile(filepath.Join(cwd, relativePathToFixtures, f.Name()))
- Expect(err).NotTo(HaveOccurred())
-
- err = ioutil.WriteFile(filepath.Join(tempDir, f.Name()), bytes, 0600)
- Expect(err).NotTo(HaveOccurred())
- }
-}
-
-func runGinkgoConvert() {
- cwd, err := os.Getwd()
- Expect(err).NotTo(HaveOccurred())
-
- pathToExecutable := filepath.Join(cwd, "tmp", "ginkgo")
- err = exec.Command(pathToExecutable, "convert", "github.com/onsi/ginkgo/tmp").Run()
- Expect(err).NotTo(HaveOccurred())
-}
-
-func readGoldMasterNamed(filename string) string {
- cwd, err := os.Getwd()
- Expect(err).NotTo(HaveOccurred())
-
- bytes, err := ioutil.ReadFile(filepath.Join(cwd, "convert_goldmasters", filename))
- Expect(err).NotTo(HaveOccurred())
-
- return string(bytes)
-}
-
-func readConvertedFileNamed(pathComponents ...string) string {
- pathToFile := filepath.Join(pathComponents...)
- bytes, err := ioutil.ReadFile(pathToFile)
- Expect(err).NotTo(HaveOccurred())
-
- return string(bytes)
-}
-
-func deleteTmpFiles() {
- cwd, err := os.Getwd()
- Expect(err).NotTo(HaveOccurred())
- tempDir := filepath.Join(cwd, "tmp")
-
- err = os.RemoveAll(tempDir)
- Expect(err).NotTo(HaveOccurred())
-}
-
-func buildGinkgo() {
- cwd, err := os.Getwd()
- Expect(err).NotTo(HaveOccurred())
-
- defer func() {
- os.Chdir(cwd)
- }()
-
- err = os.Chdir(filepath.Join(cwd, "ginkgo"))
- Expect(err).NotTo(HaveOccurred())
-
- err = exec.Command("go", "build", "-o", "../tmp/ginkgo").Run()
- Expect(err).NotTo(HaveOccurred())
-}
0  convert_fixtures/extra_functions_test.go → ...fixtures/convert_fixtures/extra_functions_test.go
View
File renamed without changes
0  convert_fixtures/ginkgo_test_goldmaster.go → ...xtures/convert_fixtures/ginkgo_test_goldmaster.go
View
File renamed without changes
0  convert_fixtures/nested/nested_test.go → .../_fixtures/convert_fixtures/nested/nested_test.go
View
File renamed without changes
0  ...hout_gofiles/subpackage/nested_subpackage_test.go → ...hout_gofiles/subpackage/nested_subpackage_test.go
View
File renamed without changes
0  convert_fixtures/outside_package_test.go → ...fixtures/convert_fixtures/outside_package_test.go
View
File renamed without changes
0  convert_fixtures/xunit_test.go → ...egration/_fixtures/convert_fixtures/xunit_test.go
View
File renamed without changes
0  convert_goldmasters/extra_functions_test.go → ...tures/convert_goldmasters/extra_functions_test.go
View
File renamed without changes
0  convert_goldmasters/fixtures_suite_test.go → ...xtures/convert_goldmasters/fixtures_suite_test.go
View
File renamed without changes
0  convert_goldmasters/nested_subpackage_test.go → ...res/convert_goldmasters/nested_subpackage_test.go
View
File renamed without changes
0  convert_goldmasters/nested_suite_test.go → ...fixtures/convert_goldmasters/nested_suite_test.go
View
File renamed without changes
0  convert_goldmasters/nested_test.go → ...tion/_fixtures/convert_goldmasters/nested_test.go
View
File renamed without changes
0  convert_goldmasters/outside_package_test.go → ...tures/convert_goldmasters/outside_package_test.go
View
File renamed without changes
13 ginkgo/integration/_fixtures/convert_goldmasters/suite_test.go
View
@@ -0,0 +1,13 @@
+package convert_fixtures_test
+
+import (
+ . "github.com/onsi/ginkgo"
+ . "github.com/onsi/gomega"
+
+ "testing"
+)
+
+func TestConvert_fixtures(t *testing.T) {
+ RegisterFailHandler(Fail)
+ RunSpecs(t, "Convert_fixtures Suite")
+}
0  convert_goldmasters/xunit_test.go → ...ation/_fixtures/convert_goldmasters/xunit_test.go
View
File renamed without changes
111 ginkgo/integration/convert_test.go
View
@@ -0,0 +1,111 @@
+package integration_test
+
+import (
+ . "github.com/onsi/ginkgo"
+ . "github.com/onsi/gomega"
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "path/filepath"
+)
+
+var _ = Describe("ginkgo convert", func() {
+ var tmpDir string
+
+ readConvertedFileNamed := func(pathComponents ...string) string {
+ pathToFile := filepath.Join(tmpDir, "convert_fixtures", filepath.Join(pathComponents...))
+ bytes, err := ioutil.ReadFile(pathToFile)
+ ExpectWithOffset(1, err).NotTo(HaveOccurred())
+
+ return string(bytes)
+ }
+
+ readGoldMasterNamed := func(filename string) string {
+ bytes, err := ioutil.ReadFile(filepath.Join("_fixtures", "convert_goldmasters", filename))
+ Ω(err).ShouldNot(HaveOccurred())
+
+ return string(bytes)
+ }
+
+ BeforeEach(func() {
+ var err error
+
+ tmpDir, err = ioutil.TempDir("", "ginkgo-convert")
+ Ω(err).ShouldNot(HaveOccurred())
+
+ err = exec.Command("cp", "-r", filepath.Join("_fixtures", "convert_fixtures"), tmpDir).Run()
+ Ω(err).ShouldNot(HaveOccurred())
+ })
+
+ JustBeforeEach(func() {
+ cwd, err := os.Getwd()
+ Ω(err).ShouldNot(HaveOccurred())
+
+ relPath, err := filepath.Rel(cwd, filepath.Join(tmpDir, "convert_fixtures"))
+ Ω(err).ShouldNot(HaveOccurred())
+
+ err = exec.Command("ginkgo", "convert", relPath).Run()
+ Ω(err).ShouldNot(HaveOccurred())
+ })
+
+ AfterEach(func() {
+ err := os.RemoveAll(tmpDir)
+ Ω(err).ShouldNot(HaveOccurred())
+ })
+
+ It("rewrites xunit tests as ginkgo tests", func() {
+ convertedFile := readConvertedFileNamed("xunit_test.go")
+ goldMaster := readGoldMasterNamed("xunit_test.go")
+ Ω(convertedFile).Should(Equal(goldMaster))
+ })
+
+ It("rewrites all usages of *testing.T as mr.T()", func() {
+ convertedFile := readConvertedFileNamed("extra_functions_test.go")
+ goldMaster := readGoldMasterNamed("extra_functions_test.go")
+ Ω(convertedFile).Should(Equal(goldMaster))
+ })
+
+ It("rewrites tests in the package dir that belong to other packages", func() {
+ convertedFile := readConvertedFileNamed("outside_package_test.go")
+ goldMaster := readGoldMasterNamed("outside_package_test.go")
+ Ω(convertedFile).Should(Equal(goldMaster))
+ })
+
+ It("rewrites tests in nested packages", func() {
+ convertedFile := readConvertedFileNamed("nested", "nested_test.go")
+ goldMaster := readGoldMasterNamed("nested_test.go")
+ Ω(convertedFile).Should(Equal(goldMaster))
+ })
+
+ Context("ginkgo test suite files", func() {
+ It("creates a ginkgo test suite file for the package you specified", func() {
+ testsuite := readConvertedFileNamed("convert_fixtures_suite_test.go")
+ goldMaster := readGoldMasterNamed("suite_test.go")
+ Ω(testsuite).Should(Equal(goldMaster))
+ })
+
+ It("converts go tests in deeply nested packages (some may not contain go files)", func() {
+ testsuite := readConvertedFileNamed("nested_without_gofiles", "subpackage", "nested_subpackage_test.go")
+ goldMaster := readGoldMasterNamed("nested_subpackage_test.go")
+ Ω(testsuite).Should(Equal(goldMaster))
+ })
+
+ It("creates ginkgo test suites for all nested packages", func() {
+ testsuite := readConvertedFileNamed("nested", "nested_suite_test.go")
+ goldMaster := readGoldMasterNamed("nested_suite_test.go")
+ Ω(testsuite).Should(Equal(goldMaster))
+ })
+ })
+
+ Context("with an existing test suite file", func() {
+ BeforeEach(func() {
+ goldMaster := readGoldMasterNamed("fixtures_suite_test.go")
+ err := ioutil.WriteFile(filepath.Join(tmpDir, "convert_fixtures", "tmp_suite_test.go"), []byte(goldMaster), 0600)
+ Ω(err).ShouldNot(HaveOccurred())
+ })
+
+ It("gracefully handles existing test suite files", func() {
+ //nothing should have gone wrong!
+ })
+ })
+})
22 ginkgo/integration/integration_suite_test.go
View
@@ -0,0 +1,22 @@
+package integration_test
+
+import (
+ "fmt"
+ . "github.com/onsi/ginkgo"
+ . "github.com/onsi/gomega"
+ "os/exec"
+
+ "testing"
+)
+
+func TestIntegration(t *testing.T) {
+ RegisterFailHandler(Fail)
+
+ installGinkgoCommand := exec.Command("go", "install", "github.com/onsi/ginkgo/ginkgo")
+ err := installGinkgoCommand.Run()
+ if err != nil {
+ fmt.Printf("Failed to compile Ginkgo\n\t%s", err.Error())
+ }
+
+ RunSpecs(t, "Integration Suite")
+}
Please sign in to comment.
Something went wrong with that request. Please try again.