2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-09-22 12:56:50 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-09-22 13:15:27 -05:00
|
|
|
"strings"
|
2014-09-22 12:56:50 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2021-09-13 12:22:36 -05:00
|
|
|
wd := tempWorkingDirFixture(t, "get")
|
2021-09-01 19:01:44 -05:00
|
|
|
defer testChdir(t, wd.RootModuleDir())()
|
2017-02-07 11:11:48 -06:00
|
|
|
|
2021-09-01 19:01:44 -05:00
|
|
|
ui := cli.NewMockUi()
|
2014-09-22 12:56:50 -05:00
|
|
|
c := &GetCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-09-01 19:01:44 -05:00
|
|
|
WorkingDir: wd,
|
2014-09-22 12:56:50 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-02 09:35:45 -06:00
|
|
|
args := []string{}
|
2014-09-22 12:56:50 -05:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2014-09-22 13:15:27 -05:00
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
2019-01-17 17:56:53 -06:00
|
|
|
if !strings.Contains(output, "- foo in") {
|
2014-09-22 13:18:49 -05:00
|
|
|
t.Fatalf("doesn't look like get: %s", output)
|
|
|
|
}
|
2014-09-22 12:56:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet_multipleArgs(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
wd := tempWorkingDir(t)
|
2021-09-01 19:01:44 -05:00
|
|
|
defer testChdir(t, wd.RootModuleDir())()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
2014-09-22 12:56:50 -05:00
|
|
|
c := &GetCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-09-01 19:01:44 -05:00
|
|
|
WorkingDir: wd,
|
2014-09-22 12:56:50 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"bad",
|
|
|
|
"bad",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 09:35:45 -06:00
|
|
|
func TestGet_update(t *testing.T) {
|
2021-09-13 12:22:36 -05:00
|
|
|
wd := tempWorkingDirFixture(t, "get")
|
2021-09-01 19:01:44 -05:00
|
|
|
defer testChdir(t, wd.RootModuleDir())()
|
2014-09-22 12:56:50 -05:00
|
|
|
|
2021-09-01 19:01:44 -05:00
|
|
|
ui := cli.NewMockUi()
|
2014-09-22 13:15:27 -05:00
|
|
|
c := &GetCommand{
|
2014-09-22 12:56:50 -05:00
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
2021-09-01 19:01:44 -05:00
|
|
|
WorkingDir: wd,
|
2014-09-22 12:56:50 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-22 13:18:49 -05:00
|
|
|
args := []string{
|
|
|
|
"-update",
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
2019-01-17 17:56:53 -06:00
|
|
|
if !strings.Contains(output, `- foo in`) {
|
2014-09-22 13:18:49 -05:00
|
|
|
t.Fatalf("doesn't look like get: %s", output)
|
|
|
|
}
|
2014-09-22 12:56:50 -05:00
|
|
|
}
|
2021-11-01 15:09:16 -05:00
|
|
|
|
|
|
|
func TestGet_cancel(t *testing.T) {
|
|
|
|
// This test runs `terraform get` as if SIGINT (or similar on other
|
|
|
|
// platforms) were sent to it, testing that it is interruptible.
|
|
|
|
|
|
|
|
wd := tempWorkingDirFixture(t, "init-registry-module")
|
|
|
|
defer testChdir(t, wd.RootModuleDir())()
|
|
|
|
|
|
|
|
// Our shutdown channel is pre-closed so init will exit as soon as it
|
|
|
|
// starts a cancelable portion of the process.
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
close(shutdownCh)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := &GetCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
WorkingDir: wd,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatalf("succeeded; wanted error\n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := ui.ErrorWriter.String(), `Module installation was canceled by an interrupt signal`; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("wrong error message\nshould contain: %s\ngot:\n%s", want, got)
|
|
|
|
}
|
|
|
|
}
|