cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / saveas_test.go
saveas_test.go 3.3K
1package ui23import (4 "os"5 "path/filepath"6 "strings"7 "testing"89 tea "charm.land/bubbletea/v2"1011 "src.rest/cat/012/internal/confine"12 "src.rest/cat/012/internal/ui/choicebar"13)1415// Save as onto another file that exists asks first: Enter replaces it,16// Esc keeps it. Saving as the open file, or a new name, doesn't ask.17func TestSaveAsAsksBeforeReplacing(t *testing.T) {18 t.Chdir(t.TempDir())19 writeSheet(t, "other.012", "theirs")20 m := newModel()21 press(t, m, "mine", "<enter>")2223 run(m, m.openSave())24 press(t, m, "other", "<enter>")25 if _, ok := m.overlay.(*choicebar.Bar); !ok || !strings.Contains(line(m, contextLine), "other.012 exists.") ||26 !strings.Contains(line(m, contextLine), "Replace") {27 t.Fatalf("no question: overlay %T, %q", m.overlay, line(m, contextLine))28 }29 press(t, m, "<esc>")30 if got, _ := readSheet("other.012"); a1Of(got) != "theirs" || m.filename != "" || !m.changed {31 t.Fatalf("Esc wrote it: A1 %q, file %q, changed %v", a1Of(got), m.filename, m.changed)32 }3334 run(m, m.openSave())35 press(t, m, "other", "<enter>", "<enter>")36 if got, _ := readSheet("other.012"); a1Of(got) != "mine" || m.filename != "other.012" || m.changed {37 t.Fatalf("Enter didn't replace: A1 %q, file %q, changed %v", a1Of(got), m.filename, m.changed)38 }3940 // Now it's the open file: Save as onto it saves without asking, as41 // Save does.42 press(t, m, "<up>", "again", "<enter>")43 run(m, m.openSave())44 press(t, m, "<enter>")45 if got, _ := readSheet("other.012"); m.overlay != nil || a1Of(got) != "again" {46 t.Fatalf("Save as the open file: overlay %T, A1 %q", m.overlay, a1Of(got))47 }48 // A new name doesn't ask.49 run(m, m.openSave())50 press(t, m, "fresh", "<enter>")51 if _, err := os.Stat("fresh.012"); err != nil || m.overlay != nil {52 t.Fatalf("new name: %v, overlay %T", err, m.overlay)53 }54}5556// In a served session the file is looked for inside the served57// directory, and :w name and :wq name go the same way; cancelling :wq's58// question cancels its quit.59func TestServedSaveAsAndVimAsk(t *testing.T) {60 top := t.TempDir()61 served := filepath.Join(top, "served")62 os.Mkdir(served, 0o755)63 writeSheet(t, filepath.Join(served, "kept.012"), "kept")64 t.Chdir(top)65 root, err := confine.New(served)66 if err != nil {67 t.Fatal(err)68 }69 m := vimModel(t)70 m.Serve(root, []string{"TERM=xterm-256color"})71 press(t, m, "x", ":w kept", "<enter>")72 if !strings.Contains(line(m, contextLine), "kept.012 exists.") {73 t.Fatalf(":w kept: %q", line(m, contextLine))74 }75 press(t, m, "<esc>")76 if got, _ := readSheet(filepath.Join(served, "kept.012")); a1Of(got) != "kept" {77 t.Fatalf("Esc wrote it: %q", a1Of(got))78 }79 if _, ok := press(t, m, ":wq kept", "<enter>", "<esc>").(tea.QuitMsg); ok {80 t.Fatal("cancelled :wq quit")81 }82 if m.quitAfterSave {83 t.Error("cancelled :wq still waits to quit")84 }85 if _, ok := press(t, m, ":wq kept", "<enter>", "<enter>").(tea.QuitMsg); !ok {86 t.Fatal(":wq kept, Replace didn't quit")87 }88 if got, _ := readSheet(filepath.Join(served, "kept.012")); a1Of(got) == "kept" {89 t.Error("Replace didn't write the served file")90 }91 // A file by that name in the server's working directory isn't the92 // served one: no question for a name that's new inside.93 writeSheet(t, filepath.Join(top, "cwd.012"), "cwd")94 press(t, m, ":w cwd", "<enter>")95 if m.overlay != nil {96 t.Errorf(":w cwd asked about the working directory's file: %q", line(m, contextLine))97 }98}