forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_path_parser_test.go
81 lines (65 loc) · 2.01 KB
/
project_path_parser_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
package npminstall_test
import (
"fmt"
"os"
"path/filepath"
"testing"
npminstall "github.com/paketo-buildpacks/npm-install"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testProjectPathParser(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
projectDir string
projectPathParser npminstall.ProjectPathParser
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
projectDir = filepath.Join(workingDir, "custom", "path")
err = os.MkdirAll(projectDir, os.ModePerm)
Expect(err).NotTo(HaveOccurred())
projectPathParser = npminstall.NewProjectPathParser()
os.Setenv("BP_NODE_PROJECT_PATH", "custom/path")
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
os.Unsetenv("BP_NODE_PROJECT_PATH")
})
context("Get", func() {
it("returns the set project path", func() {
result, err := projectPathParser.Get(workingDir)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(filepath.Join("custom", "path")))
})
})
context("failure cases", func() {
context("when the project path subdirectory isn't accessible", func() {
it.Before(func() {
Expect(os.Chmod(workingDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(workingDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, err := projectPathParser.Get(workingDir)
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the project path subdirectory does not exist", func() {
it.Before(func() {
os.Setenv("BP_NODE_PROJECT_PATH", "some-garbage")
})
it.After(func() {
os.Unsetenv("BP_NODE_PROJECT_PATH")
})
it("returns an error", func() {
_, err := projectPathParser.Get(workingDir)
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", "some-garbage"))))
})
})
})
}