diff --git a/editor/editor_test.go b/editor/editor_test.go index 48f853c..b872ce7 100644 --- a/editor/editor_test.go +++ b/editor/editor_test.go @@ -171,7 +171,7 @@ func TestEditorOpenWriteQuit(t *testing.T) { } } -func TestEditorOpenForceQuit(t *testing.T) { +func TestEditorOpenQuitBang(t *testing.T) { ui := newTestUI() editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) if err := editor.Init(); err != nil { @@ -200,6 +200,48 @@ func TestEditorOpenForceQuit(t *testing.T) { } } +func TestEditorOpenWriteQuitBang(t *testing.T) { + ui := newTestUI() + editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) + if err := editor.Init(); err != nil { + t.Fatalf("err should be nil but got: %v", err) + } + f, err := createTemp(t.TempDir(), "ab") + if err != nil { + t.Fatalf("err should be nil but got: %v", err) + } + if err := editor.Open(f.Name()); err != nil { + t.Fatalf("err should be nil but got: %v", err) + } + go func() { + ui.Emit(event.Event{Type: event.SwitchFocus}) + ui.Emit(event.Event{Type: event.StartAppendEnd}) + ui.Emit(event.Event{Type: event.Rune, Rune: 'c'}) + ui.Emit(event.Event{Type: event.ExitInsert}) + ui.Emit(event.Event{Type: event.WriteQuit, Arg: f.Name() + ".out", Bang: true}) + }() + if err := editor.Run(); err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if err := editor.Close(); err != nil { + t.Errorf("err should be nil but got: %v", err) + } + bs, err := os.ReadFile(f.Name()) + if err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if expected := "ab"; string(bs) != expected { + t.Errorf("file contents should be %q but got %q", expected, string(bs)) + } + bs, err = os.ReadFile(f.Name() + ".out") + if err != nil { + t.Errorf("err should be nil but got: %v", err) + } + if expected := "abc"; string(bs) != expected { + t.Errorf("file contents should be %q but got %q", expected, string(bs)) + } +} + func TestEditorReadWriteQuit(t *testing.T) { ui := newTestUI() editor := NewEditor(ui, window.NewManager(), cmdline.NewCmdline()) diff --git a/window/manager.go b/window/manager.go index 550c5a0..ac6e8a9 100644 --- a/window/manager.go +++ b/window/manager.go @@ -384,7 +384,7 @@ func (m *Manager) Emit(e event.Event) { case event.WriteQuit: if _, _, err := m.write(e); err != nil { m.eventCh <- event.Event{Type: event.Error, Error: err} - } else if err := m.quit(event.Event{}); err != nil { + } else if err := m.quit(event.Event{Bang: e.Bang}); err != nil { m.eventCh <- event.Event{Type: event.Error, Error: err} } default: