github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/command/get_test.go (about)

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