Skip to content

Commit f95ee58

Browse files
ForestEckhardtSophie Wigmore
and
Sophie Wigmore
authoredApr 27, 2022
API 0.8: Uses environment variables over postional args when available (#335)
Co-authored-by: Sophie Wigmore <swigmore@vmware.com>
1 parent a8d191f commit f95ee58

File tree

4 files changed

+317
-36
lines changed

4 files changed

+317
-36
lines changed
 

‎build.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,17 @@ func Build(f BuildFunc, options ...Option) {
9797
config = option(config)
9898
}
9999

100-
var (
101-
layersPath = config.args[1]
102-
platformPath = config.args[2]
103-
planPath = config.args[3]
104-
)
105-
106100
pwd, err := os.Getwd()
107101
if err != nil {
108102
config.exitHandler.Error(err)
109103
return
110104
}
111105

106+
planPath, ok := os.LookupEnv("CNB_BP_PLAN_PATH")
107+
if !ok {
108+
planPath = config.args[3]
109+
}
110+
112111
var plan BuildpackPlan
113112
_, err = toml.DecodeFile(planPath, &plan)
114113
if err != nil {
@@ -121,6 +120,16 @@ func Build(f BuildFunc, options ...Option) {
121120
cnbPath = filepath.Clean(strings.TrimSuffix(config.args[0], filepath.Join("bin", "build")))
122121
}
123122

123+
layersPath, ok := os.LookupEnv("CNB_LAYERS_DIR")
124+
if !ok {
125+
layersPath = config.args[1]
126+
}
127+
128+
platformPath, ok := os.LookupEnv("CNB_PLATFORM_DIR")
129+
if !ok {
130+
platformPath = config.args[2]
131+
}
132+
124133
var buildpackInfo struct {
125134
APIVersion string `toml:"api"`
126135
Buildpack BuildpackInfo `toml:"buildpack"`

‎build_test.go

+189-19
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
2828
layersDir string
2929
planPath string
3030
cnbDir string
31-
envCnbDir string
3231
binaryPath string
3332
exitHandler *fakes.ExitHandler
3433
)
@@ -71,9 +70,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
7170
cnbDir, err = os.MkdirTemp("", "cnb")
7271
Expect(err).NotTo(HaveOccurred())
7372

74-
envCnbDir, err = os.MkdirTemp("", "envCnb")
75-
Expect(err).NotTo(HaveOccurred())
76-
7773
bpTOML := []byte(`
7874
api = "0.8"
7975
[buildpack]
@@ -91,7 +87,6 @@ api = "0.8"
9187
uri = "some-license-uri"
9288
`)
9389
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
94-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
9590

9691
binaryPath = filepath.Join(cnbDir, "bin", "build")
9792

@@ -169,8 +164,6 @@ api = "0.4"
169164
clear-env = false
170165
`)
171166
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
172-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
173-
174167
})
175168

176169
it("updates the buildpack plan.toml with any changes", func() {
@@ -338,7 +331,6 @@ api = "0.6"
338331
clear-env = false
339332
`)
340333
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
341-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
342334
})
343335

