github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/internal/config/config_type_test.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/MakeNowJust/heredoc"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_fileConfig_Set(t *testing.T) {
    12  	mainBuf := bytes.Buffer{}
    13  	hostsBuf := bytes.Buffer{}
    14  	defer StubWriteConfig(&mainBuf, &hostsBuf)()
    15  
    16  	c := NewBlankConfig()
    17  	assert.NoError(t, c.Set("", "editor", "nano"))
    18  	assert.NoError(t, c.Set("github.com", "git_protocol", "ssh"))
    19  	assert.NoError(t, c.Set("example.com", "editor", "vim"))
    20  	assert.NoError(t, c.Set("github.com", "user", "hubot"))
    21  	assert.NoError(t, c.Write())
    22  
    23  	assert.Contains(t, mainBuf.String(), "editor: nano")
    24  	assert.Contains(t, mainBuf.String(), "git_protocol: https")
    25  	assert.Equal(t, `github.com:
    26      git_protocol: ssh
    27      user: hubot
    28  example.com:
    29      editor: vim
    30  `, hostsBuf.String())
    31  }
    32  
    33  func Test_defaultConfig(t *testing.T) {
    34  	mainBuf := bytes.Buffer{}
    35  	hostsBuf := bytes.Buffer{}
    36  	defer StubWriteConfig(&mainBuf, &hostsBuf)()
    37  
    38  	cfg := NewBlankConfig()
    39  	assert.NoError(t, cfg.Write())
    40  
    41  	expected := heredoc.Doc(`
    42  		# What protocol to use when performing git operations. Supported values: ssh, https
    43  		git_protocol: https
    44  		# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.
    45  		editor:
    46  		# When to interactively prompt. This is a global config that cannot be overridden by hostname. Supported values: enabled, disabled
    47  		prompt: enabled
    48  		# A pager program to send command output to, e.g. "less". Set the value to "cat" to disable the pager.
    49  		pager:
    50  		# Aliases allow you to create nicknames for gh commands
    51  		aliases:
    52  		    co: pr checkout
    53  		# The path to a unix socket through which send HTTP connections. If blank, HTTP traffic will be handled by net/http.DefaultTransport.
    54  		http_unix_socket:
    55  		# What web browser gh should use when opening URLs. If blank, will refer to environment.
    56  		browser:
    57  	`)
    58  	assert.Equal(t, expected, mainBuf.String())
    59  	assert.Equal(t, "", hostsBuf.String())
    60  
    61  	proto, err := cfg.Get("", "git_protocol")
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, "https", proto)
    64  
    65  	editor, err := cfg.Get("", "editor")
    66  	assert.NoError(t, err)
    67  	assert.Equal(t, "", editor)
    68  
    69  	aliases, err := cfg.Aliases()
    70  	assert.NoError(t, err)
    71  	assert.Equal(t, len(aliases.All()), 1)
    72  	expansion, _ := aliases.Get("co")
    73  	assert.Equal(t, expansion, "pr checkout")
    74  
    75  	browser, err := cfg.Get("", "browser")
    76  	assert.NoError(t, err)
    77  	assert.Equal(t, "", browser)
    78  }
    79  
    80  func Test_ValidateValue(t *testing.T) {
    81  	err := ValidateValue("git_protocol", "sshpps")
    82  	assert.EqualError(t, err, "invalid value")
    83  
    84  	err = ValidateValue("git_protocol", "ssh")
    85  	assert.NoError(t, err)
    86  
    87  	err = ValidateValue("editor", "vim")
    88  	assert.NoError(t, err)
    89  
    90  	err = ValidateValue("got", "123")
    91  	assert.NoError(t, err)
    92  
    93  	err = ValidateValue("http_unix_socket", "really_anything/is/allowed/and/net.Dial\\(...\\)/will/ultimately/validate")
    94  	assert.NoError(t, err)
    95  }
    96  
    97  func Test_ValidateKey(t *testing.T) {
    98  	err := ValidateKey("invalid")
    99  	assert.EqualError(t, err, "invalid key")
   100  
   101  	err = ValidateKey("git_protocol")
   102  	assert.NoError(t, err)
   103  
   104  	err = ValidateKey("editor")
   105  	assert.NoError(t, err)
   106  
   107  	err = ValidateKey("prompt")
   108  	assert.NoError(t, err)
   109  
   110  	err = ValidateKey("pager")
   111  	assert.NoError(t, err)
   112  
   113  	err = ValidateKey("http_unix_socket")
   114  	assert.NoError(t, err)
   115  
   116  	err = ValidateKey("browser")
   117  	assert.NoError(t, err)
   118  }