aboutsummaryrefslogtreecommitdiffstats
path: root/browserenv_unix_test.go
blob: 0d61a52f6abde8bc92297772a8269628a634ebe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// +build !windows

package browserenv

import (
	"os"
	"reflect"
	"testing"
)

func TestBrowserCommand(t *testing.T) {
	tests := []struct {
		name     string
		envValue string
		url      string
		wantCmd  string
	}{
		{
			"without URL directive",
			"open -a Firefox",
			"https://duckduckgo.com",
			"open -a Firefox 'https://duckduckgo.com'",
		},
		{
			"with URL directive at end",
			"open -a Firefox %s",
			"https://duckduckgo.com",
			"open -a Firefox https://duckduckgo.com",
		},
		{
			"with URL directive in middle",
			"open -a Firefox %s --other-arg",
			"https://duckduckgo.com",
			"open -a Firefox https://duckduckgo.com --other-arg",
		},
		{
			"escapes single quotes in URL",
			"open -a Firefox",
			"https://duckduckgo.com/?q='s-Hertogenbosch",
			"open -a Firefox 'https://duckduckgo.com/?q=%27s-Hertogenbosch'",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			cmd := browserCommand(test.envValue, test.url)

			shell := os.Getenv("SHELL")
			if shell == "" {
				shell = "/bin/sh"
			}

			wantArgs := []string{shell, "-c", test.wantCmd}
			if !reflect.DeepEqual(cmd.Args, wantArgs) {
				t.Errorf("got args '%#v' want '%#v'", cmd.Args, wantArgs)
			}
		})
	}
}