summaryrefslogtreecommitdiff
path: root/gocomplete/tests_test.go
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2021-04-08 00:04:50 +0300
committerGitHub <[email protected]>2021-04-08 00:04:50 +0300
commit3f9152130d1c1e72ef5b0091380bfbeb7fafecf5 (patch)
treea622156da3220fccf4d72d0dc3bef4175c8bc241 /gocomplete/tests_test.go
parent495a97af070e804b9000b846173193dd8d9eb1da (diff)
parente203f94963fb806fd8c7cc2339b96d458f84766e (diff)
Merge pull request #140 from antichris/gocomplete-suppress-errors
gocomplete: suppress error output by default
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())
+ }
+ })
+ }
+}