github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/command/arguments/test.go (about)

     1  package arguments
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  
     7  	"github.com/cycloidio/terraform/tfdiags"
     8  )
     9  
    10  // Test represents the command line arguments for the "terraform test" command.
    11  type Test struct {
    12  	Output TestOutput
    13  }
    14  
    15  // TestOutput represents a subset of the arguments for "terraform test"
    16  // related to how it presents its results. That is, it's the arguments that
    17  // are relevant to the command's view rather than its controller.
    18  type TestOutput struct {
    19  	// If not an empty string, JUnitXMLFile gives a filename where JUnit-style
    20  	// XML test result output should be written, in addition to the normal
    21  	// output printed to the standard output and error streams.
    22  	// (The typical usage pattern for tools that can consume this file format
    23  	// is to configure them to look for a separate test result file on disk
    24  	// after running the tests.)
    25  	JUnitXMLFile string
    26  }
    27  
    28  // ParseTest interprets a slice of raw command line arguments into a
    29  // Test value.
    30  func ParseTest(args []string) (Test, tfdiags.Diagnostics) {
    31  	var ret Test
    32  	var diags tfdiags.Diagnostics
    33  
    34  	// NOTE: ParseTest should still return at least a partial
    35  	// Test even on error, containing enough information for the
    36  	// command to report error diagnostics in a suitable way.
    37  
    38  	f := flag.NewFlagSet("test", flag.ContinueOnError)
    39  	f.SetOutput(ioutil.Discard)
    40  	f.Usage = func() {}
    41  	f.StringVar(&ret.Output.JUnitXMLFile, "junit-xml", "", "Write a JUnit XML file describing the results")
    42  
    43  	err := f.Parse(args)
    44  	if err != nil {
    45  		diags = diags.Append(err)
    46  		return ret, diags
    47  	}
    48  
    49  	// We'll now discard all of the arguments that the flag package handled,
    50  	// and focus only on the positional arguments for the rest of the function.
    51  	args = f.Args()
    52  
    53  	if len(args) != 0 {
    54  		diags = diags.Append(tfdiags.Sourceless(
    55  			tfdiags.Error,
    56  			"Invalid command arguments",
    57  			"The test command doesn't expect any positional command-line arguments.",
    58  		))
    59  		return ret, diags
    60  	}
    61  
    62  	return ret, diags
    63  }