github.com/brandonstevens/terraform@v0.9.6-0.20170512224929-5367f2607e16/command/get_test.go (about)

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/cli"
     9  )
    10  
    11  func TestGet(t *testing.T) {
    12  	tmp, cwd := testCwd(t)
    13  	defer testFixCwd(t, tmp, cwd)
    14  
    15  	ui := new(cli.MockUi)
    16  	c := &GetCommand{
    17  		Meta: Meta{
    18  			ContextOpts: testCtxConfig(testProvider()),
    19  			Ui:          ui,
    20  			dataDir:     tempDir(t),
    21  		},
    22  	}
    23  
    24  	args := []string{
    25  		testFixturePath("get"),
    26  	}
    27  	if code := c.Run(args); code != 0 {
    28  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    29  	}
    30  
    31  	output := ui.OutputWriter.String()
    32  	if !strings.Contains(output, "Get: file://") {
    33  		t.Fatalf("doesn't look like get: %s", output)
    34  	}
    35  	if strings.Contains(output, "(update)") {
    36  		t.Fatalf("doesn't look like get: %s", output)
    37  	}
    38  }
    39  
    40  func TestGet_multipleArgs(t *testing.T) {
    41  	ui := new(cli.MockUi)
    42  	c := &GetCommand{
    43  		Meta: Meta{
    44  			ContextOpts: testCtxConfig(testProvider()),
    45  			Ui:          ui,
    46  			dataDir:     tempDir(t),
    47  		},
    48  	}
    49  
    50  	args := []string{
    51  		"bad",
    52  		"bad",
    53  	}
    54  	if code := c.Run(args); code != 1 {
    55  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    56  	}
    57  }
    58  
    59  func TestGet_noArgs(t *testing.T) {
    60  	cwd, err := os.Getwd()
    61  	if err != nil {
    62  		t.Fatalf("err: %s", err)
    63  	}
    64  	if err := os.Chdir(testFixturePath("get")); err != nil {
    65  		t.Fatalf("err: %s", err)
    66  	}
    67  	defer os.Chdir(cwd)
    68  
    69  	ui := new(cli.MockUi)
    70  	c := &GetCommand{
    71  		Meta: Meta{
    72  			ContextOpts: testCtxConfig(testProvider()),
    73  			Ui:          ui,
    74  			dataDir:     tempDir(t),
    75  		},
    76  	}
    77  
    78  	args := []string{}
    79  	if code := c.Run(args); code != 0 {
    80  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    81  	}
    82  
    83  	output := ui.OutputWriter.String()
    84  	if !strings.Contains(output, "Get: file://") {
    85  		t.Fatalf("doesn't look like get: %s", output)
    86  	}
    87  	if strings.Contains(output, "(update)") {
    88  		t.Fatalf("doesn't look like get: %s", output)
    89  	}
    90  }
    91  
    92  func TestGet_update(t *testing.T) {
    93  	tmp, cwd := testCwd(t)
    94  	defer testFixCwd(t, tmp, cwd)
    95  
    96  	ui := new(cli.MockUi)
    97  	c := &GetCommand{
    98  		Meta: Meta{
    99  			ContextOpts: testCtxConfig(testProvider()),
   100  			Ui:          ui,
   101  			dataDir:     tempDir(t),
   102  		},
   103  	}
   104  
   105  	args := []string{
   106  		"-update",
   107  		testFixturePath("get"),
   108  	}
   109  	if code := c.Run(args); code != 0 {
   110  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
   111  	}
   112  
   113  	output := ui.OutputWriter.String()
   114  	if !strings.Contains(output, "Get: file://") {
   115  		t.Fatalf("doesn't look like get: %s", output)
   116  	}
   117  	if !strings.Contains(output, "(update)") {
   118  		t.Fatalf("doesn't look like get: %s", output)
   119  	}
   120  }