Update test for autocompletion of top-level commands (#37853)

* Update test for autocompletion of top-level commands

* Add test coverage for autocompletion of workspace names in `workspace select` subcommand

* Remove autocompletion tests for workspace select
pull/37880/head
Sarah French 3 months ago committed by GitHub
parent f4d0ec5136
commit 9595517730
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -4,10 +4,13 @@
package main
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/hashicorp/cli"
)
@ -279,26 +282,67 @@ func TestMain_autoComplete(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
// Restore stdout
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
defer func() { os.Stdout = old }()
// Set up test command and restore that
Commands = make(map[string]cli.CommandFactory)
defer func() {
Commands = nil
}()
// Set up test command and restore that
Commands["foo"] = func() (cli.Command, error) {
Commands["version"] = func() (cli.Command, error) {
return &testCommandCLI{}, nil
}
// Run command that should get autocomplete suggestion "version"
os.Setenv("COMP_LINE", "terraform versio")
defer os.Unsetenv("COMP_LINE")
// Run it!
os.Args = []string{"terraform", "terraform", "versio"}
exit := realMain()
if exit != 0 {
t.Fatalf("unexpected exit status %d; want 0", exit)
}
// Check autocomplete suggestion
expectedAutocomplete := "version"
b := make([]byte, 25)
n, err := r.Read(b)
if err != nil {
t.Fatal(err)
}
output := string(b[0:n])
output = strings.ReplaceAll(output, "\n", "")
if output != expectedAutocomplete {
t.Fatalf("expected autocompletion to return %q, but got %q", expectedAutocomplete, output)
}
// Run command that should NOT get an autocomplete suggestion
r, w, _ = os.Pipe()
os.Stdout = w
os.Setenv("COMP_LINE", "terraform zzz")
defer os.Unsetenv("COMP_LINE")
os.Args = []string{"terraform", "terraform", "zzz"}
exit = realMain()
if exit != 0 {
t.Fatalf("unexpected exit status %d; want 0", exit)
}
// Avoid infinite blocking in this case, where no autocomplete suggestions are returned
r.SetReadDeadline(time.Now().Add(time.Duration(1 * time.Second)))
// Check autocomplete suggestion
b = make([]byte, 25)
n, err = r.Read(b)
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
t.Fatal(err)
}
if n != 0 {
t.Fatalf("expected autocompletion to return 0 bytes, but got %d: %q", n, b[0:n])
}
}
type testCommandCLI struct {

Loading…
Cancel
Save