-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffer_test.go
More file actions
120 lines (105 loc) · 2.75 KB
/
differ_test.go
File metadata and controls
120 lines (105 loc) · 2.75 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
package main
import (
"testing"
)
func TestCompareEnvFiles(t *testing.T) {
t.Run("detect missing variables", func(t *testing.T) {
env1 := &EnvFile{
Path: "file1",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
},
}
env2 := &EnvFile{
Path: "file2",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
"KEY2": {Key: "KEY2", Value: "val2", Line: 2},
},
}
diff := CompareEnvFiles(env1, env2, "file1", "file2")
if len(diff.Missing) != 1 {
t.Errorf("expected 1 missing var, got %d", len(diff.Missing))
}
if diff.Missing[0].Key != "KEY2" {
t.Errorf("expected missing KEY2, got %s", diff.Missing[0].Key)
}
})
t.Run("detect extra variables", func(t *testing.T) {
env1 := &EnvFile{
Path: "file1",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
"KEY2": {Key: "KEY2", Value: "val2", Line: 2},
},
}
env2 := &EnvFile{
Path: "file2",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
},
}
diff := CompareEnvFiles(env1, env2, "file1", "file2")
if len(diff.Extra) != 1 {
t.Errorf("expected 1 extra var, got %d", len(diff.Extra))
}
})
t.Run("detect mismatched values", func(t *testing.T) {
env1 := &EnvFile{
Path: "file1",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
},
}
env2 := &EnvFile{
Path: "file2",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val2", Line: 1},
},
}
diff := CompareEnvFiles(env1, env2, "file1", "file2")
if len(diff.Mismatched) != 1 {
t.Errorf("expected 1 mismatch, got %d", len(diff.Mismatched))
}
})
t.Run("identical files", func(t *testing.T) {
env1 := &EnvFile{
Path: "file1",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
},
}
env2 := &EnvFile{
Path: "file2",
Vars: map[string]EnvVar{
"KEY1": {Key: "KEY1", Value: "val1", Line: 1},
},
}
diff := CompareEnvFiles(env1, env2, "file1", "file2")
if diff.HasDifferences() {
t.Error("expected no differences for identical files")
}
if len(diff.Matching) != 1 {
t.Errorf("expected 1 matching var, got %d", len(diff.Matching))
}
})
}
func TestHasDifferences(t *testing.T) {
tests := []struct {
name string
diff *DiffResult
want bool
}{
{"no diffs", &DiffResult{}, false},
{"has missing", &DiffResult{Missing: []EnvVar{{Key: "K"}}}, true},
{"has extra", &DiffResult{Extra: []EnvVar{{Key: "K"}}}, true},
{"has mismatch", &DiffResult{Mismatched: []MismatchedVar{{Key: "K"}}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.diff.HasDifferences(); got != tt.want {
t.Errorf("HasDifferences() = %v, want %v", got, tt.want)
}
})
}
}