tofu: Context.Eval now takes a context.Context

This continues our ongoing effort to get a coherent chain of
context.Context all the way from "package main" to all of our calls to
external components.

Context.Eval doesn't yet do anything with its new context, but we'll
plumb this deeper in future.

All of the _test.go file updates here are purely mechanical additions of
the extra argument. No test is materially modified by this change, which
is intentional to get some assurance that isn't a breaking change.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins
2024-11-12 15:50:02 -08:00
parent 8ae790ca06
commit 6522f73249
4 changed files with 10 additions and 5 deletions

View File

@@ -28,6 +28,8 @@ type ConsoleCommand struct {
}
func (c *ConsoleCommand) Run(args []string) int {
ctx := c.CommandContext()
args = c.Meta.process(args)
cmdFlags := c.Meta.extendedFlagSet("console")
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
@@ -144,7 +146,7 @@ func (c *ConsoleCommand) Run(args []string) int {
// Before we can evaluate expressions, we must compute and populate any
// derived values (input variables, local values, output values)
// that are not stored in the persistent state.
scope, scopeDiags := lr.Core.Eval(lr.Config, lr.InputState, addrs.RootModuleInstance, evalOpts)
scope, scopeDiags := lr.Core.Eval(ctx, lr.Config, lr.InputState, addrs.RootModuleInstance, evalOpts)
diags = diags.Append(scopeDiags)
if scope == nil {
// scope is nil if there are errors so bad that we can't even build a scope.

View File

@@ -6,6 +6,7 @@
package repl
import (
"context"
"flag"
"os"
"strings"
@@ -298,7 +299,7 @@ func testSession(t *testing.T, test testSessionTest) {
if state == nil {
state = states.NewState()
}
scope, diags := ctx.Eval(config, state, addrs.RootModuleInstance, &tofu.EvalOpts{})
scope, diags := ctx.Eval(context.Background(), config, state, addrs.RootModuleInstance, &tofu.EvalOpts{})
if diags.HasErrors() {
t.Fatalf("failed to create scope: %s", diags.Err())
}

View File

@@ -6,6 +6,7 @@
package tofu
import (
"context"
"log"
"github.com/opentofu/opentofu/internal/addrs"
@@ -37,7 +38,7 @@ type EvalOpts struct {
// the returned scope may be nil. If it is not nil then it may still be used
// to attempt expression evaluation or other analysis, but some expressions
// may not behave as expected.
func (c *Context) Eval(config *configs.Config, state *states.State, moduleAddr addrs.ModuleInstance, opts *EvalOpts) (*lang.Scope, tfdiags.Diagnostics) {
func (c *Context) Eval(ctx context.Context, config *configs.Config, state *states.State, moduleAddr addrs.ModuleInstance, opts *EvalOpts) (*lang.Scope, tfdiags.Diagnostics) {
// This is intended for external callers such as the "tofu console"
// command. Internally, we create an evaluator in c.walk before walking
// the graph, and create scopes in ContextGraphWalker.

View File

@@ -6,6 +6,7 @@
package tofu
import (
"context"
"testing"
"github.com/hashicorp/hcl/v2"
@@ -59,7 +60,7 @@ func TestContextEval(t *testing.T) {
},
})
scope, diags := ctx.Eval(m, states.NewState(), addrs.RootModuleInstance, &EvalOpts{
scope, diags := ctx.Eval(context.Background(), m, states.NewState(), addrs.RootModuleInstance, &EvalOpts{
SetVariables: testInputValuesUnset(m.Module.Variables),
})
if diags.HasErrors() {
@@ -128,7 +129,7 @@ output "out" {
},
})
_, diags := ctx.Eval(m, states.NewState(), addrs.RootModuleInstance, &EvalOpts{
_, diags := ctx.Eval(context.Background(), m, states.NewState(), addrs.RootModuleInstance, &EvalOpts{
SetVariables: testInputValuesUnset(m.Module.Variables),
})
assertNoErrors(t, diags)