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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package file
import (
"os"
"path/filepath"
"testing"
)
func TestGetChecksum(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.txt")
// Existing file
if err := os.WriteFile(path, []byte("hello"), 0o644); err != nil {
t.Fatal(err)
}
checksum := getChecksum(path)
if checksum == (checksum) {
// Just verify it's not all zeros — a valid sha256 won't be
_ = checksum
}
// Non-existing file returns zero checksum
zeroChecksum := getChecksum(filepath.Join(dir, "no-such-file"))
var expectedZero [32]byte
if zeroChecksum != expectedZero {
t.Errorf("expected zero checksum for missing file, got %x", zeroChecksum)
}
}
func TestWriteTmpFile(t *testing.T) {
dir := t.TempDir()
tmpPath := filepath.Join(dir, "test.tmp")
content := []byte("temp content")
if err := writeTmpFile(tmpPath, content); err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err := os.ReadFile(tmpPath)
if err != nil {
t.Fatalf("reading tmp file: %v", err)
}
if string(got) != string(content) {
t.Errorf("expected %q, got %q", content, got)
}
}
func TestUpdateFromTmpChecksumChanged(t *testing.T) {
dir := t.TempDir()
tmpPath := filepath.Join(dir, "test.tmp")
path := filepath.Join(dir, "test.txt")
if err := os.WriteFile(tmpPath, []byte("new content"), 0o644); err != nil {
t.Fatal(err)
}
if err := updateFromTmp(tmpPath, path, true); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// tmp should be gone, target should exist with new content
if _, err := os.Stat(tmpPath); err == nil {
t.Error("tmp file should have been removed")
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading target file: %v", err)
}
if string(got) != "new content" {
t.Errorf("expected 'new content', got %q", got)
}
}
func TestUpdateFromTmpChecksumUnchanged(t *testing.T) {
dir := t.TempDir()
tmpPath := filepath.Join(dir, "test.tmp")
path := filepath.Join(dir, "test.txt")
if err := os.WriteFile(tmpPath, []byte("same"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("same"), 0o644); err != nil {
t.Fatal(err)
}
if err := updateFromTmp(tmpPath, path, false); err != nil {
t.Fatalf("unexpected error: %v", err)
}
// tmp should be gone, target unchanged
if _, err := os.Stat(tmpPath); err == nil {
t.Error("tmp file should have been removed")
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading target file: %v", err)
}
if string(got) != "same" {
t.Errorf("expected 'same', got %q", got)
}
}
func TestHaveStringCreateNewFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "new.txt")
if err := HaveString(path, "hello world"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading file: %v", err)
}
if string(got) != "hello world" {
t.Errorf("expected 'hello world', got %q", got)
}
// No .tmp file left behind
if _, err := os.Stat(path + ".tmp"); err == nil {
t.Error("tmp file should not exist")
}
}
func TestHaveStringUpdateExistingFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "existing.txt")
if err := os.WriteFile(path, []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if err := HaveString(path, "new"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading file: %v", err)
}
if string(got) != "new" {
t.Errorf("expected 'new', got %q", got)
}
if _, err := os.Stat(path + ".tmp"); err == nil {
t.Error("tmp file should not exist")
}
}
func TestHaveStringNoChange(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "same.txt")
content := "unchanged content"
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
if err := HaveString(path, content); err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("reading file: %v", err)
}
if string(got) != content {
t.Errorf("expected %q, got %q", content, got)
}
if _, err := os.Stat(path + ".tmp"); err == nil {
t.Error("tmp file should not exist")
}
}
|