Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions cmd/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ func TestNewCmd(t *testing.T) {
}

for _, tt := range testCases {
config.Now = func() time.Time {
return time.Date(2025, 7, 13, 20, 0, 0, 0, time.UTC)
}

sb := prepareEnvironment()
defer os.RemoveAll(sb)

var wantError error
wantStdoutFilepath := filepath.Join(sb, "inbox", tt.sanitisedTitle+".md")
wantStdoutFilepath := filepath.Join(sb, "inbox", "20250713 "+tt.sanitisedTitle+".md")

newCmd.Flags().Set("no-open", "true")
gotStdout, _, gotError := captureOutput(newCmdFunction, newCmd, []string{tt.inputTitle})
Expand All @@ -55,68 +59,64 @@ func TestNewCmd(t *testing.T) {
}
}

func TestNewCmdExistingNote(t *testing.T) {
func TestNewCmdNoDateFlag(t *testing.T) {
var testCases = []struct {
inputTitle string
sanitisedTitle string
}{
{"Hello World", "Hello World"},
{"HELLO WORLD", "HELLO WORLD"},
{"hello world", "hello world"},
{"hello world", "hello world"},
{" hello world ", "hello world"},
{"-_- hello world _-_", "hello world"},
}

for _, tt := range testCases {
sb := prepareEnvironment()
defer os.RemoveAll(sb)

wantStdoutFilepath := filepath.Join(sb, "inbox", "Hello World"+".md")
wantStderr := fmt.Sprintf("Note with title %q already exists at %s", tt.sanitisedTitle, wantStdoutFilepath)

os.Create(wantStdoutFilepath)
var wantError error
wantStdoutFilepath := filepath.Join(sb, "inbox", tt.sanitisedTitle+".md")

newCmd.Flags().Set("no-open", "true")
_, gotStderr, gotError := captureOutput(newCmdFunction, newCmd, []string{tt.inputTitle})
newCmd.Flags().Set("no-date", "true")
gotStdout, _, gotError := captureOutput(newCmdFunction, newCmd, []string{tt.inputTitle})
_, newNoteErr := os.Stat(wantStdoutFilepath)

assert.Contains(t, gotStderr, wantStderr)
assert.Equal(t, wantError, gotError)
assert.Contains(t, gotStdout, wantStdoutFilepath)
assert.NoError(t, newNoteErr)
assert.ErrorContains(t, gotError, wantStderr)
}
}

func TestNewCmdDateFlag(t *testing.T) {
func TestNewCmdExistingNote(t *testing.T) {
var testCases = []struct {
inputTitle string
sanitisedTitle string
}{
{"Hello World", "20250713 Hello World"},
{"Hello World", "Hello World"},
{"HELLO WORLD", "HELLO WORLD"},
{"hello world", "hello world"},
{"hello world", "hello world"},
{" hello world ", "hello world"},
{"-_- hello world _-_", "hello world"},
}

for _, tt := range testCases {
config.Now = func() time.Time {
return time.Date(2025, 7, 13, 20, 0, 0, 0, time.UTC)
}

sb := prepareEnvironment()
defer os.RemoveAll(sb)

var wantError error
wantStdoutFilepath := filepath.Join(sb, "inbox", tt.sanitisedTitle+".md")
wantStdoutFilepath := filepath.Join(sb, "inbox", "Hello World"+".md")
wantStderr := fmt.Sprintf("Note with title %q already exists at %s", tt.sanitisedTitle, wantStdoutFilepath)

os.Create(wantStdoutFilepath)

newCmd.Flags().Set("no-open", "true")
newCmd.Flags().Set("date", "true")
gotStdout, _, gotError := captureOutput(newCmdFunction, newCmd, []string{tt.inputTitle})
newCmd.Flags().Set("no-date", "true")
_, gotStderr, gotError := captureOutput(newCmdFunction, newCmd, []string{tt.inputTitle})
_, newNoteErr := os.Stat(wantStdoutFilepath)

assert.Equal(t, wantError, gotError)
assert.Contains(t, gotStdout, wantStdoutFilepath)
assert.Contains(t, gotStderr, wantStderr)
assert.NoError(t, newNoteErr)
assert.ErrorContains(t, gotError, wantStderr)
}
}

func TestDailyCmd(t *testing.T) {
config.Now = func() time.Time {
return time.Date(2025, 7, 13, 20, 0, 0, 0, time.UTC)
Expand Down
4 changes: 2 additions & 2 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func init() {
rootCmd.AddCommand(newCmd)

newCmd.Flags().BoolP("no-open", "n", false, "prevents opening of file in editor")
newCmd.Flags().BoolP("date", "d", false, "prepends the date to the filename")
newCmd.Flags().BoolP("no-date", "d", false, "prepends the date to the filename")
}

func newCmdFunction(cmd *cobra.Command, args []string) error {
Expand All @@ -24,7 +24,7 @@ func newCmdFunction(cmd *cobra.Command, args []string) error {

sanitisedTitle := internal.SanitiseTitle(title)

if dateFlag, _ := cmd.Flags().GetBool("date"); dateFlag {
if dateFlag, _ := cmd.Flags().GetBool("no-date"); !dateFlag {
sanitisedTitle = config.Now().Format("20060102 ") + sanitisedTitle
}

Expand Down