-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathversion_shimmer_test.go
149 lines (115 loc) · 4.37 KB
/
version_shimmer_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 bundler_test
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/paketo-buildpacks/bundler"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testVersionShimmer(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
versionShimmer bundler.VersionShimmer
dir string
)
it.Before(func() {
dir = t.TempDir()
err := os.WriteFile(filepath.Join(dir, "first"), []byte("first"), 0755)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(dir, "second"), []byte("second"), 0755)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(dir, "third"), []byte("third"), 0644)
Expect(err).NotTo(HaveOccurred())
err = os.Mkdir(filepath.Join(dir, "fourth"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
versionShimmer = bundler.NewVersionShimmer()
})
context("Shim", func() {
it("creates version shims for the executables in the given directory", func() {
err := versionShimmer.Shim(dir, "some-version")
Expect(err).NotTo(HaveOccurred())
files, err := filepath.Glob(filepath.Join(dir, "*"))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(ConsistOf([]string{
filepath.Join(dir, "_first"),
filepath.Join(dir, "_second"),
filepath.Join(dir, "first"),
filepath.Join(dir, "second"),
filepath.Join(dir, "third"),
filepath.Join(dir, "fourth"),
}))
first, err := os.Open(filepath.Join(dir, "_first"))
Expect(err).NotTo(HaveOccurred())
defer first.Close()
content, err := os.ReadFile(first.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("first"))
info, err := first.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
firstShim, err := os.Open(filepath.Join(dir, "first"))
Expect(err).NotTo(HaveOccurred())
defer firstShim.Close()
content, err = os.ReadFile(firstShim.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(fmt.Sprintf("#!/usr/bin/env sh\nexec %s _some-version_ ${@:-}", filepath.Join(dir, "_first"))))
info, err = firstShim.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
second, err := os.Open(filepath.Join(dir, "_second"))
Expect(err).NotTo(HaveOccurred())
defer second.Close()
content, err = os.ReadFile(second.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("second"))
info, err = second.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
secondShim, err := os.Open(filepath.Join(dir, "second"))
Expect(err).NotTo(HaveOccurred())
defer secondShim.Close()
content, err = os.ReadFile(secondShim.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(fmt.Sprintf("#!/usr/bin/env sh\nexec %s _some-version_ ${@:-}", filepath.Join(dir, "_second"))))
info, err = secondShim.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))
third, err := os.Open(filepath.Join(dir, "third"))
Expect(err).NotTo(HaveOccurred())
defer third.Close()
content, err = os.ReadFile(third.Name())
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("third"))
info, err = third.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0644)))
fourth, err := os.Open(filepath.Join(dir, "fourth"))
Expect(err).NotTo(HaveOccurred())
defer fourth.Close()
info, err = fourth.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.ModeDir | os.FileMode(0755)))
Expect(info.IsDir()).To(BeTrue())
})
context("failure cases", func() {
context("when the files in the directory cannot be listed", func() {
it("returns an error", func() {
err := versionShimmer.Shim("[]", "some-version")
Expect(err).To(MatchError("failed to shim bundler executables: syntax error in pattern"))
})
})
context("when the directory cannot be written to", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join(dir, "first"), 0111)).To(Succeed())
})
it("returns an error", func() {
err := versionShimmer.Shim(dir, "some-version")
Expect(err).To(MatchError(ContainSubstring("failed to move bundler executables:")))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
}