From bf5037fa05851a426e288c4a05b3f5dabcd7bea0 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Fri, 28 Jun 2024 10:46:12 +0200 Subject: [PATCH] stacks: add test case --- internal/stacks/stackruntime/validate_test.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/internal/stacks/stackruntime/validate_test.go b/internal/stacks/stackruntime/validate_test.go index f603a7763d..369e3bc705 100644 --- a/internal/stacks/stackruntime/validate_test.go +++ b/internal/stacks/stackruntime/validate_test.go @@ -375,3 +375,43 @@ Terraform uses references to decide a suitable order for performing operations, t.Errorf("wrong diagnostics\n%s", diff) } } + +func TestValidate_missing_provider_from_lockfile(t *testing.T) { + ctx := context.Background() + cfg := loadMainBundleConfigForTest(t, filepath.Join("with-single-input", "input-from-component")) + lock := depsfile.NewLocks() + + diags := Validate(ctx, &ValidateRequest{ + Config: cfg, + ProviderFactories: map[addrs.Provider]providers.Factory{ + // We support both hashicorp/testing and + // terraform.io/builtin/testing as providers. This lets us + // test the provider aliasing feature. Both providers + // support the same set of resources and data sources. + addrs.NewDefaultProvider("testing"): func() (providers.Interface, error) { + return stacks_testing_provider.NewProvider(), nil + }, + addrs.NewBuiltInProvider("testing"): func() (providers.Interface, error) { + return stacks_testing_provider.NewProvider(), nil + }, + }, + DependencyLocks: *lock, + }) + + if len(diags) != 1 { + t.Fatalf("expected exactly one diagnostic, got %d", len(diags)) + } + + diag := diags[0] + if diag.Severity() != tfdiags.Error { + t.Fatalf("expected error diagnostic, got %s", diag.Severity()) + } + + if diag.Description().Summary != "Provider missing from lockfile" { + t.Fatalf("expected diagnostic summary 'Provider missing from lockfile', got %q", diag.Description().Summary) + } + + if diag.Description().Detail != "Provider \"registry.terraform.io/hashicorp/testing\" is not in the lockfile. This provider must be in the lockfile to be used in the configuration. Please run `tfstacks provider lock` to update the lockfile and run this operation again with an updated configuration." { + t.Fatalf("expected diagnostic detail to be a specific message, got %q", diag.Description().Detail) + } +}