mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-25 08:21:07 -06:00
8364383c35
Previously we did plugin discovery in the main package, but as we move towards versioned plugins we need more information available in order to resolve plugins, so we move this responsibility into the command package itself. For the moment this is just preserving the existing behavior as long as there are only internal and unversioned plugins present. This is the final state for provisioners in 0.10, since we don't want to support versioned provisioners yet. For providers this is just a checkpoint along the way, since further work is required to apply version constraints from configuration and support additional plugin search directories. The automatic plugin discovery behavior is not desirable for tests because we want to mock the plugins there, so we add a new backdoor for the tests to use to skip the plugin discovery and just provide their own mock implementations. Most of this diff is thus noisy rework of the tests to use this new mechanism.
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
// ConsoleCommand is tested primarily with tests in the "repl" package.
|
|
// It is not tested here because the Console uses a readline-like library
|
|
// that takes over stdin/stdout. It is difficult to test directly. The
|
|
// core logic is tested in "repl"
|
|
//
|
|
// This file still contains some tests using the stdin-based input.
|
|
|
|
func TestConsole_basic(t *testing.T) {
|
|
tmp, cwd := testCwd(t)
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader("1+5\n"))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
args := []string{}
|
|
code := c.Run(args)
|
|
outCloser()
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := output.String()
|
|
if actual != "6\n" {
|
|
t.Fatalf("bad: %q", actual)
|
|
}
|
|
}
|
|
|
|
func TestConsole_tfvars(t *testing.T) {
|
|
tmp, cwd := testCwd(t)
|
|
defer testFixCwd(t, tmp, cwd)
|
|
|
|
// Write a terraform.tvars
|
|
varFilePath := filepath.Join(tmp, "terraform.tfvars")
|
|
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
p := testProvider()
|
|
ui := new(cli.MockUi)
|
|
c := &ConsoleCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
defer testStdinPipe(t, strings.NewReader("var.foo\n"))()
|
|
outCloser := testStdoutCapture(t, &output)
|
|
|
|
args := []string{
|
|
testFixturePath("apply-vars"),
|
|
}
|
|
code := c.Run(args)
|
|
outCloser()
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
actual := output.String()
|
|
if actual != "bar\n" {
|
|
t.Fatalf("bad: %q", actual)
|
|
}
|
|
}
|