opentofu/internal/command/arguments/test.go
Martin Atkins ffe056bacb Move command/ to internal/command/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

64 lines
1.9 KiB
Go

package arguments
import (
"flag"
"io/ioutil"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// Test represents the command line arguments for the "terraform test" command.
type Test struct {
Output TestOutput
}
// TestOutput represents a subset of the arguments for "terraform test"
// related to how it presents its results. That is, it's the arguments that
// are relevant to the command's view rather than its controller.
type TestOutput struct {
// If not an empty string, JUnitXMLFile gives a filename where JUnit-style
// XML test result output should be written, in addition to the normal
// output printed to the standard output and error streams.
// (The typical usage pattern for tools that can consume this file format
// is to configure them to look for a separate test result file on disk
// after running the tests.)
JUnitXMLFile string
}
// ParseTest interprets a slice of raw command line arguments into a
// Test value.
func ParseTest(args []string) (Test, tfdiags.Diagnostics) {
var ret Test
var diags tfdiags.Diagnostics
// NOTE: ParseTest should still return at least a partial
// Test even on error, containing enough information for the
// command to report error diagnostics in a suitable way.
f := flag.NewFlagSet("test", flag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.Usage = func() {}
f.StringVar(&ret.Output.JUnitXMLFile, "junit-xml", "", "Write a JUnit XML file describing the results")
err := f.Parse(args)
if err != nil {
diags = diags.Append(err)
return ret, diags
}
// We'll now discard all of the arguments that the flag package handled,
// and focus only on the positional arguments for the rest of the function.
args = f.Args()
if len(args) != 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid command arguments",
"The test command doesn't expect any positional command-line arguments.",
))
return ret, diags
}
return ret, diags
}