344336
it("throws an error", func() {
@@ -462,7 +454,7 @@ api = "0.6"
462454

463455
context("when the CNB_BUILDPACK_DIR environment variable is set", func() {
464456
it.Before(func() {
465-
os.Setenv("CNB_BUILDPACK_DIR", envCnbDir)
457+
os.Setenv("CNB_BUILDPACK_DIR", cnbDir)
466458
})
467459

468460
it.After(func() {
@@ -476,10 +468,184 @@ api = "0.6"
476468
context = ctx
477469

478470
return packit.BuildResult{}, nil
479-
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, planPath}))
471+
}, packit.WithArgs([]string{"env-var-override", layersDir, platformDir, planPath}))
472+
473+
Expect(context).To(Equal(packit.BuildContext{
474+
CNBPath: cnbDir,
475+
Platform: packit.Platform{
476+
Path: platformDir,
477+
},
478+
Stack: "some-stack",
479+
WorkingDir: tmpDir,
480+
Plan: packit.BuildpackPlan{
481+
Entries: []packit.BuildpackPlanEntry{
482+
{
483+
Name: "some-entry",
484+
Metadata: map[string]interface{}{
485+
"version": "some-version",
486+
"some-key": "some-value",
487+
},
488+
},
489+
},
490+
},
491+
Layers: packit.Layers{
492+
Path: layersDir,
493+
},
494+
BuildpackInfo: packit.BuildpackInfo{
495+
ID: "some-id",
496+
Name: "some-name",
497+
Version: "some-version",
498+
Homepage: "some-homepage",
499+
Description: "some-description",
500+
Keywords: []string{"some-keyword"},
501+
SBOMFormats: []string{"some-sbom-format", "some-other-sbom-format"},
502+
Licenses: []packit.BuildpackInfoLicense{
503+
{
504+
Type: "some-license-type",
505+
URI: "some-license-uri",
506+
},
507+
},
508+
},
509+
}))
510+
})
511+
})
512+
513+
context("when the CNB_LAYERS_DIR environment variable is set", func() {
514+
it.Before(func() {
515+
os.Setenv("CNB_LAYERS_DIR", layersDir)
516+
})
517+
518+
it.After(func() {
519+
os.Unsetenv("CNB_LAYERS_DIR")
520+
})
521+
522+
it("sets the correct value for layers dir in the Build context", func() {
523+
var context packit.BuildContext
524+
525+
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
526+
context = ctx
527+
528+
return packit.BuildResult{}, nil
529+
}, packit.WithArgs([]string{binaryPath, "env-var-override", platformDir, planPath}))
530+
531+
Expect(context).To(Equal(packit.BuildContext{
532+
CNBPath: cnbDir,
533+
Platform: packit.Platform{
534+
Path: platformDir,
535+
},
536+
Stack: "some-stack",
537+
WorkingDir: tmpDir,
538+
Plan: packit.BuildpackPlan{
539+
Entries: []packit.BuildpackPlanEntry{
540+
{
541+
Name: "some-entry",
542+
Metadata: map[string]interface{}{
543+
"version": "some-version",
544+
"some-key": "some-value",
545+
},
546+
},
547+
},
548+
},
549+
Layers: packit.Layers{
550+
Path: layersDir,
551+
},
552+
BuildpackInfo: packit.BuildpackInfo{
553+
ID: "some-id",
554+
Name: "some-name",
555+
Version: "some-version",
556+
Homepage: "some-homepage",
557+
Description: "some-description",
558+
Keywords: []string{"some-keyword"},
559+
SBOMFormats: []string{"some-sbom-format", "some-other-sbom-format"},
560+
Licenses: []packit.BuildpackInfoLicense{
561+
{
562+
Type: "some-license-type",
563+
URI: "some-license-uri",
564+
},
565+
},
566+
},
567+
}))
568+
})
569+
})
570+
571+
context("when the CNB_PLATFORM_DIR environment variable is set", func() {
572+
it.Before(func() {
573+
os.Setenv("CNB_PLATFORM_DIR", platformDir)
574+
})
575+
576+
it.After(func() {
577+
os.Unsetenv("CNB_PLATFORM_DIR")
578+
})
579+
580+
it("sets the correct value for platform dir in the Build context", func() {
581+
var context packit.BuildContext
582+
583+
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
584+
context = ctx
585+
586+
return packit.BuildResult{}, nil
587+
}, packit.WithArgs([]string{binaryPath, layersDir, "env-var-override", planPath}))
588+
589+
Expect(context).To(Equal(packit.BuildContext{
590+
CNBPath: cnbDir,
591+
Platform: packit.Platform{
592+
Path: platformDir,
593+
},
594+
Stack: "some-stack",
595+
WorkingDir: tmpDir,
596+
Plan: packit.BuildpackPlan{
597+
Entries: []packit.BuildpackPlanEntry{
598+
{
599+
Name: "some-entry",
600+
Metadata: map[string]interface{}{
601+
"version": "some-version",
602+
"some-key": "some-value",
603+
},
604+
},
605+
},
606+
},
607+
Layers: packit.Layers{
608+
Path: layersDir,
609+
},
610+
BuildpackInfo: packit.BuildpackInfo{
611+
ID: "some-id",
612+
Name: "some-name",
613+
Version: "some-version",
614+
Homepage: "some-homepage",
615+
Description: "some-description",
616+
Keywords: []string{"some-keyword"},
617+
SBOMFormats: []string{"some-sbom-format", "some-other-sbom-format"},
618+
Licenses: []packit.BuildpackInfoLicense{
619+
{
620+
Type: "some-license-type",
621+
URI: "some-license-uri",
622+
},
623+
},
624+
},
625+
}))
626+
})
627+
})
628+
629+
context("when the CNB_BP_PLAN_PATH environment variable is set", func() {
630+
it.Before(func() {
631+
os.Setenv("CNB_BP_PLAN_PATH", planPath)
632+
})
633+
634+
it.After(func() {
635+
os.Unsetenv("CNB_BP_PLAN_PATH")
636+
})
637+
638+
it("sets the correct value for platform dir in the Build context", func() {
639+
var context packit.BuildContext
640+
641+
packit.Build(func(ctx packit.BuildContext) (packit.BuildResult, error) {
642+
context = ctx
643+
644+
return packit.BuildResult{}, nil
645+
}, packit.WithArgs([]string{binaryPath, layersDir, platformDir, "env-var-override"}))
480646

481647
Expect(context).To(Equal(packit.BuildContext{
482-
CNBPath: envCnbDir,
648+
CNBPath: cnbDir,
483649
Platform: packit.Platform{
484650
Path: platformDir,
485651
},
@@ -515,6 +681,18 @@ api = "0.6"
515681
},
516682
},
517683
}))
684+
685+
contents, err := os.ReadFile(planPath)
686+
Expect(err).NotTo(HaveOccurred())
687+
688+
Expect(string(contents)).To(MatchTOML(`
689+
[[entries]]
690+
name = "some-entry"
691+
692+
[entries.metadata]
693+
version = "some-version"
694+
some-key = "some-value"
695+
`))
518696
})
519697
})
520698

