-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_test.go
145 lines (119 loc) · 3.34 KB
/
test_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
package templit_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/go-cmp/cmp"
)
// MockGitClient is a mock implementation of the GitClient interface.
type MockGitClient struct{}
// DefaultBranch returns the default branch name.
func (m *MockGitClient) DefaultBranch() string {
return "main"
}
// Clone clones a repository to the given destination.
func (m *MockGitClient) Clone(host, owner, repo, dest string) error {
src := filepath.Join(host, owner, repo)
return copyDir(src, dest)
}
// Checkout checks out a branch.
func (m *MockGitClient) Checkout(path, branch string) error {
// Mocked function, doesn't need to do anything for this test.
return nil
}
// copyDir copies a directory recursively
func copyDir(src string, dst string) error {
entries, err := os.ReadDir(src)
if err != nil {
return err
}
for _, entry := range entries {
sourcePath := filepath.Join(src, entry.Name())
destPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
err = os.MkdirAll(destPath, os.ModePerm)
if err != nil {
return err
}
err = copyDir(sourcePath, destPath)
if err != nil {
return err
}
continue
}
if err := copyFile(sourcePath, destPath); err != nil {
return err
}
}
return nil
}
// copyFile copies a file
func copyFile(src, dst string) error {
bytes, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, bytes, 0644)
}
// compareDirs recursively compares the contents of two directories.
// dir1 is the expected directory, while dir2 is the actual directory.
func compareDirs(dir1, dir2 string) error {
entries1, err := os.ReadDir(dir1)
if err != nil {
return err
}
entries2, err := os.ReadDir(dir2)
if err != nil {
return err
}
entryMap2 := make(map[string]os.DirEntry)
for _, entry := range entries2 {
entryMap2[entry.Name()] = entry
}
for _, entry1 := range entries1 {
entry2, exists := entryMap2[entry1.Name()]
if !exists && strings.HasPrefix(entry1.Name(), "-") {
continue
}
if !exists {
entryType := "file"
if entry1.IsDir() {
entryType = "directory"
}
return fmt.Errorf("missing %s in %s: %s", entryType, dir2, entry1.Name())
}
if entry1.IsDir() {
if !entry2.IsDir() {
return fmt.Errorf("expected file, but found directory in %s: %s", dir2, entry1.Name())
}
if err := compareDirs(filepath.Join(dir1, entry1.Name()), filepath.Join(dir2, entry2.Name())); err != nil {
return err
}
}
if entry1.Type().IsRegular() && entry2.Type().IsRegular() {
bytes1, err := os.ReadFile(filepath.Join(dir1, entry1.Name()))
if err != nil {
return err
}
bytes2, err := os.ReadFile(filepath.Join(dir2, entry2.Name()))
if err != nil {
return err
}
if diff := cmp.Diff(string(bytes1), string(bytes2)); diff != "" {
return fmt.Errorf("mismatch in files %s vs %s\n\t(-want +got):\n%s ", filepath.Join(dir1, entry1.Name()), filepath.Join(dir2, entry2.Name()), diff)
}
continue
}
if entry1.Type().IsRegular() || entry2.Type().IsRegular() {
return fmt.Errorf("file type mismatch for %s in %s vs %s", entry1.Name(), dir1, dir2)
}
}
// Check for any extra entries in dir2 that are not present in dir1
for _, entry2 := range entries2 {
if _, exists := entryMap2[entry2.Name()]; !exists {
return fmt.Errorf("unexpected entry in %s: %s", dir2, entry2.Name())
}
}
return nil
}