Skip to content

Commit

Permalink
Close paketo-buildpacks#252 : Disable ToLower in some cases
Browse files Browse the repository at this point in the history
* use lowercase to perform the comparison, and keep the original passed
  as an argument
  • Loading branch information
anthonydahanne committed Mar 7, 2023
1 parent 703a7d0 commit a78aebe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
10 changes: 5 additions & 5 deletions jlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (j JLink) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {
}

cacertsPath := filepath.Join(layer.Path, "lib", "security", "cacerts")
if err := os.Chmod(cacertsPath, 0664); err != nil{
return libcnb.Layer{}, fmt.Errorf("unable to set keystore file permissions\n%w", err)
if err := os.Chmod(cacertsPath, 0664); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to set keystore file permissions\n%w", err)
}

if IsBeforeJava18(j.JavaVersion) {
Expand Down Expand Up @@ -155,12 +155,12 @@ func (j *JLink) buildCustomJRE(layerPath string) error {
func (j *JLink) validArgs() bool {
jlinkArgs := j.Args[:0]
var skipNext, modsFound bool
for _, a := range j.Args {
for _, original := range j.Args {
if skipNext {
skipNext = false
continue
}
a = strings.ToLower(a)
var a = strings.ToLower(original)
if strings.HasPrefix(a, "--output") {
j.Logger.Bodyf(color.New(color.Faint, color.Bold).Sprint("WARNING: explicitly specified '--output' option & value will be overridden"))
skipNext = true
Expand All @@ -169,7 +169,7 @@ func (j *JLink) validArgs() bool {
if strings.HasPrefix(a, "--add-modules") {
modsFound = true
}
jlinkArgs = append(jlinkArgs, a)
jlinkArgs = append(jlinkArgs, original)
}
j.Args = jlinkArgs
if !modsFound {
Expand Down
45 changes: 39 additions & 6 deletions jlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/paketo-buildpacks/libpak/effect"
"github.com/paketo-buildpacks/libpak/effect/mocks"
"github.com/stretchr/testify/mock"
"io/ioutil"
"io"
"os"
"path/filepath"
"reflect"
Expand All @@ -42,7 +42,7 @@ func testJLink(t *testing.T, context spec.G, it spec.S) {

cl = libjvm.CertificateLoader{
CertDirs: []string{filepath.Join("testdata", "certificates")},
Logger: ioutil.Discard,
Logger: io.Discard,
}

ctx libcnb.BuildContext
Expand All @@ -52,7 +52,7 @@ func testJLink(t *testing.T, context spec.G, it spec.S) {
var err error
Expect(os.Setenv("BP_JVM_JLINK_ENABLED", "true")).To(Succeed())

ctx.Layers.Path, err = ioutil.TempDir("", "jdk")
ctx.Layers.Path, err = os.MkdirTemp("", "jdk")
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -68,7 +68,7 @@ func testJLink(t *testing.T, context spec.G, it spec.S) {
exec := &mocks.Executor{}
j, err := libjvm.NewJLink(ctx.Application.Path, exec, args, cl, LaunchContribution, false)
Expect(err).NotTo(HaveOccurred())
j.Logger = bard.NewLogger(ioutil.Discard)
j.Logger = bard.NewLogger(io.Discard)

Expect(j.LayerContributor.ExpectedMetadata.(map[string]interface{})["cert-dir"]).To(HaveLen(4))

Expand Down Expand Up @@ -108,7 +108,7 @@ func testJLink(t *testing.T, context spec.G, it spec.S) {
exec := &mocks.Executor{}
j, err := libjvm.NewJLink(ctx.Application.Path, exec, args, cl, LaunchContribution, true)
Expect(err).NotTo(HaveOccurred())
j.Logger = bard.NewLogger(ioutil.Discard)
j.Logger = bard.NewLogger(io.Discard)

Expect(j.LayerContributor.ExpectedMetadata.(map[string]interface{})["cert-dir"]).To(HaveLen(4))

Expand All @@ -132,13 +132,46 @@ func testJLink(t *testing.T, context spec.G, it spec.S) {
Expect(e.Args).To(ContainElement("java.se"))
})

it("contributes jlink JRE with user provided all caps --add-modules argument", func() {

args := []string{"--no-man-pages", "--no-header-files", "--strip-debug", "--add-modules", "ALL-MODULE-PATH"}
exec := &mocks.Executor{}
j, err := libjvm.NewJLink(ctx.Application.Path, exec, args, cl, LaunchContribution, true)
Expect(err).NotTo(HaveOccurred())
j.Logger = bard.NewLogger(io.Discard)

Expect(j.LayerContributor.ExpectedMetadata.(map[string]interface{})["cert-dir"]).To(HaveLen(4))

layer, err := ctx.Layers.Layer("jlink")
Expect(err).NotTo(HaveOccurred())

exec.On("Execute", mock.Anything).Run(func(args mock.Arguments) {
err = os.MkdirAll(filepath.Join(ctx.Layers.Path, "jlink"), os.ModePerm)
jre, err := os.Open("testdata/3aa01010c0d3592ea248c8353d60b361231fa9bf9a7479b4f06451fef3e64524/stub-jre-11.tar.gz")
Expect(err).NotTo(HaveOccurred())
err = crush.Extract(jre, layer.Path, 1)
Expect(err).NotTo(HaveOccurred())
}).Return(nil)

layer, err = j.Contribute(layer)
Expect(err).NotTo(HaveOccurred())

e := exec.Calls[0].Arguments[0].(effect.Execution)
Expect(e.Args).To(ContainElement("--output"))
Expect(e.Args).To(ContainElement("--no-man-pages"))
Expect(e.Args).To(ContainElement("--no-header-files"))
Expect(e.Args).To(ContainElement("--strip-debug"))
Expect(e.Args).To(ContainElement("--add-modules"))
Expect(e.Args).To(ContainElement("ALL-MODULE-PATH"))
})

it("contributes jlink JRE with user provided args & missing modules", func() {

args := []string{"--no-man-pages", "--no-header-files", "--strip-debug"}
exec := &mocks.Executor{}
j, err := libjvm.NewJLink(ctx.Application.Path, exec, args, cl, LaunchContribution, true)
Expect(err).NotTo(HaveOccurred())
j.Logger = bard.NewLogger(ioutil.Discard)
j.Logger = bard.NewLogger(io.Discard)

Expect(j.LayerContributor.ExpectedMetadata.(map[string]interface{})["cert-dir"]).To(HaveLen(4))

Expand Down

0 comments on commit a78aebe

Please sign in to comment.