mirror of
https://github.com/opentofu/opentofu.git
synced 2026-06-10 00:50:50 -04:00
Some checks are pending
build / Build for freebsd_386 (push) Waiting to run
build / Build for linux_386 (push) Waiting to run
build / Build for openbsd_386 (push) Waiting to run
build / Build for windows_386 (push) Waiting to run
build / Build for freebsd_amd64 (push) Waiting to run
build / Build for linux_amd64 (push) Waiting to run
build / Build for openbsd_amd64 (push) Waiting to run
build / Build for solaris_amd64 (push) Waiting to run
build / Build for windows_amd64 (push) Waiting to run
build / Build for freebsd_arm (push) Waiting to run
build / Build for linux_arm (push) Waiting to run
build / Build for linux_arm64 (push) Waiting to run
build / Build for darwin_amd64 (push) Waiting to run
build / Build for darwin_arm64 (push) Waiting to run
build / End-to-end Tests for linux_386 (push) Waiting to run
build / End-to-end Tests for windows_386 (push) Waiting to run
build / End-to-end Tests for darwin_amd64 (push) Waiting to run
build / End-to-end Tests for linux_amd64 (push) Waiting to run
build / End-to-end Tests for windows_amd64 (push) Waiting to run
Quick Checks / List files changed for pull request (push) Waiting to run
Quick Checks / Unit tests for linux_386 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_amd64 (push) Blocked by required conditions
Quick Checks / Unit tests for windows_amd64 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_arm (push) Blocked by required conditions
Quick Checks / Unit tests for darwin_arm64 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_arm64 (push) Blocked by required conditions
Quick Checks / Race Tests (push) Blocked by required conditions
Quick Checks / End-to-end Tests (push) Blocked by required conditions
Quick Checks / Code Consistency Checks (push) Blocked by required conditions
Quick Checks / License Checks (push) Waiting to run
Website checks / List files changed for pull request (push) Waiting to run
Website checks / Build (push) Blocked by required conditions
Website checks / Test Installation Instructions (push) Blocked by required conditions
Signed-off-by: Andrei Ciobanu <andrei.ciobanu@opentofu.org>
77 lines
2.4 KiB
Go
77 lines
2.4 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 arguments
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
)
|
|
|
|
func TestParseMetadataFunctions_basicValidation(t *testing.T) {
|
|
testCases := map[string]struct {
|
|
args []string
|
|
want *MetadataFunctions
|
|
wantErrText string
|
|
}{
|
|
"defaults": {
|
|
args: nil,
|
|
want: metadataFunctionsArgsWithDefaults(nil),
|
|
wantErrText: "Invalid arguments: The `tofu metadata functions` command requires the `-json` flag.",
|
|
},
|
|
"json flag": {
|
|
args: []string{"-json"},
|
|
want: metadataFunctionsArgsWithDefaults(func(args *MetadataFunctions) {
|
|
// The -json flag is just to validate that the user understands that the output will be in json.
|
|
// While parsing the arguments, we revert back to human format to be sure that the diagnostics
|
|
// are printed in human format
|
|
args.ViewOptions.ViewType = ViewHuman
|
|
}),
|
|
},
|
|
"invalid flag": {
|
|
args: []string{"-foo"},
|
|
want: metadataFunctionsArgsWithDefaults(func(args *MetadataFunctions) {}),
|
|
wantErrText: "Failed to parse command-line flags: flag provided but not defined: -foo",
|
|
},
|
|
}
|
|
|
|
cmpOpts := cmpopts.IgnoreUnexported(ViewOptions{})
|
|
|
|
for name, tc := range testCases {
|
|
t.Run(name, func(t *testing.T) {
|
|
got, closer, diags := ParseMetadataFunctions(tc.args)
|
|
defer closer()
|
|
|
|
if tc.wantErrText != "" && len(diags) == 0 {
|
|
t.Errorf("test wanted error but got nothing")
|
|
} else if tc.wantErrText == "" && len(diags) > 0 {
|
|
t.Errorf("test didn't expect errors but got some: %s", diags.ErrWithWarnings())
|
|
} else if tc.wantErrText != "" && len(diags) > 0 {
|
|
errStr := diags.ErrWithWarnings().Error()
|
|
if !strings.Contains(errStr, tc.wantErrText) {
|
|
t.Errorf("the returned diagnostics does not contain the expected error message.\ndiags:\n%s\nwanted: %s\n", errStr, tc.wantErrText)
|
|
}
|
|
}
|
|
if diff := cmp.Diff(tc.want, got, cmpOpts); diff != "" {
|
|
t.Errorf("unexpected result\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func metadataFunctionsArgsWithDefaults(mutate func(version *MetadataFunctions)) *MetadataFunctions {
|
|
ret := &MetadataFunctions{
|
|
ViewOptions: ViewOptions{
|
|
ViewType: ViewHuman,
|
|
InputEnabled: false,
|
|
},
|
|
}
|
|
if mutate != nil {
|
|
mutate(ret)
|
|
}
|
|
return ret
|
|
}
|