mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-04-08 15:01:37 -04:00
We now have RunningInAutomation has a general concern in views.View, so we no longer need to specify it for each command-specific constructor separately. For this initial change I focused only on changing the exported interface of the views package and let the command-specific views go on having their own unexported fields containing a copy of the flag because it made this change less invasive and I wasn't feeling sure yet about whether we ought to have code within command-specific views directly access the internals of views.View. However, maybe we'll simplify this further in a later commit if we conclude that these copies of the flag are burdensome. The general version of this gets set directly inside the main package, which might at some future point allow us to make the command package itself unaware of this "running in automation" idea and thus reinforce that it's intended as a presentation-only thing rather than as a behavioral thing, but we'll save more invasive refactoring for another day.
108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package views
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/command/arguments"
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
|
"github.com/hashicorp/terraform/states"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
// Ensure that the correct view type and in-automation settings propagate to the
|
|
// Operation view.
|
|
func TestRefreshHuman_operation(t *testing.T) {
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
defer done(t)
|
|
v := NewRefresh(arguments.ViewHuman, NewView(streams).SetRunningInAutomation(true)).Operation()
|
|
if hv, ok := v.(*OperationHuman); !ok {
|
|
t.Fatalf("unexpected return type %t", v)
|
|
} else if hv.inAutomation != true {
|
|
t.Fatalf("unexpected inAutomation value on Operation view")
|
|
}
|
|
}
|
|
|
|
// Verify that Hooks includes a UI hook
|
|
func TestRefreshHuman_hooks(t *testing.T) {
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
defer done(t)
|
|
v := NewRefresh(arguments.ViewHuman, NewView(streams).SetRunningInAutomation(true))
|
|
hooks := v.Hooks()
|
|
|
|
var uiHook *UiHook
|
|
for _, hook := range hooks {
|
|
if ch, ok := hook.(*UiHook); ok {
|
|
uiHook = ch
|
|
}
|
|
}
|
|
if uiHook == nil {
|
|
t.Fatalf("expected Hooks to include a UiHook: %#v", hooks)
|
|
}
|
|
}
|
|
|
|
// Basic test coverage of Outputs, since most of its functionality is tested
|
|
// elsewhere.
|
|
func TestRefreshHuman_outputs(t *testing.T) {
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
v := NewRefresh(arguments.ViewHuman, NewView(streams))
|
|
|
|
v.Outputs(map[string]*states.OutputValue{
|
|
"foo": {Value: cty.StringVal("secret")},
|
|
})
|
|
|
|
got := done(t).Stdout()
|
|
for _, want := range []string{"Outputs:", `foo = "secret"`} {
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("wrong result\ngot: %q\nwant: %q", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Outputs should do nothing if there are no outputs to render.
|
|
func TestRefreshHuman_outputsEmpty(t *testing.T) {
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
v := NewRefresh(arguments.ViewHuman, NewView(streams))
|
|
|
|
v.Outputs(map[string]*states.OutputValue{})
|
|
|
|
got := done(t).Stdout()
|
|
if got != "" {
|
|
t.Errorf("output should be empty, but got: %q", got)
|
|
}
|
|
}
|
|
|
|
// Basic test coverage of Outputs, since most of its functionality is tested
|
|
// elsewhere.
|
|
func TestRefreshJSON_outputs(t *testing.T) {
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
v := NewRefresh(arguments.ViewJSON, NewView(streams))
|
|
|
|
v.Outputs(map[string]*states.OutputValue{
|
|
"boop_count": {Value: cty.NumberIntVal(92)},
|
|
"password": {Value: cty.StringVal("horse-battery").Mark("sensitive"), Sensitive: true},
|
|
})
|
|
|
|
want := []map[string]interface{}{
|
|
{
|
|
"@level": "info",
|
|
"@message": "Outputs: 2",
|
|
"@module": "terraform.ui",
|
|
"type": "outputs",
|
|
"outputs": map[string]interface{}{
|
|
"boop_count": map[string]interface{}{
|
|
"sensitive": false,
|
|
"value": float64(92),
|
|
"type": "number",
|
|
},
|
|
"password": map[string]interface{}{
|
|
"sensitive": true,
|
|
"value": "horse-battery",
|
|
"type": "string",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testJSONViewOutputEquals(t, done(t).Stdout(), want)
|
|
}
|