-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_test.go
149 lines (125 loc) · 5.31 KB
/
run_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main_test
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testRun(t *testing.T, context spec.G, it spec.S) {
var (
withT = NewWithT(t)
Expect = withT.Expect
Eventually = withT.Eventually
outputPath string
templatePath string
buffer *bytes.Buffer
)
it.Before(func() {
var err error
outputPath, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
templatePath, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(templatePath, "go.mod"), []byte("module github.com/test/test"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
buffer = bytes.NewBuffer(nil)
})
it.After(func() {
Expect(os.RemoveAll(outputPath)).To(Succeed())
Expect(os.RemoveAll(templatePath)).To(Succeed())
})
context("bootstrapping a buildpack from default template", func() {
it("creates a buildpack with runnable tests and scripts in the output directory", func() {
command := exec.Command(
path, "run",
"--buildpack-name", "some-org/some-buildpack",
"--output", outputPath,
)
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0), func() string { return buffer.String() })
Expect(session.Out).To(gbytes.Say("Bootstrapping some-org/some-buildpack buildpack from template at template-cnb"))
Expect(session.Out).To(gbytes.Say(fmt.Sprintf("Success. Buildpack available at %s", outputPath)))
Expect(outputPath).To(BeADirectory())
b, err := os.ReadFile(filepath.Join(outputPath, "go.mod"))
Expect(err).NotTo(HaveOccurred())
Eventually(string(b)).Should(ContainSubstring("module github.com/some-org/some-buildpack"))
unitCmd := exec.Command(filepath.Join(outputPath, "scripts", "unit.sh"))
unitCmd.Dir = outputPath
unitBuffer := gbytes.NewBuffer()
unitSession, err := gexec.Start(unitCmd, unitBuffer, unitBuffer)
Expect(err).NotTo(HaveOccurred())
Eventually(unitSession).Should(gexec.Exit(0), func() string { return fmt.Sprintf("output:\n%s\n", unitBuffer.Contents()) })
integrationCmd := exec.Command(filepath.Join(outputPath, "scripts", "integration.sh"))
integrationCmd.Dir = outputPath
integrationBuffer := gbytes.NewBuffer()
integrationSession, err := gexec.Start(integrationCmd, integrationBuffer, integrationBuffer)
Expect(err).NotTo(HaveOccurred())
Eventually(integrationSession).Should(gexec.Exit(1), string(integrationBuffer.Contents()))
Expect(string(integrationBuffer.Contents())).To(ContainSubstring("Not Implemented"))
})
})
context("with a custom template that contains a go.mod", func() {
it("creates a custom-template based buildpack in the output directory", func() {
command := exec.Command(
path, "run",
"--buildpack-name", "some-org/someBuildpack",
"--output", outputPath,
"--template", templatePath,
)
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0), func() string { return buffer.String() })
Expect(session.Out).To(gbytes.Say(fmt.Sprintf("Bootstrapping some-org/someBuildpack buildpack from template at %s", templatePath)))
Expect(session.Out).To(gbytes.Say(fmt.Sprintf("Success. Buildpack available at %s", outputPath)))
Expect(outputPath).To(BeADirectory())
Eventually(filepath.Join(outputPath, "go.mod")).Should(BeAnExistingFile())
b, err := os.ReadFile(filepath.Join(outputPath, "go.mod"))
Expect(err).NotTo(HaveOccurred())
Eventually(string(b)).Should(ContainSubstring("module github.com/some-org/someBuildpack"))
})
})
context("failure cases", func() {
context("when the all the required flags are not set", func() {
it("prints an error message", func() {
command := exec.Command(path, "run")
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1), func() string { return buffer.String() })
Expect(string(session.Err.Contents())).To(ContainSubstring("failed to execute: required flag(s) \"buildpack-name\", \"output\" not set"))
})
})
context("when the required buildpack-name flag is not set", func() {
it("prints an error message", func() {
command := exec.Command(
path, "run",
"--output", outputPath,
"--template", templatePath,
)
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1), func() string { return buffer.String() })
Expect(string(session.Err.Contents())).To(ContainSubstring("failed to execute: required flag(s) \"buildpack-name\" not set"))
})
})
context("when the required output flag is not set", func() {
it("prints an error message", func() {
command := exec.Command(
path, "run",
"--buildpack-name", "some-org/someBuildpack",
"--template", templatePath,
)
session, err := gexec.Start(command, buffer, buffer)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(1), func() string { return buffer.String() })
Expect(string(session.Err.Contents())).To(ContainSubstring("failed to execute: required flag(s) \"output\" not set"))
})
})
})
}