Skip to content

Commit

Permalink
Improve build/detect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrox committed Jun 16, 2023
1 parent 59967f3 commit d89f797
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
20 changes: 16 additions & 4 deletions build_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package permissions_test
package permissions

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

permissions "github.com/ninech/buildpack-rails-permissions"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
"github.com/sclevine/spec"
Expand Down Expand Up @@ -35,7 +35,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())

build = permissions.Build(scribe.NewEmitter(bytes.NewBuffer(nil)))
build = Build(scribe.NewEmitter(bytes.NewBuffer(nil)))
})

it.After(func() {
Expand All @@ -44,7 +44,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})

it("returns a result that builds correctly", func() {
it("creates rails dirs and sets permissions", func() {
_, err := build(packit.BuildContext{
WorkingDir: workingDir,
CNBPath: cnbDir,
Expand All @@ -59,5 +59,17 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Layers: packit.Layers{Path: layersDir},
})
Expect(err).NotTo(HaveOccurred())

tmp, err := os.Stat(filepath.Join(workingDir, tmpDirName))
Expect(err).NotTo(HaveOccurred())
Expect(tmp.Mode()).To(Equal(os.ModeDir | 0770))

pids, err := os.Stat(filepath.Join(workingDir, tmpDirName, pidsDirName))
Expect(err).NotTo(HaveOccurred())
Expect(pids.Mode()).To(Equal(os.ModeDir | 0770))

cache, err := os.Stat(filepath.Join(workingDir, tmpDirName, cacheDirName))
Expect(err).NotTo(HaveOccurred())
Expect(cache.Mode()).To(Equal(os.ModeDir | 0770))
})
}
71 changes: 56 additions & 15 deletions detect_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package permissions_test
package permissions

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

permissions "github.com/ninech/buildpack-rails-permissions"
"github.com/paketo-buildpacks/packit/v2"
"github.com/sclevine/spec"

Expand All @@ -20,32 +19,74 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
detect packit.DetectFunc
)

it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
context("when a rails gemfile is present", func() {
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir-*")
Expect(err).NotTo(HaveOccurred())

const gemfile = `
const gemfile = `
source "https://rubygems.org"
gem "rails", "~> 7.0.0"
`
err = os.WriteFile(filepath.Join(workingDir, "Gemfile"), []byte(gemfile), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(workingDir, "Gemfile"), []byte(gemfile), os.ModePerm)
Expect(err).NotTo(HaveOccurred())

detect = permissions.Detect()
})
detect = Detect()
})

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

context("when conditions for detect true are met", func() {
it("detects", func() {
_, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).NotTo(HaveOccurred())
})
})

context("when a gemfile is present without rails", func() {
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir-*")
Expect(err).NotTo(HaveOccurred())

const gemfile = `
source "https://rubygems.org"
gem "something-else", "~> 1.0.0"
`
err = os.WriteFile(filepath.Join(workingDir, "Gemfile"), []byte(gemfile), os.ModePerm)
Expect(err).NotTo(HaveOccurred())

detect = Detect()
})

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

it("fails detection", func() {
_, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).To(HaveOccurred())
})
})

context("when no gemfile is present", func() {
it.Before(func() {
detect = Detect()
})

it("fails detection", func() {
_, err := detect(packit.DetectContext{
WorkingDir: workingDir,
})
Expect(err).To(HaveOccurred())
})
})
}
2 changes: 1 addition & 1 deletion init_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package permissions_test
package permissions

import (
"testing"
Expand Down

0 comments on commit d89f797

Please sign in to comment.