Repository: opentofu/opentofu
File: internal/command/webbrowser/exec_test.go (Go)

Code before:
	}
	if got, want := string(result), "http://example.com/"; got != want {
		t.Errorf("wrong URL written to output file\ngot:  %s\nwant: %s", got, want)
	}
}

func TestParseBrowserEnv_success(t *testing.T) {
	// ParseBrowserEnv only actually needs to work on Unix-like systems, so
	// the set scenario below is not written to be portable.
	// There's no runtime equivalent of the "unix" build tag, so we just
	// explicitly test the two main Unix OSes we support here.
	if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
		t.Skip("ParseBrowserEnv is only for unix systems")
	}

	tmpDir := t.TempDir()
	fakeExec := filepath.Join(tmpDir, "fake-launch-browser")
	err := os.WriteFile(fakeExec, []byte(`not a real program`), 0755)
	if err != nil {
		// NOTE: This test requires the temp directory to be somewhere that
		// allows executables, so this won't work if the temp directory is
		// on a "noexec" mount on a Unix-style system.
		t.Fatal(err)
	}
	// Temporarily we'll reset the search path to just our temp directory
	os.Setenv("PATH", tmpDir)
	result := ParseBrowserEnv("fake-launch-browser")
	if result == "" {
		t.Fatal("failed to find fake executable")
	}
	if got, want := filepath.Base(result), "fake-launch-browser"; got != want {
		t.Fatalf("returned path %q has wrong basename %q; want %q", result, got, want)
	}
}

func TestParseBrowserEnv_empty(t *testing.T) {
	result := ParseBrowserEnv("")
	if result != "" {
		t.Errorf("returned %q, but wanted empty string", result)
	}
}

func TestParseBrowserEnv_esrComplexSpec(t *testing.T) {
	// The following tests with a strings following the more complex
	// interpretation of BROWSER from http://www.catb.org/~esr/BROWSER/ , which
	// OpenTofu intentionally doesn't support and so should be treated as
	// if the environment variable isn't set at all.
	t.Run(`with %s`, func(t *testing.T) {
		// The esr proposal calls for checking whether there's a %s sequence
		// in the value and then, if so, substituting the URL there and then
		// passing the entire result to a shell. This is the main thing that

Review comment (suggestion):
```suggestion
	t.Setenv("PATH", tmpDir)
```
`t.Setenv` should be used instead since the only difference is that the suggested one reverts the change on test cleanup.

Code after:
	}
	if got, want := string(result), "http://example.com/"; got != want {
		t.Errorf("wrong URL written to output file\ngot:  %s\nwant: %s", got, want)
	}
}

func TestParseBrowserEnv_success(t *testing.T) {
	// ParseBrowserEnv only actually needs to work on Unix-like systems, so
	// the test scenario below is not written to be portable.
	// There's no runtime equivalent of the "unix" build tag, so we just
	// explicitly test the two main Unix OSes we support here.
	if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
		t.Skip("ParseBrowserEnv is only for unix systems")
	}

	tmpDir := t.TempDir()
	fakeExec := filepath.Join(tmpDir, "fake-launch-browser")
	err := os.WriteFile(fakeExec, []byte(`not a real program`), 0755)
	if err != nil {
		// NOTE: This test requires the temp directory to be somewhere that
		// allows executables, so this won't work if the temp directory is
		// on a "noexec" mount on a Unix-style system.
		t.Fatal(err)
	}
	// Temporarily we'll reset the search path to just our temp directory
	t.Setenv("PATH", tmpDir)
	result := ParseBrowserEnv("fake-launch-browser")
	if result == "" {
		t.Fatal("failed to find fake executable")
	}
	if got, want := filepath.Base(result), "fake-launch-browser"; got != want {
		t.Fatalf("returned path %q has wrong basename %q; want %q", result, got, want)
	}
}

func TestParseBrowserEnv_empty(t *testing.T) {
	result := ParseBrowserEnv("")
	if result != "" {
		t.Errorf("returned %q, but wanted empty string", result)
	}
}

func TestParseBrowserEnv_esrComplexSpec(t *testing.T) {
	// The following tests with a strings following the more complex
	// interpretation of BROWSER from http://www.catb.org/~esr/BROWSER/ , which
	// OpenTofu intentionally doesn't support and so should be treated as
	// if the environment variable isn't set at all.
	t.Run(`with %s`, func(t *testing.T) {
		// The esr proposal calls for checking whether there's a %s sequence
		// in the value and then, if so, substituting the URL there and then
		// passing the entire result to a shell. This is the main thing that