mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-21 07:00:37 -04:00
We don't typically just broadly run automatic rewriting tools like "go fix" across our codebase because that tends to cause annoying and unnecessary merge conflicts when we're backporting to earlier release branches. But all of the files in this commit were changed in some non-trivial way already during the OpenTofu v1.11 development period anyway, and so the likelyhood we'd be able to successfully backport from them is reduced and therefore this seems like a good opportunity to do some focused modernization using "go fix". My rules for what to include or not are admittedly quite "vibes-based", but the general idea was: - Focusing on files under the "command" directory only, because that's already been an area of intentional refactoring during this development period. - If the existing diff in a file is already significantly larger than the changes the fixer proposed to make, or if the fixer is proposing to change a line that was already changed in this development period. - More willing to include "_test.go" files than non-test files, even if they hadn't changed as much already, just because backports from test files for bug fixes tend to be entirely new test cases more than they are modifications to existing test cases, and so the risk of conflicts is lower there. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
148 lines
5.1 KiB
Go
148 lines
5.1 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package views
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/go-version"
|
|
"github.com/opentofu/opentofu/internal/initwd"
|
|
)
|
|
|
|
func TestModuleInstallationHooks(t *testing.T) {
|
|
tests := map[string]struct {
|
|
viewCall func(hook initwd.ModuleInstallHooks)
|
|
showLocalPaths bool
|
|
wantJson []map[string]any
|
|
wantStdout string
|
|
wantStderr string
|
|
}{
|
|
"download_with_version": {
|
|
viewCall: func(hook initwd.ModuleInstallHooks) {
|
|
hook.Download("root.networking", "git::https://example.com/module.git", version.Must(version.NewVersion("2.5.3")))
|
|
},
|
|
wantJson: []map[string]any{
|
|
{
|
|
"@level": "info",
|
|
"@message": "Downloading git::https://example.com/module.git 2.5.3 for root.networking...",
|
|
"@module": "tofu.ui",
|
|
},
|
|
},
|
|
wantStdout: withNewline("Downloading git::https://example.com/module.git 2.5.3 for root.networking..."),
|
|
},
|
|
"download_without_version": {
|
|
viewCall: func(hook initwd.ModuleInstallHooks) {
|
|
hook.Download("root.storage", "git::https://example.com/storage.git", nil)
|
|
},
|
|
wantJson: []map[string]any{
|
|
{
|
|
"@level": "info",
|
|
"@message": "Downloading git::https://example.com/storage.git for root.storage...",
|
|
"@module": "tofu.ui",
|
|
},
|
|
},
|
|
wantStdout: withNewline("Downloading git::https://example.com/storage.git for root.storage..."),
|
|
},
|
|
"install_without_local_path": {
|
|
viewCall: func(hook initwd.ModuleInstallHooks) {
|
|
hook.Install("root.networking", version.Must(version.NewVersion("2.5.3")), "/path/to/.terraform/modules/networking")
|
|
},
|
|
wantJson: []map[string]any{
|
|
{
|
|
"@level": "info",
|
|
"@message": "installing root.networking",
|
|
"@module": "tofu.ui",
|
|
},
|
|
},
|
|
wantStdout: withNewline("- root.networking"),
|
|
},
|
|
"install_with_local_path": {
|
|
viewCall: func(hook initwd.ModuleInstallHooks) {
|
|
hook.Install("root.networking", version.Must(version.NewVersion("2.5.3")), "/path/to/.terraform/modules/networking")
|
|
},
|
|
showLocalPaths: true,
|
|
wantJson: []map[string]any{
|
|
{
|
|
"@level": "info",
|
|
"@message": "installing root.networking in /path/to/.terraform/modules/networking",
|
|
"@module": "tofu.ui",
|
|
},
|
|
},
|
|
wantStdout: withNewline("- root.networking in /path/to/.terraform/modules/networking"),
|
|
},
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
testModuleInstallationHookHuman(t, tc.viewCall, tc.showLocalPaths, tc.wantStdout, tc.wantStderr)
|
|
testModuleInstallationHookJson(t, tc.viewCall, tc.showLocalPaths, tc.wantJson)
|
|
testModuleInstallationHookMulti(t, tc.viewCall, tc.showLocalPaths, tc.wantStdout, tc.wantStderr, tc.wantJson)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testModuleInstallationHookHuman(t *testing.T, call func(init initwd.ModuleInstallHooks), showLocalPaths bool, wantStdout, wantStderr string) {
|
|
view, done := testView(t)
|
|
moduleInstallationViewCall := moduleInstallationHookHuman{v: view, showLocalPaths: showLocalPaths}
|
|
call(moduleInstallationViewCall)
|
|
output := done(t)
|
|
if diff := cmp.Diff(wantStderr, output.Stderr()); diff != "" {
|
|
t.Errorf("invalid stderr (-want, +got):\n%s", diff)
|
|
}
|
|
if diff := cmp.Diff(wantStdout, output.Stdout()); diff != "" {
|
|
t.Errorf("invalid stdout (-want, +got):\n%s", diff)
|
|
}
|
|
}
|
|
|
|
func testModuleInstallationHookJson(t *testing.T, call func(init initwd.ModuleInstallHooks), showLocalPaths bool, want []map[string]any) {
|
|
// New type just to assert the fields that we are interested in
|
|
view, done := testView(t)
|
|
moduleInstallationViewCall := moduleInstallationHookJSON{v: NewJSONView(view, nil), showLocalPaths: showLocalPaths}
|
|
call(moduleInstallationViewCall)
|
|
output := done(t)
|
|
if output.Stderr() != "" {
|
|
t.Errorf("expected no stderr but got:\n%s", output.Stderr())
|
|
}
|
|
|
|
testJSONViewOutputEquals(t, output.Stdout(), want)
|
|
}
|
|
|
|
func testModuleInstallationHookMulti(t *testing.T, call func(init initwd.ModuleInstallHooks), showLocalPaths bool, wantStdout string, wantStderr string, want []map[string]any) {
|
|
jsonInto, err := os.CreateTemp(t.TempDir(), "json-into-*")
|
|
if err != nil {
|
|
t.Fatalf("failed to create the file to write json content into: %s", err)
|
|
}
|
|
view, done := testView(t)
|
|
moduleInstallationViewCall := moduleInstallationHookMulti{
|
|
moduleInstallationHookHuman{v: view, showLocalPaths: showLocalPaths},
|
|
moduleInstallationHookJSON{v: NewJSONView(view, jsonInto), showLocalPaths: showLocalPaths},
|
|
}
|
|
call(moduleInstallationViewCall)
|
|
{
|
|
if err := jsonInto.Close(); err != nil {
|
|
t.Fatalf("failed to close the jsonInto file: %s", err)
|
|
}
|
|
// check the fileInto content
|
|
fileContent, err := os.ReadFile(jsonInto.Name())
|
|
if err != nil {
|
|
t.Fatalf("failed to read the file content with the json output: %s", err)
|
|
}
|
|
testJSONViewOutputEquals(t, string(fileContent), want)
|
|
}
|
|
{
|
|
// check the human output
|
|
output := done(t)
|
|
if diff := cmp.Diff(wantStderr, output.Stderr()); diff != "" {
|
|
t.Errorf("invalid stderr (-want, +got):\n%s", diff)
|
|
}
|
|
if diff := cmp.Diff(wantStdout, output.Stdout()); diff != "" {
|
|
t.Errorf("invalid stdout (-want, +got):\n%s", diff)
|
|
}
|
|
}
|
|
}
|