-
Notifications
You must be signed in to change notification settings - Fork 11
/
main_test.go
86 lines (80 loc) · 1.93 KB
/
main_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
// Copyright 2019 The Morning Consult, LLC or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestNewLogWriter(t *testing.T) {
noop := func() {}
cases := []struct {
name string
pre func()
config map[string]interface{}
err string
post func()
}{
{
name: "log-dir-from-env",
pre: func() {
os.Setenv(envLogDir, "testdata")
},
err: "",
post: func() {
os.Unsetenv(envLogDir)
},
},
{
name: "log-dir-from-config",
pre: func() {
os.Unsetenv(envLogDir)
},
config: map[string]interface{}{"log_dir": "testdata"},
err: "",
post: noop,
},
{
name: "error-expanding-log-dir",
pre: noop,
config: map[string]interface{}{"log_dir": "~asdgweq"},
err: "error expanding logging directory : cannot expand user-specific home dir",
post: noop,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc.pre()
defer tc.post()
file, err := newLogWriter(tc.config)
if tc.err != "" {
if err == nil {
t.Fatal("expected an error but didn't receive one")
}
if err.Error() != tc.err {
t.Fatalf("Results differ:\n%v", cmp.Diff(err.Error(), tc.err))
}
return
}
if err != nil {
t.Fatal(err)
}
file.Close()
filename := file.Name()
if _, err = os.Stat(filename); err != nil {
t.Fatal(err)
}
os.Remove(filename)
})
}
}