1package integration_test
2
3import (
4	"io"
5	"io/ioutil"
6	"os"
7	"os/exec"
8	"path/filepath"
9
10	. "github.com/onsi/ginkgo"
11	. "github.com/onsi/gomega"
12	"github.com/onsi/gomega/gexec"
13
14	"testing"
15	"time"
16)
17
18var tmpDir string
19var pathToGinkgo string
20
21func TestIntegration(t *testing.T) {
22	SetDefaultEventuallyTimeout(30 * time.Second)
23	RegisterFailHandler(Fail)
24	RunSpecs(t, "Integration Suite")
25}
26
27var _ = SynchronizedBeforeSuite(func() []byte {
28	pathToGinkgo, err := gexec.Build("github.com/onsi/ginkgo/ginkgo")
29	Ω(err).ShouldNot(HaveOccurred())
30	return []byte(pathToGinkgo)
31}, func(computedPathToGinkgo []byte) {
32	pathToGinkgo = string(computedPathToGinkgo)
33})
34
35var _ = BeforeEach(func() {
36	var err error
37	tmpDir, err = ioutil.TempDir("", "ginkgo-run")
38	Ω(err).ShouldNot(HaveOccurred())
39})
40
41var _ = AfterEach(func() {
42	err := os.RemoveAll(tmpDir)
43	Ω(err).ShouldNot(HaveOccurred())
44})
45
46var _ = SynchronizedAfterSuite(func() {}, func() {
47	gexec.CleanupBuildArtifacts()
48})
49
50func tmpPath(destination string) string {
51	return filepath.Join(tmpDir, destination)
52}
53
54func fixturePath(name string) string {
55	return filepath.Join("_fixtures", name)
56}
57
58func copyIn(sourcePath, destinationPath string, recursive bool) {
59	err := os.MkdirAll(destinationPath, 0777)
60	Expect(err).NotTo(HaveOccurred())
61
62	files, err := ioutil.ReadDir(sourcePath)
63	Expect(err).NotTo(HaveOccurred())
64	for _, f := range files {
65		srcPath := filepath.Join(sourcePath, f.Name())
66		dstPath := filepath.Join(destinationPath, f.Name())
67		if f.IsDir() {
68			if recursive {
69				copyIn(srcPath, dstPath, recursive)
70			}
71			continue
72		}
73
74		src, err := os.Open(srcPath)
75
76		Expect(err).NotTo(HaveOccurred())
77		defer src.Close()
78
79		dst, err := os.Create(dstPath)
80		Expect(err).NotTo(HaveOccurred())
81		defer dst.Close()
82
83		_, err = io.Copy(dst, src)
84		Expect(err).NotTo(HaveOccurred())
85	}
86}
87
88func sameFile(filePath, otherFilePath string) bool {
89	content, readErr := ioutil.ReadFile(filePath)
90	Expect(readErr).NotTo(HaveOccurred())
91	otherContent, readErr := ioutil.ReadFile(otherFilePath)
92	Expect(readErr).NotTo(HaveOccurred())
93	Expect(string(content)).To(Equal(string(otherContent)))
94	return true
95}
96
97func sameFolder(sourcePath, destinationPath string) bool {
98	files, err := ioutil.ReadDir(sourcePath)
99	Expect(err).NotTo(HaveOccurred())
100	for _, f := range files {
101		srcPath := filepath.Join(sourcePath, f.Name())
102		dstPath := filepath.Join(destinationPath, f.Name())
103		if f.IsDir() {
104			sameFolder(srcPath, dstPath)
105			continue
106		}
107		Expect(sameFile(srcPath, dstPath)).To(BeTrue())
108	}
109	return true
110}
111
112func ginkgoCommand(dir string, args ...string) *exec.Cmd {
113	cmd := exec.Command(pathToGinkgo, args...)
114	cmd.Dir = dir
115
116	return cmd
117}
118
119func startGinkgo(dir string, args ...string) *gexec.Session {
120	cmd := ginkgoCommand(dir, args...)
121	session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
122	Ω(err).ShouldNot(HaveOccurred())
123	return session
124}
125
126func removeSuccessfully(path string) {
127	err := os.RemoveAll(path)
128	Expect(err).NotTo(HaveOccurred())
129}
130