Kaynağa Gözat

pkg/mods/file: Only test is-tty with /dev/tty if it can be opened.

The file cannot be opened when not running in a terminal.
Qi Xiao 1 yıl önce
ebeveyn
işleme
1f0f2a3e9d
1 değiştirilmiş dosya ile 19 ekleme ve 6 silme
  1. 19 6
      pkg/mods/file/file_unix_test.go

+ 19 - 6
pkg/mods/file/file_unix_test.go

@@ -3,6 +3,7 @@
 package file
 
 import (
+	"os"
 	"testing"
 
 	"src.elv.sh/pkg/eval"
@@ -14,10 +15,22 @@ func TestIsTTY(t *testing.T) {
 		ev.ExtendGlobal(eval.BuildNs().AddNs("file", Ns))
 	}
 
-	evaltest.TestWithSetup(t, setup,
-		That("file:is-tty 0 < /dev/null").Puts(false),
-		That("file:is-tty (num 0) < /dev/null").Puts(false),
-		That("file:is-tty 0 < /dev/tty").Puts(true),
-		That("file:is-tty (num 0) < /dev/tty").Puts(true),
-	)
+	if canOpen("/dev/null") {
+		evaltest.TestWithSetup(t, setup,
+			That("file:is-tty 0 < /dev/null").Puts(false),
+			That("file:is-tty (num 0) < /dev/null").Puts(false),
+		)
+	}
+	if canOpen("/dev/tty") {
+		evaltest.TestWithSetup(t, setup,
+			That("file:is-tty 0 < /dev/tty").Puts(true),
+			That("file:is-tty (num 0) < /dev/tty").Puts(true),
+		)
+	}
+}
+
+func canOpen(name string) bool {
+	f, err := os.Open(name)
+	f.Close()
+	return err == nil
 }