Skip to content

Commit 7a61b19

Browse files
committed
Improve config tests
1 parent 53503f9 commit 7a61b19

File tree

5 files changed

+161
-120
lines changed

5 files changed

+161
-120
lines changed

src/config/parse_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestParsingConfig(t *testing.T) {
3333
"target/",
3434
".angular/",
3535
".next/",
36+
"*.partial",
3637
},
3738
Soft: []string{
3839
"*.bak",

src/models/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package models
33
import (
44
"hudson-newey/2rm/src/util"
55
"path/filepath"
6+
"strings"
67
)
78

89
type Config struct {
@@ -77,6 +78,13 @@ func (config Config) SoftDeleteDir() string {
7778
}
7879

7980
func matchesPattern(pattern string, path string) bool {
81+
// we put a wildcard at the start so that we don't have to match full
82+
// paths
83+
isAbsolutePath := strings.HasPrefix(path, "/")
84+
if !isAbsolutePath {
85+
pattern = "*" + pattern
86+
}
87+
8088
matched, _ := filepath.Match(pattern, path)
8189
return matched
8290
}

src/models/config_test.go

+147-120
Original file line numberDiff line numberDiff line change
@@ -13,134 +13,161 @@ func loadConfig(path string) models.Config {
1313
return config.ParseConfig(absolutePath)
1414
}
1515

16-
func TestShouldHardDelete(t *testing.T) {
17-
testedConfig := loadConfig("valid.yml")
18-
19-
expected := true
20-
realized := testedConfig.ShouldHardDelete("node_modules/")
21-
22-
if expected != realized {
23-
t.Fatalf("Expected %v but got %v", expected, realized)
24-
}
25-
}
26-
27-
func TestShouldNotHardDelete(t *testing.T) {
28-
testedConfig := loadConfig("valid.yml")
29-
30-
expected := false
31-
realized := testedConfig.ShouldHardDelete("src/")
32-
33-
if expected != realized {
34-
t.Fatalf("Expected %v but got %v", expected, realized)
35-
}
36-
}
37-
38-
func TestShouldHardDeleteEmpty(t *testing.T) {
39-
testedConfig := loadConfig("only_backups.yml")
40-
41-
expected := false
42-
realized := testedConfig.ShouldHardDelete("node_modules/")
43-
44-
if expected != realized {
45-
t.Fatalf("Expected %v but got %v", expected, realized)
46-
}
47-
}
48-
49-
func TestShouldSoftDelete(t *testing.T) {
50-
testedConfig := loadConfig("valid.yml")
51-
52-
expected := true
53-
realized := testedConfig.ShouldSoftDelete("file.bak")
54-
55-
if expected != realized {
56-
t.Fatalf("Expected %v but got %v", expected, realized)
57-
}
58-
}
59-
60-
func TestShouldNotSoftDelete(t *testing.T) {
61-
testedConfig := loadConfig("valid.yml")
62-
63-
expected := false
64-
realized := testedConfig.ShouldSoftDelete("file.txt")
65-
66-
if expected != realized {
67-
t.Fatalf("Expected %v but got %v", expected, realized)
68-
}
69-
}
70-
71-
func TestShouldSoftDeleteEmpty(t *testing.T) {
72-
testedConfig := loadConfig("only_backups.yml")
73-
74-
expected := false
75-
realized := testedConfig.ShouldSoftDelete("file.bak")
76-
77-
if expected != realized {
78-
t.Fatalf("Expected %v but got %v", expected, realized)
79-
}
80-
}
81-
82-
func TestHardMatchesAbsolutePath(t *testing.T) {
83-
testedConfig := loadConfig("abs_path.yml")
84-
85-
expected := true
86-
realized := testedConfig.ShouldHardDelete("/tmp/2rm/")
87-
88-
if expected != realized {
89-
t.Fatalf("Expected %v but got %v", expected, realized)
90-
}
91-
}
92-
93-
func TestSoftMatchesAbsolutePath(t *testing.T) {
94-
testedConfig := loadConfig("abs_path.yml")
95-
96-
expected := true
97-
realized := testedConfig.ShouldSoftDelete("/home/john-doe/.local/share/2rm/config.yml")
98-
99-
if expected != realized {
100-
t.Fatalf("Expected %v but got %v", expected, realized)
16+
func assertConfig(
17+
t *testing.T,
18+
configPath string,
19+
configFunction func(models.Config, string) bool,
20+
testedPath string,
21+
expectedResult bool,
22+
) {
23+
expectedConfig := expectedResult
24+
25+
testedConfig := loadConfig(configPath)
26+
realizedConfig := configFunction(testedConfig, testedPath)
27+
28+
if expectedConfig != realizedConfig {
29+
t.Fatalf("Expected %v but got %v", expectedConfig, realizedConfig)
10130
}
10231
}
10332

104-
func TestIsProtected(t *testing.T) {
105-
testedConfig := loadConfig("valid.yml")
106-
107-
expected := true
108-
realized := testedConfig.IsProtected(".ssh/")
109-
110-
if expected != realized {
111-
t.Fatalf("Expected %v but got %v", expected, realized)
33+
func runTests(t *testing.T, tests []Test) {
34+
for _, test := range tests {
35+
t.Run(test.name, func(t *testing.T) {
36+
assertConfig(t, test.configPath, test.configFunction, test.testedPath, test.expectedResult)
37+
})
11238
}
11339
}
11440

115-
func TestNotProtected(t *testing.T) {
116-
testedConfig := loadConfig("valid.yml")
117-
118-
expected := false
119-
realized := testedConfig.IsProtected("src/")
120-
121-
if expected != realized {
122-
t.Fatalf("Expected %v but got %v", expected, realized)
123-
}
41+
type Test struct {
42+
name string
43+
configPath string
44+
configFunction func(models.Config, string) bool
45+
testedPath string
46+
expectedResult bool
12447
}
12548

126-
func TestShouldOverwrite(t *testing.T) {
127-
testedConfig := loadConfig("valid.yml")
128-
129-
expected := true
130-
realized := testedConfig.ShouldOverwrite(".ssh/test.pem")
131-
132-
if expected != realized {
133-
t.Fatalf("Expected %v but got %v", expected, realized)
49+
func TestConfig(t *testing.T) {
50+
tests := []Test{
51+
// hard deletes
52+
{
53+
name: "HardDelete",
54+
configPath: "valid.yml",
55+
configFunction: models.Config.ShouldHardDelete,
56+
testedPath: "node_modules/",
57+
expectedResult: true,
58+
},
59+
{
60+
name: "NotHardDelete",
61+
configPath: "valid.yml",
62+
configFunction: models.Config.ShouldHardDelete,
63+
testedPath: "src/",
64+
expectedResult: false,
65+
},
66+
{
67+
name: "HardDeleteEmpty",
68+
configPath: "only_backups.yml",
69+
configFunction: models.Config.ShouldHardDelete,
70+
testedPath: "node_modules/",
71+
expectedResult: false,
72+
},
73+
{
74+
name: "HardMatchesAbsolutePath",
75+
configPath: "abs_path.yml",
76+
configFunction: models.Config.ShouldHardDelete,
77+
testedPath: "/tmp/2rm/",
78+
expectedResult: true,
79+
},
80+
81+
// soft deletes
82+
{
83+
name: "SoftDelete",
84+
configPath: "valid.yml",
85+
configFunction: models.Config.ShouldSoftDelete,
86+
testedPath: "file.bak",
87+
expectedResult: true,
88+
},
89+
{
90+
name: "NotSoftDelete",
91+
configPath: "valid.yml",
92+
configFunction: models.Config.ShouldSoftDelete,
93+
testedPath: "file.txt",
94+
expectedResult: false,
95+
},
96+
{
97+
name: "SoftDeleteEmpty",
98+
configPath: "only_backups.yml",
99+
configFunction: models.Config.ShouldSoftDelete,
100+
testedPath: "file.bak",
101+
expectedResult: false,
102+
},
103+
{
104+
name: "SoftMatchesAbsolutePath",
105+
configPath: "abs_path.yml",
106+
configFunction: models.Config.ShouldSoftDelete,
107+
testedPath: "/home/john-doe/.local/share/2rm/config.yml",
108+
expectedResult: true,
109+
},
110+
111+
// protected files
112+
{
113+
name: "IsProtected",
114+
configPath: "valid.yml",
115+
configFunction: models.Config.IsProtected,
116+
testedPath: ".ssh/",
117+
expectedResult: true,
118+
},
119+
{
120+
name: "NotProtected",
121+
configPath: "valid.yml",
122+
configFunction: models.Config.IsProtected,
123+
testedPath: "src/",
124+
expectedResult: false,
125+
},
126+
{
127+
name: "ProtectedEmpty",
128+
configPath: "only_backups.yml",
129+
configFunction: models.Config.IsProtected,
130+
testedPath: ".ssh/",
131+
expectedResult: false,
132+
},
133+
{
134+
name: "ProtectedMatchesAbsolutePath",
135+
configPath: "abs_path.yml",
136+
configFunction: models.Config.IsProtected,
137+
testedPath: "/home/john-doe/.ssh/id_rsa",
138+
expectedResult: true,
139+
},
140+
141+
// overwrite
142+
{
143+
name: "Overwrite",
144+
configPath: "valid.yml",
145+
configFunction: models.Config.ShouldOverwrite,
146+
testedPath: ".ssh/test.pem",
147+
expectedResult: true,
148+
},
149+
{
150+
name: "DontOverwrite",
151+
configPath: "valid.yml",
152+
configFunction: models.Config.ShouldOverwrite,
153+
testedPath: "non-existent.txt",
154+
expectedResult: false,
155+
},
156+
{
157+
name: "OverwriteEmpty",
158+
configPath: "only_backups.yml",
159+
configFunction: models.Config.ShouldOverwrite,
160+
testedPath: ".ssh/test.pem",
161+
expectedResult: false,
162+
},
163+
{
164+
name: "OverwriteAbsolutePath",
165+
configPath: "abs_path.yml",
166+
configFunction: models.Config.ShouldOverwrite,
167+
testedPath: "/home/john-doe/.ssh/key.pem",
168+
expectedResult: true,
169+
},
134170
}
135-
}
136171

137-
func TestNotShouldOverwrite(t *testing.T) {
138-
testedConfig := loadConfig("valid.yml")
139-
140-
expected := false
141-
realized := testedConfig.ShouldOverwrite("non-existent.txt")
142-
143-
if expected != realized {
144-
t.Fatalf("Expected %v but got %v", expected, realized)
145-
}
172+
runTests(t, tests)
146173
}

tests/assets/configs/abs_path.yml

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ hard:
22
- "/tmp/2rm/"
33
soft:
44
- "/home/john-doe/.local/share/2rm/config.yml"
5+
protected:
6+
- "/home/john-doe/.ssh/id_rsa"
7+
overwrite:
8+
- "/home/john-doe/.ssh/key.pem"

tests/assets/configs/valid.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ hard:
66
- "target/"
77
- ".angular/"
88
- ".next/"
9+
- "*.partial"
910
soft:
1011
- "*.bak"
1112
protected:

0 commit comments

Comments
 (0)