-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_test.go
More file actions
159 lines (111 loc) · 3.52 KB
/
Copy pathconfig_test.go
File metadata and controls
159 lines (111 loc) · 3.52 KB
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
153
154
155
156
157
158
159
package main
import (
"encoding/json"
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig_AddProfile(t *testing.T) {
t.Parallel()
conf := NewConfig(newDiscardLogger(), "", "")
conf.AddProfile("/some/path")
require.Len(t, conf.data.Profiles, 1)
assert.Equal(t, "/some/path", conf.data.Profiles[0].Path)
}
func TestConfig_AddProfile_NoDuplicates(t *testing.T) {
t.Parallel()
conf := NewConfig(newDiscardLogger(), "", "")
conf.AddProfile("/some/path")
conf.AddProfile("/some/path")
assert.Len(t, conf.data.Profiles, 1)
}
func TestConfig_GetProfilePaths(t *testing.T) {
t.Parallel()
conf := NewConfig(newDiscardLogger(), "", "")
conf.AddProfile("/path/one")
conf.AddProfile("/path/two")
paths := conf.GetProfilePaths()
require.Len(t, paths, 2)
assert.Equal(t, "/path/one", paths[0])
assert.Equal(t, "/path/two", paths[1])
}
func TestConfig_Load_NotExists(t *testing.T) {
t.Parallel()
conf := NewConfig(
newDiscardLogger(),
"testdata/non_existent_config.json",
"testdata/non_existent_profile.json",
)
require.NoError(t, conf.Load(t.Context()))
assert.Empty(t, conf.data.Profiles)
}
func TestConfig_Load_Exists(t *testing.T) {
t.Parallel()
conf := NewConfig(
newDiscardLogger(),
"testdata/config.json",
"testdata/non_existent_profile.json",
)
require.NoError(t, conf.Load(t.Context()))
require.Len(t, conf.data.Profiles, 1)
assert.Equal(t, "/test/profile/path", conf.data.Profiles[0].Path)
}
func TestConfig_Load_MigrationPaths(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.json")
legacyPath := filepath.Join(tmpDir, "profile.json")
copyFile(t, "testdata/legacy_profile_1.json", legacyPath)
conf := NewConfig(newDiscardLogger(), configPath, legacyPath)
require.NoError(t, conf.Load(t.Context()))
// Validate - config.json should exist, profile.json should not
assert.FileExists(t, configPath)
assert.NoFileExists(t, legacyPath)
require.Len(t, conf.data.Profiles, 2)
assert.Equal(t, "/first/path", conf.data.Profiles[0].Path)
assert.Equal(t, "/second/path", conf.data.Profiles[1].Path)
}
func TestConfig_Load_MigrationPath(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.json")
legacyPath := filepath.Join(tmpDir, "profile.json")
copyFile(t, "testdata/legacy_profile_2.json", legacyPath)
conf := NewConfig(newDiscardLogger(), configPath, legacyPath)
require.NoError(t, conf.Load(t.Context()))
// Validate - config.json should exist, profile.json should not
assert.FileExists(t, configPath)
assert.NoFileExists(t, legacyPath)
require.Len(t, conf.data.Profiles, 1)
assert.Equal(t, "/one/path", conf.data.Profiles[0].Path)
}
func TestConfig_Save(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.json")
conf := NewConfig(newDiscardLogger(), configPath, "")
conf.AddProfile("/test/profile/path")
require.NoError(t, conf.Save(t.Context()))
// Validate
var data ConfigData
f, err := os.Open(configPath)
require.NoError(t, err)
require.NoError(t, json.NewDecoder(f).Decode(&data))
f.Close()
require.Len(t, data.Profiles, 1)
assert.Equal(t, "/test/profile/path", data.Profiles[0].Path)
}
func copyFile(t *testing.T, src, dst string) {
t.Helper()
srcFile, err := os.Open(src)
require.NoError(t, err)
defer srcFile.Close()
dstFile, err := os.Create(dst)
require.NoError(t, err)
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
require.NoError(t, err)
}