-
Notifications
You must be signed in to change notification settings - Fork 4
/
symlinker_test.go
117 lines (96 loc) · 3.76 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package dotnetpublish_test
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
dotnetpublish "github.com/paketo-buildpacks/dotnet-publish"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testSymlinker(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workingDir string
symlinker dotnetpublish.Symlinker
)
it.Before(func() {
var err error
workingDir, err = os.MkdirTemp("", "working-dir")
Expect(err).NotTo(HaveOccurred())
symlinker = dotnetpublish.NewSymlinker()
})
it.After(func() {
Expect(os.RemoveAll(workingDir)).To(Succeed())
})
context("Link", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "old"), []byte("old file contents"), os.ModePerm)).To(Succeed())
})
it("symlinks the given old location to the new location", func() {
Expect(symlinker.Link(filepath.Join(workingDir, "old"), filepath.Join(workingDir, "new"))).To(Succeed())
link, err := os.Readlink(filepath.Join(workingDir, "new"))
Expect(err).NotTo(HaveOccurred())
Expect(link).To(Equal(filepath.Join(workingDir, "old")))
})
context("when newname includes directories that don't yet exist", func() {
it("creates the directories and then makes the symlink", func() {
Expect(symlinker.Link(filepath.Join(workingDir, "old"), filepath.Join(workingDir, "subdir", "new"))).To(Succeed())
link, err := os.Readlink(filepath.Join(workingDir, "subdir", "new"))
Expect(err).NotTo(HaveOccurred())
Expect(link).To(Equal(filepath.Join(workingDir, "old")))
})
})
context("failure cases", func() {
context("directory cannot be created for symlink", func() {
it.Before(func() {
Expect(os.Mkdir(filepath.Join(workingDir, "subdir"), 0000)).To(Succeed())
})
it.After(func() {
Expect(os.RemoveAll(filepath.Join(workingDir, "subdir"))).To(Succeed())
})
it("returns an error", func() {
err := symlinker.Link(filepath.Join(workingDir, "old"), filepath.Join(workingDir, "subdir", "anotherdir", "new"))
Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("failed to make directory for symlink: mkdir %s", filepath.Join(workingDir, "subdir", "anotherdir"))))
})
})
context("symlink cannot be created", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "preexisting"), []byte("preexisting file contents"), os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := symlinker.Link(filepath.Join(workingDir, "old"), filepath.Join(workingDir, "preexisting"))
Expect(err).To(HaveOccurred())
})
})
})
})
context("Unlink", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(workingDir, "old"), []byte("old file contents"), os.ModePerm)).To(Succeed())
Expect(os.Symlink(filepath.Join(workingDir, "old"), filepath.Join(workingDir, "new"))).To(Succeed())
})
it("unlinks the given old location from the new location", func() {
Expect(symlinker.Unlink(filepath.Join(workingDir, "new"))).To(Succeed())
_, err := os.Readlink(filepath.Join(workingDir, "new"))
Expect(errors.Is(err, os.ErrNotExist)).To(BeTrue())
})
context("the given file doesn't exist", func() {
it("does nothing", func() {
err := symlinker.Unlink(filepath.Join(workingDir, "other"))
Expect(err).NotTo(HaveOccurred())
_, err = os.Readlink(filepath.Join(workingDir, "other"))
Expect(errors.Is(err, os.ErrNotExist)).To(BeTrue())
})
})
context("failure cases", func() {
context("the given file doesn't have a symlink", func() {
it("returns an error", func() {
err := symlinker.Unlink(filepath.Join(workingDir, "old"))
Expect(err).To(MatchError(ContainSubstring(fmt.Sprintf("cannot unlink %s because it is not a symlink", filepath.Join(workingDir, "old")))))
})
})
})
})
}