github.com/daveadams/terraform@v0.6.4-0.20160830094355-13ce74975936/command/state_list_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/mitchellh/cli"
     8  )
     9  
    10  func TestStateList(t *testing.T) {
    11  	state := testState()
    12  	statePath := testStateFile(t, state)
    13  
    14  	p := testProvider()
    15  	ui := new(cli.MockUi)
    16  	c := &StateListCommand{
    17  		Meta: Meta{
    18  			ContextOpts: testCtxConfig(p),
    19  			Ui:          ui,
    20  		},
    21  	}
    22  
    23  	args := []string{
    24  		"-state", statePath,
    25  	}
    26  	if code := c.Run(args); code != 0 {
    27  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    28  	}
    29  
    30  	// Test that outputs were displayed
    31  	expected := strings.TrimSpace(testStateListOutput) + "\n"
    32  	actual := ui.OutputWriter.String()
    33  	if actual != expected {
    34  		t.Fatalf("Expected:\n%q\n\nTo equal: %q", actual, expected)
    35  	}
    36  }
    37  
    38  func TestStateList_noState(t *testing.T) {
    39  	tmp, cwd := testCwd(t)
    40  	defer testFixCwd(t, tmp, cwd)
    41  
    42  	p := testProvider()
    43  	ui := new(cli.MockUi)
    44  	c := &StateListCommand{
    45  		Meta: Meta{
    46  			ContextOpts: testCtxConfig(p),
    47  			Ui:          ui,
    48  		},
    49  	}
    50  
    51  	args := []string{}
    52  	if code := c.Run(args); code != 1 {
    53  		t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
    54  	}
    55  }
    56  
    57  const testStateListOutput = `
    58  test_instance.foo
    59  `