-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymlinker_test.go
93 lines (77 loc) · 2.74 KB
/
symlinker_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
package yarninstall_test
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
. "github.com/onsi/gomega"
yarninstall "github.com/initializ-buildpacks/yarn-install"
"github.com/sclevine/spec"
)
func testSymlinker(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
symlinker yarninstall.Symlinker
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
symlinker = yarninstall.NewSymlinker()
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("Link", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "oldname"), []byte("Hello world"), os.ModePerm)).To(Succeed())
})
it("creates a symlink from oldname to newname", func() {
Expect(symlinker.Link(filepath.Join(workingDir, "oldname"), filepath.Join(workingDir, "newname"))).To(Succeed())
fi, err := os.Lstat(filepath.Join(workingDir, "newname"))
Expect(err).NotTo(HaveOccurred())
Expect(fi.Mode() & os.ModeSymlink).ToNot(BeZero())
link, err := os.Readlink(filepath.Join(workingDir, "newname"))
Expect(err).NotTo(HaveOccurred())
Expect(link).To(Equal(filepath.Join(workingDir, "oldname")))
})
context("failure cases", func() {
context("when the symlink cannot be created", func() {
it("errors", func() {
err := symlinker.Link(filepath.Join(workingDir, "oldname"), filepath.Join(workingDir, "oldname"))
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("file exists"))
})
})
})
})
context("Unlink", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "oldname"), []byte("Hello world"), os.ModePerm)).To(Succeed())
Expect(os.Symlink(filepath.Join(workingDir, "oldname"), filepath.Join(workingDir, "newname"))).To(Succeed())
})
it("removes the symlink", func() {
Expect(symlinker.Unlink(filepath.Join(workingDir, "newname"))).To(Succeed())
_, err := os.Lstat(filepath.Join(workingDir, "newname"))
Expect(errors.Is(err, os.ErrNotExist)).To(BeTrue())
_, err = os.Lstat(filepath.Join(workingDir, "oldname"))
Expect(err).NotTo(HaveOccurred())
})
context("when the provided file does not exist", func() {
it("is a no op", func() {
err := symlinker.Unlink(filepath.Join(workingDir, "othername"))
Expect(err).NotTo(HaveOccurred())
})
})
context("failure cases", func() {
context("when the provided file is not a symlink", func() {
it("errors", func() {
err := symlinker.Unlink(filepath.Join(workingDir, "oldname"))
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("cannot unlink %s because it is not a symlink", filepath.Join(workingDir, "oldname")))))
})
})
})
})
}