Skip to content

Commit a3895c7

Browse files
committed
Specs: use By instead of comments; extract helper funcs to shared location
1 parent 10c1ef3 commit a3895c7

File tree

6 files changed

+185
-239
lines changed

6 files changed

+185
-239
lines changed

integration/create_release_test.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"regexp"
87
"strings"
98

109
"github.com/cloudfoundry/bosh-utils/uuid"
@@ -15,18 +14,13 @@ import (
1514
boshrelman "github.com/cloudfoundry/bosh-cli/v7/release/manifest"
1615
)
1716

18-
var _ = Describe("create-release command", func() {
19-
removeSHA256s := func(contents string) string {
20-
matchSHA256s := regexp.MustCompile("sha1: sha256:[a-z0-9]{64}\n")
21-
return matchSHA256s.ReplaceAllString(contents, "sha1: replaced\n")
22-
}
23-
24-
expectSha256Checksums := func(filePath string) {
25-
contents, err := fs.ReadFileString(filePath)
26-
Expect(err).ToNot(HaveOccurred())
27-
Expect(contents).To(MatchRegexp("sha1: sha256:.*"))
28-
}
17+
func expectSha256Checksums(filePath string) {
18+
contents, err := fs.ReadFileString(filePath)
19+
Expect(err).ToNot(HaveOccurred())
20+
Expect(contents).To(MatchRegexp("sha1: sha256:.*"))
21+
}
2922

23+
var _ = Describe("create-release command", func() {
3024
It("can iterate on a basic release", func() {
3125
suffix, err := uuid.NewGenerator().Generate()
3226
Expect(err).ToNot(HaveOccurred())
@@ -50,42 +44,47 @@ var _ = Describe("create-release command", func() {
5044

5145
relName := filepath.Base(tmpDir)
5246

53-
{
47+
By("running `init-release`", func() {
5448
createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir})
5549
Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue())
5650
Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue())
5751
Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue())
5852
Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue())
59-
}
53+
})
54+
55+
By("running `generate-job`", func() {
56+
createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir})
57+
})
6058

61-
createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir})
62-
createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir})
63-
createAndExecCommand(cmdFactory, []string{"generate-package", "pkg2", "--dir", tmpDir})
59+
By("running `generate-package` twice", func() {
60+
createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir})
61+
createAndExecCommand(cmdFactory, []string{"generate-package", "pkg2", "--dir", tmpDir})
62+
})
6463

6564
err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE")
6665
Expect(err).ToNot(HaveOccurred())
6766

68-
{ // pkg1 depends on pkg2 for compilation
67+
By("making one package depend on another", func() {
6968
pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec")
7069

7170
contents, err := fs.ReadFileString(pkg1SpecPath)
7271
Expect(err).ToNot(HaveOccurred())
7372

7473
err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: [pkg2]", -1))
7574
Expect(err).ToNot(HaveOccurred())
76-
}
75+
})
7776

78-
{ // job1 depends on both packages
77+
By("making a job depend on two packages", func() {
7978
jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec")
8079

8180
contents, err := fs.ReadFileString(jobSpecPath)
8281
Expect(err).ToNot(HaveOccurred())
8382

8483
err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1, pkg2]", -1))
8584
Expect(err).ToNot(HaveOccurred())
86-
}
85+
})
8786

