cat / 012
clone

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

main
012 / e2e / numodule_test.go
numodule_test.go 3.4K
1package e2e23import (4 "os"5 "os/exec"6 "path/filepath"7 "strings"8 "testing"9)1011// The nushell module (012 nu --install-module, then `sheet` in nu): nu12// runs as a pipeline stage in the terminal, with 012 on its PATH, the13// module installed where 012 puts it and used by the line 012 prints14// for config.nu.1516// startSheet installs the module in a temporary nu config directory and17// runs script in nu with it, standard output captured.18func startSheet(t *testing.T, startsOn, script string) *session {19 t.Helper()20 needNu(t)21 dir := t.TempDir()22 xdg := filepath.Join(dir, ".config") // the session's own (configEnv)23 install := exec.Command(binPath, "nu", "--install-module")24 install.Env = append(os.Environ(), "XDG_CONFIG_HOME="+xdg)25 out, err := install.CombinedOutput()26 if err != nil {27 t.Fatalf("installing the module: %v\n%s", err, out)28 }29 lines := strings.Split(strings.TrimSpace(string(out)), "\n")30 config := filepath.Join(xdg, "nushell", "config.nu")31 if err := os.WriteFile(config, []byte(lines[len(lines)-1]+"\n"), 0o644); err != nil {32 t.Fatal(err)33 }34 path := "PATH=" + filepath.Dir(binPath) + string(os.PathListSeparator) + os.Getenv("PATH")35 return startWith(t, options{dir: dir, piped: true, program: "nu", startsOn: startsOn, env: []string{path}},36 "--config", config, "-c", script)37}3839const sizesNu = "[[name size]; [a 2kb] [b 10b] [c 5mb]]"4041// A table goes through sheet: a selection is sent back, and nu gets it42// with its types, file sizes as file sizes.43func TestSheetSendsSelection(t *testing.T) {44 t.Parallel()45 s := startSheet(t, "5.0 MB", "let t = "+sizesNu+" | sheet; print ($t | get size | describe); $t | to nuon")46 s.keys("<shift+down>", "<shift+down>", "<shift+right>")47 s.waitFor("send A1:B3 as NUON")48 s.keys("<ctrl+q>")49 s.waitFor("Send A1:B3 as NUON?")50 s.keys("<enter>")51 out, code := s.finish()52 if want := "list<filesize>\n[[name, size]; [a, 2000b], [b, 10b]]\n"; code != 0 || out != want {53 t.Errorf("exit %d, stdout %q, want %q", code, out, want)54 }55}5657// sheet --send sheet gives back all of ls's table on Ctrl+Q, whatever58// is selected.59func TestSheetSendSheet(t *testing.T) {60 t.Parallel()61 s := startSheet(t, "b.txt", "cd (mktemp -d); 'x' | save a.txt; 'yyy' | save b.txt; "+62 "let t = ls | sheet --send sheet; print ($t | columns | to nuon); "+63 "print (($t | select name type size) == (ls | select name type size)); print ($t | length)")64 s.keys("<shift+down>")65 s.waitFor("Ctrl+Q send the sheet as NUON")66 s.keys("<ctrl+q>")67 out, code := s.finish()68 if want := "[name, type, size, modified]\ntrue\n2\n"; code != 0 || out != want {69 t.Errorf("exit %d, stdout %q, want %q", code, out, want)70 }71}7273// Quitting without sending raises sheet's error, with 012's reason.74func TestSheetNotSent(t *testing.T) {75 t.Parallel()76 s := startSheet(t, "5.0 MB", "try { "+sizesNu+" | sheet } catch {|e| print $'caught: ($e.msg)' }")77 s.keys("<ctrl+q>", "d")78 out, code := s.finish()79 if want := "caught: quit without sending anything to the pipeline\n"; code != 0 || out != want {80 t.Errorf("exit %d, stdout %q, want %q", code, out, want)81 }82}8384// sheet view shows the table and returns nothing.85func TestSheetView(t *testing.T) {86 t.Parallel()87 s := startSheet(t, "5.0 MB", sizesNu+" | sheet view | describe")88 s.keys("<ctrl+q>")89 s.waitFor("You have unsaved changes.")90 s.keys("d")91 out, code := s.finish()92 if code != 0 || out != "nothing\n" {93 t.Errorf("exit %d, stdout %q", code, out)94 }95}