summaryrefslogtreecommitdiff
path: root/internal/showcase/images_test.go
blob: c88da00afd2426cac10d3b5b13d494b6c11d5c5f (plain)
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
package showcase

import (
	"os"
	"path/filepath"
	"testing"
)

// TestCopyFile_CopiesContent verifies the basic success path still works
// after copyFile was changed to use a named return so that a destination
// Close() error can be surfaced without changing the happy-path result.
func TestCopyFile_CopiesContent(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	src := filepath.Join(dir, "source.txt")
	dst := filepath.Join(dir, "dest.txt")
	want := "hello showcase\n"

	if err := os.WriteFile(src, []byte(want), 0644); err != nil {
		t.Fatalf("failed to write source file: %v", err)
	}

	if err := copyFile(src, dst); err != nil {
		t.Fatalf("copyFile() returned error: %v", err)
	}

	got, err := os.ReadFile(dst)
	if err != nil {
		t.Fatalf("failed to read destination file: %v", err)
	}
	if string(got) != want {
		t.Fatalf("copyFile() wrote %q, want %q", string(got), want)
	}
}

// TestCopyFile_MissingSourceReturnsError verifies that a missing source file
// still produces an error (and never a nil error masked by the deferred
// destination-close handling) and does not leave a destination file behind.
func TestCopyFile_MissingSourceReturnsError(t *testing.T) {
	t.Parallel()

	dir := t.TempDir()
	src := filepath.Join(dir, "does-not-exist.txt")
	dst := filepath.Join(dir, "dest.txt")

	err := copyFile(src, dst)
	if err == nil {
		t.Fatal("copyFile() with missing source returned nil error, want non-nil")
	}

	if _, statErr := os.Stat(dst); statErr == nil {
		t.Fatal("copyFile() with missing source created a destination file, want none")
	}
}