github.com/alexaandru/terraform@v0.11.1-0.20171120185746-28632790b723/command/autocomplete_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/mitchellh/cli"
    10  	"github.com/posener/complete"
    11  )
    12  
    13  func TestMetaCompletePredictWorkspaceName(t *testing.T) {
    14  	// Create a temporary working directory that is empty
    15  	td := tempDir(t)
    16  	os.MkdirAll(td, 0755)
    17  	defer os.RemoveAll(td)
    18  	defer testChdir(t, td)()
    19  
    20  	// make sure a vars file doesn't interfere
    21  	err := ioutil.WriteFile(DefaultVarsFilename, nil, 0644)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	ui := new(cli.MockUi)
    27  	meta := &Meta{Ui: ui}
    28  
    29  	predictor := meta.completePredictWorkspaceName()
    30  
    31  	t.Run("no prefix", func(t *testing.T) {
    32  		got := predictor.Predict(complete.Args{
    33  			Last: "",
    34  		})
    35  		want := []string{"default"}
    36  		if !reflect.DeepEqual(got, want) {
    37  			t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, want)
    38  		}
    39  	})
    40  
    41  	t.Run("prefix that matches", func(t *testing.T) {
    42  		got := predictor.Predict(complete.Args{
    43  			Last: "def",
    44  		})
    45  		want := []string{"default"}
    46  		if !reflect.DeepEqual(got, want) {
    47  			t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, want)
    48  		}
    49  	})
    50  
    51  	t.Run("prefix that doesn't match", func(t *testing.T) {
    52  		got := predictor.Predict(complete.Args{
    53  			Last: "x",
    54  		})
    55  		want := []string{}
    56  		if !reflect.DeepEqual(got, want) {
    57  			t.Errorf("wrong result\ngot:  %#v\nwant: %#v", got, want)
    58  		}
    59  	})
    60  }