mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix: Interactive pty identification for proxy mode (#174)
This commit is contained in:
+10
-2
@@ -36,13 +36,21 @@ type InteractiveSession interface {
|
||||
|
||||
// IsInteractiveTerminal returns true if stdin is a real terminal (TTY).
|
||||
// Returns false in CI environments (when the "CI" env var set to "true"),
|
||||
// when input is piped, or in non-interactive shells.
|
||||
// when input or output is piped, or in non-interactive shells.
|
||||
func IsInteractiveTerminal() bool {
|
||||
if ci := os.Getenv("CI"); ci != "" && strings.ToLower(ci) == "true" {
|
||||
return false
|
||||
}
|
||||
|
||||
return term.IsTerminal(int(os.Stdin.Fd()))
|
||||
if !term.IsTerminal(int(os.Stdout.Fd())) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
var _ InteractiveSession = &session{}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package pty
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsInteractiveTerminal(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ciEnv string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "returns false when CI env is set to true",
|
||||
ciEnv: "true",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false when CI env is set to TRUE (case insensitive)",
|
||||
ciEnv: "TRUE",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false when CI env is set to True (mixed case)",
|
||||
ciEnv: "True",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "returns false in test runner (stdin/stdout are pipes)",
|
||||
ciEnv: "",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.ciEnv != "" {
|
||||
t.Setenv("CI", tt.ciEnv)
|
||||
} else {
|
||||
t.Setenv("CI", "")
|
||||
}
|
||||
|
||||
result := IsInteractiveTerminal()
|
||||
assert.Equal(t, tt.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user