Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
54 lines (50 loc) · 1.48 KB
/
Copy patherrors_test.go
File metadata and controls
54 lines (50 loc) · 1.48 KB
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
package atomicfile
import (
"errors"
"testing"
)
func TestWritePhase_String(t *testing.T) {
t.Parallel()
tests := []struct {
name string
want string
phase WritePhase
}{
{"create", "create temp file", PhaseTempCreate},
{"write", "write temp file", PhaseTempWrite},
{"chmod", "chmod temp file", PhaseTempChmod},
{"sync", "sync temp file", PhaseTempSync},
{"close", "close temp file", PhaseTempClose},
{"rename", "rename to final path", PhaseRename},
{"zero_is_unknown", "unknown phase", WritePhase(0)},
{"out_of_range_is_unknown", "unknown phase", WritePhase(99)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := tt.phase.String(); got != tt.want {
t.Errorf("WritePhase(%d).String() = %q, want %q", int(tt.phase), got, tt.want)
}
})
}
}
func TestWriteError_Unwrap_PreservesChain(t *testing.T) {
t.Parallel()
sentinel := errors.New("underlying io failure")
we := &WriteError{Phase: PhaseTempWrite, Err: sentinel}
if !errors.Is(we, sentinel) {
t.Errorf("errors.Is(WriteError, sentinel) = false, want true")
}
if got := we.Unwrap(); got != sentinel {
t.Errorf("WriteError.Unwrap() = %v, want %v", got, sentinel)
}
}
func TestWriteError_Error_FormatsPhasePrefix(t *testing.T) {
t.Parallel()
we := &WriteError{Phase: PhaseRename, Err: errors.New("disk gone")}
got := we.Error()
want := "atomicfile: rename to final path: disk gone"
if got != want {
t.Errorf("WriteError.Error() = %q, want %q", got, want)
}
}