From 1057ab70ee929c65957907ea8408770035a32aa6 Mon Sep 17 00:00:00 2001 From: Forest Eckhardt Date: Tue, 24 Mar 2020 16:40:34 -0400 Subject: [PATCH] Fixes tests that modified test data Signed-off-by: Ryan Moran --- cargo/directory_duplicator_test.go | 90 +++++++++++++++++++----------- cargo/jam/pack_test.go | 2 - 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/cargo/directory_duplicator_test.go b/cargo/directory_duplicator_test.go index df7e96ce..f8b0be6e 100644 --- a/cargo/directory_duplicator_test.go +++ b/cargo/directory_duplicator_test.go @@ -15,65 +15,88 @@ 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:"))) }) }) @@ -81,31 +104,34 @@ func testDirectoryDuplicator(t *testing.T, context spec.G, it spec.S) { 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"))) }) diff --git a/cargo/jam/pack_test.go b/cargo/jam/pack_test.go index 221df84e..a9df394d 100644 --- a/cargo/jam/pack_test.go +++ b/cargo/jam/pack_test.go @@ -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"))