Skip to content

Commit d2eec33

Browse files
authored
Merge pull request #538 from tomhuang12/fix-defer-close
Update file close operation to not use defer and add test case for CopyFromPath
2 parents 07d1a4f + 5bb4283 commit d2eec33

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

controllers/storage.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,13 @@ func (s *Storage) CopyFromPath(artifact *sourcev1.Artifact, path string) (err er
355355
if err != nil {
356356
return err
357357
}
358-
defer f.Close()
359-
return s.Copy(artifact, f)
358+
defer func() {
359+
if cerr := f.Close(); cerr != nil && err == nil {
360+
err = cerr
361+
}
362+
}()
363+
err = s.Copy(artifact, f)
364+
return err
360365
}
361366

362367
// CopyToPath copies the contents in the (sub)path of the given artifact to the given path.

controllers/storage_test.go

+105
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,108 @@ func TestStorageRemoveAllButCurrent(t *testing.T) {
259259
}
260260
})
261261
}
262+
263+
func TestStorageCopyFromPath(t *testing.T) {
264+
type File struct {
265+
Name string
266+
Content []byte
267+
}
268+
269+
dir, err := createStoragePath()
270+
if err != nil {
271+
t.Fatal(err)
272+
}
273+
t.Cleanup(cleanupStoragePath(dir))
274+
275+
storage, err := NewStorage(dir, "hostname", time.Minute)
276+
if err != nil {
277+
t.Fatalf("error while bootstrapping storage: %v", err)
278+
}
279+
280+
createFile := func(file *File) (absPath string, err error) {
281+
defer func() {
282+
if err != nil && dir != "" {
283+
os.RemoveAll(dir)
284+
}
285+
}()
286+
dir, err = os.MkdirTemp("", "test-files-")
287+
if err != nil {
288+
return
289+
}
290+
absPath = filepath.Join(dir, file.Name)
291+
if err = os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
292+
return
293+
}
294+
f, err := os.Create(absPath)
295+
if err != nil {
296+
return "", fmt.Errorf("could not create file %q: %w", absPath, err)
297+
}
298+
if n, err := f.Write(file.Content); err != nil {
299+
f.Close()
300+
return "", fmt.Errorf("could not write %d bytes to file %q: %w", n, f.Name(), err)
301+
}
302+
f.Close()
303+
return
304+
}
305+
306+
matchFile := func(t *testing.T, storage *Storage, artifact sourcev1.Artifact, file *File, expectMismatch bool) {
307+
c, err := os.ReadFile(storage.LocalPath(artifact))
308+
if err != nil {
309+
t.Fatalf("failed reading file: %v", err)
310+
}
311+
if (string(c) != string(file.Content)) != expectMismatch {
312+
t.Errorf("artifact content does not match and not expecting mismatch, got: %q, want: %q", string(c), string(file.Content))
313+
}
314+
}
315+
316+
tests := []struct {
317+
name string
318+
file *File
319+
want *File
320+
expectMismatch bool
321+
}{
322+
{
323+
name: "content match",
324+
file: &File{
325+
Name: "manifest.yaml",
326+
Content: []byte(`contents`),
327+
},
328+
want: &File{
329+
Name: "manifest.yaml",
330+
Content: []byte(`contents`),
331+
},
332+
},
333+
{
334+
name: "content not match",
335+
file: &File{
336+
Name: "manifest.yaml",
337+
Content: []byte(`contents`),
338+
},
339+
want: &File{
340+
Name: "manifest.yaml",
341+
Content: []byte(`mismatch contents`),
342+
},
343+
expectMismatch: true,
344+
},
345+
}
346+
for _, tt := range tests {
347+
t.Run(tt.name, func(t *testing.T) {
348+
absPath, err := createFile(tt.file)
349+
if err != nil {
350+
t.Error(err)
351+
return
352+
}
353+
defer os.RemoveAll(absPath)
354+
artifact := sourcev1.Artifact{
355+
Path: filepath.Join(randStringRunes(10), randStringRunes(10), randStringRunes(10)),
356+
}
357+
if err := storage.MkdirAll(artifact); err != nil {
358+
t.Fatalf("artifact directory creation failed: %v", err)
359+
}
360+
if err := storage.CopyFromPath(&artifact, absPath); err != nil {
361+
t.Errorf("CopyFromPath() error = %v", err)
362+
}
363+
matchFile(t, storage, artifact, tt.want, tt.expectMismatch)
364+
})
365+
}
366+
}

0 commit comments

Comments
 (0)