Repository: opentofu/opentofu
File: cmd/tofu/provider_source_test.go (Go)

Code before:

import (
	"context"
	"os"
	"path/filepath"
	"testing"

	"github.com/opentofu/opentofu/internal/addrs"
	"github.com/opentofu/opentofu/internal/command/cliconfig"
	"github.com/opentofu/opentofu/internal/command/cliconfig/ociauthconfig"
	"github.com/opentofu/svchost/disco"
)

func TestProviderSource(t *testing.T) {
	tests := []struct {
		name               string
		originalWorkingDir string
		setupFunc          func(t *testing.T) (string, string) // returns (originalDir, overrideDir)
		expectedProvider   string
	}{
		{
			name:               "without overrideWd - should use terraform.d/plugins in original working directory",
			originalWorkingDir: "",
			setupFunc: func(t *testing.T) (string, string) {
				// Create terraform.d/plugins in the original working directory
				currentDir, err := os.Getwd()
				if err != nil {
					t.Fatalf("Failed to get current directory: %v", err)
				}
				pluginsDir := filepath.Join(currentDir, "terraform.d", "plugins")
				err = os.MkdirAll(pluginsDir, 0755)
				if err != nil {
					t.Fatalf("Failed to create plugins directory: %v", err)
				}

				// Create a mock provider in the plugins directory
				providerDir := filepath.Join(pluginsDir, "registry.opentofu.org", "hashicorp", "test-provider", "1.0.0", "linux_amd64")
				err = os.MkdirAll(providerDir, 0755)
				if err != nil {
					t.Fatalf("Failed to create provider directory: %v", err)
				}

				// Create a mock provider binary
				providerBinary := filepath.Join(providerDir, "terraform-provider-test-provider")
				err = os.WriteFile(providerBinary, []byte("mock provider binary"), 0755)
				if err != nil {
					t.Fatalf("Failed to create mock provider binary: %v", err)
				}

				return currentDir, ""
			},

Review comment (suggestion):
We should use the test's temporary directory here. `t.TempDir` and alter the runs based on that. After which, you will need to use `os.Chdir` in each case at the start to simulate the current behaviour.

Code after:

import (
	"context"
	"os"
	"path/filepath"
	"testing"

	"github.com/opentofu/opentofu/internal/addrs"
	"github.com/opentofu/opentofu/internal/command/cliconfig"
	"github.com/opentofu/opentofu/internal/command/cliconfig/ociauthconfig"
	"github.com/opentofu/svchost/disco"
)

func TestProviderSource(t *testing.T) {
	tests := []struct {
		name             string
		setupFunc        func(t *testing.T) (string, string) // returns (originalDir, overrideDir)
		expectedProvider string
	}{
		{
			name: "without overrideWd - should use terraform.d/plugins in original working directory",
			setupFunc: func(t *testing.T) (string, string) {
				// Create a temporary directory to conduct the test in
				tempDir := t.TempDir()

				// Create terraform.d/plugins in the testing directory
				pluginsDir := filepath.Join(tempDir, "terraform.d", "plugins")
				err := os.MkdirAll(pluginsDir, 0755)
				if err != nil {
					t.Fatalf("Failed to create plugins directory: %v", err)
				}

				// Create a mock provider in the plugins directory
				providerDir := filepath.Join(pluginsDir, "registry.opentofu.org", "hashicorp", "test-provider", "1.0.0", "linux_amd64")
				err = os.MkdirAll(providerDir, 0755)
				if err != nil {
					t.Fatalf("Failed to create provider directory: %v", err)
				}

				// Create a mock provider binary
				providerBinary := filepath.Join(providerDir, "terraform-provider-test-provider")
				err = os.WriteFile(providerBinary, []byte("mock provider binary"), 0755)
				if err != nil {
					t.Fatalf("Failed to create mock provider binary: %v", err)
				}

				return tempDir, ""
			},
			expectedProvider: "hashicorp/test-provider",
		},
		{