add non-interactive mode: -c, script files and commands from stdin
- gosh -c 'command' runs the command and exits, so gosh works as login shell for ssh host command, scp and rsync - gosh script runs a script file, lines starting with # are skipped - without a terminal, commands are read from stdin (byte by byte, so started commands can read the rest of stdin) - the exit status is the one of the last command (127 not found, 126 not executable, 128+n killed by signal n) - -i forces interactive mode, -l/--login is accepted Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f51680f83c
commit
b602e17622
3 files changed
+252
-7
No files matched your search
+83
-2
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
@@ -1173,7 +1174,7 @@ func TestShell_History(t *testing.T) {
|
||||
histFile := filepath.Join(t.TempDir(), "gosh", "history")
|
||||
workDir := t.TempDir()
|
||||
|
||||
cmd := exec.Command(bin)
|
||||
cmd := exec.Command(bin, "-i")
|
||||
cmd.Dir = workDir
|
||||
cmd.Env = append(os.Environ(), "GOSH_HISTFILE="+histFile)
|
||||
cmd.Stdin = strings.NewReader("echo first\n set SECRET=hunter2\nexit\n")
|
||||
@@ -1205,7 +1206,7 @@ func TestShell_History(t *testing.T) {
|
||||
func TestShell_SIGINTKillsOnlyChild(t *testing.T) {
|
||||
bin := buildShell(t)
|
||||
|
||||
cmd := exec.Command(bin)
|
||||
cmd := exec.Command(bin, "-i")
|
||||
// Own process group, so that we can signal it like the terminal does
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
stdin, err := cmd.StdinPipe()
|
||||
@@ -1267,3 +1268,83 @@ func TestExecInput_PassesStdin(t *testing.T) {
|
||||
t.Errorf("command read %q from stdin, want %q", got, "hello")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExitStatus(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want int
|
||||
}{
|
||||
{"true", 0},
|
||||
{"false", 1},
|
||||
{`sh -c "exit 42"`, 42},
|
||||
{`sh -c "kill -TERM \$\$"`, 128 + 15},
|
||||
{"gosh-no-such-command", 127},
|
||||
{"cd /gosh-no-such-dir", 127},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
if got := exitStatus(execInput(tc.input)); got != tc.want {
|
||||
t.Errorf("exitStatus(execInput(%q)) = %d, want %d", tc.input, got, tc.want)
|
||||
}
|
||||
}
|
||||
if got := exitStatus(errors.New("other")); got != 1 {
|
||||
t.Errorf("exitStatus(other error) = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunScript(t *testing.T) {
|
||||
origDir, _ := os.Getwd()
|
||||
defer os.Chdir(origDir)
|
||||
|
||||
dir := t.TempDir()
|
||||
script := "#!/usr/bin/gosh\n" +
|
||||
"# comment\n" +
|
||||
"\n" +
|
||||
"cd " + dir + "\n" +
|
||||
" # indented comment\n" +
|
||||
"touch first\n" +
|
||||
"false\n" +
|
||||
"touch second" // no trailing newline
|
||||
if got := runScript(strings.NewReader(script)); got != 0 {
|
||||
t.Errorf("runScript() = %d, want 0", got)
|
||||
}
|
||||
for _, name := range []string{"first", "second"} {
|
||||
if _, err := os.Stat(filepath.Join(dir, name)); err != nil {
|
||||
t.Errorf("expected %s to be created: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// The status of the last command is returned
|
||||
if got := runScript(strings.NewReader("true\nsh -c 'exit 3'\n")); got != 3 {
|
||||
t.Errorf("runScript() = %d, want 3", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_Options(t *testing.T) {
|
||||
tests := []struct {
|
||||
args []string
|
||||
want int
|
||||
}{
|
||||
{[]string{"-c", "true"}, 0},
|
||||
{[]string{"-c", "false"}, 1},
|
||||
{[]string{"-l", "-c", "sh -c 'exit 5'"}, 5},
|
||||
{[]string{"-c"}, 2},
|
||||
{[]string{"-x"}, 2},
|
||||
{[]string{"/gosh/no/such/script"}, 127},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
if got := run(tc.args); got != tc.want {
|
||||
t.Errorf("run(%q) = %d, want %d", tc.args, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadLine_DoesNotReadAhead(t *testing.T) {
|
||||
r := strings.NewReader("first\nsecond\n")
|
||||
line, err := readLine(r)
|
||||
if line != "first" || err != nil {
|
||||
t.Fatalf("readLine() = %q, %v", line, err)
|
||||
}
|
||||
if r.Len() != len("second\n") {
|
||||
t.Errorf("readLine() consumed more than one line, %d bytes left", r.Len())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user