Skip to content

Commit 00e0c61

Browse files
authored
bugfix: Fix global and suite variable interpolation bug #10
Ensure global variables and suite variables are interpolated correctly
1 parent 7606cac commit 00e0c61

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

internal/runner/runner.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Runner struct {
2424
// Map to track processed hooks to prevent infinite recursion
2525
processedHooks map[string]bool
2626
// Mutex to protect the processed hooks map
27-
hooksMutex sync.Mutex
27+
hooksMutex sync.Mutex
2828
}
2929

3030
func NewRunner(opts *TestRunnerOptions) TestRunner {
@@ -241,7 +241,14 @@ func (r *Runner) executeTestDefinition(def *tests.TestDefinition) (tests.TestDef
241241
Suites: make(map[string]tests.TestSuiteResult, len(def.Suites)),
242242
}
243243

244+
// Process environment variables in variable values
245+
if err := tests.InterpolateVariableValues(def.Variables); err != nil {
246+
r.Logger.Error("Error interpolating environment variables in definition variables", zap.Error(err))
247+
// Continue execution despite interpolation errors
248+
}
249+
244250
r.Logger.Debug(fmt.Sprintf("executing test definition: %s", def.Name))
251+
r.Logger.Debug("test definition variables", zap.Any("variables", def.Variables))
245252

246253
// Execute BeforeAll hooks if they exist
247254
if len(def.BeforeAll) > 0 {
@@ -268,14 +275,20 @@ func (r *Runner) executeTestDefinition(def *tests.TestDefinition) (tests.TestDef
268275

269276
// Create a copy of definition variables for the suite
270277
suiteVars := make(map[string]tests.Variable)
271-
278+
272279
// First add definition-level variables
273280
for k, v := range def.Variables {
274281
suiteVars[k] = v
275282
}
276-
283+
277284
// Then add suite-level variables (to override any definition variables with the same name)
278285
if suite.Variables != nil {
286+
// First process environment variables in suite-level variable values
287+
if err := tests.InterpolateVariableValues(suite.Variables); err != nil {
288+
r.Logger.Error("Error interpolating environment variables in suite variables", zap.Error(err))
289+
// Continue execution despite interpolation errors
290+
}
291+
279292
for k, v := range suite.Variables {
280293
suiteVars[k] = v
281294
}
@@ -298,12 +311,13 @@ func (r *Runner) executeTestDefinition(def *tests.TestDefinition) (tests.TestDef
298311
}
299312

300313
// Set up the suite with variables
301-
302314
// Pass variables to suite
303315
suite.Variables = suiteVars
304316

305317
// Execute the test suite
306318
r.Logger.Debug(fmt.Sprintf("executing test suite: %s", suite.Name))
319+
r.Logger.Debug("suite variables", zap.Any("variables", suite.Variables))
320+
307321
suiteResult, err := suite.Run(r.Logger, r.HttpClient)
308322
if err != nil {
309323
r.Logger.Error("error executing test suite", zap.Error(err))

internal/tests/variables.go

+25
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,31 @@ func InterpolateObject(obj interface{}, variables map[string]Variable) (interfac
175175
}
176176
}
177177

178+
// InterpolateVariableValues interpolates variable templates definted in the variable values of a map[string]Variable
179+
func InterpolateVariableValues(variables map[string]Variable) error {
180+
if variables == nil {
181+
return nil
182+
}
183+
184+
for name, variable := range variables {
185+
if variable.Value == "" {
186+
continue
187+
}
188+
189+
// Process environment variables in the variable value
190+
interpolated, err := InterpolateVariables(variable.Value, nil) // nil variables because we're only processing env vars
191+
if err != nil {
192+
return fmt.Errorf("error interpolating variables in variable %s: %w", name, err)
193+
}
194+
195+
// Update the variable value with the interpolated value
196+
variable.Value = interpolated
197+
variables[name] = variable
198+
}
199+
200+
return nil
201+
}
202+
178203
// InterpolateRequest applies variable interpolation to all fields in a Request
179204
func InterpolateRequest(request *Request, variables map[string]Variable) error {
180205
var err error

samples/.env.sample

-4
This file was deleted.

0 commit comments

Comments
 (0)