forked from dustinblackman/gomodrun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg_test.go
189 lines (158 loc) · 6.1 KB
/
pkg_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package gomodrun_test
import (
"os"
"os/exec"
"path"
"runtime"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/dustinblackman/gomodrun"
)
const testPackage string = "github.com/dustinblackman/[email protected]/hello-world"
const testPackageNoGoMod string = "github.com/dustinblackman/[email protected]/hello-world-no-gomod"
func formatForOS(pkgPath string) string {
if runtime.GOOS == "windows" {
return strings.ReplaceAll(pkgPath, "/", "\\")
}
return pkgPath
}
var _ = Describe("pkg", func() {
cwd, _ := os.Getwd()
goVersionOutput, _ := exec.Command("go", "version").Output()
goVersion := strings.Split(string(goVersionOutput), " ")[2]
Context("GetPkgRoot", func() {
It("should return the current working directory that contains a go.mod", func() {
dir, err := gomodrun.GetPkgRoot()
Expect(err).To(BeNil())
Expect(dir).To(Equal(cwd))
})
It("should return an error when it can not find a go.mod", func() {
err := os.Chdir("../")
if err != nil {
panic(err)
}
dir, err := gomodrun.GetPkgRoot()
Expect(err).ToNot(BeNil())
Expect(dir).To(Equal(""))
err = os.Chdir(cwd)
if err != nil {
panic(err)
}
})
})
Context("GetCommandVersionedPkgPath", func() {
It("should get the binaries binaries command path", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(cwd, "hello-world")
Expect(err).To(BeNil())
Expect(cmdPath).To(Equal(testPackage))
})
It("should get the binaries binaries command path with suffix includes .exe", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(cwd, "hello-world.exe")
Expect(err).To(BeNil())
Expect(cmdPath).To(Equal(testPackage))
})
It("should throw an error when it cant find tools imports", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "../../"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(cmdPath).To(Equal(""))
})
It("should throw an error when it cant find specified bin in imports", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(cwd, "not-real")
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), "cant find bin in tools file")).To(BeTrue())
Expect(cmdPath).To(Equal(""))
})
It("should throw an error when go.mod cant be found", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "./tests/missing-go-mod"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(cmdPath).To(Equal(""))
})
It("should throw an error when go.mod is corrupted", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "./tests/corrupted-go-mod"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(cmdPath).To(Equal(""))
})
It("should throw an error when go.mod is missing the requested dependency", func() {
cmdPath, err := gomodrun.GetCommandVersionedPkgPath(path.Join(cwd, "./tests/incomplete-go-mod"), "hello-world")
Expect(err).ToNot(BeNil())
Expect(strings.Contains(err.Error(), "cant find require")).To(BeTrue())
Expect(cmdPath).To(Equal(""))
})
})
Context("GetCachedBin", func() {
Context("with go.mod", func() {
It("should return the bin path when it does not exist in cache", func() {
err := os.RemoveAll(path.Join(".gomodrun", goVersion, "github.com/dustinblackman"))
if err != nil {
panic(err)
}
binPath, err := gomodrun.GetCachedBin(cwd, "hello-world", testPackage)
Expect(err).To(BeNil())
Expect(binPath).To(ContainSubstring(formatForOS(testPackage)))
Expect(binPath).To(ContainSubstring(".gomodrun"))
})
It("should return the bin path when it exists in cache", func() {
binPath, err := gomodrun.GetCachedBin(cwd, "hello-world", testPackage)
Expect(err).To(BeNil())
Expect(binPath).To(ContainSubstring(formatForOS(testPackage)))
Expect(binPath).To(ContainSubstring(".gomodrun"))
})
})
Context("without go.mod", func() {
It("should return the bin path when it does not exist in cache", func() {
err := os.RemoveAll(path.Join(".gomodrun", goVersion, "github.com/dustinblackman"))
if err != nil {
panic(err)
}
binPath, err := gomodrun.GetCachedBin(cwd, "hello-world-no-gomod", testPackageNoGoMod)
Expect(err).To(BeNil())
Expect(binPath).To(ContainSubstring(formatForOS(testPackageNoGoMod)))
Expect(binPath).To(ContainSubstring(".gomodrun"))
})
It("should return the bin path when it exists in cache", func() {
binPath, err := gomodrun.GetCachedBin(cwd, "hello-world-no-gomod", testPackageNoGoMod)
Expect(err).To(BeNil())
Expect(binPath).To(ContainSubstring(formatForOS(testPackageNoGoMod)))
Expect(binPath).To(ContainSubstring(".gomodrun"))
})
})
})
Context("Run", func() {
It("should return exit code 0 when binary exits with 0", func() {
exitCode, err := gomodrun.Run("hello-world", []string{}, &gomodrun.Options{})
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(0))
})
It("should return exit code 1 when the binary exists with 1", func() {
exitCode, err := gomodrun.Run("hello-world", []string{"1"}, &gomodrun.Options{})
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(1))
})
It("should return an error when the binary does not exist", func() {
exitCode, err := gomodrun.Run("not-real", []string{}, &gomodrun.Options{})
Expect(err).ToNot(BeNil())
Expect(exitCode).To(Equal(-1))
})
Context("Alternative tools directory", func() {
options := &gomodrun.Options{
PkgRoot: path.Join(cwd, "./tests/alternative-tools-dir"),
}
It("should return exit code 0 when binary exits with 0", func() {
exitCode, err := gomodrun.Run("hello-world", []string{}, options)
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(0))
})
It("should return exit code 1 when the binary exists with 1", func() {
exitCode, err := gomodrun.Run("hello-world", []string{"1"}, options)
Expect(err).To(BeNil())
Expect(exitCode).To(Equal(1))
})
It("should return an error when the binary does not exist", func() {
exitCode, err := gomodrun.Run("not-real", []string{}, options)
Expect(err).ToNot(BeNil())
Expect(exitCode).To(Equal(-1))
})
})
})
})