github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/command/get_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/mitchellh/cli"
     8  )
     9  
    10  func TestGet(t *testing.T) {
    11  	wd := tempWorkingDirFixture(t, "get")
    12  	defer testChdir(t, wd.RootModuleDir())()
    13  
    14  	ui := cli.NewMockUi()
    15  	c := &GetCommand{
    16  		Meta: Meta{
    17  			testingOverrides: metaOverridesForProvider(testProvider()),
    18  			Ui:               ui,
    19  			WorkingDir:       wd,
    20  		},
    21  	}
    22  
    23  	args := []string{}
    24  	if code := c.Run(args); code != 0 {
    25  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    26  	}
    27  
    28  	output := ui.OutputWriter.String()
    29  	if !strings.Contains(output, "- foo in") {
    30  		t.Fatalf("doesn't look like get: %s", output)
    31  	}
    32  }
    33  
    34  func TestGet_multipleArgs(t *testing.T) {
    35  	wd, cleanup := tempWorkingDir(t)
    36  	defer cleanup()
    37  	defer testChdir(t, wd.RootModuleDir())()
    38  
    39  	ui := cli.NewMockUi()
    40  	c := &GetCommand{
    41  		Meta: Meta{
    42  			testingOverrides: metaOverridesForProvider(testProvider()),
    43  			Ui:               ui,
    44  			WorkingDir:       wd,
    45  		},
    46  	}
    47  
    48  	args := []string{
    49  		"bad",
    50  		"bad",
    51  	}
    52  	if code := c.Run(args); code != 1 {
    53  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    54  	}
    55  }
    56  
    57  func TestGet_update(t *testing.T) {
    58  	wd := tempWorkingDirFixture(t, "get")
    59  	defer testChdir(t, wd.RootModuleDir())()
    60  
    61  	ui := cli.NewMockUi()
    62  	c := &GetCommand{
    63  		Meta: Meta{
    64  			testingOverrides: metaOverridesForProvider(testProvider()),
    65  			Ui:               ui,
    66  			WorkingDir:       wd,
    67  		},
    68  	}
    69  
    70  	args := []string{
    71  		"-update",
    72  	}
    73  	if code := c.Run(args); code != 0 {
    74  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    75  	}
    76  
    77  	output := ui.OutputWriter.String()
    78  	if !strings.Contains(output, `- foo in`) {
    79  		t.Fatalf("doesn't look like get: %s", output)
    80  	}
    81  }
    82  
    83  func TestGet_cancel(t *testing.T) {
    84  	// This test runs `terraform get` as if SIGINT (or similar on other
    85  	// platforms) were sent to it, testing that it is interruptible.
    86  
    87  	wd := tempWorkingDirFixture(t, "init-registry-module")
    88  	defer testChdir(t, wd.RootModuleDir())()
    89  
    90  	// Our shutdown channel is pre-closed so init will exit as soon as it
    91  	// starts a cancelable portion of the process.
    92  	shutdownCh := make(chan struct{})
    93  	close(shutdownCh)
    94  
    95  	ui := cli.NewMockUi()
    96  	c := &GetCommand{
    97  		Meta: Meta{
    98  			testingOverrides: metaOverridesForProvider(testProvider()),
    99  			Ui:               ui,
   100  			WorkingDir:       wd,
   101  			ShutdownCh:       shutdownCh,
   102  		},
   103  	}
   104  
   105  	args := []string{}
   106  	if code := c.Run(args); code == 0 {
   107  		t.Fatalf("succeeded; wanted error\n%s", ui.OutputWriter.String())
   108  	}
   109  
   110  	if got, want := ui.ErrorWriter.String(), `Module installation was canceled by an interrupt signal`; !strings.Contains(got, want) {
   111  		t.Fatalf("wrong error message\nshould contain: %s\ngot:\n%s", want, got)
   112  	}
   113  }