Expose CacheDir on Config

This commit is contained in:
William Martin 2024-04-05 14:58:12 +02:00
parent ca39101b3e
commit f808dcee62
3 changed files with 46 additions and 0 deletions

View file

@ -36,6 +36,8 @@ type Config interface {
Write() error
Migrate(Migration) error
CacheDir() string
Aliases() *AliasConfig
Authentication() *AuthConfig
Browser(string) string
@ -191,6 +193,10 @@ func (c *cfg) Migrate(m Migration) error {
return nil
}
func (c *cfg) CacheDir() string {
return ghConfig.CacheDir()
}
func defaultFor(key string) (string, bool) {
for _, co := range ConfigOptions() {
if co.Key == key {

View file

@ -26,6 +26,9 @@ var _ Config = &ConfigMock{}
// BrowserFunc: func(s string) string {
// panic("mock out the Browser method")
// },
// CacheDirFunc: func() string {
// panic("mock out the CacheDir method")
// },
// EditorFunc: func(s string) string {
// panic("mock out the Editor method")
// },
@ -72,6 +75,9 @@ type ConfigMock struct {
// BrowserFunc mocks the Browser method.
BrowserFunc func(s string) string
// CacheDirFunc mocks the CacheDir method.
CacheDirFunc func() string
// EditorFunc mocks the Editor method.
EditorFunc func(s string) string
@ -115,6 +121,9 @@ type ConfigMock struct {
// S is the s argument value.
S string
}
// CacheDir holds details about calls to the CacheDir method.
CacheDir []struct {
}
// Editor holds details about calls to the Editor method.
Editor []struct {
// S is the s argument value.
@ -171,6 +180,7 @@ type ConfigMock struct {
lockAliases sync.RWMutex
lockAuthentication sync.RWMutex
lockBrowser sync.RWMutex
lockCacheDir sync.RWMutex
lockEditor sync.RWMutex
lockGetOrDefault sync.RWMutex
lockGitProtocol sync.RWMutex
@ -269,6 +279,33 @@ func (mock *ConfigMock) BrowserCalls() []struct {
return calls
}
// CacheDir calls CacheDirFunc.
func (mock *ConfigMock) CacheDir() string {
if mock.CacheDirFunc == nil {
panic("ConfigMock.CacheDirFunc: method is nil but Config.CacheDir was just called")
}
callInfo := struct {
}{}
mock.lockCacheDir.Lock()
mock.calls.CacheDir = append(mock.calls.CacheDir, callInfo)
mock.lockCacheDir.Unlock()
return mock.CacheDirFunc()
}
// CacheDirCalls gets all the calls that were made to CacheDir.
// Check the length with:
//
// len(mockedConfig.CacheDirCalls())
func (mock *ConfigMock) CacheDirCalls() []struct {
} {
var calls []struct {
}
mock.lockCacheDir.RLock()
calls = mock.calls.CacheDir
mock.lockCacheDir.RUnlock()
return calls
}
// Editor calls EditorFunc.
func (mock *ConfigMock) Editor(s string) string {
if mock.EditorFunc == nil {

View file

@ -77,6 +77,9 @@ func NewFromString(cfgStr string) *ConfigMock {
val, _ := cfg.GetOrDefault("", versionKey)
return val
}
mock.CacheDirFunc = func() string {
return cfg.CacheDir()
}
return mock
}