-
Notifications
You must be signed in to change notification settings - Fork 12
/
go_target_manager_test.go
154 lines (121 loc) · 4.8 KB
/
go_target_manager_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
150
151
152
153
154
package gobuild_test
import (
"fmt"
"os"
"path/filepath"
"testing"
gobuild "github.com/paketo-buildpacks/go-build"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testGoTargetManager(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
targetManager gobuild.GoTargetManager
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
targetManager = gobuild.NewGoTargetManager()
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("CleanAndValidate", func() {
context("when the targets contain a *.go", func() {
it.Before(func() {
targetDir := filepath.Join(workingDir, "first")
Expect(os.MkdirAll(targetDir, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(targetDir, "main.go"), nil, 0644)).To(Succeed())
targetDir = filepath.Join(workingDir, "second")
Expect(os.MkdirAll(targetDir, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(targetDir, "main.go"), nil, 0644)).To(Succeed())
})
it("returns a slice of targets that have been cleaned", func() {
targets, err := targetManager.CleanAndValidate([]string{"first", "./second"}, workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal([]string{"./first", "./second"}))
})
})
context("when one of the targets in an absolute path", func() {
it("returns an error", func() {
_, err := targetManager.CleanAndValidate([]string{"/first"}, workingDir)
Expect(err).To(MatchError(ContainSubstring(`failed to determine build targets: "/first" is an absolute path, targets must be relative to the source directory`)))
})
})
context("when one of the targets does not contain a *.go", func() {
it.Before(func() {
targetDir := filepath.Join(workingDir, "first")
Expect(os.MkdirAll(targetDir, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(targetDir, "main.go"), nil, 0644)).To(Succeed())
})
it("returns a slice of targets that have been cleaned", func() {
_, err := targetManager.CleanAndValidate([]string{"first", "./second"}, workingDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("there were no *.go files present in %q", filepath.Join(workingDir, "second")))))
})
})
context("failure cases", func() {
context("when file glob failes", func() {
it("returns an error", func() {
_, err := targetManager.CleanAndValidate([]string{`\`}, `\`)
Expect(err).To(MatchError(ContainSubstring("syntax error in pattern")))
})
})
})
})
context("GenerateDefaults", func() {
context("when there is a *.go file in the workingDir", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "main.go"), nil, 0644)).To(Succeed())
})
it("returns . as the target", func() {
targets, err := targetManager.GenerateDefaults(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal([]string{"."}))
})
})
context("when there are go files nested inside of a ./cmd folder", func() {
it.Before(func() {
targetDir := filepath.Join(workingDir, "cmd", "first")
Expect(os.MkdirAll(targetDir, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(targetDir, "main.go"), nil, 0644)).To(Succeed())
targetDir = filepath.Join(workingDir, "cmd", "something", "second")
Expect(os.MkdirAll(targetDir, os.ModePerm)).To(Succeed())
Expect(os.WriteFile(filepath.Join(targetDir, "main.go"), nil, 0644)).To(Succeed())
})
it("returns a target list of all top level directories in ./cmd that contain *.go files", func() {
targets, err := targetManager.GenerateDefaults(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(targets).To(Equal([]string{"./cmd/first", "./cmd/something/second"}))
})
})
context("when there is no *.go in the app root in ./cmd", func() {
it("returns a target list of all top level directories in ./cmd that contain *.go files", func() {
_, err := targetManager.GenerateDefaults(workingDir)
Expect(err).To(MatchError(ContainSubstring("no *.go files could be found")))
})
})
context("failure cases", func() {
context("when file glob failes", func() {
it("returns an error", func() {
_, err := targetManager.GenerateDefaults(`\`)
Expect(err).To(MatchError(ContainSubstring("syntax error in pattern")))
})
})
context("when the workingDir is unstatable", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := targetManager.GenerateDefaults(workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
}