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