88-
{ // Make empty release
87+
By("using `create-release` to make an empty release", func() {
8988
createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir})
9089

9190
contents, err := fs.ReadFileString(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml"))
@@ -121,9 +120,9 @@ license:
121120
fingerprint: 42a33a7295936a632c8f54e70f2553975ee38a476d6aae93f3676e68c9db2f86
122121
sha1: replaced
123122
`))
124-
}
123+
})
125124

126-
{ // Add a bit of content
125+
By("adding a file under `src/`", func() {
127126
err := fs.WriteFileString(filepath.Join(tmpDir, "src", "in-src"), "in-src")
128127
Expect(err).ToNot(HaveOccurred())
129128

@@ -141,9 +140,9 @@ license:
141140

142141
err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "files: []", "files:\n- in-src\n- in-blobs", -1))
143142
Expect(err).ToNot(HaveOccurred())
144-
}
143+
})
145144

146-
{ // Make release with some contents
145+
By("running `create-release` to make a release with some content", func() {
147146
createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir})
148147

149148
rel1File := filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml")
@@ -197,18 +196,18 @@ license:
197196
// and pkg2 did not change
198197
Expect(man1.Packages[1].Name).To(Equal(man2.Packages[1].Name))
199198
Expect(man1.Packages[1].Fingerprint).To(Equal(man2.Packages[1].Fingerprint))
200-
}
199+
})
201200

202-
{ // check contents of index files when sha2 flag is supplied
201+
By("running `create-release` with `--sha2`", func() {
203202
createAndExecCommand(cmdFactory, []string{"create-release", "--sha2", "--dir", tmpDir})
204203

205204
expectSha256Checksums(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.3.yml"))
206205
expectSha256Checksums(filepath.Join(tmpDir, ".dev_builds", "jobs", "job1", "index.yml"))
207206
expectSha256Checksums(filepath.Join(tmpDir, ".dev_builds", "packages", "pkg1", "index.yml"))
208207
expectSha256Checksums(filepath.Join(tmpDir, ".dev_builds", "license", "index.yml"))
209-
}
208+
})
210209

211-
{ // Check contents of made release via its tarball
210+
By("running `create-release` with `--tarball`", func() {
212211
tgzFile := filepath.Join(tmpDir, "release-3.tgz")
213212

214213
createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir, "--tarball", tgzFile})
@@ -233,17 +232,17 @@ license:
233232

234233
job1 := release.Jobs()[0]
235234
Expect(job1.PackageNames).To(ConsistOf("pkg1", "pkg2"))
236-
}
235+
})
237236

238-
{ // Check that tarballs will not overwrite a directory
237+
By("running `create-release` with `--tarball` which points at an existing directory", func() {
239238
directoryPath := filepath.Join(tmpDir, "tarball-collision-dir")
240239
Expect(fs.MkdirAll(directoryPath, os.ModeDir)).To(Succeed())
241240
_, err := cmdFactory.New([]string{"create-release", "--dir", tmpDir, "--tarball", directoryPath})
242241
Expect(err).To(HaveOccurred())
243242
Expect(err.Error()).To(ContainSubstring("Path must not be directory"))
244-
}
243+
})
245244

246-
{ // removes unknown blobs, keeping known blobs
245+
By("running `create-release` unknown blobs will be removed from the `blobs/` dir", func() {
247246
blobPath := filepath.Join(tmpDir, "blobs", "unknown-blob.tgz")
248247

249248
err := fs.WriteFileString(blobPath, "i don't belong here")
@@ -252,6 +251,6 @@ license:
252251
createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir})
253252
Expect(fs.FileExists(blobPath)).To(BeFalse())
254253
Expect(fs.FileExists(filepath.Join(tmpDir, "blobs", "in-blobs"))).To(BeTrue())
255-
}
254+
})
256255
})
257256
})

integration/sha1ify_release_test.go

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package integration_test
22

33
import (
44
"path/filepath"
5-
"strings"
65

76
. "github.com/onsi/ginkgo/v2"
87
. "github.com/onsi/gomega"
@@ -11,11 +10,7 @@ import (
1110
)
1211

1312
var _ = Describe("sha1ify-release", func() {
14-
15-
var (
16-
releaseProvider boshrel.Provider
17-
createSimpleRelease func() string
18-
)
13+
var releaseProvider boshrel.Provider
1914

2015
BeforeEach(func() {
2116
releaseProvider =
@@ -83,56 +78,4 @@ var _ = Describe("sha1ify-release", func() {
8378
By("preserving the version string exactly")
8479
Expect(release.Version()).To(Equal("0+dev.3"))
8580
})
86-
87-
createSimpleRelease = func() string {
88-
tmpDir, err := fs.TempDir("bosh-create-release-int-test")
89-
Expect(err).ToNot(HaveOccurred())
90-
91-
relName := filepath.Base(tmpDir)
92-
93-
{
94-
createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir})
95-
Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue())
96-
Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue())
97-
Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue())
98-
Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue())
99-
}
100-
101-
createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir})
102-
createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir})
103-
104-
err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE")
105-
Expect(err).ToNot(HaveOccurred())
106-
107-
{
108-
pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec")
109-
110-
contents, err := fs.ReadFileString(pkg1SpecPath)
111-
Expect(err).ToNot(HaveOccurred())
112-
113-
err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: []", -1))
114-
Expect(err).ToNot(HaveOccurred())
115-
}
116-
117-
{
118-
jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec")
119-
120-
contents, err := fs.ReadFileString(jobSpecPath)
121-
Expect(err).ToNot(HaveOccurred())
122-
123-
err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1))
124-
Expect(err).ToNot(HaveOccurred())
125-
}
126-
127-
sha2ifyReleasePath := filepath.Join(tmpDir, "sha2ify-release.tgz")
128-
129-
{ // Make empty release
130-
createAndExecCommand(cmdFactory, []string{"create-release", "--sha2", "--dir", tmpDir, "--tarball", sha2ifyReleasePath})
131-
132-
_, err := fs.ReadFileString(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml"))
133-
Expect(err).ToNot(HaveOccurred())
134-
}
135-
136-
return sha2ifyReleasePath
137-
}
13881
})

integration/sha2ify_release_test.go

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package integration_test
22

33
import (
44
"path/filepath"
5-
"regexp"
6-
"strings"
75

86
. "github.com/onsi/ginkgo/v2"
97
. "github.com/onsi/gomega"
@@ -12,17 +10,11 @@ import (
1210
)
1311

1412
var _ = Describe("sha2ify-release", func() {
15-
16-
var (
17-
releaseProvider boshrel.Provider
18-
createSimpleRelease func() string
19-
removeSHA1s func(string) string
20-
)
13+
var releaseProvider boshrel.Provider
2114

2215
BeforeEach(func() {
2316
releaseProvider =
2417
boshrel.NewProvider(deps.CmdRunner, deps.Compressor, deps.DigestCalculator, deps.FS, deps.Logger)
25-
2618
})
2719

2820
It("converts the SHA1s into SHA2s for packages and jobs", func() {
@@ -86,61 +78,4 @@ var _ = Describe("sha2ify-release", func() {
8678
By("preserving the version string exactly")
8779
Expect(release.Version()).To(Equal("0+dev.3"))
8880
})
89-
90-
removeSHA1s = func(contents string) string {
91-
matchSHA1s := regexp.MustCompile("sha256:[a-z0-9]{64}")
92-
return matchSHA1s.ReplaceAllString(contents, "sha256:replaced")
93-
}
94-
95-
createSimpleRelease = func() string {
96-
tmpDir, err := fs.TempDir("bosh-create-release-int-test")
97-
Expect(err).ToNot(HaveOccurred())
98-
99-
relName := filepath.Base(tmpDir)
100-
101-
{
102-
createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir})
103-
Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue())
104-
Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue())
105-
Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue())
106-
Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue())
107-
}
108-
109-
createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir})
110-
createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir})
111-
112-
err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE")
113-
Expect(err).ToNot(HaveOccurred())
114-
115-
{
116-
pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec")
117-
118-
contents, err := fs.ReadFileString(pkg1SpecPath)
119-
Expect(err).ToNot(HaveOccurred())
120-
121-
err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: []", -1))
122-
Expect(err).ToNot(HaveOccurred())
123-
}
124-
125-
{
126-
jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec")
127-
128-
contents, err := fs.ReadFileString(jobSpecPath)
129-
Expect(err).ToNot(HaveOccurred())
130-
131-
err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1))
132-
Expect(err).ToNot(HaveOccurred())
133-
}
134-
135-
sha2ifyReleasePath := filepath.Join(tmpDir, "sha2ify-release.tgz")
136-
137-
{ // Make empty release
138-
createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir, "--tarball", sha2ifyReleasePath})
139-
140-
_, err := fs.ReadFileString(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml"))
141-
Expect(err).ToNot(HaveOccurred())
142-
}
143-
144-
return sha2ifyReleasePath
145-
}
14681
})

0 commit comments

Comments
 (0)