refactor providers command argument parsing

This commit is contained in:
Daniel Schmidt 2026-02-13 14:40:27 +01:00
parent 3a2686938f
commit 6f32f249f7
No known key found for this signature in database
GPG key ID: 377C3A4D62FBBBE2
3 changed files with 136 additions and 13 deletions

View file

@ -0,0 +1,42 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package arguments
import "github.com/hashicorp/terraform/internal/tfdiags"
// Providers represents the command-line arguments for the providers command.
type Providers struct {
// TestsDirectory is the directory containing Terraform test files.
TestsDirectory string
}
// ParseProviders processes CLI arguments, returning a Providers value and
// errors. If errors are encountered, a Providers value is still returned
// representing the best effort interpretation of the arguments.
func ParseProviders(args []string) (*Providers, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
providers := &Providers{}
cmdFlags := defaultFlagSet("providers")
cmdFlags.StringVar(&providers.TestsDirectory, "test-directory", "tests", "test-directory")
if err := cmdFlags.Parse(args); err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to parse command-line flags",
err.Error(),
))
}
args = cmdFlags.Args()
if len(args) > 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Too many command line arguments",
"Did you mean to use -chdir?",
))
}
return providers, diags
}

View file

@ -0,0 +1,88 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package arguments
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform/internal/tfdiags"
)
func TestParseProviders_valid(t *testing.T) {
testCases := map[string]struct {
args []string
want *Providers
}{
"defaults": {
nil,
&Providers{
TestsDirectory: "tests",
},
},
"test directory": {
[]string{"-test-directory=integration-tests"},
&Providers{
TestsDirectory: "integration-tests",
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, diags := ParseProviders(tc.args)
if len(diags) > 0 {
t.Fatalf("unexpected diags: %v", diags)
}
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf("unexpected result\n%s", diff)
}
})
}
}
func TestParseProviders_invalid(t *testing.T) {
testCases := map[string]struct {
args []string
want *Providers
wantDiags tfdiags.Diagnostics
}{
"unknown flag": {
[]string{"-wat"},
&Providers{
TestsDirectory: "tests",
},
tfdiags.Diagnostics{
tfdiags.Sourceless(
tfdiags.Error,
"Failed to parse command-line flags",
"flag provided but not defined: -wat",
),
},
},
"too many positional arguments": {
[]string{"foo"},
&Providers{
TestsDirectory: "tests",
},
tfdiags.Diagnostics{
tfdiags.Sourceless(
tfdiags.Error,
"Too many command line arguments",
"Did you mean to use -chdir?",
),
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got, gotDiags := ParseProviders(tc.args)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf("unexpected result\n%s", diff)
}
tfdiags.AssertDiagnosticsMatch(t, gotDiags, tc.wantDiags)
})
}
}

View file

@ -31,26 +31,19 @@ func (c *ProvidersCommand) Synopsis() string {
}
func (c *ProvidersCommand) Run(args []string) int {
var testsDirectory string
args = c.Meta.process(args)
cmdFlags := c.Meta.defaultFlagSet("providers")
cmdFlags.StringVar(&testsDirectory, "test-directory", "tests", "test-directory")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
parsedArgs, diags := arguments.ParseProviders(c.Meta.process(args))
if diags.HasErrors() {
c.showDiagnostics(diags)
return 1
}
configPath, err := ModulePath(cmdFlags.Args())
configPath, err := ModulePath(nil)
if err != nil {
c.Ui.Error(err.Error())
return 1
}
var diags tfdiags.Diagnostics
empty, err := configs.IsEmptyDir(configPath, testsDirectory)
empty, err := configs.IsEmptyDir(configPath, parsedArgs.TestsDirectory)
if err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
@ -74,7 +67,7 @@ func (c *ProvidersCommand) Run(args []string) int {
return 1
}
config, configDiags := c.loadConfigWithTests(configPath, testsDirectory)
config, configDiags := c.loadConfigWithTests(configPath, parsedArgs.TestsDirectory)
diags = diags.Append(configDiags)
if configDiags.HasErrors() {
c.showDiagnostics(diags)