forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_buildpack_test.go
138 lines (116 loc) · 3.5 KB
/
new_buildpack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package pack_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/heroku/color"
"github.com/pelletier/go-toml"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/buildpacks/pack"
"github.com/buildpacks/pack/internal/dist"
h "github.com/buildpacks/pack/testhelpers"
)
func TestNewBuildpack(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "NewBuildpack", testNewBuildpack, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testNewBuildpack(t *testing.T, when spec.G, it spec.S) {
var (
subject *pack.Client
tmpDir string
)
it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "new-buildpack-test")
h.AssertNil(t, err)
subject, err = pack.NewClient()
h.AssertNil(t, err)
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(tmpDir))
})
when("#NewBuildpack", func() {
it("should create bash scripts", func() {
err := subject.NewBuildpack(context.TODO(), pack.NewBuildpackOptions{
API: "0.4",
Path: tmpDir,
ID: "example/my-cnb",
Version: "0.0.0",
Stacks: []dist.Stack{
{
ID: "some-stack",
Mixins: []string{"some-mixin"},
},
},
})
h.AssertNil(t, err)
info, err := os.Stat(filepath.Join(tmpDir, "bin/build"))
h.AssertFalse(t, os.IsNotExist(err))
if runtime.GOOS != "windows" {
h.AssertTrue(t, info.Mode()&0100 != 0)
}
info, err = os.Stat(filepath.Join(tmpDir, "bin/detect"))
h.AssertFalse(t, os.IsNotExist(err))
if runtime.GOOS != "windows" {
h.AssertTrue(t, info.Mode()&0100 != 0)
}
assertBuildpackToml(t, tmpDir, "example/my-cnb")
})
when("files exist", func() {
it.Before(func() {
var err error
err = os.MkdirAll(filepath.Join(tmpDir, "bin"), 0755)
h.AssertNil(t, err)
err = ioutil.WriteFile(filepath.Join(tmpDir, "buildpack.toml"), []byte("expected value"), 0655)
h.AssertNil(t, err)
err = ioutil.WriteFile(filepath.Join(tmpDir, "bin", "build"), []byte("expected value"), 0755)
h.AssertNil(t, err)
err = ioutil.WriteFile(filepath.Join(tmpDir, "bin", "detect"), []byte("expected value"), 0755)
h.AssertNil(t, err)
})
it("should not clobber files that exist", func() {
err := subject.NewBuildpack(context.TODO(), pack.NewBuildpackOptions{
API: "0.4",
Path: tmpDir,
ID: "example/my-cnb",
Version: "0.0.0",
Stacks: []dist.Stack{
{
ID: "some-stack",
Mixins: []string{"some-mixin"},
},
},
})
h.AssertNil(t, err)
content, err := ioutil.ReadFile(filepath.Join(tmpDir, "buildpack.toml"))
h.AssertNil(t, err)
h.AssertEq(t, content, []byte("expected value"))
content, err = ioutil.ReadFile(filepath.Join(tmpDir, "bin", "build"))
h.AssertNil(t, err)
h.AssertEq(t, content, []byte("expected value"))
content, err = ioutil.ReadFile(filepath.Join(tmpDir, "bin", "detect"))
h.AssertNil(t, err)
h.AssertEq(t, content, []byte("expected value"))
})
})
})
}
func assertBuildpackToml(t *testing.T, path string, id string) {
buildpackTOML := filepath.Join(path, "buildpack.toml")
_, err := os.Stat(buildpackTOML)
h.AssertFalse(t, os.IsNotExist(err))
f, err := os.Open(buildpackTOML)
h.AssertNil(t, err)
var buildpackDescriptor dist.BuildpackDescriptor
err = toml.NewDecoder(f).Decode(&buildpackDescriptor)
h.AssertNil(t, err)
defer f.Close()
fmt.Printf("%s\n", buildpackDescriptor)
h.AssertEq(t, buildpackDescriptor.Info.ID, "example/my-cnb")
}