Skip to content

Commit

Permalink
Fixes tests that modified test data
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Moran <rmoran@pivotal.io>
  • Loading branch information
ForestEckhardt authored and Ryan Moran committed Mar 24, 2020
1 parent a0ce524 commit 1057ab7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
90 changes: 58 additions & 32 deletions cargo/directory_duplicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,97 +15,123 @@ import (
func testDirectoryDuplicator(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
tmpDir string
destDir string
sourceDir string
directoryDup cargo.DirectoryDuplicator
)

it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "dup-dest")

sourceDir, err = ioutil.TempDir("", "source")
Expect(err).NotTo(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(sourceDir, "some-file"), []byte("some content"), 0644)).To(Succeed())

Expect(os.MkdirAll(filepath.Join(sourceDir, "some-dir"), os.ModePerm)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(sourceDir, "some-dir", "other-file"), []byte("other content"), 0755)).To(Succeed())

destDir, err = ioutil.TempDir("", "dest")
Expect(err).NotTo(HaveOccurred())

directoryDup = cargo.NewDirectoryDuplicator()
})

it.After(func() {
Expect(os.RemoveAll(sourceDir)).To(Succeed())
Expect(os.RemoveAll(destDir)).To(Succeed())
})

context("Duplicate", func() {
it("duplicates the contents of a directory", func() {
Expect(directoryDup.Duplicate(filepath.Join("jam", "testdata", "example-cnb"), tmpDir)).To(Succeed())
Expect(directoryDup.Duplicate(sourceDir, destDir)).To(Succeed())

var files []string
err := filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
file, err := os.Open(filepath.Join(destDir, "some-file"))
Expect(err).NotTo(HaveOccurred())

if !info.IsDir() {
files = append(files, path)
content, err := ioutil.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some content"))

rel, err := filepath.Rel(tmpDir, path)
Expect(err).NotTo(HaveOccurred())
info, err := file.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0644)))

original, err := os.Stat(filepath.Join("jam", "testdata", "example-cnb", rel))
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(original.Mode()))
}
Expect(file.Close()).To(Succeed())

return nil
})
info, err = os.Stat(filepath.Join(destDir, "some-dir"))
Expect(err).NotTo(HaveOccurred())
Expect(files).To(HaveLen(4))
Expect(info.IsDir()).To(BeTrue())

file, err = os.Open(filepath.Join(destDir, "some-dir", "other-file"))
Expect(err).NotTo(HaveOccurred())

content, err = ioutil.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("other content"))

info, err = file.Stat()
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode()).To(Equal(os.FileMode(0755)))

Expect(file.Close()).To(Succeed())
})
})
context("Error cases", func() {

context("failure cases", func() {
context("source dir does not exist", func() {
it("fails", func() {
err := directoryDup.Duplicate(filepath.Join("jam", "testdata", "does-not-exits"), tmpDir)
err := directoryDup.Duplicate("does-not-exist", destDir)
Expect(err).To(MatchError(ContainSubstring("source dir does not exist: ")))
})
})

context("when source file has bad permissions", func() {
it.Before(func() {
Expect(os.Chmod(filepath.Join("jam", "testdata", "example-cnb", "buildpack.toml"), 0000)).To(Succeed())
Expect(os.Chmod(filepath.Join(sourceDir, "some-file"), 0000)).To(Succeed())
})

it.After(func() {
Expect(os.Chmod(filepath.Join("jam", "testdata", "example-cnb", "buildpack.toml"), 0644)).To(Succeed())
Expect(os.Chmod(filepath.Join(sourceDir, "some-file"), 0644)).To(Succeed())
})

it("fails", func() {
err := directoryDup.Duplicate(filepath.Join("jam", "testdata", "example-cnb"), tmpDir)
err := directoryDup.Duplicate(sourceDir, destDir)
Expect(err).To(MatchError(ContainSubstring("opening source file failed:")))
})
})

context("when destination directory bad permissions", func() {
context("when creating dir", func() {
it.Before(func() {
os.Chmod(tmpDir, 0000)
Expect(os.Chmod(destDir, 0000)).To(Succeed())
})

it.After(func() {
os.Chmod(tmpDir, os.ModePerm)
Expect(os.Chmod(destDir, os.ModePerm)).To(Succeed())
})

it("fails", func() {
err := directoryDup.Duplicate(filepath.Join("jam", "testdata", "example-cnb"), tmpDir)
err := directoryDup.Duplicate(sourceDir, destDir)
Expect(err).To(MatchError(ContainSubstring("duplicate error creating dir:")))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})

context("when creating file", func() {
var binPath string
var dirPath string

it.Before(func() {
binPath = filepath.Join(tmpDir, "bin")
os.MkdirAll(binPath, 0000)
dirPath = filepath.Join(destDir, "some-dir")
Expect(os.MkdirAll(dirPath, 0000)).To(Succeed())
})

it.After(func() {
os.Chmod(binPath, os.ModePerm)
Expect(os.Chmod(dirPath, os.ModePerm)).To(Succeed())
})

it("fails", func() {
err := directoryDup.Duplicate(filepath.Join("jam", "testdata", "example-cnb"), tmpDir)
err := directoryDup.Duplicate(sourceDir, destDir)
Expect(err).To(MatchError(ContainSubstring("duplicate error creating file:")))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
Expand Down
2 changes: 0 additions & 2 deletions cargo/jam/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ func testPack(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0), func() string { return buffer.String() })

fmt.Printf("session ->\n%s\n", session.Out.Contents())

Expect(session.Out).To(gbytes.Say("Packing some-buildpack-name some-version..."))
Expect(session.Out).To(gbytes.Say(" Executing pre-packaging script: ./scripts/build.sh"))
Expect(session.Out).To(gbytes.Say(" hello from the pre-packaging script"))
Expand Down

0 comments on commit 1057ab7

Please sign in to comment.