github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/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 := tempWorkingDir(t)
    36  	defer testChdir(t, wd.RootModuleDir())()
    37  
    38  	ui := cli.NewMockUi()
    39  	c := &GetCommand{
    40  		Meta: Meta{
    41  			testingOverrides: metaOverridesForProvider(testProvider()),
    42  			Ui:               ui,
    43  			WorkingDir:       wd,
    44  		},
    45  	}
    46  
    47  	args := []string{
    48  		"bad",
    49  		"bad",
    50  	}
    51  	if code := c.Run(args); code != 1 {
    52  		t.Fatalf("bad: \n%s", ui.OutputWriter.String())
    53  	}
    54  }
    55  
    56  func TestGet_update(t *testing.T) {
    57  	wd := tempWorkingDirFixture(t, "get")
    58  	defer testChdir(t, wd.RootModuleDir())()
    59  
    60  	ui := cli.NewMockUi()
    61  	c := &GetCommand{
    62  		Meta: Meta{
    63  			testingOverrides: metaOverridesForProvider(testProvider()),
    64  			Ui:               ui,
    65  			WorkingDir:       wd,
    66  		},
    67  	}
    68  
    69  	args := []string{
    70  		"-update",
    71  	}
    72  	if code := c.Run(args); code != 0 {
    73  		t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
    74  	}
    75  
    76  	output := ui.OutputWriter.String()
    77  	if !strings.Contains(output, `- foo in`) {
    78  		t.Fatalf("doesn't look like get: %s", output)
    79  	}
    80  }
    81  
    82  func TestGet_cancel(t *testing.T) {
    83  	// This test runs `terraform get` as if SIGINT (or similar on other
    84  	// platforms) were sent to it, testing that it is interruptible.
    85  
    86  	wd := tempWorkingDirFixture(t, "init-registry-module")
    87  	defer testChdir(t, wd.RootModuleDir())()
    88  
    89  	// Our shutdown channel is pre-closed so init will exit as soon as it
    90  	// starts a cancelable portion of the process.
    91  	shutdownCh := make(chan struct{})
    92  	close(shutdownCh)
    93  
    94  	ui := cli.NewMockUi()
    95  	c := &GetCommand{
    96  		Meta: Meta{
    97  			testingOverrides: metaOverridesForProvider(testProvider()),
    98  			Ui:               ui,
    99  			WorkingDir:       wd,
   100  			ShutdownCh:       shutdownCh,
   101  		},
   102  	}
   103  
   104  	args := []string{}
   105  	if code := c.Run(args); code == 0 {
   106  		t.Fatalf("succeeded; wanted error\n%s", ui.OutputWriter.String())
   107  	}
   108  
   109  	if got, want := ui.ErrorWriter.String(), `Module installation was canceled by an interrupt signal`; !strings.Contains(got, want) {
   110  		t.Fatalf("wrong error message\nshould contain: %s\ngot:\n%s", want, got)
   111  	}
   112  }