-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathutil.go
More file actions
84 lines (71 loc) · 1.67 KB
/
util.go
File metadata and controls
84 lines (71 loc) · 1.67 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
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
package testutil
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/observiq/stanza/logger"
"github.com/observiq/stanza/operator"
"go.etcd.io/bbolt"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
// NewTempDir will return a new temp directory for testing
func NewTempDir(t testing.TB) string {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("%v", err)
t.FailNow()
}
t.Cleanup(func() {
if err := os.RemoveAll(tempDir); err != nil {
t.Errorf("%v", err)
}
})
return tempDir
}
// NewTestDatabase will return a new database for testing
func NewTestDatabase(t testing.TB) *bbolt.DB {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Errorf("%v", err)
t.FailNow()
}
t.Cleanup(func() {
if err := os.RemoveAll(tempDir); err != nil {
t.Errorf("%v", err)
}
})
db, err := bbolt.Open(filepath.Join(tempDir, "test.db"), 0666, nil)
if err != nil {
t.Errorf("%v", err)
t.FailNow()
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Errorf("%v", err)
}
})
return db
}
// NewBuildContext will return a new build context for testing
func NewBuildContext(t testing.TB) operator.BuildContext {
return operator.BuildContext{
Database: NewTestDatabase(t),
Logger: logger.New(zaptest.NewLogger(t, zaptest.Level(zapcore.ErrorLevel)).Sugar()),
Namespace: "$",
}
}
// Trim removes white space from the lines of a string
func Trim(s string) string {
lines := strings.Split(s, "\n")
trimmed := make([]string, 0, len(lines))
for _, line := range lines {
if len(line) == 0 {
continue
}
trimmed = append(trimmed, strings.Trim(line, " \t\n"))
}
return strings.Join(trimmed, "\n")
}