Use common v0.8.0 UI, transport, and translation APIs
This commit is contained in:
1 parent
317303adb7
commit
92fd479336
344 files changed
+4102
-4973
No files matched your search
@@ -60,7 +60,7 @@ func Paths(home, configured, bundled string) []string {
|
||||
return paths
|
||||
}
|
||||
|
||||
func Parse_filename(filename string) (Script, bool) {
|
||||
func ParseFilename(filename string) (Script, bool) {
|
||||
if strings.HasPrefix(filename, ".") {
|
||||
return Script{}, false
|
||||
}
|
||||
@@ -75,7 +75,7 @@ func Parse_filename(filename string) (Script, bool) {
|
||||
keys := strings.Split(parts[len(parts)-1], "__+__")
|
||||
hasScript := false
|
||||
for _, key := range keys {
|
||||
if !input.Valid_script_key(key) {
|
||||
if !input.ValidScriptKey(key) {
|
||||
return Script{}, false
|
||||
}
|
||||
hasScript = hasScript || key == "KEY_SCRIPT"
|
||||
@@ -115,11 +115,11 @@ func Discover(paths []string, occupied map[string]bool) ([]Script, []error) {
|
||||
if err != nil || !info.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
script, ok := Parse_filename(entry.Name())
|
||||
script, ok := ParseFilename(entry.Name())
|
||||
if !ok || loadedNames[script.Name] || loadedIDs[script.ID] {
|
||||
continue
|
||||
}
|
||||
chord := input.Script_chord(script.Keys)
|
||||
chord := input.ScriptChord(script.Keys)
|
||||
if shortcuts[chord] {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -38,13 +38,13 @@ func TestPythonFilenameGrammar(t *testing.T) {
|
||||
{"keys__-__KEY_A__+__KEY_B", []string{"KEY_A", "KEY_B", "KEY_SCRIPT"}},
|
||||
{"media__-__KEY_PLAYPAUSE", []string{"KEY_PLAYPAUSE", "KEY_SCRIPT"}},
|
||||
} {
|
||||
script, ok := Parse_filename(fixture.name)
|
||||
script, ok := ParseFilename(fixture.name)
|
||||
if !ok || !reflect.DeepEqual(script.Keys, fixture.keys) {
|
||||
t.Fatal(fixture.name, script, ok)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"__hidden__-__KEY_A.sh", ".hidden__-__KEY_A", "bad__-__KEY_A__-__KEY_B", "bad__-__KEY_NO_SUCH_KEY", "bad__-__KEY_A__+__", "unbound.sh"} {
|
||||
if _, ok := Parse_filename(name); ok {
|
||||
if _, ok := ParseFilename(name); ok {
|
||||
t.Fatal("accepted invalid filename", name)
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func TestPythonDiscoveryPrecedenceVariantsAndConflicts(t *testing.T) {
|
||||
if err := os.Mkdir(filepath.Join(local, "directory__-__KEY_F.sh"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, problems := Discover([]string{filepath.Join(root, "missing"), local, system}, map[string]bool{input.Script_chord([]string{"KEY_D", "KEY_SCRIPT"}): true})
|
||||
result, problems := Discover([]string{filepath.Join(root, "missing"), local, system}, map[string]bool{input.ScriptChord([]string{"KEY_D", "KEY_SCRIPT"}): true})
|
||||
if len(problems) != 0 {
|
||||
t.Fatal(problems)
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ func TestInstalledMusicControllerWithFakePlayerctl(t *testing.T) {
|
||||
if err := os.WriteFile(path, data, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := New_runner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 4096, Timeout: time.Second}, func(int) string { return "Username" })
|
||||
runner := NewRunner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 4096, Timeout: time.Second}, func(int) string { return "Username" })
|
||||
defer runner.Close()
|
||||
for _, fixture := range []struct{ key, action string }{{"z", "previous"}, {"x", "play"}, {"c", "play-pause"}, {"v", "stop"}, {"b", "next"}, {"s", "shuffle toggle"}, {"u", "metadata"}, {"minus", "volume 0.05-"}, {"equal", "volume 0.05+"}} {
|
||||
link := filepath.Join(root, "musicplayer__-__key_shift__+__key_"+fixture.key+".sh")
|
||||
@@ -69,7 +69,7 @@ func TestInstalledMusicControllerWithFakePlayerctl(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner.Submit(Invocation{Script: Script{Path: link}})
|
||||
result := receive_result(t, runner)
|
||||
result := receiveResult(t, runner)
|
||||
actions, err := os.ReadFile(filepath.Join(root, "actions"))
|
||||
if err != nil || result.Err != nil || result.Stderr != "" || !strings.Contains(string(actions), fixture.action) {
|
||||
t.Fatal(fixture, result, string(actions), err)
|
||||
|
||||
@@ -66,7 +66,7 @@ type Runner struct {
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func New_runner(ctx context.Context, limits Limits, username func(int) string) *Runner {
|
||||
func NewRunner(ctx context.Context, limits Limits, username func(int) string) *Runner {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
r := &Runner{ctx: ctx, cancel: cancel, limits: limits, username: username, jobs: make(chan Invocation, limits.Queue), results: make(chan Result, limits.Workers), active: make(map[*Invocation]context.CancelFunc), done: make(chan struct{})}
|
||||
for range limits.Workers {
|
||||
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func fixture_script(t *testing.T, body string) Script {
|
||||
func fixtureScript(t *testing.T, body string) Script {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "script with spaces__-__KEY_A.sh")
|
||||
if err := os.WriteFile(path, []byte("#!/bin/sh\n"+body+"\n"), 0700); err != nil {
|
||||
@@ -36,7 +36,7 @@ func fixture_script(t *testing.T, body string) Script {
|
||||
return Script{Path: path}
|
||||
}
|
||||
|
||||
func receive_result(t *testing.T, runner *Runner) Result {
|
||||
func receiveResult(t *testing.T, runner *Runner) Result {
|
||||
t.Helper()
|
||||
select {
|
||||
case result := <-runner.Results():
|
||||
@@ -48,70 +48,70 @@ func receive_result(t *testing.T, runner *Runner) Result {
|
||||
}
|
||||
|
||||
func TestRunnerPreservesArgumentsOutputAndNonzeroExit(t *testing.T) {
|
||||
runner := New_runner(context.Background(), Limits{Workers: 1, Queue: 2, OutputBytes: 4096}, func(terminal int) string {
|
||||
runner := NewRunner(context.Background(), Limits{Workers: 1, Queue: 2, OutputBytes: 4096}, func(terminal int) string {
|
||||
if terminal != 3 {
|
||||
t.Error(terminal)
|
||||
}
|
||||
return "Username; echo not-a-shell"
|
||||
})
|
||||
defer runner.Close()
|
||||
script := fixture_script(t, "echo \"$1\"\necho problem >&2\nexit 9")
|
||||
script := fixtureScript(t, "echo \"$1\"\necho problem >&2\nexit 9")
|
||||
if !runner.Submit(Invocation{Script: script, Terminal: 3}) {
|
||||
t.Fatal("submit failed")
|
||||
}
|
||||
result := receive_result(t, runner)
|
||||
result := receiveResult(t, runner)
|
||||
if result.Err != nil || result.Stdout != "Username; echo not-a-shell\n" || result.Stderr != "problem\n" {
|
||||
t.Fatal(result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerDrainsFloodedOutputAndBoundsRetention(t *testing.T) {
|
||||
runner := New_runner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 64, Timeout: time.Second}, nil)
|
||||
runner := NewRunner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 64, Timeout: time.Second}, nil)
|
||||
defer runner.Close()
|
||||
script := fixture_script(t, "i=0; while [ \"$i\" -lt 10000 ]; do echo output; echo error >&2; i=$((i+1)); done")
|
||||
script := fixtureScript(t, "i=0; while [ \"$i\" -lt 10000 ]; do echo output; echo error >&2; i=$((i+1)); done")
|
||||
runner.Submit(Invocation{Script: script})
|
||||
result := receive_result(t, runner)
|
||||
result := receiveResult(t, runner)
|
||||
if result.Err != nil || !result.Truncated || len(result.Stdout) != 64 || len(result.Stderr) != 64 {
|
||||
t.Fatal(result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerTimeoutFreesWorkerAndCancellationInvalidatesQueuedWork(t *testing.T) {
|
||||
runner := New_runner(context.Background(), Limits{Workers: 1, Queue: 2, OutputBytes: 4096, Timeout: 150 * time.Millisecond}, nil)
|
||||
runner := NewRunner(context.Background(), Limits{Workers: 1, Queue: 2, OutputBytes: 4096, Timeout: 150 * time.Millisecond}, nil)
|
||||
defer runner.Close()
|
||||
slow := fixture_script(t, "sleep 30 &\nwait")
|
||||
slow := fixtureScript(t, "sleep 30 &\nwait")
|
||||
runner.Submit(Invocation{Script: slow})
|
||||
if result := receive_result(t, runner); !errors.Is(result.Err, context.DeadlineExceeded) {
|
||||
if result := receiveResult(t, runner); !errors.Is(result.Err, context.DeadlineExceeded) {
|
||||
t.Fatal(result)
|
||||
}
|
||||
runner.Submit(Invocation{Script: fixture_script(t, "echo recovered")})
|
||||
if result := receive_result(t, runner); result.Err != nil || result.Stdout != "recovered\n" {
|
||||
runner.Submit(Invocation{Script: fixtureScript(t, "echo recovered")})
|
||||
if result := receiveResult(t, runner); result.Err != nil || result.Stdout != "recovered\n" {
|
||||
t.Fatal(result)
|
||||
}
|
||||
generation := runner.Cancel()
|
||||
marker := filepath.Join(t.TempDir(), "must-not-run")
|
||||
runner.Submit(Invocation{Script: fixture_script(t, "touch '"+marker+"'")})
|
||||
if result := receive_result(t, runner); !errors.Is(result.Err, context.Canceled) {
|
||||
runner.Submit(Invocation{Script: fixtureScript(t, "touch '"+marker+"'")})
|
||||
if result := receiveResult(t, runner); !errors.Is(result.Err, context.Canceled) {
|
||||
t.Fatal(result)
|
||||
}
|
||||
if _, err := os.Stat(marker); !os.IsNotExist(err) {
|
||||
t.Fatal("stale invocation executed", err)
|
||||
}
|
||||
runner.Submit(Invocation{Script: fixture_script(t, "echo new-session"), Generation: generation})
|
||||
if result := receive_result(t, runner); result.Stdout != "new-session\n" {
|
||||
runner.Submit(Invocation{Script: fixtureScript(t, "echo new-session"), Generation: generation})
|
||||
if result := receiveResult(t, runner); result.Stdout != "new-session\n" {
|
||||
t.Fatal(result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerBoundsInheritedPipeWaitAndShutdown(t *testing.T) {
|
||||
runner := New_runner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 4096}, nil)
|
||||
script := fixture_script(t, "sleep 30 &\necho parent-finished")
|
||||
runner := NewRunner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 4096}, nil)
|
||||
script := fixtureScript(t, "sleep 30 &\necho parent-finished")
|
||||
runner.Submit(Invocation{Script: script})
|
||||
result := receive_result(t, runner)
|
||||
result := receiveResult(t, runner)
|
||||
if result.Err == nil || !strings.Contains(result.Stdout, "parent-finished") {
|
||||
t.Fatal(result)
|
||||
}
|
||||
runner.Submit(Invocation{Script: fixture_script(t, "sleep 30")})
|
||||
runner.Submit(Invocation{Script: fixtureScript(t, "sleep 30")})
|
||||
started := time.Now()
|
||||
runner.Close()
|
||||
if time.Since(started) > time.Second {
|
||||
@@ -120,7 +120,7 @@ func TestRunnerBoundsInheritedPipeWaitAndShutdown(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunnerFileChecksAndQueueBound(t *testing.T) {
|
||||
runner := New_runner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 32}, nil)
|
||||
runner := NewRunner(context.Background(), Limits{Workers: 1, Queue: 1, OutputBytes: 32}, nil)
|
||||
defer runner.Close()
|
||||
path := filepath.Join(t.TempDir(), "not-executable")
|
||||
if err := os.WriteFile(path, []byte("#!/bin/sh\n"), 0600); err != nil {
|
||||
@@ -128,7 +128,7 @@ func TestRunnerFileChecksAndQueueBound(t *testing.T) {
|
||||
}
|
||||
for _, fixture := range []struct{ path, want string }{{path, "Script file is not executable"}, {filepath.Dir(path), "Script source is not a valid file"}, {path + "missing", "Script file not found"}} {
|
||||
runner.Submit(Invocation{Script: Script{Path: fixture.path}})
|
||||
result := receive_result(t, runner)
|
||||
result := receiveResult(t, runner)
|
||||
if result.Err == nil || result.Err.Error() != fixture.want {
|
||||
t.Fatal(result)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user