-
Notifications
You must be signed in to change notification settings - Fork 1
/
jote_internal_test.go
155 lines (150 loc) · 4.33 KB
/
jote_internal_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package jote
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/go-git/go-git/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupJote() (string, Jote, error) {
tmpdir, err := os.MkdirTemp("", "jote-test-*")
if err != nil {
return "", Jote{}, errNewTmpdir
}
js, err := NewJote(tmpdir, exec.Cmd{})
if err != nil {
os.RemoveAll(tmpdir)
return "", Jote{}, errNewJote
}
return tmpdir, js, nil
}
//nolint:funlen
func Test_edit(t *testing.T) {
t.Parallel()
t.Run("should return false if there were no changes made to the file", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
filename := filepath.Join(tmpdir, "edittest")
if err := os.WriteFile(filename, []byte(""), 0o644); err != nil { //nolint:gosec
assert.FailNow("unable to create testfile")
}
testEdit := filepath.Join(tmpdir, "test-edit")
data := []byte("#!/bin/sh\necho true\n")
if err := os.WriteFile(testEdit, data, 0o755); err != nil { //nolint:gosec
assert.FailNow("unable to create test-editor command")
}
js.editor.Path = testEdit
changed, err := js.edit(filename)
require.NoError(t, err)
assert.False(changed)
})
t.Run("should return true if there were changes made to the file", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
filename := filepath.Join(tmpdir, "edittest")
if err := os.WriteFile(filename, []byte(""), 0o644); err != nil { //nolint:gosec
assert.FailNow("unable to create testfile")
}
testEdit := filepath.Join(tmpdir, "test-edit")
data := []byte(fmt.Sprintf("#!/bin/sh\nprintf 'changed\n' > %s\n", filename))
if err := os.WriteFile(testEdit, data, 0o755); err != nil { //nolint:gosec
assert.FailNow("unable to create test-editor command")
}
js.editor.Path = testEdit
changed, err := js.edit(filename)
require.NoError(t, err)
assert.True(changed)
})
t.Run("should error if the file cannot be found", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
filename := filepath.Join(tmpdir, "fakefile")
_, err = js.edit(filename)
require.Error(t, err)
assert.Contains(err.Error(), "fakefile: no such file or directory")
})
t.Run("should error if the editor cannot be called", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
filename := filepath.Join(tmpdir, "edittest")
if err := os.WriteFile(filename, []byte(""), 0o644); err != nil { //nolint:gosec
assert.FailNow("unable to create testfile")
}
editorname := filepath.Join(tmpdir, "nosuchfile")
js.editor.Path = editorname
_, err = js.edit(filename)
require.Error(t, err)
assert.Contains(err.Error(), "nosuchfile: no such file or directory")
})
}
func TestNewJote(t *testing.T) {
t.Parallel()
t.Run("should have a predictable root location", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
root := filepath.Join(tmpdir, "jote")
require.NoError(t, err)
assert.Equal(root, js.root)
})
t.Run("should be git-backed", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
require.NoError(t, err)
if ok := assert.NotNil(js.repo); !ok {
return
}
assert.IsType(&git.Repository{}, js.repo)
})
t.Run("should create the root location if it doesn't exist", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, js, err := setupJote()
if err != nil {
assert.FailNow(err.Error())
}
defer os.RemoveAll(tmpdir)
require.NoError(t, err)
gitRoot := filepath.Join(js.root, ".git")
finfo, err := os.Stat(gitRoot)
if err != nil {
if os.IsNotExist(err) {
assert.FailNowf("root was not created", "%s does not exist", gitRoot)
}
assert.FailNow("encountered an unexpected stat error")
}
assert.True(finfo.IsDir())
})
}