Update test for autocompletion of top-level commands (#37853)
Some checks failed
build / Determine intended Terraform version (push) Has been cancelled
build / Determine Go toolchain version (push) Has been cancelled
Quick Checks / Unit Tests (push) Has been cancelled
Quick Checks / Race Tests (push) Has been cancelled
Quick Checks / End-to-end Tests (push) Has been cancelled
Quick Checks / Code Consistency Checks (push) Has been cancelled
build / Generate release metadata (push) Has been cancelled
build / Build for freebsd_386 (push) Has been cancelled
build / Build for linux_386 (push) Has been cancelled
build / Build for openbsd_386 (push) Has been cancelled
build / Build for windows_386 (push) Has been cancelled
build / Build for darwin_amd64 (push) Has been cancelled
build / Build for freebsd_amd64 (push) Has been cancelled
build / Build for linux_amd64 (push) Has been cancelled
build / Build for openbsd_amd64 (push) Has been cancelled
build / Build for solaris_amd64 (push) Has been cancelled
build / Build for windows_amd64 (push) Has been cancelled
build / Build for freebsd_arm (push) Has been cancelled
build / Build for linux_arm (push) Has been cancelled
build / Build for darwin_arm64 (push) Has been cancelled
build / Build for linux_arm64 (push) Has been cancelled
build / Build for windows_arm64 (push) Has been cancelled
build / Build Docker image for linux_386 (push) Has been cancelled
build / Build Docker image for linux_amd64 (push) Has been cancelled
build / Build Docker image for linux_arm (push) Has been cancelled
build / Build Docker image for linux_arm64 (push) Has been cancelled
build / Build e2etest for linux_386 (push) Has been cancelled
build / Build e2etest for windows_386 (push) Has been cancelled
build / Build e2etest for darwin_amd64 (push) Has been cancelled
build / Build e2etest for linux_amd64 (push) Has been cancelled
build / Build e2etest for windows_amd64 (push) Has been cancelled
build / Build e2etest for linux_arm (push) Has been cancelled
build / Build e2etest for darwin_arm64 (push) Has been cancelled
build / Build e2etest for linux_arm64 (push) Has been cancelled
build / Run e2e test for linux_386 (push) Has been cancelled
build / Run e2e test for windows_386 (push) Has been cancelled
build / Run e2e test for darwin_amd64 (push) Has been cancelled
build / Run e2e test for linux_amd64 (push) Has been cancelled
build / Run e2e test for windows_amd64 (push) Has been cancelled
build / Run e2e test for linux_arm (push) Has been cancelled
build / Run e2e test for linux_arm64 (push) Has been cancelled
build / Run terraform-exec test for linux amd64 (push) Has been cancelled

* 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
This commit is contained in:
Sarah French 2025-11-05 11:24:29 +00:00 committed by GitHub
parent f4d0ec5136
commit 9595517730
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 {