forked from uber-go/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
125 lines (110 loc) · 3.67 KB
/
writer_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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package zap
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)
func TestOpenNoPaths(t *testing.T) {
ws, cleanup, err := Open()
defer cleanup()
assert.NoError(t, err, "Expected opening no paths to succeed.")
assert.Equal(
t,
zapcore.AddSync(ioutil.Discard),
ws,
"Expected opening no paths to return a no-op WriteSyncer.",
)
}
func TestOpen(t *testing.T) {
temp, err := ioutil.TempFile("", "zap-open-test")
require.NoError(t, err, "Couldn't create a temporary file for test.")
defer os.Remove(temp.Name())
tests := []struct {
paths []string
filenames []string
error string
}{
{[]string{"stdout"}, []string{os.Stdout.Name()}, ""},
{[]string{"stderr"}, []string{os.Stderr.Name()}, ""},
{[]string{temp.Name()}, []string{temp.Name()}, ""},
{[]string{"/foo/bar/baz"}, []string{}, "open /foo/bar/baz: no such file or directory"},
{
paths: []string{"stdout", "/foo/bar/baz", temp.Name(), "/baz/quux"},
filenames: []string{os.Stdout.Name(), temp.Name()},
error: "open /foo/bar/baz: no such file or directory; open /baz/quux: no such file or directory",
},
}
for _, tt := range tests {
wss, cleanup, err := open(tt.paths)
if err == nil {
defer cleanup()
}
if tt.error == "" {
assert.NoError(t, err, "Unexpected error opening paths %v.", tt.paths)
} else {
assert.Equal(t, tt.error, err.Error(), "Unexpected error opening paths %v.", tt.paths)
}
names := make([]string, len(wss))
for i, ws := range wss {
f, ok := ws.(*os.File)
require.True(t, ok, "Expected all WriteSyncers returned from open() to be files.")
names[i] = f.Name()
}
assert.Equal(t, tt.filenames, names, "Opened unexpected files given paths %v.", tt.paths)
}
}
func TestOpenFails(t *testing.T) {
tests := []struct {
paths []string
}{
{
paths: []string{"./non-existent-dir/file"},
},
{
paths: []string{"stdout", "./non-existent-dir/file"},
},
}
for _, tt := range tests {
_, cleanup, err := Open(tt.paths...)
require.Nil(t, cleanup, "Cleanup function should never be nil")
assert.Error(t, err, "Open with non-existent directory should fail")
}
}
type testWriter struct {
expected string
t testing.TB
}
func (w *testWriter) Write(actual []byte) (int, error) {
assert.Equal(w.t, []byte(w.expected), actual, "Unexpected write error.")
return len(actual), nil
}
func (w *testWriter) Sync() error {
return nil
}
func TestCombineWriteSyncers(t *testing.T) {
tw := &testWriter{"test", t}
w := CombineWriteSyncers(tw)
w.Write([]byte("test"))
}