github.com/opentofu/opentofu@v1.7.1/internal/command/get_test.go (about)

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