@@ -557,7 +735,6 @@ api = "0.6"
557735
clear-env = false
558736
`)
559737
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
560-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
561738
})
562739

563740
it("throws an error", func() {
@@ -622,7 +799,6 @@ api = "0.4"
622799
clear-env = false
623800
`)
624801
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
625-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
626802
})
627803

628804
it("throws an error", func() {
@@ -686,8 +862,6 @@ api = "0.4"
686862
clear-env = false
687863
`)
688864
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
689-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
690-
691865
})
692866

693867
it("throws an error", func() {
@@ -750,7 +924,6 @@ api = "0.6"
750924
clear-env = false
751925
`)
752926
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
753-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
754927
})
755928

756929
it("throws an error", func() {
@@ -1168,7 +1341,6 @@ api = "0.5"
11681341
clear-env = false
11691342
`)
11701343
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
1171-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
11721344
Expect(os.Chmod(planPath, 0444)).To(Succeed())
11731345
})
11741346

@@ -1241,7 +1413,6 @@ api = "0.4"
12411413
clear-env = false
12421414
`)
12431415
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
1244-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
12451416
Expect(os.Chmod(planPath, 0444)).To(Succeed())
12461417
})
12471418

@@ -1332,7 +1503,6 @@ api = "0.4"
13321503
clear-env = false
13331504
`)
13341505
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
1335-
Expect(os.WriteFile(filepath.Join(envCnbDir, "buildpack.toml"), bpTOML, 0600)).To(Succeed())
13361506
Expect(os.Chmod(planPath, 0444)).To(Succeed())
13371507
})
13381508

‎detect.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ func Detect(f DetectFunc, options ...Option) {
8383
return
8484
}
8585

86+
platformPath, ok := os.LookupEnv("CNB_PLATFORM_DIR")
87+
if !ok {
88+
platformPath = config.args[1]
89+
}
90+
8691
result, err := f(DetectContext{
8792
WorkingDir: dir,
8893
Platform: Platform{
89-
Path: config.args[1],
94+
Path: platformPath,
9095
},
9196
CNBPath: cnbPath,
9297
BuildpackInfo: buildpackInfo.Buildpack,
@@ -97,7 +102,12 @@ func Detect(f DetectFunc, options ...Option) {
97102
return
98103
}
99104

100-
file, err := os.OpenFile(config.args[2], os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
105+
planPath, ok := os.LookupEnv("CNB_BUILD_PLAN_PATH")
106+
if !ok {
107+
planPath = config.args[2]
108+
}
109+
110+
file, err := os.OpenFile(planPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
101111
if err != nil {
102112
config.exitHandler.Error(err)
103113
return

‎detect_test.go

+101-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
2424
tmpDir string
2525
platformDir string
2626
cnbDir string
27-
cnbEnvDir string
2827
binaryPath string
2928
stackID string
3029
planDir string
@@ -55,10 +54,6 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
5554
stackID = "io.packit.test.stack"
5655
Expect(os.Setenv("CNB_STACK_ID", stackID)).To(Succeed())
5756

58-
//Separate, but valid CNB dir for testing env parsing
59-
cnbEnvDir, err = os.MkdirTemp("", "cnbEnv")
60-
Expect(err).NotTo(HaveOccurred())
61-
6257
binaryPath = filepath.Join(cnbDir, "bin", "detect")
6358

6459
bpTOMLContent := []byte(`
@@ -70,7 +65,6 @@ api = "0.5"
7065
clear-env = false
7166
`)
7267
Expect(os.WriteFile(filepath.Join(cnbDir, "buildpack.toml"), bpTOMLContent, 0600)).To(Succeed())
73-
Expect(os.WriteFile(filepath.Join(cnbEnvDir, "buildpack.toml"), bpTOMLContent, 0600)).To(Succeed())
7468

7569
planDir, err = os.MkdirTemp("", "buildplan.toml")
7670
Expect(err).NotTo(HaveOccurred())
@@ -242,7 +236,7 @@ api = "0.5"
242236

243237
context("when CNB_BUILDPACK_DIR is set", func() {
244238
it.Before(func() {
245-
Expect(os.Setenv("CNB_BUILDPACK_DIR", cnbEnvDir)).To(Succeed())
239+
Expect(os.Setenv("CNB_BUILDPACK_DIR", cnbDir)).To(Succeed())
246240
})
247241

248242
it.After(func() {
@@ -256,11 +250,94 @@ api = "0.5"
256250
context = ctx
257251

258252
return packit.DetectResult{}, nil
259-
}, packit.WithArgs([]string{binaryPath, platformDir, planPath}))
253+
}, packit.WithArgs([]string{"env-var-override", platformDir, planPath}))
254+
255+
Expect(context).To(Equal(packit.DetectContext{
256+
WorkingDir: tmpDir,
257+
CNBPath: cnbDir,
258+
Platform: packit.Platform{
259+
Path: platformDir,
260+
},
261+
BuildpackInfo: packit.BuildpackInfo{
262+
ID: "some-id",
263+
Name: "some-name",
264+
Version: "some-version",
265+
},
266+
Stack: stackID,
267+
}))
268+
})
269+
})
270+
271+
context("when CNB_PLATFORM_DIR is set", func() {
272+
it.Before(func() {
273+
Expect(os.Setenv("CNB_PLATFORM_DIR", platformDir)).To(Succeed())
274+
})
275+
276+
it.After(func() {
277+
Expect(os.Unsetenv("CNB_PLATFORM_DIR")).To(Succeed())
278+
})
279+
280+
it("the Detect context receives the correct value", func() {
281+
var context packit.DetectContext
282+
283+
packit.Detect(func(ctx packit.DetectContext) (packit.DetectResult, error) {
284+
context = ctx
285+
286+
return packit.DetectResult{}, nil
287+
}, packit.WithArgs([]string{binaryPath, "env-var-override", planPath}))
288+
289+
Expect(context).To(Equal(packit.DetectContext{
290+
WorkingDir: tmpDir,
291+
CNBPath: cnbDir,
292+
Platform: packit.Platform{
293+
Path: platformDir,
294+
},
295+
BuildpackInfo: packit.BuildpackInfo{
296+
ID: "some-id",
297+
Name: "some-name",
298+
Version: "some-version",
299+
},
300+
Stack: stackID,
301+
}))
302+
})
303+
})
304+
305+
context("when CNB_BUILD_PLAN_PATH is set", func() {
306+
it.Before(func() {
307+
Expect(os.Setenv("CNB_BUILD_PLAN_PATH", planPath)).To(Succeed())
308+
})
309+
310+
it.After(func() {
311+
Expect(os.Unsetenv("CNB_BUILD_PLAN_PATH")).To(Succeed())
312+
})
313+
314+
it("the Detect context receives the correct value", func() {
315+
var context packit.DetectContext
316+
317+
packit.Detect(func(ctx packit.DetectContext) (packit.DetectResult, error) {
318+
context = ctx
319+
320+
return packit.DetectResult{
321+
Plan: packit.BuildPlan{
322+
Provides: []packit.BuildPlanProvision{
323+
{Name: "some-provision"},
324+
},
325+
Requires: []packit.BuildPlanRequirement{
326+
{
327+
Name: "some-requirement",
328+
Metadata: map[string]string{
329+
"version": "some-version",
330+
"some-key": "some-value",
331+
},
332+
},
333+
},
334+
},
335+
}, nil
336+
}, packit.WithArgs([]string{binaryPath, platformDir, "env-var-override"}))
260337

261338
Expect(context).To(Equal(packit.DetectContext{
262339
WorkingDir: tmpDir,
263-
CNBPath: cnbEnvDir,
340+
CNBPath: cnbDir,
264341
Platform: packit.Platform{
265342
Path: platformDir,
266343
},
@@ -271,6 +348,21 @@ api = "0.5"
271348
},
272349
Stack: stackID,
273350
}))
351+
352+
contents, err := os.ReadFile(planPath)
353+
Expect(err).NotTo(HaveOccurred())
354+
355+
Expect(string(contents)).To(MatchTOML(`
356+
[[provides]]
357+
name = "some-provision"
358+
359+
[[requires]]
360+
name = "some-requirement"
361+
362+
[requires.metadata]
363+
version = "some-version"
364+
some-key = "some-value"
365+
`))
274366
})
275367
})
276368

0 commit comments

Comments
 (0)
Please sign in to comment.