@@ -10,6 +10,8 @@ package util
10
10
11
11
import (
12
12
"bytes"
13
+ "os"
14
+ "path/filepath"
13
15
"testing"
14
16
)
15
17
@@ -55,8 +57,51 @@ func TestConformingFileName(t *testing.T) {
55
57
}
56
58
}
57
59
58
- func TestNWriter (t * testing.T ) {
60
+ func TestIDMatchesFilename (t * testing.T ) {
61
+ pathEval := NewPathEval ()
62
+
63
+ doc := make (map [string ]any )
64
+ doc ["document" ] = map [string ]any {
65
+ "tracking" : map [string ]any {
66
+ "id" : "valid.json" ,
67
+ },
68
+ }
69
+
70
+ if err := IDMatchesFilename (pathEval , doc , "valid.json" ); err != nil {
71
+ t .Errorf ("IDMatchesFilename: Expected nil, got %q" , err )
72
+ }
73
+
74
+ if err := IDMatchesFilename (pathEval , doc , "different_file_name.json" ); err == nil {
75
+ t .Error ("IDMatchesFilename: Expected error, got nil" )
76
+ }
59
77
78
+ doc ["document" ] = map [string ]any {
79
+ "tracking" : map [string ]any {},
80
+ }
81
+ if err := IDMatchesFilename (pathEval , doc , "valid.json" ); err == nil {
82
+ t .Error ("IDMatchesFilename: Expected error, got nil" )
83
+ }
84
+ }
85
+
86
+ func TestPathExists (t * testing.T ) {
87
+ got , err := PathExists ("/this/path/does/not/exist" )
88
+ if err != nil {
89
+ t .Error (err )
90
+ }
91
+ if got != false {
92
+ t .Error ("PathExists: Expected false, got true" )
93
+ }
94
+ dir := t .TempDir ()
95
+ got , err = PathExists (dir )
96
+ if err != nil {
97
+ t .Error (err )
98
+ }
99
+ if got != true {
100
+ t .Error ("PathExists: Expected true, got false" )
101
+ }
102
+ }
103
+
104
+ func TestNWriter (t * testing.T ) {
60
105
msg := []byte ("Gruß!\n " )
61
106
62
107
first , second := msg [:len (msg )/ 2 ], msg [len (msg )/ 2 :]
@@ -78,3 +123,93 @@ func TestNWriter(t *testing.T) {
78
123
t .Errorf ("Expected %q, but got %q" , msg , out )
79
124
}
80
125
}
126
+
127
+ func TestWriteToFile (t * testing.T ) {
128
+ filename := filepath .Join (t .TempDir (), "test_file" )
129
+ wt := bytes .NewBufferString ("test_data" )
130
+ if err := WriteToFile (filename , wt ); err != nil {
131
+ t .Error (err )
132
+ }
133
+ fileData , err := os .ReadFile (filename )
134
+ if err != nil {
135
+ t .Error (err )
136
+ }
137
+ if ! bytes .Equal (fileData , []byte ("test_data" )) {
138
+ t .Errorf ("DeepCopy: Expected test_data, got %v" , fileData )
139
+ }
140
+ }
141
+
142
+ func TestMakeUniqFile (t * testing.T ) {
143
+ dir := t .TempDir ()
144
+ _ , file , err := MakeUniqFile (dir )
145
+ if err != nil {
146
+ t .Error (err )
147
+ }
148
+ if _ , err = file .Write ([]byte ("test_data" )); err != nil {
149
+ t .Error (err )
150
+ }
151
+ if err = file .Close (); err != nil {
152
+ t .Error (err )
153
+ }
154
+ }
155
+
156
+ func Test_mkUniq (t * testing.T ) {
157
+ dir := t .TempDir ()
158
+ name , err := mkUniq (dir + "/" , func (name string ) error {
159
+ return nil
160
+ })
161
+ if err != nil {
162
+ t .Error (err )
163
+ }
164
+ firstTime := true
165
+ name1 , err := mkUniq (dir + "/" , func (_ string ) error {
166
+ if firstTime {
167
+ firstTime = false
168
+ return os .ErrExist
169
+ }
170
+ return nil
171
+ })
172
+ if err != nil {
173
+ t .Error (err )
174
+ }
175
+ if name == name1 {
176
+ t .Errorf ("mkUniq: Expected unique names, got %v and %v" , name , name1 )
177
+ }
178
+ }
179
+
180
+ func TestDeepCopy (t * testing.T ) {
181
+ dir := t .TempDir ()
182
+ if err := os .MkdirAll (filepath .Join (dir , "src/folder0" ), 0755 ); err != nil {
183
+ t .Fatal (err )
184
+ }
185
+ if err := os .MkdirAll (filepath .Join (dir , "dst" ), 0755 ); err != nil {
186
+ t .Fatal (err )
187
+ }
188
+ if err := os .MkdirAll (filepath .Join (dir , "dst1" ), 0755 ); err != nil {
189
+ t .Fatal (err )
190
+ }
191
+ if err := os .WriteFile (filepath .Join (dir , "src/folder0/test_file" ), []byte ("test_data" ), 0755 ); err != nil {
192
+ t .Fatal (err )
193
+ }
194
+
195
+ if err := DeepCopy (filepath .Join (dir , "dst" ), filepath .Join (dir , "src" )); err != nil {
196
+ t .Error (err )
197
+ }
198
+
199
+ fileData , err := os .ReadFile (filepath .Join (dir , "dst/folder0/test_file" ))
200
+ if err != nil {
201
+ t .Error (err )
202
+ }
203
+
204
+ if ! bytes .Equal (fileData , []byte ("test_data" )) {
205
+ t .Errorf ("DeepCopy: Expected test_data, got %v" , fileData )
206
+ }
207
+
208
+ if err = DeepCopy ("/path/does/not/exist" , filepath .Join (dir , "src" )); err == nil {
209
+ t .Error ("DeepCopy: Expected error, got nil" )
210
+ }
211
+
212
+ if err = DeepCopy (filepath .Join (dir , "dst1" ), "/path/does/not/exist" ); err == nil {
213
+ t .Error ("DeepCopy: Expected error, got nil" )
214
+ }
215
+ }
0 commit comments