...
1
16
17 package editor
18
19 import (
20 "bytes"
21 "os"
22 "reflect"
23 "strings"
24 "testing"
25 )
26
27 func TestArgs(t *testing.T) {
28 if e, a := []string{"/bin/bash", "-c \"test\""}, (Editor{Args: []string{"/bin/bash", "-c"}, Shell: true}).args("test"); !reflect.DeepEqual(e, a) {
29 t.Errorf("unexpected args: %v", a)
30 }
31 if e, a := []string{"/bin/bash", "-c", "test"}, (Editor{Args: []string{"/bin/bash", "-c"}, Shell: false}).args("test"); !reflect.DeepEqual(e, a) {
32 t.Errorf("unexpected args: %v", a)
33 }
34 if e, a := []string{"/bin/bash", "-i -c \"test\""}, (Editor{Args: []string{"/bin/bash", "-i -c"}, Shell: true}).args("test"); !reflect.DeepEqual(e, a) {
35 t.Errorf("unexpected args: %v", a)
36 }
37 if e, a := []string{"/test", "test"}, (Editor{Args: []string{"/test"}}).args("test"); !reflect.DeepEqual(e, a) {
38 t.Errorf("unexpected args: %v", a)
39 }
40 }
41
42 func TestEditor(t *testing.T) {
43 edit := Editor{Args: []string{"cat"}}
44 testStr := "test something\n"
45 contents, path, err := edit.LaunchTempFile("", "someprefix", bytes.NewBufferString(testStr))
46 if err != nil {
47 t.Fatalf("unexpected error: %v", err)
48 }
49 if _, err := os.Stat(path); err != nil {
50 t.Fatalf("no temp file: %s", path)
51 }
52 defer os.Remove(path)
53 if disk, err := os.ReadFile(path); err != nil || !bytes.Equal(contents, disk) {
54 t.Errorf("unexpected file on disk: %v %s", err, string(disk))
55 }
56 if !bytes.Equal(contents, []byte(testStr)) {
57 t.Errorf("unexpected contents: %s", string(contents))
58 }
59 if !strings.Contains(path, "someprefix") {
60 t.Errorf("path not expected: %s", path)
61 }
62 }
63
View as plain text