@@ -19,6 +19,8 @@ import (
1919// ValidateCommand is a Command implementation that validates the terraform files
2020type ValidateCommand struct {
2121 Meta
22+
23+ ParsedArgs * arguments.Validate
2224}
2325
2426func (c * ValidateCommand ) Run (rawArgs []string ) int {
@@ -34,6 +36,7 @@ func (c *ValidateCommand) Run(rawArgs []string) int {
3436 return 1
3537 }
3638
39+ c .ParsedArgs = args
3740 view := views .NewValidate (args .ViewType , c .View )
3841
3942 // After this point, we must only produce JSON output if JSON mode is
@@ -54,7 +57,7 @@ func (c *ValidateCommand) Run(rawArgs []string) int {
5457 return view .Results (diags )
5558 }
5659
57- validateDiags := c .validate (dir , args . TestDirectory , args . NoTests )
60+ validateDiags := c .validate (dir )
5861 diags = diags .Append (validateDiags )
5962
6063 // Validating with dev overrides in effect means that the result might
@@ -66,47 +69,54 @@ func (c *ValidateCommand) Run(rawArgs []string) int {
6669 return view .Results (diags )
6770}
6871
69- func (c * ValidateCommand ) validate (dir , testDir string , noTests bool ) tfdiags.Diagnostics {
72+ func (c * ValidateCommand ) validate (dir string ) tfdiags.Diagnostics {
7073 var diags tfdiags.Diagnostics
7174 var cfg * configs.Config
7275
73- if noTests {
76+ // If the query flag is set, include query files in the validation.
77+ c .includeQueryFiles = c .ParsedArgs .Query
78+
79+ if c .ParsedArgs .NoTests {
7480 cfg , diags = c .loadConfig (dir )
7581 } else {
76- cfg , diags = c .loadConfigWithTests (dir , testDir )
82+ cfg , diags = c .loadConfigWithTests (dir , c . ParsedArgs . TestDirectory )
7783 }
7884 if diags .HasErrors () {
7985 return diags
8086 }
8187
82- validate := func (cfg * configs.Config ) tfdiags.Diagnostics {
83- var diags tfdiags.Diagnostics
88+ diags = diags .Append (c .validateConfig (cfg ))
8489
85- opts , err := c . contextOpts ()
86- if err != nil {
87- diags = diags . Append ( err )
88- return diags
89- }
90+ // Unless excluded, we'll also do a quick validation of the Terraform test files. These live
91+ // outside the Terraform graph so we have to do this separately.
92+ if ! c . ParsedArgs . NoTests {
93+ diags = diags . Append ( c . validateTestFiles ( cfg ))
94+ }
9095
91- tfCtx , ctxDiags := terraform .NewContext (opts )
92- diags = diags .Append (ctxDiags )
93- if ctxDiags .HasErrors () {
94- return diags
95- }
96+ return diags
97+ }
9698
97- return diags . Append ( tfCtx . Validate (cfg , nil ))
98- }
99+ func ( c * ValidateCommand ) validateConfig (cfg * configs. Config ) tfdiags. Diagnostics {
100+ var diags tfdiags. Diagnostics
99101
100- diags = diags .Append (validate (cfg ))
102+ opts , err := c .contextOpts ()
103+ if err != nil {
104+ diags = diags .Append (err )
105+ return diags
106+ }
101107
102- if noTests {
108+ tfCtx , ctxDiags := terraform .NewContext (opts )
109+ diags = diags .Append (ctxDiags )
110+ if ctxDiags .HasErrors () {
103111 return diags
104112 }
105113
106- validatedModules := make (map [string ]bool )
114+ return diags .Append (tfCtx .Validate (cfg , nil ))
115+ }
107116
108- // We'll also do a quick validation of the Terraform test files. These live
109- // outside the Terraform graph so we have to do this separately.
117+ func (c * ValidateCommand ) validateTestFiles (cfg * configs.Config ) tfdiags.Diagnostics {
118+ diags := tfdiags.Diagnostics {}
119+ validatedModules := make (map [string ]bool )
110120 for _ , file := range cfg .Module .Tests {
111121
112122 // The file validation only returns warnings so we'll just add them
@@ -131,7 +141,7 @@ func (c *ValidateCommand) validate(dir, testDir string, noTests bool) tfdiags.Di
131141 // not validate the same thing multiple times.
132142
133143 validatedModules [run .Module .Source .String ()] = true
134- diags = diags .Append (validate (run .ConfigUnderTest ))
144+ diags = diags .Append (c . validateConfig (run .ConfigUnderTest ))
135145 }
136146
137147 }
@@ -188,6 +198,8 @@ Options:
188198 -no-tests If specified, Terraform will not validate test files.
189199
190200 -test-directory=path Set the Terraform test directory, defaults to "tests".
201+
202+ -query If specified, the command will also validate .tfquery.hcl files.
191203`
192204 return strings .TrimSpace (helpText )
193205}
0 commit comments