Skip to content

Commit

Permalink
Move runtime.txt logic into python-compat-cnb
Browse files Browse the repository at this point in the history
[#163886457]

Co-authored-by: Chhavi Kankaria <[email protected]>
  • Loading branch information
Zander Mackie and chhhavi committed Apr 1, 2019
1 parent af683ab commit dce4c84
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
.idea
.bin
python-cnb_*
python-cnb.tgz
26 changes: 7 additions & 19 deletions cmd/detect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func main() {
context, err := detect.DefaultDetect()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to create a default detection context: %s", err)
os.Exit(100)
}

if err := context.BuildPlan.Init(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Failed to initialize Build Plan: %s\n", err)
os.Exit(101)
}

Expand All @@ -31,26 +36,14 @@ func main() {
}

func runDetect(context detect.Detect) (int, error) {
runtimePath := filepath.Join(context.Application.Root, "runtime.txt")
exists, err := helper.FileExists(runtimePath)
if err != nil {
return detect.FailStatusCode, err
}

var version string
if exists {
version, err = readRuntimeTxtVersion(runtimePath)
if err != nil {
return detect.FailStatusCode, err
}
}

buildpackYAMLPath := filepath.Join(context.Application.Root, "buildpack.yml")
exists, err = helper.FileExists(buildpackYAMLPath)
exists, err := helper.FileExists(buildpackYAMLPath)
if err != nil {
return detect.FailStatusCode, err
}

version := context.BuildPlan[python.Dependency].Version
if exists {
version, err = readBuildpackYamlVersion(buildpackYAMLPath)
if err != nil {
Expand All @@ -66,11 +59,6 @@ func runDetect(context detect.Detect) (int, error) {
})
}

func readRuntimeTxtVersion(runtimePath string) (string, error) {
buf, err := ioutil.ReadFile(runtimePath)
return string(buf), err
}

func readBuildpackYamlVersion(buildpackYAMLPath string) (string, error) {
buf, err := ioutil.ReadFile(buildpackYAMLPath)
if err != nil {
Expand Down
34 changes: 8 additions & 26 deletions cmd/detect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func testDetect(t *testing.T, when spec.G, it spec.S) {

when("testing versions", func() {

when("there is no runtime.txt or buildpack.yml", func() {
when("there is no buildpack.yml", func() {
it("shouldn't set the version in the buildplan", func() {
code, err := runDetect(factory.Detect)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -57,28 +57,7 @@ func testDetect(t *testing.T, when spec.G, it spec.S) {
})
})

when("there is a runtime.txt but no buildpack.yml", func() {
const version string = "1.2.3"

it.Before(func() {
Expect(helper.WriteFile(filepath.Join(factory.Detect.Application.Root, "runtime.txt"), 0666, version)).To(Succeed())
})

it("should pass with the requested version of python", func() {
code, err := runDetect(factory.Detect)
Expect(err).NotTo(HaveOccurred())
Expect(code).To(Equal(detect.PassStatusCode))

Expect(factory.Output).To(Equal(buildplan.BuildPlan{
python.Dependency: buildplan.Dependency{
Version: version,
Metadata: buildplan.Metadata{"build": true, "launch": true},
},
}))
})
})

when("there is a buildpack.yml but no runtime.txt", func() {
when("there is a buildpack.yml", func() {
const version string = "1.2.3"

it.Before(func() {
Expand All @@ -100,14 +79,17 @@ func testDetect(t *testing.T, when spec.G, it spec.S) {
})
})

when("there is a buildpack.yml and a runtime.txt", func() {
when("there is a is an existing version from the build plan and a buildpack.yml", func() {
const buildpackYAMLVersion string = "1.2.3"
const runtimeVersion string = "4.5.6"
const existingVersion string = "4.5.6"

it.Before(func() {
factory.AddBuildPlan(python.Dependency, buildplan.Dependency{
Version: existingVersion,
})

buildpackYAMLString := fmt.Sprintf("python:\n version: %s", buildpackYAMLVersion)
Expect(helper.WriteFile(filepath.Join(factory.Detect.Application.Root, "buildpack.yml"), 0666, buildpackYAMLString)).To(Succeed())
Expect(helper.WriteFile(filepath.Join(factory.Detect.Application.Root, "runtime.txt"), 0666, runtimeVersion)).To(Succeed())
})

it("should pass with the requested version of python defined in buildpack.yml", func() {
Expand Down

0 comments on commit dce4c84

Please sign in to comment.