Skip to content

Commit cf5d142

Browse files
committed
Simplify the building of a note path
This also sneakily fixes the problem where I forgot to add a slash to these paths because trailing slashes are stripped since #5a5c407.
1 parent ece27b2 commit cf5d142

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

notes.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func showNote(params ...string) {
7979
query = params[1]
8080
}
8181

82-
path := fmt.Sprintf("%v%v.txt", getNotesDir(), note)
82+
path := getNote(note)
8383
file, err := os.Open(path)
8484
if err != nil {
8585
// File does not exist, create it
@@ -129,9 +129,9 @@ func colorizeComment(line string) {
129129
}
130130

131131
func editOrCreateNote(note string) {
132-
file := fmt.Sprintf("%v%v.txt", getNotesDir(), note)
132+
path := getNote(note)
133133

134-
command := exec.Command(getEditor(), file)
134+
command := exec.Command(getEditor(), path)
135135
command.Stdin = os.Stdin
136136
command.Stdout = os.Stdout
137137
command.Stderr = os.Stderr
@@ -162,6 +162,11 @@ func getNotesDir() string {
162162
return filepath.Clean(dir)
163163
}
164164

165+
// getNote returns the path to the note given
166+
func getNote(path string) string {
167+
return fmt.Sprintf("%s/%s.txt", getNotesDir(), path)
168+
}
169+
165170
func main() {
166171
app := cli.NewApp()
167172
app.Name = "notes"

notes_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
package main
22

33
import (
4+
"log"
45
"os"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
89
)
910

11+
func init() {
12+
err := os.Setenv("HOME", "/home/testuser")
13+
if err != nil {
14+
log.Fatalf("Setenv failed")
15+
}
16+
}
17+
1018
func TestGetEditorDefault(t *testing.T) {
1119
expected := "vi"
1220
outcome := getEditor()
@@ -24,10 +32,6 @@ func TestGetEditorWithEnvVariable(t *testing.T) {
2432
}
2533

2634
func TestGetNotesDir(t *testing.T) {
27-
err := os.Setenv("HOME", "/home/testuser")
28-
if err != nil {
29-
t.Fatalf("Setenv failed")
30-
}
3135
expected := "/home/testuser/dotfiles/notes"
3236
outcome := getNotesDir()
3337
assert.Equal(t, expected, outcome)
@@ -41,6 +45,8 @@ func TestGetNotesDirWithCustomEnvVariable(t *testing.T) {
4145
expected := "/some/other/path"
4246
outcome := getNotesDir()
4347
assert.Equal(t, expected, outcome)
48+
49+
_ = os.Unsetenv("NOTESDIR")
4450
}
4551

4652
func TestGetNotesDirUsesCustomEnvVariableOverHomeEnvVariable(t *testing.T) {
@@ -55,6 +61,8 @@ func TestGetNotesDirUsesCustomEnvVariableOverHomeEnvVariable(t *testing.T) {
5561
expected := "/some/other/path"
5662
outcome := getNotesDir()
5763
assert.Equal(t, expected, outcome)
64+
65+
_ = os.Unsetenv("NOTESDIR")
5866
}
5967

6068
func TestGetNotesDirUsesCustomEnvVariableWithoutTrailingSlash(t *testing.T) {
@@ -65,6 +73,8 @@ func TestGetNotesDirUsesCustomEnvVariableWithoutTrailingSlash(t *testing.T) {
6573
expected := "/some/other/path"
6674
outcome := getNotesDir()
6775
assert.Equal(t, expected, outcome)
76+
77+
_ = os.Unsetenv("NOTESDIR")
6878
}
6979

7080
func TestFindNotes(t *testing.T) {
@@ -108,3 +118,9 @@ func TestFindNotesCaseInsensitivity(t *testing.T) {
108118
result := findNotes(notes, "match")
109119
assert.Equal(t, 1, len(result))
110120
}
121+
122+
func TestGetNote(t *testing.T) {
123+
expected := "/home/testuser/dotfiles/notes/git.txt"
124+
outcome := getNote("git")
125+
assert.Equal(t, expected, outcome)
126+
}

0 commit comments

Comments
 (0)