Skip to content

Commit

Permalink
Simplify the building of a note path
Browse files Browse the repository at this point in the history
This also sneakily fixes the problem where I forgot to add a slash
to these paths because trailing slashes are stripped since #5a5c407.
  • Loading branch information
bittersweet committed Apr 7, 2016
1 parent ece27b2 commit cf5d142
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
11 changes: 8 additions & 3 deletions notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func showNote(params ...string) {
query = params[1]
}

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

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

command := exec.Command(getEditor(), file)
command := exec.Command(getEditor(), path)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
Expand Down Expand Up @@ -162,6 +162,11 @@ func getNotesDir() string {
return filepath.Clean(dir)
}

// getNote returns the path to the note given
func getNote(path string) string {
return fmt.Sprintf("%s/%s.txt", getNotesDir(), path)
}

func main() {
app := cli.NewApp()
app.Name = "notes"
Expand Down
24 changes: 20 additions & 4 deletions notes_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package main

import (
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func init() {
err := os.Setenv("HOME", "/home/testuser")
if err != nil {
log.Fatalf("Setenv failed")
}
}

func TestGetEditorDefault(t *testing.T) {
expected := "vi"
outcome := getEditor()
Expand All @@ -24,10 +32,6 @@ func TestGetEditorWithEnvVariable(t *testing.T) {
}

func TestGetNotesDir(t *testing.T) {
err := os.Setenv("HOME", "/home/testuser")
if err != nil {
t.Fatalf("Setenv failed")
}
expected := "/home/testuser/dotfiles/notes"
outcome := getNotesDir()
assert.Equal(t, expected, outcome)
Expand All @@ -41,6 +45,8 @@ func TestGetNotesDirWithCustomEnvVariable(t *testing.T) {
expected := "/some/other/path"
outcome := getNotesDir()
assert.Equal(t, expected, outcome)

_ = os.Unsetenv("NOTESDIR")
}

func TestGetNotesDirUsesCustomEnvVariableOverHomeEnvVariable(t *testing.T) {
Expand All @@ -55,6 +61,8 @@ func TestGetNotesDirUsesCustomEnvVariableOverHomeEnvVariable(t *testing.T) {
expected := "/some/other/path"
outcome := getNotesDir()
assert.Equal(t, expected, outcome)

_ = os.Unsetenv("NOTESDIR")
}

func TestGetNotesDirUsesCustomEnvVariableWithoutTrailingSlash(t *testing.T) {
Expand All @@ -65,6 +73,8 @@ func TestGetNotesDirUsesCustomEnvVariableWithoutTrailingSlash(t *testing.T) {
expected := "/some/other/path"
outcome := getNotesDir()
assert.Equal(t, expected, outcome)

_ = os.Unsetenv("NOTESDIR")
}

func TestFindNotes(t *testing.T) {
Expand Down Expand Up @@ -108,3 +118,9 @@ func TestFindNotesCaseInsensitivity(t *testing.T) {
result := findNotes(notes, "match")
assert.Equal(t, 1, len(result))
}

func TestGetNote(t *testing.T) {
expected := "/home/testuser/dotfiles/notes/git.txt"
outcome := getNote("git")
assert.Equal(t, expected, outcome)
}

0 comments on commit cf5d142

Please sign in to comment.