68 lines
2.5 KiB
Go
68 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// echoFails makes the echoing test binary end with a failure, as a read that failed would.
|
|
const echoFails = "MESH_TEST_ECHO_FAILS"
|
|
|
|
// journalled runs lines through runForMeshCLI and answers what the journal said, with a fresh count of read calls.
|
|
func journalled(t *testing.T) *[]string {
|
|
t.Helper()
|
|
t.Setenv(echoEnvironment, "1")
|
|
said := &[]string{}
|
|
was, wasReads := cliJournal, cliReads
|
|
cliJournal = func(line string) { *said = append(*said, line) }
|
|
cliReads = &readCalls{}
|
|
t.Cleanup(func() { cliJournal, cliReads = was, wasReads })
|
|
return said
|
|
}
|
|
|
|
// **A call that only reads writes no journal line; one that changes something does** (novox/hq issue 762).
|
|
|
|
// **A refused or failed read keeps its line** (novox/hq issue 762).
|
|
|
|
// **A verb is classed by what the call does, not its name** (novox/hq issue 762): the same command word reads with
|
|
// some arguments and writes with others, and a line no reading form names is said.
|
|
func TestAVerbIsClassedByItsArguments(t *testing.T) {
|
|
ordinary := cliVerdict{why: "not the terminal"}
|
|
for _, c := range []struct {
|
|
line []string
|
|
reads bool
|
|
}{
|
|
{[]string{"settings", "show", "zsh"}, true},
|
|
{[]string{"settings", "preferences"}, true},
|
|
{[]string{"settings", "set", "zsh", `{"a":1}`}, false},
|
|
{[]string{"settings", "clear", "zsh"}, false},
|
|
{[]string{"plans"}, true},
|
|
{[]string{"plans", "p-1"}, true},
|
|
{[]string{"plans", "go", "p-1"}, false},
|
|
{[]string{"plans", "--json", "stop", "p-1"}, false},
|
|
{[]string{"plans", "close", "p-1"}, false},
|
|
{[]string{"plans", "retry", "p-1"}, false},
|
|
{[]string{"conditions"}, true},
|
|
{[]string{"conditions", "show", "x"}, true},
|
|
{[]string{"conditions", "silence", "x"}, false},
|
|
{[]string{"doctor", "run"}, false},
|
|
{[]string{"node", "account", "g14", "x"}, false},
|
|
{[]string{"assign", "g14", "zsh"}, false},
|
|
{[]string{"a-command-added-later"}, false},
|
|
{[]string{"serve"}, false},
|
|
} {
|
|
if got := onlyReads(cliAsked("jochen", 1000, c.line...), ordinary); got != c.reads {
|
|
t.Errorf("%q judged a read: %v, want %v", c.line, got, c.reads)
|
|
}
|
|
}
|
|
if onlyReads(cliAsked("jochen", 1000, "plans"), cliVerdict{refused: "no"}) {
|
|
t.Error("a refused line judged a read")
|
|
}
|
|
stdin := cliAsked("jochen", 1000, "status")
|
|
stdin.Stdin = []byte("x")
|
|
if onlyReads(stdin, cliVerdict{terminal: true}) {
|
|
t.Error("a line carrying standard input judged a read")
|
|
}
|
|
}
|
|
|
|
// **Once an hour the read calls are counted per caller, only if there were any** (novox/hq issue 762).
|