-
Notifications
You must be signed in to change notification settings - Fork 12
/
util_test.go
111 lines (95 loc) · 2.36 KB
/
util_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
package flop
import (
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
// memoizeTmpDir holds memoization info for the temporary directory
var memoizeTmpDir string
// unusedFileNum tracks a unique file identifier
var unusedFileNum int
// unusedDirNum tracks a unique dir identifier
var unusedDirNum int
// tmpDirPath gets the path of a temporary directory on the system
func tmpDirPath() string {
// memoize
if memoizeTmpDir != "" {
return memoizeTmpDir
}
d, _ := ioutil.TempDir("", "")
return d
}
// tmpDirPathUnused returns the path for a temp directory that does not exist yet
func tmpDirPathUnused() string {
d, err := ioutil.TempDir("", "")
if err != nil {
panic(err)
}
for {
d = filepath.Join(d, fmt.Sprintf("%s%d", "dir", unusedDirNum))
// we expect to see an error if the dir path is unused
if _, err := os.Stat(d); err == nil {
// bump file number if the file created with that number exists
unusedDirNum += 1
} else {
return d
}
}
}
// tmpFile creates a new, empty temporary file and returns the full path
func tmpFile() string {
src, err := ioutil.TempFile("", "*.txt")
if err != nil {
panic(fmt.Sprintf("temp file creation failed: %s", err))
}
defer func() {
_ = src.Close()
}()
return src.Name()
}
// tmpFilePathUnused returns the path for a temp file that does not yet exist
func tmpFilePathUnused() string {
d, err := ioutil.TempDir("", "")
if err != nil {
panic(err)
}
// derive file name of potentially unused file
tmpFile := func() string {
return path.Join(d, fmt.Sprintf("%s%d", "file", unusedFileNum))
}
for {
// we expect to see an error if the file path is unused
if _, err := os.Stat(tmpFile()); err == nil {
// bump file number if the file created with that number exists
unusedFileNum += 1
} else {
return tmpFile()
}
}
}
func errContains(err error, substring string) bool {
errString := fmt.Sprintf("%s", err)
if strings.Contains(errString, substring) {
return true
}
fmt.Println("error:", errString)
fmt.Println("substring:", substring)
return false
}
func debugLogger(msg string) {
if debug {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Debug().Msg(msg)
}
}
func infoLogger(msg string) {
if debug {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Info().Msg(msg)
}
}