...

Source file src/github.com/cli/go-gh/v2/pkg/browser/browser_test.go

Documentation: github.com/cli/go-gh/v2/pkg/browser

     1  package browser
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/cli/go-gh/v2/pkg/config"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestHelperProcess(t *testing.T) {
    14  	if os.Getenv("GH_WANT_HELPER_PROCESS") != "1" {
    15  		return
    16  	}
    17  	fmt.Fprintf(os.Stdout, "%v", os.Args[3:])
    18  	os.Exit(0)
    19  }
    20  
    21  func TestBrowse(t *testing.T) {
    22  	launcher := fmt.Sprintf("%q -test.run=TestHelperProcess -- chrome", os.Args[0])
    23  	stdout := &bytes.Buffer{}
    24  	stderr := &bytes.Buffer{}
    25  	b := Browser{launcher: launcher, stdout: stdout, stderr: stderr}
    26  	err := b.browse("github.com", []string{"GH_WANT_HELPER_PROCESS=1"})
    27  	assert.NoError(t, err)
    28  	assert.Equal(t, "[chrome github.com]", stdout.String())
    29  	assert.Equal(t, "", stderr.String())
    30  }
    31  
    32  func TestResolveLauncher(t *testing.T) {
    33  	tests := []struct {
    34  		name         string
    35  		env          map[string]string
    36  		config       *config.Config
    37  		wantLauncher string
    38  	}{
    39  		{
    40  			name: "GH_BROWSER set",
    41  			env: map[string]string{
    42  				"GH_BROWSER": "GH_BROWSER",
    43  			},
    44  			wantLauncher: "GH_BROWSER",
    45  		},
    46  		{
    47  			name:         "config browser set",
    48  			config:       config.ReadFromString("browser: CONFIG_BROWSER"),
    49  			wantLauncher: "CONFIG_BROWSER",
    50  		},
    51  		{
    52  			name: "BROWSER set",
    53  			env: map[string]string{
    54  				"BROWSER": "BROWSER",
    55  			},
    56  			wantLauncher: "BROWSER",
    57  		},
    58  		{
    59  			name: "GH_BROWSER and config browser set",
    60  			env: map[string]string{
    61  				"GH_BROWSER": "GH_BROWSER",
    62  			},
    63  			config:       config.ReadFromString("browser: CONFIG_BROWSER"),
    64  			wantLauncher: "GH_BROWSER",
    65  		},
    66  		{
    67  			name: "config browser and BROWSER set",
    68  			env: map[string]string{
    69  				"BROWSER": "BROWSER",
    70  			},
    71  			config:       config.ReadFromString("browser: CONFIG_BROWSER"),
    72  			wantLauncher: "CONFIG_BROWSER",
    73  		},
    74  		{
    75  			name: "GH_BROWSER and BROWSER set",
    76  			env: map[string]string{
    77  				"BROWSER":    "BROWSER",
    78  				"GH_BROWSER": "GH_BROWSER",
    79  			},
    80  			wantLauncher: "GH_BROWSER",
    81  		},
    82  	}
    83  	for _, tt := range tests {
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			if tt.env != nil {
    86  				for k, v := range tt.env {
    87  					t.Setenv(k, v)
    88  				}
    89  			}
    90  			if tt.config != nil {
    91  				old := config.Read
    92  				config.Read = func(_ *config.Config) (*config.Config, error) {
    93  					return tt.config, nil
    94  				}
    95  				defer func() { config.Read = old }()
    96  			}
    97  			launcher := resolveLauncher()
    98  			assert.Equal(t, tt.wantLauncher, launcher)
    99  		})
   100  	}
   101  }
   102  

View as plain text