-
Notifications
You must be signed in to change notification settings - Fork 1
/
jote_test.go
73 lines (65 loc) · 1.92 KB
/
jote_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
package jote_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/jhuntwork/jote"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewJote(t *testing.T) {
t.Parallel()
t.Run("should get a Store", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, err := os.MkdirTemp("", "jote-test-*")
if err != nil {
assert.FailNow("unable to create a temporary directory")
}
defer os.RemoveAll(tmpdir)
js, err := jote.NewJote(tmpdir, exec.Cmd{})
assert.NotNil(js)
require.NoError(t, err)
})
}
func TestAdd(t *testing.T) {
t.Parallel()
t.Run("should launch the command specified by the command", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
tmpdir, err := os.MkdirTemp("", "jote-test-*")
if err != nil {
assert.FailNow("unable to create a temporary directory")
}
defer os.RemoveAll(tmpdir)
testout := filepath.Join(tmpdir, "testout")
data := []byte(fmt.Sprintf("#!/bin/sh\nprintf 'Called with: %%s' \"$*\" > %s\n", testout))
filename := filepath.Join(tmpdir, "test-editor")
if err := os.WriteFile(filename, data, 0o755); err != nil { //nolint:gosec
assert.FailNow("unable to create test-editor command")
}
cmd := exec.Command(filename, "arg1", "arg2")
js, err := jote.NewJote(tmpdir, *cmd)
if err != nil {
assert.FailNow("encountered an error creating Store")
}
if err := js.Add(); err != nil {
assert.FailNowf("encountered an unexpected error when Adding: %s", err.Error())
}
output, err := os.ReadFile(testout)
require.NoError(t, err)
assert.Contains(string(output), "Called with: arg1 arg2")
})
}
func TestDefaultStoreLocation(t *testing.T) {
t.Parallel()
t.Run("should return a predictable location", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
loc := jote.DefaultStoreLocation()
home, _ := os.UserHomeDir()
assert.Equal(filepath.Join(home, ".local", "share"), loc)
})
}