-
Notifications
You must be signed in to change notification settings - Fork 51
/
editor_test.go
64 lines (51 loc) · 1.55 KB
/
editor_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
package editor
import (
"os"
"os/exec"
"strings"
"testing"
"github.com/mkchoi212/fac/conflict"
"github.com/mkchoi212/fac/testhelper"
)
func TestEditorCmd(t *testing.T) {
os.Setenv("EDITOR", "")
editor := editorCmd("foobar")
testhelper.Assert(t, editor != nil, "editor should not be nil")
testhelper.Equals(t, editor.Args, []string{"vim", "foobar"})
os.Setenv("EDITOR", "subl -w")
editor = editorCmd("foobar")
testhelper.Assert(t, editor != nil, "editor should not be nil")
testhelper.Equals(t, editor.Args, []string{"subl", "-w", "foobar"})
}
func TestWriteTmpFile(t *testing.T) {
r := strings.NewReader("Hello, Reader!")
name, err := writeTmpFile(r)
testhelper.Ok(t, err)
testhelper.Assert(t, name != "", "tmp file name should not be empty")
}
func TestOpen(t *testing.T) {
execCommand = mockExecCommand
defer func() { execCommand = exec.Command }()
f := conflict.File{AbsolutePath: "testdata/lorem_ipsum"}
err := f.Read()
testhelper.Ok(t, err)
c := conflict.Conflict{File: &f, Start: 0, End: 5}
output, err := Open(&c)
testhelper.Ok(t, err)
testhelper.Equals(t, f.Lines, output)
}
func TestHelperProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
os.Exit(0)
}
// Allows us to mock exec.Command, thanks to
// https://npf.io/2015/06/testing-exec-command/
func mockExecCommand(command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--", command}
cs = append(cs, args...)
cmd := exec.Command(os.Args[0], cs...)
cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1")
return cmd
}