terraform/internal/command/arguments/test.go
Martin Atkins b43ddd8ff6 terraform test: Experimental JUnit XML reporting
This adds an experimental new option -junit-xml=FILENAME for the
"terraform test" command. When specified, it writes a JUnit XML report to
the specified filename once the test run is complete, while continuing to
report test progress in the UI in the usual way.

This is only experimental for now because it remains to be seen if this
particular mapping to the JUnit XML schema is actually useful in real
software -- this format is woefully underdocumented and implemented
slightly differently by each consumer -- and so we might change this
significantly before stabilizing it, or remove it altogether if it turns
out that there's no useful mapping to JUnit XML here. Hopefully those who
are interested in JUnit XML reports will try this experiment against their
favorite JUnit XML-consuming software and report back whether the report
is presented in a helpful way.

It's a de-facto convention for JUnit XML to be reported separately to a
file, rather than replacing the normal test run output, since tools that
consume this format tend to present its results in a separate and less
prominent place than the output of the command itself. This option is
designed to follow that convention for consistency with various other
software that produces this format.

The implementation here is intentionally pretty minimal and simplistic just
as a starting point for gathering feedback. The main priority is that it
be easy to evolve this based on feedback and to remove it altogether if we
decide not to stabilize this at all. If this does become stabilized, it
might deserve being factored out into a separate package so that we can
minimize the amount of logic embedded directly inside the views package,
and it will certainly need some unit tests to represent what we've
committed to supporting in future versions.
2024-01-15 08:51:02 -08:00

77 lines
2.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package arguments
import (
"github.com/hashicorp/terraform/internal/configs"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// Test represents the command-line arguments for the test command.
type Test struct {
// CloudRunSource specifies the remote private module that this test run
// should execute against in a remote Terraform Cloud run.
CloudRunSource string
// Filter contains a list of test files to execute. If empty, all test files
// will be executed.
Filter []string
// TestDirectory allows the user to override the directory that the test
// command will use to discover test files, defaults to "tests". Regardless
// of the value here, test files within the configuration directory will
// always be discovered.
TestDirectory string
// ViewType specifies which output format to use: human or JSON.
ViewType ViewType
// JUnitXMLFile specifies an optional filename to write a JUnit XML test
// result report to, in addition to the information written to the selected
// view type.
JUnitXMLFile string
// You can specify common variables for all tests from the command line.
Vars *Vars
// Verbose tells the test command to print out the plan either in
// human-readable format or JSON for each run step depending on the
// ViewType.
Verbose bool
}
func ParseTest(args []string) (*Test, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
test := Test{
Vars: new(Vars),
}
var jsonOutput bool
cmdFlags := extendedFlagSet("test", nil, nil, test.Vars)
cmdFlags.Var((*flagStringSlice)(&test.Filter), "filter", "filter")
cmdFlags.StringVar(&test.TestDirectory, "test-directory", configs.DefaultTestDirectory, "test-directory")
cmdFlags.BoolVar(&jsonOutput, "json", false, "json")
cmdFlags.StringVar(&test.JUnitXMLFile, "junit-xml", "", "junit-xml")
cmdFlags.BoolVar(&test.Verbose, "verbose", false, "verbose")
// TODO: Finalise the name of this flag.
cmdFlags.StringVar(&test.CloudRunSource, "cloud-run", "", "cloud-run")
if err := cmdFlags.Parse(args); err != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Failed to parse command-line flags",
err.Error()))
}
switch {
case jsonOutput:
test.ViewType = ViewJSON
default:
test.ViewType = ViewHuman
}
return &test, diags
}