2023-08-16 14:04:52 -04:00
// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
2023-07-19 04:07:46 -04:00
package arguments
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/terraform/internal/tfdiags"
)
func TestParseTest_Vars ( t * testing . T ) {
tcs := map [ string ] struct {
args [ ] string
want [ ] FlagNameValue
} {
"no var flags by default" : {
args : nil ,
want : nil ,
} ,
"one var" : {
args : [ ] string { "-var" , "foo=bar" } ,
want : [ ] FlagNameValue {
{ Name : "-var" , Value : "foo=bar" } ,
} ,
} ,
"one var-file" : {
args : [ ] string { "-var-file" , "cool.tfvars" } ,
want : [ ] FlagNameValue {
{ Name : "-var-file" , Value : "cool.tfvars" } ,
} ,
} ,
"ordering preserved" : {
args : [ ] string {
"-var" , "foo=bar" ,
"-var-file" , "cool.tfvars" ,
"-var" , "boop=beep" ,
} ,
want : [ ] FlagNameValue {
{ Name : "-var" , Value : "foo=bar" } ,
{ Name : "-var-file" , Value : "cool.tfvars" } ,
{ Name : "-var" , Value : "boop=beep" } ,
} ,
} ,
}
for name , tc := range tcs {
t . Run ( name , func ( t * testing . T ) {
got , diags := ParseTest ( tc . args )
if len ( diags ) > 0 {
t . Fatalf ( "unexpected diags: %v" , diags )
}
if vars := got . Vars . All ( ) ; ! cmp . Equal ( vars , tc . want ) {
t . Fatalf ( "unexpected result\n%s" , cmp . Diff ( vars , tc . want ) )
}
if got , want := got . Vars . Empty ( ) , len ( tc . want ) == 0 ; got != want {
t . Fatalf ( "expected Empty() to return %t, but was %t" , want , got )
}
} )
}
}
func TestParseTest ( t * testing . T ) {
tcs := map [ string ] struct {
args [ ] string
want * Test
wantDiags tfdiags . Diagnostics
} {
"defaults" : {
args : nil ,
want : & Test {
2025-02-05 03:48:05 -05:00
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2023-07-19 04:07:46 -04:00
} ,
wantDiags : nil ,
} ,
"with-filters" : {
2023-07-20 10:57:05 -04:00
args : [ ] string { "-filter=one.tftest.hcl" , "-filter=two.tftest.hcl" } ,
2023-07-19 04:07:46 -04:00
want : & Test {
2025-02-05 03:48:05 -05:00
Filter : [ ] string { "one.tftest.hcl" , "two.tftest.hcl" } ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2023-07-19 04:07:46 -04:00
} ,
wantDiags : nil ,
} ,
"json" : {
args : [ ] string { "-json" } ,
want : & Test {
2025-02-05 03:48:05 -05:00
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewJSON ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2023-07-19 04:07:46 -04:00
} ,
wantDiags : nil ,
} ,
"test-directory" : {
args : [ ] string { "-test-directory=other" } ,
want : & Test {
2025-02-05 03:48:05 -05:00
Filter : nil ,
TestDirectory : "other" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2023-07-19 04:07:46 -04:00
} ,
wantDiags : nil ,
} ,
"verbose" : {
args : [ ] string { "-verbose" } ,
want : & Test {
2025-02-05 03:48:05 -05:00
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Verbose : true ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2023-07-19 04:07:46 -04:00
} ,
} ,
2025-02-05 03:48:05 -05:00
"with-parallelism-set" : {
args : [ ] string { "-parallelism=5" } ,
want : & Test {
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 5 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2025-02-05 03:48:05 -05:00
} ,
wantDiags : nil ,
} ,
"with-parallelism-0" : {
args : [ ] string { "-parallelism=0" } ,
want : & Test {
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2025-02-05 03:48:05 -05:00
} ,
wantDiags : nil ,
} ,
2025-07-07 03:44:32 -04:00
2025-02-06 02:54:08 -05:00
"cloud-with-parallelism-0" : {
args : [ ] string { "-parallelism=0" , "-cloud-run=foobar" } ,
want : & Test {
CloudRunSource : "foobar" ,
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 0 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
} ,
wantDiags : nil ,
} ,
"with-run-parallelism-set" : {
args : [ ] string { "-run-parallelism=10" } ,
want : & Test {
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
RunParallelism : 10 ,
} ,
wantDiags : nil ,
} ,
"with-run-parallelism-0" : {
args : [ ] string { "-run-parallelism=0" } ,
want : & Test {
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
RunParallelism : 0 ,
2025-02-06 02:54:08 -05:00
} ,
wantDiags : nil ,
} ,
2023-07-19 04:07:46 -04:00
"unknown flag" : {
args : [ ] string { "-boop" } ,
want : & Test {
2025-02-05 03:48:05 -05:00
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2023-07-19 04:07:46 -04:00
} ,
wantDiags : tfdiags . Diagnostics {
tfdiags . Sourceless (
tfdiags . Error ,
"Failed to parse command-line flags" ,
"flag provided but not defined: -boop" ,
) ,
} ,
} ,
2025-01-15 06:44:35 -05:00
"incompatible flags: -junit-xml and -cloud-run" : {
args : [ ] string { "-junit-xml=./output.xml" , "-cloud-run=foobar" } ,
want : & Test {
2025-02-05 03:48:05 -05:00
CloudRunSource : "foobar" ,
JUnitXMLFile : "./output.xml" ,
Filter : nil ,
TestDirectory : "tests" ,
ViewType : ViewHuman ,
Vars : & Vars { } ,
OperationParallelism : 10 ,
2025-07-07 03:44:32 -04:00
RunParallelism : 10 ,
2025-01-15 06:44:35 -05:00
} ,
wantDiags : tfdiags . Diagnostics {
tfdiags . Sourceless (
tfdiags . Error ,
"Incompatible command-line flags" ,
"The -junit-xml option is currently not compatible with remote test execution via the -cloud-run flag. If you are interested in JUnit XML output for remotely-executed tests please open an issue in GitHub." ,
) ,
} ,
} ,
2023-07-19 04:07:46 -04:00
}
cmpOpts := cmpopts . IgnoreUnexported ( Operation { } , Vars { } , State { } )
for name , tc := range tcs {
t . Run ( name , func ( t * testing . T ) {
got , diags := ParseTest ( tc . args )
if diff := cmp . Diff ( tc . want , got , cmpOpts ) ; len ( diff ) > 0 {
t . Errorf ( "diff:\n%s" , diff )
}
2025-02-19 08:59:29 -05:00
tfdiags . AssertDiagnosticsMatch ( t , diags , tc . wantDiags )
2023-07-19 04:07:46 -04:00
} )
}
}