-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
115 lines (96 loc) · 3.26 KB
/
env_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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDataPath(t *testing.T) {
assert.Equal(t, ".", DataPath())
}
func TestSetupEnv(t *testing.T) {
assert.Equal(t, ".", *DefaultPath)
assert.Equal(t, "menace.db", *DefaultDbName)
testDir := GetTestDir()
defer testDir.Close()
oldDefault := *DefaultPath
defer func() { *DefaultPath = oldDefault }()
*DefaultPath = filepath.Join(testDir.Path(), "upper", "lower", "dir")
err := SetupEnv()
assert.Nil(t, err)
_, err = os.Stat(*DefaultPath)
assert.Nil(t, err)
// It shouldn't care if we do it again.
err = SetupEnv()
assert.Nil(t, err)
// Check for an error if the specified data directory is a file.
*DefaultPath = filepath.Join(testDir.Path(), "collide.file")
file, err := os.Create(*DefaultPath)
require.Nil(t, err)
assert.Nil(t, file.Close())
err = SetupEnv()
if assert.Error(t, err) {
assert.True(t, os.IsExist(err))
}
}
func TestFilterError(t *testing.T) {
// nil error should return nil with no output.
result := captureLog(t, func(t *testing.T) {
assert.Nil(t, FilterError(nil))
})
assert.Empty(t, result)
t.Run("Check Duplicate Entry errors", func(t *testing.T) {
// ErrDuplicateEntity should be filtered until we make it an error,
// which means nil error returned and no logging generated, yet.
result = captureLog(t, func(t *testing.T) {
assert.Nil(t, FilterError(fmt.Errorf("test: %w", ErrDuplicateEntity)))
})
assert.Nil(t, result)
// enabling duplicationErrors should change this to just returning an error/no log
defer func() { *ErrorOnDuplicate = false }()
*ErrorOnDuplicate = true
result = captureLog(t, func(t *testing.T) {
err := FilterError(fmt.Errorf("test: %w", ErrDuplicateEntity))
assert.True(t, errors.Is(err, ErrDuplicateEntity))
})
assert.Nil(t, result)
})
t.Run("Check Unknown Entity errors", func(t *testing.T) {
// ErrUnknownEntity should likewise be filtered/logged.
result = captureLog(t, func(t *testing.T) {
assert.Nil(t, FilterError(fmt.Errorf("test: %w", ErrUnknownEntity)))
})
assert.Nil(t, result)
defer func() { *ErrorOnUnknown = false }()
*ErrorOnUnknown = true
result = captureLog(t, func(t *testing.T) {
err := FilterError(fmt.Errorf("test: %w", ErrUnknownEntity))
assert.True(t, errors.Is(err, ErrUnknownEntity))
})
assert.Nil(t, result)
})
t.Run("Check ShowWarnings", func(t *testing.T) {
// Turning onShowWarnings should get nils but no outputs
defer func() { *ShowWarnings = false }()
*ShowWarnings = true
result = captureLog(t, func(t *testing.T) {
assert.Nil(t, FilterError(fmt.Errorf("test: %w", ErrDuplicateEntity)))
assert.Nil(t, FilterError(fmt.Errorf("test: %w", ErrUnknownEntity)))
})
if assert.NotNil(t, result) {
assert.Len(t, result, 2)
assert.Contains(t, result[0], "test: duplicate")
assert.Contains(t, result[1], "test: unknown")
}
})
}
func TestDataFilePath(t *testing.T) {
var oldDefaultPath = *DefaultPath
defer func() { *DefaultPath = oldDefaultPath }()
*DefaultPath = "data/test"
assert.Equal(t, filepath.Join("data/test", "foo.txt"), DataFilePath("foo.txt"))
assert.Equal(t, filepath.Join("data/test", "foo", "bar", "baz.txt"), DataFilePath("foo", "bar", "baz.txt"))
}