@@ -885,6 +885,94 @@ func TestContext2Plan_queryList(t *testing.T) {
885885 }
886886 },
887887 },
888+ {
889+ name : ".tf file blocks should not be processed in query mode" ,
890+ mainConfig : `
891+ terraform {
892+ required_providers {
893+ test = {
894+ source = "hashicorp/test"
895+ version = "1.0.0"
896+ }
897+ }
898+ }
899+
900+ locals {
901+ foo = "bar"
902+ }
903+
904+ // This would produce an error if triggered, but we expect it to be ignored in query mode
905+ resource "test_resource" "example" {
906+ instance_type = "ami-123456"
907+
908+ lifecycle {
909+ precondition {
910+ condition = local.foo != "bar"
911+ error_message = "This should not be executed"
912+ }
913+ }
914+ }
915+
916+ // This would produce an error if triggered, but we expect it to be ignored in query mode
917+ // output "resource_attr" {
918+ // value = sensitive(test_resource.example.instance_type)
919+ // }
920+ ` ,
921+ queryConfig : `
922+ list "test_resource" "test" {
923+ provider = test
924+ include_resource = true
925+
926+ config {
927+ filter = {
928+ attr = "foo"
929+ }
930+ }
931+ }
932+ ` ,
933+ listResourceFn : func (request providers.ListResourceRequest ) providers.ListResourceResponse {
934+ madeUp := []cty.Value {
935+ cty .ObjectVal (map [string ]cty.Value {"instance_type" : cty .StringVal ("ami-123456" )}),
936+ cty .ObjectVal (map [string ]cty.Value {"instance_type" : cty .StringVal ("ami-654321" )}),
937+ cty .ObjectVal (map [string ]cty.Value {"instance_type" : cty .StringVal ("ami-789012" )}),
938+ }
939+ ids := []cty.Value {}
940+ for i := range madeUp {
941+ ids = append (ids , cty .ObjectVal (map [string ]cty.Value {
942+ "id" : cty .StringVal (fmt .Sprintf ("i-v%d" , i + 1 )),
943+ }))
944+ }
945+
946+ resp := []cty.Value {}
947+ for i , v := range madeUp {
948+ mp := map [string ]cty.Value {
949+ "identity" : ids [i ],
950+ "display_name" : cty .StringVal (fmt .Sprintf ("Instance %d" , i + 1 )),
951+ }
952+ if request .IncludeResourceObject {
953+ mp ["state" ] = v
954+ }
955+ resp = append (resp , cty .ObjectVal (mp ))
956+ }
957+
958+ ret := request .Config .AsValueMap ()
959+ maps .Copy (ret , map [string ]cty.Value {
960+ "data" : cty .TupleVal (resp ),
961+ })
962+
963+ return providers.ListResourceResponse {Result : cty .ObjectVal (ret )}
964+ },
965+ assertChanges : func (sch providers.ProviderSchema , changes * plans.ChangesSrc ) {
966+ expectedResources := []string {"list.test_resource.test" }
967+ actualResources := make ([]string , 0 )
968+ for _ , change := range changes .Queries {
969+ actualResources = append (actualResources , change .Addr .String ())
970+ }
971+ if diff := cmp .Diff (expectedResources , actualResources ); diff != "" {
972+ t .Fatalf ("Expected resources to match, but they differ: %s" , diff )
973+ }
974+ },
975+ },
888976 }
889977
890978 for _ , tc := range cases {
@@ -922,7 +1010,9 @@ func TestContext2Plan_queryList(t *testing.T) {
9221010 })
9231011 tfdiags .AssertNoDiagnostics (t , diags )
9241012
925- diags = ctx .Validate (mod , & ValidateOpts {})
1013+ diags = ctx .Validate (mod , & ValidateOpts {
1014+ Query : true ,
1015+ })
9261016 if tc .assertValidateDiags != nil {
9271017 tc .assertValidateDiags (t , diags )
9281018 return
0 commit comments