github.com/paketo-buildpacks/packit@v1.3.2-0.20211206231111-86b75c657449/internal/environment_writer_test.go (about) 1 package internal_test 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/paketo-buildpacks/packit/internal" 9 "github.com/sclevine/spec" 10 11 . "github.com/onsi/gomega" 12 ) 13 14 func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) { 15 var ( 16 Expect = NewWithT(t).Expect 17 18 tmpDir string 19 writer internal.EnvironmentWriter 20 ) 21 22 it.Before(func() { 23 var err error 24 tmpDir, err = os.MkdirTemp("", "env-vars") 25 Expect(err).NotTo(HaveOccurred()) 26 27 Expect(os.RemoveAll(tmpDir)).To(Succeed()) 28 29 writer = internal.NewEnvironmentWriter() 30 }) 31 32 it.After(func() { 33 Expect(os.RemoveAll(tmpDir)).To(Succeed()) 34 }) 35 36 it("writes the given environment to a directory", func() { 37 err := writer.Write(tmpDir, map[string]string{ 38 "some-name": "some-content", 39 "other-name": "other-content", 40 }) 41 Expect(err).NotTo(HaveOccurred()) 42 43 content, err := os.ReadFile(filepath.Join(tmpDir, "some-name")) 44 Expect(err).NotTo(HaveOccurred()) 45 Expect(string(content)).To(Equal("some-content")) 46 47 content, err = os.ReadFile(filepath.Join(tmpDir, "other-name")) 48 Expect(err).NotTo(HaveOccurred()) 49 Expect(string(content)).To(Equal("other-content")) 50 }) 51 52 it("writes does not create a directory of the env map is empty", func() { 53 err := writer.Write(tmpDir, map[string]string{}) 54 Expect(err).NotTo(HaveOccurred()) 55 56 Expect(tmpDir).NotTo(BeAnExistingFile()) 57 }) 58 59 context("failure cases", func() { 60 context("when the directory cannot be created", func() { 61 it.Before(func() { 62 Expect(os.MkdirAll(tmpDir, 0000)).To(Succeed()) 63 }) 64 65 it("returns an error", func() { 66 err := writer.Write(filepath.Join(tmpDir, "sub-dir"), map[string]string{ 67 "some-name": "some-content", 68 "other-name": "other-content", 69 }) 70 Expect(err).To(MatchError(ContainSubstring("permission denied"))) 71 }) 72 }) 73 74 context("when the env file cannot be created", func() { 75 it.Before(func() { 76 Expect(os.MkdirAll(tmpDir, 0000)).To(Succeed()) 77 }) 78 79 it("returns an error", func() { 80 err := writer.Write(tmpDir, map[string]string{ 81 "some-name": "some-content", 82 "other-name": "other-content", 83 }) 84 Expect(err).To(MatchError(ContainSubstring("permission denied"))) 85 }) 86 }) 87 }) 88 }