-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_test.go
133 lines (115 loc) · 2.82 KB
/
output_test.go
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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func withReplacedStdoutWriter(t *testing.T) *strings.Builder {
origStdoutWriter := stdoutWriter
t.Cleanup(func() {
stdoutWriter = origStdoutWriter
})
buf := &strings.Builder{}
stdoutWriter = buf
return buf
}
func TestWithOutput(t *testing.T) {
for _, tc := range []struct {
name string
path string
fn writeFunc
want string
wantStdout string
checkErr func(*testing.T, error)
}{
{
name: "stdout",
fn: func(w io.Writer) error {
io.WriteString(w, "text\n")
return nil
},
wantStdout: "text\n",
},
{
name: "normal",
path: filepath.Join(t.TempDir(), "test.txt"),
fn: func(w io.Writer) error {
io.WriteString(w, "content\n")
return nil
},
want: "content\n",
},
{
name: "target dir does not exist",
path: filepath.Join(t.TempDir(), "dir", "missing", "test.txt"),
fn: func(w io.Writer) error {
return nil
},
checkErr: func(t *testing.T, err error) {
if err == nil || !os.IsNotExist(err) {
t.Errorf("Failed with %v, want IsNotExist", err)
}
},
},
} {
t.Run(tc.name, func(t *testing.T) {
stdout := withReplacedStdoutWriter(t)
err := withOutput(tc.path, tc.fn)
if tc.checkErr != nil {
tc.checkErr(t, err)
} else if err != nil {
t.Errorf("withOutput() failed with %v", err)
}
if err == nil && tc.path != "" {
got, err := os.ReadFile(tc.path)
if err != nil {
t.Errorf("ReadFile() failed: %v", err)
}
if diff := cmp.Diff(string(got), tc.want); diff != "" {
t.Errorf("File content difference (-got +want):\n%s", diff)
}
}
if diff := cmp.Diff(stdout.String(), tc.wantStdout); diff != "" {
t.Errorf("stdout difference (-got +want):\n%s", diff)
}
})
}
}
func TestWithFileOutputPreserveMode(t *testing.T) {
for _, mode := range []os.FileMode{
0o644,
0o600,
0o755,
} {
t.Run(fmt.Sprintf("%04o", mode), func(t *testing.T) {
path := filepath.Join(t.TempDir(), "test.txt")
if fh, err := os.Create(path); err != nil {
t.Fatalf("Create(%q) failed: %v", path, err)
} else {
if err := fh.Chmod(mode); err != nil {
t.Errorf("Chmod() failed: %v", err)
}
fh.Close()
}
if err := withFileOutput(path, func(w io.Writer) error {
io.WriteString(w, "content\n")
return nil
}); err != nil {
t.Errorf("withFileOutput() failed: %v", err)
}
if fi, err := os.Lstat(path); err != nil {
t.Errorf("Lstat(%q) failed: %v", path, err)
} else if runtime.GOOS == "windows" {
// As of Go 1.19 only the 0200 bit (owner writable) is used
// (https://pkg.go.dev/os#Chmod).
} else if got := fi.Mode() & os.ModePerm; got != mode {
t.Errorf("Got file mode %04o, want %04o", got, mode)
}
})
}
}