Repository: opentofu/opentofu
File: internal/command/e2etest/init_test.go (Go)

Code before:

		expectedErr := `╷
│ Error: Failed to query available provider packages
│` + ` ` + `
│ Could not retrieve the list of available versions for provider
│ hashicorp/nonexist: provider registry registry.opentofu.org does not have a
│ provider named registry.opentofu.org/hashicorp/nonexist
│ 
│ All modules should specify their required_providers so that external
│ consumers will get the correct providers when using a module. To see which
│ modules are currently depending on hashicorp/nonexist, run the following
│ command:
│     tofu providers
│ 
│ If you believe this provider is missing from the registry, please submit a
│ issue on the OpenTofu Registry
│ https://github.com/opentofu/registry/issues/new/choose
╵

`
		if stripAnsi(stderr) != expectedErr {
			t.Errorf("wrong output:\n%s", cmp.Diff(stripAnsi(stderr), expectedErr))
		}
	})

	t.Run("implicit provider not found", func(t *testing.T) {
		implicitFixturePath := filepath.Join("testdata", "provider-implicit-ref-not-found")
		tf := e2e.NewBinary(t, tofuBin, implicitFixturePath)
		stdout, _, err := tf.Run("init")
		if err == nil {
			t.Fatal("expected error, got success")
		}

		// Testing that the warn wrote to the user is containing the resource address from where the provider
		// was registered to be downloaded
		expectedOutput := `Initializing the backend...
Initializing modules...
- testmod in mod

Initializing provider plugins...
- Finding latest version of hashicorp/nonexistingprov...
╷
│ Warning: No explicit definition for the provider
│ 
│ The provider hashicorp/nonexistingprov could not be found in any
│ required_providers blocks. Ensure that the root or any used module is
│ having the required_providers block properly configured. 
│ Or if the definition is already there, ensure that the resources are having
│ 'provider' configured properly.
│ 
│ Implicitly referenced from main.tf:2,1-39. And is referenced from 1 more

Review comment (suggestion):
EDIT: Updated the screenshot.

To help with the reviewing process, here is a screenshot of the expected output of this test:
![image](https://github.com/user-attachments/assets/5658ed9a-f147-4a85-af09-cb6b2f2334fc)


What I tried here is to gather the in-file references and use those for an easier navigation.
For sure there could be arguments against this, and I would be more than happy to trigger a conversation around this subject.

Code after:

		expectedErr := `╷
│ Error: Failed to query available provider packages
│` + ` ` + `
│ Could not retrieve the list of available versions for provider
│ hashicorp/nonexist: provider registry registry.opentofu.org does not have a
│ provider named registry.opentofu.org/hashicorp/nonexist
│ 
│ All modules should specify their required_providers so that external
│ consumers will get the correct providers when using a module. To see which
│ modules are currently depending on hashicorp/nonexist, run the following
│ command:
│     tofu providers
│ 
│ If you believe this provider is missing from the registry, please submit a
│ issue on the OpenTofu Registry
│ https://github.com/opentofu/registry/issues/new/choose
╵

`
		if stripAnsi(stderr) != expectedErr {
			t.Errorf("wrong output:\n%s", cmp.Diff(stripAnsi(stderr), expectedErr))
		}
	})

	t.Run("implicit provider resource and data not found", func(t *testing.T) {
		implicitFixturePath := filepath.Join("testdata", "provider-implicit-ref-not-found/implicit-by-resource-and-data")
		tf := e2e.NewBinary(t, tofuBin, implicitFixturePath)
		stdout, _, err := tf.Run("init")
		if err == nil {
			t.Fatal("expected error, got success")
		}

		// Testing that the warn wrote to the user is containing the resource address from where the provider
		// was registered to be downloaded
		expectedContentInOutput := []string{
			`(and one more similar warning elsewhere)`,
			`
╷
│ Warning: Automatically-inferred provider dependency
│ 
│   on main.tf line 2:
│    2: resource "nonexistingProv_res" "test1" {
│ 
│ Due to the prefix of the resource type name OpenTofu guessed that you
│ intended to associate nonexistingProv_res.test1 with a provider whose local
│ name is "nonexistingprov", but that name is not declared in this module's
│ required_providers block. OpenTofu therefore guessed that you intended to
│ use hashicorp/nonexistingprov, but that provider does not exist.
│ 
│ Make at least one of the following changes to tell OpenTofu which provider