-
Notifications
You must be signed in to change notification settings - Fork 0
/
symlink_test.go
152 lines (129 loc) · 4.23 KB
/
symlink_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/limero/offlinerss/client"
"github.com/limero/offlinerss/client/newsboat"
"github.com/limero/offlinerss/helpers"
"github.com/limero/offlinerss/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func CaptureStdout(f func()) string {
originalStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() {
os.Stdout = originalStdout
}()
f()
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func Test_symlinkClientPaths(t *testing.T) {
t.Setenv("DEBUG", "true")
sourceDir := t.TempDir()
dataPath := models.DataPath(sourceDir)
sourceFile := "source-file"
sourcePath := dataPath.GetFile(sourceFile)
helpers.WriteFile("hello", sourcePath)
setupClients := func(targetPaths ...string) []models.Client {
client := client.Client{
DataPath: dataPath,
Files: models.ClientFiles{
{
FileName: sourceFile,
TargetPaths: targetPaths,
},
},
}
return []models.Client{
newsboat.Newsboat{client},
}
}
t.Run("should link if link doesn't exist", func(t *testing.T) {
targetPath := filepath.Join(t.TempDir(), "new-link")
clients := setupClients(targetPath)
output := CaptureStdout(func() {
require.NoError(t, symlinkClientPaths(clients))
})
assert.Contains(t, output, "Symlinking")
dest, err := os.Readlink(targetPath)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest)
})
t.Run("should create subdirs and link if link and dirs doesn't exist", func(t *testing.T) {
targetPath := filepath.Join(t.TempDir(), "sub/dir/new-link")
clients := setupClients(targetPath)
output := CaptureStdout(func() {
require.NoError(t, symlinkClientPaths(clients))
})
assert.Contains(t, output, "Symlinking")
dest, err := os.Readlink(targetPath)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest)
})
t.Run("should link multiple if links doesn't exist", func(t *testing.T) {
targetPath1 := filepath.Join(t.TempDir(), "new-link1")
targetPath2 := filepath.Join(t.TempDir(), "new-link2")
clients := setupClients([]string{targetPath1, targetPath2}...)
output := CaptureStdout(func() {
require.NoError(t, symlinkClientPaths(clients))
})
assert.Contains(t, output, "Symlinking")
assert.Equal(t, 2, strings.Count(output, "Symlinking"))
dest1, err := os.Readlink(targetPath1)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest1)
dest2, err := os.Readlink(targetPath1)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest2)
})
t.Run("should skip if link exists and is correct", func(t *testing.T) {
targetPath := filepath.Join(t.TempDir(), "existing-link")
require.NoError(t, os.Symlink(sourcePath, targetPath))
clients := setupClients(targetPath)
output := CaptureStdout(func() {
require.NoError(t, symlinkClientPaths(clients))
})
assert.Contains(t, output, "is already correct")
dest, err := os.Readlink(targetPath)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest)
})
t.Run("should unlink and link if link exists but is incorrect", func(t *testing.T) {
incorrectSourcePath := dataPath.GetFile("incorrect")
helpers.WriteFile("incorrect", incorrectSourcePath)
targetPath := filepath.Join(t.TempDir(), "existing-incorrect-link")
require.NoError(t, os.Symlink(incorrectSourcePath, targetPath))
clients := setupClients(targetPath)
output := CaptureStdout(func() {
require.NoError(t, symlinkClientPaths(clients))
})
assert.Contains(t, output, "Removing incorrect symlink")
assert.Contains(t, output, "Symlinking")
dest, err := os.Readlink(targetPath)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest)
})
t.Run("should rename and link if path exists and is file", func(t *testing.T) {
targetPath := filepath.Join(t.TempDir(), "existing-file")
helpers.WriteFile("different", targetPath)
clients := setupClients(targetPath)
output := CaptureStdout(func() {
require.NoError(t, symlinkClientPaths(clients))
})
assert.Contains(t, output, "Non-symlink found at target")
assert.Contains(t, output, "Symlinking")
assert.True(t, helpers.FileExists(targetPath+".bak"))
dest, err := os.Readlink(targetPath)
require.NoError(t, err)
assert.Equal(t, sourcePath, dest)
})
}