Skip to content

Commit

Permalink
adding bundler on fileBundler for extension, with tests included
Browse files Browse the repository at this point in the history
  • Loading branch information
pacostas committed Jun 6, 2023
1 parent 3278986 commit 5970fe4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/file_bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,55 @@ func (b FileBundler) Bundle(root string, paths []string, config cargo.Config) ([

return files, nil
}

func (b FileBundler) BundleExtension(root string, paths []string, config cargo.ExtensionConfig) ([]File, error) {
var files []File

for _, path := range paths {
file := File{Name: path}

switch path {
case "extension.toml":
buf := bytes.NewBuffer(nil)
err := cargo.EncodeExtensionConfig(buf, config)
if err != nil {
return nil, fmt.Errorf("error encoding extension.toml: %s", err)
}

file.ReadCloser = io.NopCloser(buf)
file.Info = NewFileInfo("extension.toml", buf.Len(), 0644, time.Now())

default:
var err error
file.Info, err = os.Lstat(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error stating included file: %s", err)
}

if file.Info.Mode()&os.ModeType != 0 {
link, err := os.Readlink(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error readlinking included file: %s", err)
}

if !strings.HasPrefix(link, string(filepath.Separator)) {
link = filepath.Clean(filepath.Join(root, link))
}

file.Link, err = filepath.Rel(root, link)
if err != nil {
return nil, fmt.Errorf("error finding relative link path: %s", err)
}
} else {
file.ReadCloser, err = os.Open(filepath.Join(root, path))
if err != nil {
return nil, fmt.Errorf("error opening included file: %s", err)
}
}
}

files = append(files, file)
}

return files, nil
}
75 changes: 75 additions & 0 deletions internal/file_bundler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,72 @@ include-files = ["bin/build", "bin/detect", "bin/link", "buildpack.toml"]
pre-package = "some-pre-package-script.sh"`))
})

context("Bundle Extension", func() {
it("returns a list of cargo files", func() {
files, err := fileBundler.BundleExtension(filepath.Join("..", "integration", "testdata", "example-cnb"), []string{"bin/build", "bin/detect", "bin/link", "extension.toml"}, cargo.ExtensionConfig{
API: "0.2",
Extension: cargo.ConfigExtension{
ID: "other-extension-id",
Name: "other-extension-name",
Version: "other-extension-version",
},
Metadata: cargo.ConfigExtensionMetadata{
IncludeFiles: []string{
"bin/build",
"bin/detect",
"bin/link",
"extension.toml",
},
PrePackage: "some-pre-package-script.sh",
},
})
Expect(err).NotTo(HaveOccurred())

Expect(files).To(HaveLen(4))

Expect(files[0].Name).To(Equal("bin/build"))
Expect(files[0].Info.Size()).To(Equal(int64(14)))
Expect(files[0].Info.Mode()).To(Equal(os.FileMode(0755)))
Expect(files[0].Link).To(Equal(""))

content, err := io.ReadAll(files[0])
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("build-contents"))

Expect(files[1].Name).To(Equal("bin/detect"))
Expect(files[1].Info.Size()).To(Equal(int64(15)))
Expect(files[1].Info.Mode()).To(Equal(os.FileMode(0755)))
Expect(files[1].Link).To(Equal(""))

content, err = io.ReadAll(files[1])
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("detect-contents"))

Expect(files[2].Name).To(Equal("bin/link"))
Expect(files[2].Info.Size()).To(Equal(int64(7)))
Expect(files[2].Info.Mode() & os.ModeSymlink).To(Equal(os.ModeSymlink))
Expect(files[2].Link).To(Equal("build"))
Expect(files[2].ReadCloser).To(BeNil())

Expect(files[3].Name).To(Equal("extension.toml"))
Expect(files[3].Info.Size()).To(Equal(int64(256)))
Expect(files[3].Info.Mode()).To(Equal(os.FileMode(0644)))
Expect(files[3].Link).To(Equal(""))

content, err = io.ReadAll(files[3])
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(MatchTOML(`api = "0.2"
[extension]
id = "other-extension-id"
name = "other-extension-name"
version = "other-extension-version"
[metadata]
include-files = ["bin/build", "bin/detect", "bin/link", "extension.toml"]
pre-package = "some-pre-package-script.sh"`))
})
})

context("error cases", func() {
context("when included file does not exist", func() {
it("fails", func() {
Expand All @@ -98,5 +164,14 @@ pre-package = "some-pre-package-script.sh"`))
})
})
})

context("error cases for extension", func() {
context("when included file does not exist", func() {
it("fails", func() {
_, err := fileBundler.BundleExtension(filepath.Join("jam", "testdata", "example-cnb"), []string{"bin/fake/build", "bin/detect", "extension.toml"}, cargo.ExtensionConfig{})
Expect(err).To(MatchError(ContainSubstring("error stating included file:")))
})
})
})
})
}

0 comments on commit 5970fe4

Please sign in to comment.