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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
package browser
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/cli/go-gh/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestHelperProcess(t *testing.T) {
if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
return
}
fmt.Fprintf(os.Stdout, "%v", os.Args[3:])
os.Exit(0)
}
func TestBrowse(t *testing.T) {
launcher := fmt.Sprintf("%q -test.run=TestHelperProcess -- chrome", os.Args[0])
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
b := Browser{launcher: launcher, stdout: stdout, stderr: stderr}
err := b.browse("github.com", []string{"GH_WANT_HELPER_PROCESS=1"})
assert.NoError(t, err)
assert.Equal(t, "[chrome github.com]", stdout.String())
assert.Equal(t, "", stderr.String())
}
func TestResolveLauncher(t *testing.T) {
tests := []struct {
name string
env map[string]string
config *config.Config
wantLauncher string
}{
{
name: "GH_BROWSER set",
env: map[string]string{
"GH_BROWSER": "GH_BROWSER",
},
wantLauncher: "GH_BROWSER",
},
{
name: "config browser set",
config: config.ReadFromString("browser: CONFIG_BROWSER"),
wantLauncher: "CONFIG_BROWSER",
},
{
name: "BROWSER set",
env: map[string]string{
"BROWSER": "BROWSER",
},
wantLauncher: "BROWSER",
},
{
name: "GH_BROWSER and config browser set",
env: map[string]string{
"GH_BROWSER": "GH_BROWSER",
},
config: config.ReadFromString("browser: CONFIG_BROWSER"),
wantLauncher: "GH_BROWSER",
},
{
name: "config browser and BROWSER set",
env: map[string]string{
"BROWSER": "BROWSER",
},
config: config.ReadFromString("browser: CONFIG_BROWSER"),
wantLauncher: "CONFIG_BROWSER",
},
{
name: "GH_BROWSER and BROWSER set",
env: map[string]string{
"BROWSER": "BROWSER",
"GH_BROWSER": "GH_BROWSER",
},
wantLauncher: "GH_BROWSER",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.env != nil {
for k, v := range tt.env {
t.Setenv(k, v)
}
}
if tt.config != nil {
old := config.Read
config.Read = func() (*config.Config, error) {
return tt.config, nil
}
defer func() { config.Read = old }()
}
launcher := resolveLauncher()
assert.Equal(t, tt.wantLauncher, launcher)
})
}
}
|