-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcommon_test.go
61 lines (54 loc) · 1.17 KB
/
common_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
package notes
import (
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
// Precondition for tests
func init() {
for _, env := range []string{
"NOTES_CLI_HOME",
"NOTES_CLI_GIT",
"NOTES_CLI_EDITOR",
"NOTES_CLI_PAGER",
"EDITOR",
"XDG_DATA_HOME",
"PAGER",
} {
os.Unsetenv(env)
}
}
// Test utilities
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
func testExternalCommandBinaryDir(name string, t *testing.T) string {
cwd, err := os.Getwd()
panicIfErr(err)
pkg := "./testdata/external/notes-external-" + name
exeFile := "notes-external-" + name
if runtime.GOOS == "windows" {
exeFile += ".exe"
}
exe := filepath.Join(cwd, filepath.FromSlash(pkg), exeFile)
if _, err := os.Stat(exe); err != nil {
c := exec.Command("go", "build", "-o", exe, pkg)
out, err := c.CombinedOutput()
if err != nil {
t.Fatal("Cannot build package", pkg, "to create executable", exe, "due to", err, "output:", string(out))
}
_, err = os.Stat(exe) // Verify
panicIfErr(err)
}
return filepath.Dir(exe)
}
type alwaysErrorWriter struct {
}
func (w alwaysErrorWriter) Write(p []byte) (int, error) {
return 0, errors.New("Write error for test")
}