Skip to content

Commit

Permalink
Adds support for writing text files in Vacation
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Hitchener <thitchener@pivotal.io>
  • Loading branch information
ForestEckhardt authored and ryanmoran committed Mar 30, 2021
1 parent b3dad1d commit cb4becf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
6 changes: 4 additions & 2 deletions postal/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ version = "this is super not semver"

context("when the file contents are empty", func() {
it.Before(func() {
buffer := bytes.NewBuffer(nil)
// This is a FLAC header
buffer := bytes.NewBuffer([]byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)

sum := sha256.Sum256(buffer.Bytes())
Expand Down Expand Up @@ -703,7 +704,8 @@ version = "this is super not semver"

context("when the file contents are empty", func() {
it.Before(func() {
buffer := bytes.NewBuffer(nil)
// This is a FLAC header
buffer := bytes.NewBuffer([]byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))
transport.DropCall.Returns.ReadCloser = io.NopCloser(buffer)

sum := sha256.Sum256(buffer.Bytes())
Expand Down
1 change: 1 addition & 0 deletions vacation/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestVacation(t *testing.T) {
suite("VacationTar", testVacationTar)
suite("VacationTarGzip", testVacationTarGzip)
suite("VacationTarXZ", testVacationTarXZ)
suite("VacationText", testVacationText)
suite("VacationZip", testVacationZip)
suite.Run(t)
}
23 changes: 22 additions & 1 deletion vacation/vacation.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func (ta TarArchive) Decompress(destination string) error {
if err != nil {
return fmt.Errorf("failed to extract symlink: %s", err)
}

}
}

Expand All @@ -150,6 +149,10 @@ func (ta TarArchive) Decompress(destination string) error {

// Decompress reads from Archive, determines the archive type of the input
// stream, and writes files into the destination specified.
//
// Archive decompression will also handle files that are types "text/plain;
// charset=utf-8" and write the contents of the input stream to a file name
// "artifact" in the destination directory.
func (a Archive) Decompress(destination string) error {
// Convert reader into a buffered read so that the header can be peeked to
// determine the type.
Expand All @@ -176,6 +179,10 @@ func (a Archive) Decompress(destination string) error {
return NewTarXZArchive(bufferedReader).StripComponents(a.components).Decompress(destination)
case "application/zip":
return NewZipArchive(bufferedReader).Decompress(destination)
case "text/plain; charset=utf-8":
// This function will write the contents of the reader to file called
// "artifact" in the destination directory
return writeTextFile(bufferedReader, destination)
default:
return fmt.Errorf("unsupported archive type: %s", mime.String())
}
Expand Down Expand Up @@ -203,6 +210,20 @@ func (txz TarXZArchive) Decompress(destination string) error {
return NewTarArchive(xzr).StripComponents(txz.components).Decompress(destination)
}

func writeTextFile(reader io.Reader, destination string) error {
file, err := os.Create(filepath.Join(destination, "artifact"))
if err != nil {
panic(err)
}

_, err = io.Copy(file, reader)
if err != nil {
return err
}

return nil
}

// StripComponents behaves like the --strip-components flag on tar command
// removing the first n levels from the final decompression destination.
// Setting this is a no-op for archive types that do not use --strip-components
Expand Down
3 changes: 2 additions & 1 deletion vacation/vacation_archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func testVacationArchive(t *testing.T, context spec.G, it spec.S) {
tempDir, err = os.MkdirTemp("", "vacation")
Expect(err).NotTo(HaveOccurred())

buffer := bytes.NewBuffer([]byte(`some contents`))
// This is a FLAC header
buffer := bytes.NewBuffer([]byte("\x66\x4C\x61\x43\x00\x00\x00\x22"))

archive = vacation.NewArchive(buffer)
})
Expand Down
49 changes: 49 additions & 0 deletions vacation/vacation_text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package vacation_test

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/paketo-buildpacks/packit/vacation"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
)

func testVacationText(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
)

context("when passed the reader of a text file", func() {
var (
archive vacation.Archive
tempDir string
)

it.Before(func() {
var err error
tempDir, err = os.MkdirTemp("", "vacation")
Expect(err).NotTo(HaveOccurred())

buffer := bytes.NewBuffer([]byte(`some contents`))

archive = vacation.NewArchive(buffer)
})

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

it("writes a text file onto the path", func() {
err := archive.Decompress(tempDir)
Expect(err).NotTo(HaveOccurred())

content, err := os.ReadFile(filepath.Join(tempDir, "artifact"))
Expect(err).NotTo(HaveOccurred())
Expect(content).To(Equal([]byte(`some contents`)))
})
})
}

0 comments on commit cb4becf

Please sign in to comment.