Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/devbox/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"os"

"github.com/docker/compose/v5/pkg/api"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -91,7 +92,7 @@ func runRun(ctx context.Context, p *project.Project, command string, args []stri
case scenario.Tty != nil:
tty = *scenario.Tty
default:
tty = isTTYAvailable()
tty = isTTYAvailable(os.Stdin)
}

opts := project.RunOptions{
Expand Down
12 changes: 4 additions & 8 deletions cmd/devbox/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/moby/moby/api/pkg/stdcopy"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
"golang.org/x/term"

"github.com/pilat/devbox/internal/project"
)
Expand Down Expand Up @@ -84,7 +85,7 @@ func runShell(ctx context.Context, p *project.Project, serviceName string, noTty
if noTtyFlag {
tty = false
} else {
tty = isTTYAvailable()
tty = isTTYAvailable(os.Stdin)
}

opts := project.RunOptions{
Expand Down Expand Up @@ -160,11 +161,6 @@ func filterLabels(projectName, serviceName string) client.Filters {
Add("label", "com.docker.compose.container-number=1")
}

func isTTYAvailable() bool {
stat, err := os.Stdin.Stat()
if err != nil {
return false
}

return (stat.Mode() & os.ModeCharDevice) != 0
func isTTYAvailable(f *os.File) bool {
return term.IsTerminal(int(f.Fd()))
}
47 changes: 47 additions & 0 deletions cmd/devbox/tty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"os"
"testing"
)

func TestIsTTYAvailable(t *testing.T) {
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatalf("failed to open %s: %v", os.DevNull, err)
}
t.Cleanup(func() { devNull.Close() })

tmpFile, err := os.CreateTemp(t.TempDir(), "tty")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
t.Cleanup(func() { tmpFile.Close() })

pipeR, pipeW, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe: %v", err)
}
t.Cleanup(func() {
pipeR.Close()
pipeW.Close()
})

tests := []struct {
name string
file *os.File
want bool
}{
{name: "dev null is not a terminal", file: devNull, want: false},
{name: "regular file is not a terminal", file: tmpFile, want: false},
{name: "pipe is not a terminal", file: pipeR, want: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isTTYAvailable(tt.file); got != tt.want {
t.Errorf("isTTYAvailable() = %v, want %v", got, tt.want)
}
})
}
}