Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
package main

import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"testing"
"time"

"github.com/hashicorp/cli"
)
Expand Down Expand Up @@ -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 }()
Comment on lines +288 to +289
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we could find a way of testing this without having to override the global os.Stdout and os.Args but I suppose it's hard if this is to be kept as "E2E" and use realMain() as the testable entrypoint. 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think the alternative to this particular test is setting up a full E2E test that actually builds and uses a Terraform binary.

Regarding overriding global variables, my personal favourite is:

// Set to true when we're testing
var test bool = false


// 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 {
Expand Down