summaryrefslogtreecommitdiff
path: root/gocomplete/tests_test.go
diff options
context:
space:
mode:
authorChris Nobody <[email protected]>2021-04-06 14:23:51 +0300
committerChris Nobody <[email protected]>2021-04-06 14:23:51 +0300
commite203f94963fb806fd8c7cc2339b96d458f84766e (patch)
treefebc157f142baa89fe8ae5466a4ff0ad990417ec /gocomplete/tests_test.go
parenta2c2f61e14485dafe98feb0613528a633a04e835 (diff)
gocomplete: suppress error output by default
Prevent the Go command completion from jumbling up the shell prompt on errors by setting the Writer of the standard lib default log.Logger to ioutil.Discard unless the system environment variable `GOCOMP_VERBOSE` has been explicitly set to "1" (e.g. for debugging purposes). Fixes #139
Diffstat (limited to 'gocomplete/tests_test.go')
-rw-r--r--gocomplete/tests_test.go80
1 files changed, 79 insertions, 1 deletions
diff --git a/gocomplete/tests_test.go b/gocomplete/tests_test.go
index a1238a4..dbe72cf 100644
--- a/gocomplete/tests_test.go
+++ b/gocomplete/tests_test.go
@@ -1,8 +1,14 @@
package main
import (
+ "fmt"
+ "io"
+ "io/ioutil"
+ "log"
"os"
"sort"
+ "strconv"
+ "strings"
"testing"
"bou.ke/monkey"
@@ -21,7 +27,11 @@ func TestPredictions(t *testing.T) {
{
name: "predict tests ok",
predictor: predictTest,
- want: []string{"TestPredictions", "Example"},
+ want: []string{
+ "TestPredictions",
+ "Example",
+ "TestErrorSupression",
+ },
},
{
name: "predict benchmark ok",
@@ -64,3 +74,71 @@ func equal(s1, s2 []string) bool {
}
return true
}
+
+func TestErrorSupression(t *testing.T) {
+ defer monkey.Patch(os.Exit, func(int) {}).Unpatch()
+
+ // Completion API environment variable names.
+ const envLine, envPoint = "COMP_LINE", "COMP_POINT"
+
+ // line should work out to
+ //
+ // * on most POSIX:
+ // go test /tmp/
+ // * on MacOS X:
+ // go test /var/folders/<randomized_pathname>/T//
+ // * on Windows:
+ // go test C:\Users\<username>\AppData\Local\Temp\
+ //
+ // which should trigger "failed importing directory: ... no
+ // buildable Go source files..." error messages.
+ var line = "go test " + os.TempDir() + string(os.PathSeparator)
+
+ defer os.Unsetenv(envLine)
+ defer os.Unsetenv(envPoint)
+ os.Setenv(envLine, line)
+ os.Setenv(envPoint, strconv.Itoa(len(line)))
+
+ tests := []struct {
+ verbose string
+ wantErr bool
+ }{{
+ verbose: "",
+ wantErr: false,
+ }, {
+ verbose: "1",
+ wantErr: true,
+ }}
+ for _, tt := range tests {
+ t.Run(fmt.Sprintf(
+ "%s=%q", envVerbose, tt.verbose,
+ ), func(t *testing.T) {
+ // Discard completion (stdout).
+ r, w, err := os.Pipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer w.Close()
+ defer func(o *os.File) { os.Stdout = o }(os.Stdout)
+ os.Stdout = w
+ go io.Copy(ioutil.Discard, r)
+
+ // "Redirect" stderr into a buffer.
+ b := &strings.Builder{}
+ log.SetOutput(b)
+
+ defer os.Unsetenv(envVerbose)
+ os.Setenv(envVerbose, tt.verbose)
+
+ main()
+
+ gotErr := b.Len() != 0
+ if tt.wantErr && !gotErr {
+ t.Fatal("want something in stderr, got nothing")
+ } else if !tt.wantErr && gotErr {
+ t.Fatalf("want nothing in stderr, got %d bytes",
+ b.Len())
+ }
+ })
+ }
+}