/
test.go
66 lines (48 loc) · 1.31 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfexec
import (
"context"
"fmt"
"io"
"os/exec"
)
type testConfig struct {
testsDirectory string
}
var defaultTestOptions = testConfig{}
type TestOption interface {
configureTest(*testConfig)
}
func (opt *TestsDirectoryOption) configureTest(conf *testConfig) {
conf.testsDirectory = opt.testsDirectory
}
// Test represents the terraform test -json subcommand.
//
// The given io.Writer, if specified, will receive
// [machine-readable](https://developer.hashicorp.com/terraform/internals/machine-readable-ui)
// JSON from Terraform including test results.
func (tf *Terraform) Test(ctx context.Context, w io.Writer, opts ...TestOption) error {
err := tf.compatible(ctx, tf1_6_0, nil)
if err != nil {
return fmt.Errorf("terraform test was added in 1.6.0: %w", err)
}
tf.SetStdout(w)
testCmd := tf.testCmd(ctx)
err = tf.runTerraformCmd(ctx, testCmd)
if err != nil {
return err
}
return nil
}
func (tf *Terraform) testCmd(ctx context.Context, opts ...TestOption) *exec.Cmd {
c := defaultTestOptions
for _, o := range opts {
o.configureTest(&c)
}
args := []string{"test", "-json"}
if c.testsDirectory != "" {
args = append(args, "-tests-directory="+c.testsDirectory)
}
return tf.buildTerraformCmd(ctx, nil, args...)
}