forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker_test.go
116 lines (87 loc) · 2.81 KB
/
linker_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
package npminstall_test
import (
"os"
"path/filepath"
"testing"
npminstall "github.com/paketo-buildpacks/npm-install"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testLinker(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
var (
sourceDir, targetDir, tmpDir string
source, target string
linker npminstall.Linker
)
it.Before(func() {
var err error
sourceDir, err = os.MkdirTemp("", "source")
Expect(err).NotTo(HaveOccurred())
source = filepath.Join(sourceDir, "file")
file, err := os.Create(source)
Expect(err).NotTo(HaveOccurred())
Expect(file.Close()).To(Succeed())
targetDir, err = os.MkdirTemp("", "target")
Expect(err).NotTo(HaveOccurred())
target = filepath.Join(targetDir, "file")
tmpDir, err = os.MkdirTemp("", "tmp")
Expect(err).NotTo(HaveOccurred())
linker = npminstall.NewLinker(tmpDir)
})
it.After(func() {
Expect(os.RemoveAll(sourceDir)).To(Succeed())
Expect(os.RemoveAll(targetDir)).To(Succeed())
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})
context("Link", func() {
it("links the source to the target through an indirection path in a temporary directory", func() {
err := linker.Link(source, target)
Expect(err).NotTo(HaveOccurred())
link, err := os.Readlink(source)
Expect(err).NotTo(HaveOccurred())
Expect(link).To(HavePrefix(tmpDir))
link, err = os.Readlink(link)
Expect(err).NotTo(HaveOccurred())
Expect(link).To(Equal(target))
})
context("failure cases", func() {
context("when the source cannot be removed", func() {
it.Before(func() {
Expect(os.Chmod(sourceDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(sourceDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := linker.Link(source, target)
Expect(err).To(MatchError(ContainSubstring("failed to remove link source:")))
})
})
context("when the indirection link cannot be removed", func() {
it.Before(func() {
Expect(os.Chmod(tmpDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(tmpDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := linker.Link(source, target)
Expect(err).To(MatchError(ContainSubstring("failed to remove link indirection:")))
})
})
})
})
context("WithPath", func() {
it("places the symlink at the given path within the indirection directory", func() {
err := linker.WithPath("some/link/path").Link(source, target)
Expect(err).NotTo(HaveOccurred())
link, err := os.Readlink(source)
Expect(err).NotTo(HaveOccurred())
Expect(link).To(Equal(filepath.Join(tmpDir, "some", "link", "path")))
link, err = os.Readlink(link)
Expect(err).NotTo(HaveOccurred())
Expect(link).To(Equal(target))
})
})
}