diff --git a/cmd/integration_test.go b/cmd/integration_test.go index f554161..0865619 100644 --- a/cmd/integration_test.go +++ b/cmd/integration_test.go @@ -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}) @@ -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) diff --git a/cmd/new.go b/cmd/new.go index 7b20a3e..bef3535 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -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 { @@ -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 }