github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/config/stub.go (about)

     1  package config
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	ghConfig "github.com/cli/go-gh/pkg/config"
    10  )
    11  
    12  func NewBlankConfig() *ConfigMock {
    13  	defaultStr := `
    14  # What protocol to use when performing git operations. Supported values: ssh, https
    15  git_protocol: https
    16  # What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.
    17  editor:
    18  # When to interactively prompt. This is a global config that cannot be overridden by hostname. Supported values: enabled, disabled
    19  prompt: enabled
    20  # A pager program to send command output to, e.g. "less". Set the value to "cat" to disable the pager.
    21  pager:
    22  # Aliases allow you to create nicknames for gh commands
    23  aliases:
    24    co: pr checkout
    25  # The path to a unix socket through which send HTTP connections. If blank, HTTP traffic will be handled by net/http.DefaultTransport.
    26  http_unix_socket:
    27  # What web browser gh should use when opening URLs. If blank, will refer to environment.
    28  browser:
    29  `
    30  	return NewFromString(defaultStr)
    31  }
    32  
    33  func NewFromString(cfgStr string) *ConfigMock {
    34  	c := ghConfig.ReadFromString(cfgStr)
    35  	cfg := cfg{c}
    36  	mock := &ConfigMock{}
    37  	mock.AuthTokenFunc = func(host string) (string, string) {
    38  		token, _ := c.Get([]string{"hosts", host, "oauth_token"})
    39  		return token, "oauth_token"
    40  	}
    41  	mock.GetFunc = func(host, key string) (string, error) {
    42  		return cfg.Get(host, key)
    43  	}
    44  	mock.GetOrDefaultFunc = func(host, key string) (string, error) {
    45  		return cfg.GetOrDefault(host, key)
    46  	}
    47  	mock.SetFunc = func(host, key, value string) {
    48  		cfg.Set(host, key, value)
    49  	}
    50  	mock.UnsetHostFunc = func(host string) {
    51  		cfg.UnsetHost(host)
    52  	}
    53  	mock.HostsFunc = func() []string {
    54  		keys, _ := c.Keys([]string{"hosts"})
    55  		return keys
    56  	}
    57  	mock.DefaultHostFunc = func() (string, string) {
    58  		return "github.com", "default"
    59  	}
    60  	mock.AliasesFunc = func() *AliasConfig {
    61  		return &AliasConfig{cfg: c}
    62  	}
    63  	mock.WriteFunc = func() error {
    64  		return cfg.Write()
    65  	}
    66  	return mock
    67  }
    68  
    69  // StubWriteConfig stubs out the filesystem where config file are written.
    70  // It then returns a function that will read in the config files into io.Writers.
    71  // It automatically cleans up environment variables and written files.
    72  func StubWriteConfig(t *testing.T) func(io.Writer, io.Writer) {
    73  	t.Helper()
    74  	tempDir := t.TempDir()
    75  	t.Setenv("GH_CONFIG_DIR", tempDir)
    76  	return func(wc io.Writer, wh io.Writer) {
    77  		config, err := os.Open(filepath.Join(tempDir, "config.yml"))
    78  		if err != nil {
    79  			return
    80  		}
    81  		defer config.Close()
    82  		configData, err := io.ReadAll(config)
    83  		if err != nil {
    84  			return
    85  		}
    86  		_, err = wc.Write(configData)
    87  		if err != nil {
    88  			return
    89  		}
    90  
    91  		hosts, err := os.Open(filepath.Join(tempDir, "hosts.yml"))
    92  		if err != nil {
    93  			return
    94  		}
    95  		defer hosts.Close()
    96  		hostsData, err := io.ReadAll(hosts)
    97  		if err != nil {
    98  			return
    99  		}
   100  		_, err = wh.Write(hostsData)
   101  		if err != nil {
   102  			return
   103  		}
   104  	}
   105  }