Repository: opentofu/opentofu
File: internal/configs/module_test.go (Go)

Code before:
		)
	}
	if got := mod.ManagedResources["bar_thing.bt"].Provider; !got.Equals(want) {
		t.Errorf("wrong provider addr for \"bar_thing.bt\"\ngot:  %s\nwant: %s",
			got, want,
		)
	}
}

// When having multiple required providers defined, and one with syntax error,
// ensure that the diagnostics are returned correctly for each and every validation.
// In case a required_provider is containing syntax errors, we are returning an empty one just to allow the
// later validations to add their results.
func TestModule_required_providers_multiple_one_with_syntax_error(t *testing.T) {
	_, diags := testModuleFromDir("testdata/invalid-modules/multiple-required-providers-with-syntax-error")
	if !diags.HasErrors() {
		t.Fatal("module should have error diags, but does not")
	}

	want := []string{
		`Missing attribute value; Expected an attribute value`,
		`Unexpected "resource" block; Blocks are not allowed here`,
		`Duplicate required providers configuration`,
	}
	if wantLen, gotLen := len(want), len(diags.Errs()); wantLen != gotLen {
		t.Fatalf("got less errors than expected. Expected %d but got %d", wantLen, gotLen)
	}
	for i, e := range diags.Errs() {
		if got := e.Error(); !strings.Contains(got, want[i]) {
			t.Errorf("expected error to contain %q\nerror was: \n\t%q\n", want[i], got)
		}
	}
}

// Resources without explicit provider configuration are assigned a provider
// implied based on the resource type. For example, this resource:
//
//	resource "foo_instance" "test" {}
//
// ...is assigned to whichever provider has local name "foo" in the current
// module.
//
// To find the correct provider, we first look in the module's provider
// requirements map for a local name matching the resource type, and fall back
// to a default provider if none is found. This applies to both managed and
// data resources.
func TestModule_implied_provider(t *testing.T) {
	mod, diags := testModuleFromDir("testdata/valid-modules/implied-providers")
	if diags.HasErrors() {
		t.Fatal(diags.Error())
	}

Review comment (suggestion):
nit

It's not necessarily "less errors than expected"

```suggestion
		t.Fatalf("number of errors is not as expected. Expected %d but got %d", wantLen, gotLen)
```

Code after:
		)
	}
	if got := mod.ManagedResources["bar_thing.bt"].Provider; !got.Equals(want) {
		t.Errorf("wrong provider addr for \"bar_thing.bt\"\ngot:  %s\nwant: %s",
			got, want,
		)
	}
}

// When having multiple required providers defined, and one with syntax error,
// ensure that the diagnostics are returned correctly for each and every validation.
// In case a required_provider is containing syntax errors, we are returning an empty one just to allow the
// later validations to add their results.
func TestModule_required_providers_multiple_one_with_syntax_error(t *testing.T) {
	_, diags := testModuleFromDir("testdata/invalid-modules/multiple-required-providers-with-syntax-error")
	if !diags.HasErrors() {
		t.Fatal("module should have error diags, but does not")
	}

	want := []string{
		`Missing attribute value; Expected an attribute value`,
		`Unexpected "resource" block; Blocks are not allowed here`,
		`Duplicate required providers configuration`,
	}
	if wantLen, gotLen := len(want), len(diags.Errs()); wantLen != gotLen {
		t.Fatalf("expected %d errors but got %d", wantLen, gotLen)
	}
	for i, e := range diags.Errs() {
		if got := e.Error(); !strings.Contains(got, want[i]) {
			t.Errorf("expected error to contain %q\nerror was: \n\t%q\n", want[i], got)
		}
	}
}

// Resources without explicit provider configuration are assigned a provider
// implied based on the resource type. For example, this resource:
//
//	resource "foo_instance" "test" {}
//
// ...is assigned to whichever provider has local name "foo" in the current
// module.
//
// To find the correct provider, we first look in the module's provider
// requirements map for a local name matching the resource type, and fall back
// to a default provider if none is found. This applies to both managed and
// data resources.
func TestModule_implied_provider(t *testing.T) {
	mod, diags := testModuleFromDir("testdata/valid-modules/implied-providers")
	if diags.HasErrors() {
		t.Fatal(diags.Error())
	}