Files
iris-context/root/init_test.go

75 lines
1.6 KiB
Go

package root
import (
"io"
"os"
"strings"
"testing"
)
func captureInitScript(t *testing.T, shell string) string {
t.Helper()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
original := os.Stdout
os.Stdout = w
initCmd.Run(initCmd, []string{shell})
_ = w.Close()
os.Stdout = original
out, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
return string(out)
}
// a tool runner that sources the rc file in a non-interactive shell must not
// exec a second iris, which would seize the tty from the one already running
func TestInitAutostartRequiresInteractiveShell(t *testing.T) {
guards := map[string]string{
"zsh": "[[ -o interactive ]]",
"bash": "[[ $- == *i* ]]",
"fish": "status is-interactive",
}
for shell, guard := range guards {
t.Run(shell, func(t *testing.T) {
script := captureInitScript(t, shell)
autostart := `exec "${IRIS_BIN:-iris}"`
if shell == "fish" {
autostart = `exec "$IRIS_BIN"`
}
head, _, found := strings.Cut(script, autostart)
if !found {
t.Fatalf("%s init script has no autostart", shell)
}
cond := strings.LastIndex(head, "\nif ")
if cond < 0 {
t.Fatalf("%s autostart is not inside an if", shell)
}
if !strings.Contains(head[cond:], guard) {
t.Fatalf("%s autostart is not guarded by %q:\n%s", shell, guard, head[cond:])
}
})
}
}
func TestInitAutostartSupportsBinaryOverride(t *testing.T) {
for _, shell := range []string{"zsh", "bash", "fish"} {
t.Run(shell, func(t *testing.T) {
script := captureInitScript(t, shell)
if !strings.Contains(script, "IRIS_BIN") {
t.Fatalf("%s init script does not support IRIS_BIN", shell)
}
})
}
}