mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
refactor: merge net and http under runtime module
This commit is contained in:
parent
05251e1ac3
commit
822e2b5b48
49
src/cache/cache.go
vendored
Normal file
49
src/cache/cache.go
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cache interface {
|
||||
Init(home string)
|
||||
Close()
|
||||
// Gets the value for a given key.
|
||||
// Returns the value and a boolean indicating if the key was found.
|
||||
// In case the ttl expired, the function returns false.
|
||||
Get(key string) (string, bool)
|
||||
// Sets a value for a given key.
|
||||
// The ttl indicates how many minutes to cache the value.
|
||||
Set(key, value string, ttl int)
|
||||
// Deletes a key from the cache.
|
||||
Delete(key string)
|
||||
}
|
||||
|
||||
func pid() string {
|
||||
pid := os.Getenv("POSH_PID")
|
||||
if len(pid) == 0 {
|
||||
pid = strconv.Itoa(os.Getppid())
|
||||
}
|
||||
return pid
|
||||
}
|
||||
|
||||
var (
|
||||
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", pid())
|
||||
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
|
||||
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
|
||||
)
|
||||
|
||||
type Entry struct {
|
||||
Value string `json:"value"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
TTL int `json:"ttl"`
|
||||
}
|
||||
|
||||
func (c *Entry) Expired() bool {
|
||||
if c.TTL < 0 {
|
||||
return false
|
||||
}
|
||||
return time.Now().Unix() >= (c.Timestamp + int64(c.TTL)*60)
|
||||
}
|
20
src/cache/command.go
vendored
Normal file
20
src/cache/command.go
vendored
Normal file
|
@ -0,0 +1,20 @@
|
|||
package cache
|
||||
|
||||
import "github.com/jandedobbeleer/oh-my-posh/src/concurrent"
|
||||
|
||||
type Command struct {
|
||||
Commands *concurrent.Map
|
||||
}
|
||||
|
||||
func (c *Command) Set(command, path string) {
|
||||
c.Commands.Set(command, path)
|
||||
}
|
||||
|
||||
func (c *Command) Get(command string) (string, bool) {
|
||||
cacheCommand, found := c.Commands.Get(command)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
command, ok := cacheCommand.(string)
|
||||
return command, ok
|
||||
}
|
85
src/cache/file.go
vendored
Normal file
85
src/cache/file.go
vendored
Normal file
|
@ -0,0 +1,85 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/concurrent"
|
||||
)
|
||||
|
||||
const (
|
||||
CacheFile = "/omp.cache"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
cache *concurrent.Map
|
||||
cachePath string
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (fc *File) Init(cachePath string) {
|
||||
fc.cache = concurrent.NewMap()
|
||||
fc.cachePath = cachePath
|
||||
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
|
||||
content, err := os.ReadFile(cacheFilePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var list map[string]*Entry
|
||||
if err := json.Unmarshal(content, &list); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for key, co := range list {
|
||||
if co.Expired() {
|
||||
continue
|
||||
}
|
||||
|
||||
fc.cache.Set(key, co)
|
||||
}
|
||||
}
|
||||
|
||||
func (fc *File) Close() {
|
||||
if !fc.dirty {
|
||||
return
|
||||
}
|
||||
|
||||
cache := fc.cache.ToSimpleMap()
|
||||
|
||||
if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
|
||||
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
|
||||
_ = os.WriteFile(cacheFilePath, dump, 0644)
|
||||
}
|
||||
}
|
||||
|
||||
// returns the value for the given key as long as
|
||||
// the TTL (minutes) is not expired
|
||||
func (fc *File) Get(key string) (string, bool) {
|
||||
val, found := fc.cache.Get(key)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
if co, ok := val.(*Entry); ok {
|
||||
return co.Value, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// sets the value for the given key with a TTL (minutes)
|
||||
func (fc *File) Set(key, value string, ttl int) {
|
||||
fc.cache.Set(key, &Entry{
|
||||
Value: value,
|
||||
Timestamp: time.Now().Unix(),
|
||||
TTL: ttl,
|
||||
})
|
||||
fc.dirty = true
|
||||
}
|
||||
|
||||
// delete the key from the cache
|
||||
func (fc *File) Delete(key string) {
|
||||
fc.cache.Delete(key)
|
||||
fc.dirty = true
|
||||
}
|
14
src/mock/cache.go → src/cache/mock/cache.go
vendored
14
src/mock/cache.go → src/cache/mock/cache.go
vendored
|
@ -2,18 +2,18 @@ package mock
|
|||
|
||||
import mock "github.com/stretchr/testify/mock"
|
||||
|
||||
// MockedCache is an autogenerated mock type for the cache type
|
||||
type MockedCache struct {
|
||||
// Cache is an autogenerated mock type for the cache type
|
||||
type Cache struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// close provides a mock function with given fields:
|
||||
func (_m *MockedCache) Close() {
|
||||
func (_m *Cache) Close() {
|
||||
_m.Called()
|
||||
}
|
||||
|
||||
// get provides a mock function with given fields: key
|
||||
func (_m *MockedCache) Get(key string) (string, bool) {
|
||||
func (_m *Cache) Get(key string) (string, bool) {
|
||||
ret := _m.Called(key)
|
||||
|
||||
var r0 string
|
||||
|
@ -34,16 +34,16 @@ func (_m *MockedCache) Get(key string) (string, bool) {
|
|||
}
|
||||
|
||||
// init provides a mock function with given fields: home
|
||||
func (_m *MockedCache) Init(home string) {
|
||||
func (_m *Cache) Init(home string) {
|
||||
_m.Called(home)
|
||||
}
|
||||
|
||||
// set provides a mock function with given fields: key, value, ttl
|
||||
func (_m *MockedCache) Set(key, value string, ttl int) {
|
||||
func (_m *Cache) Set(key, value string, ttl int) {
|
||||
_m.Called(key, value, ttl)
|
||||
}
|
||||
|
||||
// delete provides a mock function with given fields: key
|
||||
func (_m *MockedCache) Delete(key string) {
|
||||
func (_m *Cache) Delete(key string) {
|
||||
_m.Called(key)
|
||||
}
|
38
src/cache/template.go
vendored
Normal file
38
src/cache/template.go
vendored
Normal file
|
@ -0,0 +1,38 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/concurrent"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
Root bool
|
||||
PWD string
|
||||
AbsolutePWD string
|
||||
Folder string
|
||||
Shell string
|
||||
ShellVersion string
|
||||
UserName string
|
||||
HostName string
|
||||
Code int
|
||||
Env map[string]string
|
||||
Var concurrent.SimpleMap
|
||||
OS string
|
||||
WSL bool
|
||||
PromptCount int
|
||||
SHLVL int
|
||||
Segments *concurrent.Map
|
||||
SegmentsCache concurrent.SimpleMap
|
||||
|
||||
Initialized bool
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (t *Template) AddSegmentData(key string, value any) {
|
||||
t.Segments.Set(key, value)
|
||||
}
|
||||
|
||||
func (t *Template) RemoveSegmentData(key string) {
|
||||
t.Segments.Delete(key)
|
||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -43,7 +44,7 @@ You can do the following:
|
|||
case "path":
|
||||
fmt.Print(env.CachePath())
|
||||
case "clear":
|
||||
cacheFilePath := filepath.Join(env.CachePath(), runtime.CacheFile)
|
||||
cacheFilePath := filepath.Join(env.CachePath(), cache.CacheFile)
|
||||
err := os.Remove(cacheFilePath)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
|
@ -51,7 +52,7 @@ You can do the following:
|
|||
}
|
||||
fmt.Printf("removed cache file at %s\n", cacheFilePath)
|
||||
case "edit":
|
||||
cacheFilePath := filepath.Join(env.CachePath(), runtime.CacheFile)
|
||||
cacheFilePath := filepath.Join(env.CachePath(), cache.CacheFile)
|
||||
editFileWithEditor(cacheFilePath)
|
||||
}
|
||||
},
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/color"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
|
||||
|
@ -64,8 +65,7 @@ This command is used to get the value of the following variables:
|
|||
accent := color2.RGB(rgb.R, rgb.G, rgb.B)
|
||||
fmt.Println("#" + accent.Hex())
|
||||
case "toggles":
|
||||
cache := env.Cache()
|
||||
togglesCache, _ := cache.Get(runtime.TOGGLECACHE)
|
||||
togglesCache, _ := env.Cache().Get(cache.TOGGLECACHE)
|
||||
var toggles []string
|
||||
if len(togglesCache) != 0 {
|
||||
toggles = strings.Split(togglesCache, ",")
|
||||
|
|
|
@ -3,6 +3,7 @@ package cli
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
@ -22,8 +23,7 @@ var toggleCmd = &cobra.Command{
|
|||
env.Init()
|
||||
defer env.Close()
|
||||
|
||||
cache := env.Cache()
|
||||
togglesCache, _ := cache.Get(runtime.TOGGLECACHE)
|
||||
togglesCache, _ := env.Cache().Get(cache.TOGGLECACHE)
|
||||
var toggles []string
|
||||
if len(togglesCache) != 0 {
|
||||
toggles = strings.Split(togglesCache, ",")
|
||||
|
@ -44,7 +44,7 @@ var toggleCmd = &cobra.Command{
|
|||
newToggles = append(newToggles, segment)
|
||||
}
|
||||
|
||||
cache.Set(runtime.TOGGLECACHE, strings.Join(newToggles, ","), 1440)
|
||||
env.Cache().Set(cache.TOGGLECACHE, strings.Join(newToggles, ","), 1440)
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
)
|
||||
|
||||
func TestGetAnsiFromColorString(t *testing.T) {
|
||||
|
@ -38,7 +38,7 @@ func TestGetAnsiFromColorString(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMakeColors(t *testing.T) {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
|
||||
env.On("WindowsRegistryKeyValue", `HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM\ColorizationColor`).Return(&runtime.WindowsRegistryValue{}, errors.New("err"))
|
||||
colors := MakeColors(nil, false, "", env)
|
||||
|
|
46
src/concurrent/map.go
Normal file
46
src/concurrent/map.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package concurrent
|
||||
|
||||
import "sync"
|
||||
|
||||
func NewMap() *Map {
|
||||
var cm Map
|
||||
return &cm
|
||||
}
|
||||
|
||||
type Map sync.Map
|
||||
|
||||
func (cm *Map) Set(key string, value any) {
|
||||
(*sync.Map)(cm).Store(key, value)
|
||||
}
|
||||
|
||||
func (cm *Map) Get(key string) (any, bool) {
|
||||
return (*sync.Map)(cm).Load(key)
|
||||
}
|
||||
|
||||
func (cm *Map) Delete(key string) {
|
||||
(*sync.Map)(cm).Delete(key)
|
||||
}
|
||||
|
||||
func (cm *Map) Contains(key string) bool {
|
||||
_, ok := (*sync.Map)(cm).Load(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (cm *Map) ToSimpleMap() SimpleMap {
|
||||
list := make(map[string]any)
|
||||
(*sync.Map)(cm).Range(func(key, value any) bool {
|
||||
list[key.(string)] = value
|
||||
return true
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
type SimpleMap map[string]any
|
||||
|
||||
func (m SimpleMap) ConcurrentMap() *Map {
|
||||
var cm Map
|
||||
for k, v := range m {
|
||||
cm.Set(k, v)
|
||||
}
|
||||
return &cm
|
||||
}
|
|
@ -3,12 +3,12 @@ package config
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/color"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestGetPalette(t *testing.T) {
|
||||
|
@ -70,12 +70,12 @@ func TestGetPalette(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := &mock.Environment{}
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{},
|
||||
Shell: "bash",
|
||||
})
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
cfg := &Config{
|
||||
env: env,
|
||||
Palette: tc.Palette,
|
||||
|
|
|
@ -4,12 +4,12 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
httplib "net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/net"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
)
|
||||
|
||||
type ConnectionError struct {
|
||||
|
@ -28,12 +28,12 @@ func getGlyphCodePoints() (codePoints, error) {
|
|||
ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
|
||||
defer cncl()
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://ohmyposh.dev/codepoints.csv", nil)
|
||||
request, err := httplib.NewRequestWithContext(ctx, httplib.MethodGet, "https://ohmyposh.dev/codepoints.csv", nil)
|
||||
if err != nil {
|
||||
return codePoints, &ConnectionError{reason: err.Error()}
|
||||
}
|
||||
|
||||
response, err := net.HTTPClient.Do(request)
|
||||
response, err := http.HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
return codePoints, err
|
||||
}
|
||||
|
|
|
@ -3,13 +3,13 @@ package config
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/segments"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -327,8 +327,8 @@ func TestSegmentTemplateMigration(t *testing.T) {
|
|||
Type: tc.Type,
|
||||
Properties: tc.Props,
|
||||
}
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("Debug", mock2.Anything).Return(nil)
|
||||
env := &mock.Environment{}
|
||||
env.On("Debug", testify_.Anything).Return(nil)
|
||||
segment.migrationOne(env)
|
||||
assert.Equal(t, tc.Expected, segment.Properties[segmentTemplate], tc.Case)
|
||||
}
|
||||
|
@ -431,7 +431,7 @@ func TestMigrateConfig(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
cfg := &Config{
|
||||
ConsoleTitleTemplate: tc.Template,
|
||||
env: &mock.MockedEnvironment{},
|
||||
env: &mock.Environment{},
|
||||
}
|
||||
cfg.Migrate()
|
||||
assert.Equal(t, tc.Expected, cfg.ConsoleTitleTemplate, tc.Case)
|
||||
|
@ -465,7 +465,7 @@ func TestMigrationTwo(t *testing.T) {
|
|||
if tc.Template != "" {
|
||||
segment.Properties[segmentTemplate] = tc.Template
|
||||
}
|
||||
segment.migrationTwo(&mock.MockedEnvironment{})
|
||||
segment.migrationTwo(&mock.Environment{})
|
||||
assert.Equal(t, tc.Expected, segment.Template, tc.Case)
|
||||
assert.NotContains(t, segment.Properties, segmentTemplate, tc.Case)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package config
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -27,7 +27,7 @@ func TestShouldHideForWidth(t *testing.T) {
|
|||
{Case: "Min & Max cols - show", MinWidth: 10, MaxWidth: 20, Width: 11, Expected: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("TerminalWidth").Return(tc.Width, tc.Error)
|
||||
got := shouldHideForWidth(env, tc.MinWidth, tc.MaxWidth)
|
||||
assert.Equal(t, tc.Expected, got, tc.Case)
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/color"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
|
@ -112,7 +113,7 @@ func (segment *Segment) SetEnabled(env runtime.Environment) {
|
|||
segment.env.DebugF("Segment: %s", segment.Name())
|
||||
|
||||
// validate toggles
|
||||
if toggles, OK := segment.env.Cache().Get(runtime.TOGGLECACHE); OK && len(toggles) > 0 {
|
||||
if toggles, OK := segment.env.Cache().Get(cache.TOGGLECACHE); OK && len(toggles) > 0 {
|
||||
list := strings.Split(toggles, ",")
|
||||
for _, toggle := range list {
|
||||
if SegmentType(toggle) == segment.Type || toggle == segment.Alias {
|
||||
|
|
|
@ -4,14 +4,15 @@ import (
|
|||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/color"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/segments"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -22,7 +23,7 @@ func TestMapSegmentWriterCanMap(t *testing.T) {
|
|||
sc := &Segment{
|
||||
Type: SESSION,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
err := sc.MapSegmentWithWriter(env)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, sc.writer)
|
||||
|
@ -32,7 +33,7 @@ func TestMapSegmentWriterCannotMap(t *testing.T) {
|
|||
sc := &Segment{
|
||||
Type: "nilwriter",
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
err := sc.MapSegmentWithWriter(env)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
@ -72,7 +73,7 @@ func TestShouldIncludeFolder(t *testing.T) {
|
|||
{Case: "!Include & !Exclude", Included: false, Excluded: false, Expected: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return(runtime.LINUX)
|
||||
env.On("Home").Return("")
|
||||
env.On("Pwd").Return(cwd)
|
||||
|
@ -143,9 +144,9 @@ func TestGetColors(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := new(mock.Environment)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
|
||||
|
|
|
@ -319,7 +319,6 @@ func (segment *Segment) MapSegmentWithWriter(env runtime.Environment) error {
|
|||
writer := f()
|
||||
wrapper := &properties.Wrapper{
|
||||
Properties: segment.Properties,
|
||||
Env: env,
|
||||
}
|
||||
writer.Init(wrapper, env)
|
||||
segment.writer = writer
|
||||
|
|
|
@ -8,10 +8,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
httplib "net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/net"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
)
|
||||
|
||||
func Download(fontPath string) ([]byte, error) {
|
||||
|
@ -33,22 +33,22 @@ func Download(fontPath string) ([]byte, error) {
|
|||
}
|
||||
|
||||
func isZipFile(data []byte) bool {
|
||||
contentType := http.DetectContentType(data)
|
||||
contentType := httplib.DetectContentType(data)
|
||||
return contentType == "application/zip"
|
||||
}
|
||||
|
||||
func getRemoteFile(location string) (data []byte, err error) {
|
||||
req, err := http.NewRequestWithContext(context.Background(), "GET", location, nil)
|
||||
req, err := httplib.NewRequestWithContext(context.Background(), "GET", location, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := net.HTTPClient.Do(req)
|
||||
resp, err := http.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode != httplib.StatusOK {
|
||||
return data, fmt.Errorf("Failed to download zip file: %s\n→ %s", resp.Status, location)
|
||||
}
|
||||
|
||||
|
|
|
@ -5,12 +5,12 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
httplib "net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/net"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
)
|
||||
|
||||
type release struct {
|
||||
|
@ -51,14 +51,14 @@ func fetchFontAssets(repo string) ([]*Asset, error) {
|
|||
defer cancelF()
|
||||
|
||||
repoURL := "https://api.github.com/repos/" + repo + "/releases/latest"
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", repoURL, nil)
|
||||
req, err := httplib.NewRequestWithContext(ctx, "GET", repoURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "application/vnd.github.v3+json")
|
||||
response, err := net.HTTPClient.Do(req)
|
||||
if err != nil || response.StatusCode != http.StatusOK {
|
||||
response, err := http.HTTPClient.Do(req)
|
||||
if err != nil || response.StatusCode != httplib.StatusOK {
|
||||
return nil, fmt.Errorf("failed to get %s release", repo)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
props properties.Properties
|
||||
env runtime.Environment
|
||||
}
|
||||
|
||||
func (r *Request) Init(env runtime.Environment, props properties.Properties) {
|
||||
r.env = env
|
||||
r.props = props
|
||||
}
|
||||
|
||||
func Do[a any](r *Request, url string, requestModifiers ...runtime.HTTPRequestModifier) (a, error) {
|
||||
if data, err := getCacheValue[a](r, url); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
return do[a](r, url, nil, requestModifiers...)
|
||||
}
|
||||
|
||||
func getCacheValue[a any](r *Request, key string) (a, error) {
|
||||
var data a
|
||||
cacheTimeout := r.props.GetInt(properties.CacheTimeout, 30)
|
||||
if cacheTimeout <= 0 {
|
||||
return data, errors.New("no cache needed")
|
||||
}
|
||||
if val, found := r.env.Cache().Get(key); found {
|
||||
err := json.Unmarshal([]byte(val), &data)
|
||||
if err != nil {
|
||||
r.env.Error(err)
|
||||
return data, err
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
err := errors.New("no data in cache")
|
||||
r.env.Error(err)
|
||||
return data, err
|
||||
}
|
||||
|
||||
func do[a any](r *Request, url string, body io.Reader, requestModifiers ...runtime.HTTPRequestModifier) (a, error) {
|
||||
var data a
|
||||
httpTimeout := r.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
|
||||
responseBody, err := r.env.HTTPRequest(url, body, httpTimeout, requestModifiers...)
|
||||
if err != nil {
|
||||
r.env.Error(err)
|
||||
return data, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(responseBody, &data)
|
||||
if err != nil {
|
||||
r.env.Error(err)
|
||||
return data, err
|
||||
}
|
||||
|
||||
cacheTimeout := r.props.GetInt(properties.CacheTimeout, 30)
|
||||
if cacheTimeout > 0 {
|
||||
r.env.Cache().Set(url, string(responseBody), cacheTimeout)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
|
@ -1,293 +0,0 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/battery"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockedEnvironment struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Getenv(key string) string {
|
||||
args := env.Called(key)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Pwd() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Home() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HasFiles(pattern string) bool {
|
||||
args := env.Called(pattern)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HasFilesInDir(dir, pattern string) bool {
|
||||
args := env.Called(dir, pattern)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HasFolder(folder string) bool {
|
||||
args := env.Called(folder)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) ResolveSymlink(path string) (string, error) {
|
||||
args := env.Called(path)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) FileContent(file string) string {
|
||||
args := env.Called(file)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) LsDir(path string) []fs.DirEntry {
|
||||
args := env.Called(path)
|
||||
return args.Get(0).([]fs.DirEntry)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) PathSeparator() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) User() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Host() (string, error) {
|
||||
args := env.Called()
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) GOOS() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Platform() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) CommandPath(command string) string {
|
||||
args := env.Called(command)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HasCommand(command string) bool {
|
||||
args := env.Called(command)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) RunCommand(command string, args ...string) (string, error) {
|
||||
arguments := env.Called(command, args)
|
||||
return arguments.String(0), arguments.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) RunShellCommand(shell, command string) string {
|
||||
args := env.Called(shell, command)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) StatusCodes() (int, string) {
|
||||
args := env.Called()
|
||||
return args.Int(0), args.String(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) ExecutionTime() float64 {
|
||||
args := env.Called()
|
||||
return float64(args.Int(0))
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Root() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Flags() *runtime.Flags {
|
||||
arguments := env.Called()
|
||||
return arguments.Get(0).(*runtime.Flags)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) BatteryState() (*battery.Info, error) {
|
||||
args := env.Called()
|
||||
return args.Get(0).(*battery.Info), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Shell() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) QueryWindowTitles(processName, windowTitleRegex string) (string, error) {
|
||||
args := env.Called(processName, windowTitleRegex)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) WindowsRegistryKeyValue(path string) (*runtime.WindowsRegistryValue, error) {
|
||||
args := env.Called(path)
|
||||
return args.Get(0).(*runtime.WindowsRegistryValue), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HTTPRequest(url string, _ io.Reader, _ int, _ ...runtime.HTTPRequestModifier) ([]byte, error) {
|
||||
args := env.Called(url)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HasParentFilePath(path string) (*runtime.FileInfo, error) {
|
||||
args := env.Called(path)
|
||||
return args.Get(0).(*runtime.FileInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) StackCount() int {
|
||||
args := env.Called()
|
||||
return args.Int(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) IsWsl() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) IsWsl2() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) TerminalWidth() (int, error) {
|
||||
args := env.Called()
|
||||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) CachePath() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Cache() runtime.Cache {
|
||||
args := env.Called()
|
||||
return args.Get(0).(runtime.Cache)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Close() {
|
||||
_ = env.Called()
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Logs() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) InWSLSharedDrive() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) ConvertToWindowsPath(_ string) string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) ConvertToLinuxPath(_ string) string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Connection(connectionType runtime.ConnectionType) (*runtime.Connection, error) {
|
||||
args := env.Called(connectionType)
|
||||
return args.Get(0).(*runtime.Connection), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) TemplateCache() *runtime.TemplateCache {
|
||||
args := env.Called()
|
||||
return args.Get(0).(*runtime.TemplateCache)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) LoadTemplateCache() {
|
||||
_ = env.Called()
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) MockGitCommand(dir, returnValue string, args ...string) {
|
||||
args = append([]string{"-C", dir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
||||
env.On("RunCommand", "git", args).Return(returnValue, nil)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) MockHgCommand(dir, returnValue string, args ...string) {
|
||||
args = append([]string{"-R", dir}, args...)
|
||||
env.On("RunCommand", "hg", args).Return(returnValue, nil)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) MockSvnCommand(dir, returnValue string, args ...string) {
|
||||
args = append([]string{"-C", dir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
||||
env.On("RunCommand", "svn", args).Return(returnValue, nil)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HasFileInParentDirs(pattern string, depth uint) bool {
|
||||
args := env.Called(pattern, depth)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) DirMatchesOneOf(dir string, regexes []string) bool {
|
||||
args := env.Called(dir, regexes)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Trace(start time.Time, args ...string) {
|
||||
_ = env.Called(start, args)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Debug(message string) {
|
||||
_ = env.Called(message)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) DebugF(format string, a ...any) {
|
||||
_ = env.Called(format, a)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Error(err error) {
|
||||
_ = env.Called(err)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) DirIsWritable(path string) bool {
|
||||
args := env.Called(path)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) SetPromptCount() {
|
||||
_ = env.Called()
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) CursorPosition() (int, int) {
|
||||
args := env.Called()
|
||||
return args.Int(0), args.Int(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) SystemInfo() (*runtime.SystemInfo, error) {
|
||||
args := env.Called()
|
||||
return args.Get(0).(*runtime.SystemInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Unset(name string) {
|
||||
for i := 0; i < len(env.ExpectedCalls); i++ {
|
||||
f := env.ExpectedCalls[i]
|
||||
if f.Method == name {
|
||||
f.Unset()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,14 +4,15 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/config"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/shell"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/terminal"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestCanWriteRPrompt(t *testing.T) {
|
||||
|
@ -33,7 +34,7 @@ func TestCanWriteRPrompt(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("TerminalWidth").Return(tc.TerminalWidth, tc.TerminalWidthError)
|
||||
engine := &Engine{
|
||||
Env: env,
|
||||
|
@ -72,7 +73,7 @@ func TestPrintPWD(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
if len(tc.Pwd) == 0 {
|
||||
tc.Pwd = "pwd"
|
||||
}
|
||||
|
@ -80,8 +81,8 @@ func TestPrintPWD(t *testing.T) {
|
|||
env.On("Shell").Return(tc.Shell)
|
||||
env.On("User").Return("user")
|
||||
env.On("Host").Return("host", nil)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
Shell: "shell",
|
||||
})
|
||||
|
@ -170,12 +171,12 @@ func TestGetTitle(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Pwd").Return(tc.Cwd)
|
||||
env.On("Home").Return("/usr/home")
|
||||
env.On("PathSeparator").Return(tc.PathSeparator)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{
|
||||
"USERDOMAIN": "MyCompany",
|
||||
},
|
||||
|
@ -234,11 +235,11 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Pwd").Return(tc.Cwd)
|
||||
env.On("Home").Return("/usr/home")
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{
|
||||
"USERDOMAIN": "MyCompany",
|
||||
},
|
||||
|
|
|
@ -4,58 +4,57 @@ import (
|
|||
"fmt"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/color"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/log"
|
||||
)
|
||||
|
||||
type Wrapper struct {
|
||||
Properties Map
|
||||
Env runtime.Environment
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetColor(property Property, defaultColor color.Ansi) color.Ansi {
|
||||
value := w.Properties.GetColor(property, defaultColor)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %s", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %s", property, value))
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetBool(property Property, defaultValue bool) bool {
|
||||
value := w.Properties.GetBool(property, defaultValue)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %t", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %t", property, value))
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetString(property Property, defaultValue string) string {
|
||||
value := w.Properties.GetString(property, defaultValue)
|
||||
w.Env.Debug(value)
|
||||
log.Debug(value)
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetFloat64(property Property, defaultValue float64) float64 {
|
||||
value := w.Properties.GetFloat64(property, defaultValue)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %f", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %f", property, value))
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetInt(property Property, defaultValue int) int {
|
||||
value := w.Properties.GetInt(property, defaultValue)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %d", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %d", property, value))
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
|
||||
value := w.Properties.GetKeyValueMap(property, defaultValue)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) GetStringArray(property Property, defaultValue []string) []string {
|
||||
value := w.Properties.GetStringArray(property, defaultValue)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||
return value
|
||||
}
|
||||
|
||||
func (w *Wrapper) Get(property Property, defaultValue any) any {
|
||||
value := w.Properties.Get(property, defaultValue)
|
||||
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||
log.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||
return value
|
||||
}
|
||||
|
|
|
@ -1,172 +0,0 @@
|
|||
package runtime
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cache interface {
|
||||
Init(home string)
|
||||
Close()
|
||||
// Gets the value for a given key.
|
||||
// Returns the value and a boolean indicating if the key was found.
|
||||
// In case the ttl expired, the function returns false.
|
||||
Get(key string) (string, bool)
|
||||
// Sets a value for a given key.
|
||||
// The ttl indicates how many minutes to cache the value.
|
||||
Set(key, value string, ttl int)
|
||||
// Deletes a key from the cache.
|
||||
Delete(key string)
|
||||
}
|
||||
|
||||
const (
|
||||
CacheFile = "/omp.cache"
|
||||
)
|
||||
|
||||
func pid() string {
|
||||
pid := os.Getenv("POSH_PID")
|
||||
if len(pid) == 0 {
|
||||
pid = strconv.Itoa(os.Getppid())
|
||||
}
|
||||
return pid
|
||||
}
|
||||
|
||||
var (
|
||||
TEMPLATECACHE = fmt.Sprintf("template_cache_%s", pid())
|
||||
TOGGLECACHE = fmt.Sprintf("toggle_cache_%s", pid())
|
||||
PROMPTCOUNTCACHE = fmt.Sprintf("prompt_count_cache_%s", pid())
|
||||
)
|
||||
|
||||
type cacheObject struct {
|
||||
Value string `json:"value"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
TTL int `json:"ttl"`
|
||||
}
|
||||
|
||||
func (c *cacheObject) expired() bool {
|
||||
if c.TTL < 0 {
|
||||
return false
|
||||
}
|
||||
return time.Now().Unix() >= (c.Timestamp + int64(c.TTL)*60)
|
||||
}
|
||||
|
||||
type fileCache struct {
|
||||
cache *ConcurrentMap
|
||||
cachePath string
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (fc *fileCache) Init(cachePath string) {
|
||||
fc.cache = NewConcurrentMap()
|
||||
fc.cachePath = cachePath
|
||||
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
|
||||
content, err := os.ReadFile(cacheFilePath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var list map[string]*cacheObject
|
||||
if err := json.Unmarshal(content, &list); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for key, co := range list {
|
||||
if co.expired() {
|
||||
continue
|
||||
}
|
||||
fc.cache.Set(key, co)
|
||||
}
|
||||
}
|
||||
|
||||
func (fc *fileCache) Close() {
|
||||
if !fc.dirty {
|
||||
return
|
||||
}
|
||||
cache := fc.cache.SimpleMap()
|
||||
if dump, err := json.MarshalIndent(cache, "", " "); err == nil {
|
||||
cacheFilePath := filepath.Join(fc.cachePath, CacheFile)
|
||||
_ = os.WriteFile(cacheFilePath, dump, 0644)
|
||||
}
|
||||
}
|
||||
|
||||
// returns the value for the given key as long as
|
||||
// the TTL (minutes) is not expired
|
||||
func (fc *fileCache) Get(key string) (string, bool) {
|
||||
val, found := fc.cache.Get(key)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
if co, ok := val.(*cacheObject); ok {
|
||||
return co.Value, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// sets the value for the given key with a TTL (minutes)
|
||||
func (fc *fileCache) Set(key, value string, ttl int) {
|
||||
fc.cache.Set(key, &cacheObject{
|
||||
Value: value,
|
||||
Timestamp: time.Now().Unix(),
|
||||
TTL: ttl,
|
||||
})
|
||||
fc.dirty = true
|
||||
}
|
||||
|
||||
// delete the key from the cache
|
||||
func (fc *fileCache) Delete(key string) {
|
||||
fc.cache.Delete(key)
|
||||
fc.dirty = true
|
||||
}
|
||||
|
||||
type commandCache struct {
|
||||
commands *ConcurrentMap
|
||||
}
|
||||
|
||||
func (c *commandCache) set(command, path string) {
|
||||
c.commands.Set(command, path)
|
||||
}
|
||||
|
||||
func (c *commandCache) get(command string) (string, bool) {
|
||||
cacheCommand, found := c.commands.Get(command)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
command, ok := cacheCommand.(string)
|
||||
return command, ok
|
||||
}
|
||||
|
||||
type TemplateCache struct {
|
||||
Root bool
|
||||
PWD string
|
||||
AbsolutePWD string
|
||||
Folder string
|
||||
Shell string
|
||||
ShellVersion string
|
||||
UserName string
|
||||
HostName string
|
||||
Code int
|
||||
Env map[string]string
|
||||
Var SimpleMap
|
||||
OS string
|
||||
WSL bool
|
||||
PromptCount int
|
||||
SHLVL int
|
||||
Segments *ConcurrentMap
|
||||
SegmentsCache SimpleMap
|
||||
|
||||
initialized bool
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (t *TemplateCache) AddSegmentData(key string, value any) {
|
||||
t.Segments.Set(key, value)
|
||||
}
|
||||
|
||||
func (t *TemplateCache) RemoveSegmentData(key string) {
|
||||
t.Segments.Delete(key)
|
||||
}
|
|
@ -4,14 +4,14 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
httplib "net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/log"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/net"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
)
|
||||
|
||||
func Download(cachePath, url string) (string, error) {
|
||||
|
@ -27,13 +27,13 @@ func Download(cachePath, url string) (string, error) {
|
|||
ctx, cncl := context.WithTimeout(context.Background(), time.Second*time.Duration(5))
|
||||
defer cncl()
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
request, err := httplib.NewRequestWithContext(ctx, httplib.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
response, err := net.HTTPClient.Do(request)
|
||||
response, err := http.HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return "", err
|
||||
|
@ -41,7 +41,7 @@ func Download(cachePath, url string) (string, error) {
|
|||
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
if response.StatusCode != httplib.StatusOK {
|
||||
err := fmt.Errorf("unexpected status code: %d", response.StatusCode)
|
||||
log.Error(err)
|
||||
return "", err
|
||||
|
@ -77,13 +77,13 @@ func shouldUpdate(cachePath, url string) (string, bool) {
|
|||
ctx, cncl := context.WithTimeout(context.Background(), time.Second*time.Duration(5))
|
||||
defer cncl()
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
|
||||
request, err := httplib.NewRequestWithContext(ctx, httplib.MethodHead, url, nil)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return "", true
|
||||
}
|
||||
|
||||
response, err := net.HTTPClient.Do(request)
|
||||
response, err := http.HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return "", true
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package net
|
||||
package http
|
||||
|
||||
import (
|
||||
"net"
|
|
@ -4,10 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
httplib "net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -37,36 +34,46 @@ type OAuthRequest struct {
|
|||
AccessTokenKey string
|
||||
RefreshTokenKey string
|
||||
SegmentName string
|
||||
|
||||
RefreshToken string
|
||||
AccessToken string
|
||||
}
|
||||
|
||||
func (o *OAuthRequest) getAccessToken() (string, error) {
|
||||
// get directly from cache
|
||||
if acccessToken, OK := o.env.Cache().Get(o.AccessTokenKey); OK && len(acccessToken) != 0 {
|
||||
if acccessToken, OK := o.Env.Cache().Get(o.AccessTokenKey); OK && len(acccessToken) != 0 {
|
||||
return acccessToken, nil
|
||||
}
|
||||
|
||||
// use cached refresh token to get new access token
|
||||
if refreshToken, OK := o.env.Cache().Get(o.RefreshTokenKey); OK && len(refreshToken) != 0 {
|
||||
if refreshToken, OK := o.Env.Cache().Get(o.RefreshTokenKey); OK && len(refreshToken) != 0 {
|
||||
if acccessToken, err := o.refreshToken(refreshToken); err == nil {
|
||||
return acccessToken, nil
|
||||
}
|
||||
}
|
||||
|
||||
// use initial refresh token from property
|
||||
refreshToken := o.props.GetString(properties.RefreshToken, "")
|
||||
// refreshToken := o.props.GetString(properties.RefreshToken, "")
|
||||
// ignore an empty or default refresh token
|
||||
if len(refreshToken) == 0 || refreshToken == DefaultRefreshToken {
|
||||
if len(o.RefreshToken) == 0 || o.RefreshToken == DefaultRefreshToken {
|
||||
return "", &OAuthError{
|
||||
message: InvalidRefreshToken,
|
||||
}
|
||||
}
|
||||
|
||||
// no need to let the user provide access token, we'll always verify the refresh token
|
||||
acccessToken, err := o.refreshToken(refreshToken)
|
||||
acccessToken, err := o.refreshToken(o.RefreshToken)
|
||||
return acccessToken, err
|
||||
}
|
||||
|
||||
func (o *OAuthRequest) refreshToken(refreshToken string) (string, error) {
|
||||
httpTimeout := o.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
// httpTimeout := o.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
if o.HTTPTimeout == 0 {
|
||||
o.HTTPTimeout = 20
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("https://ohmyposh.dev/api/refresh?segment=%s&token=%s", o.SegmentName, refreshToken)
|
||||
body, err := o.env.HTTPRequest(url, nil, httpTimeout)
|
||||
body, err := o.Env.HTTPRequest(url, nil, o.HTTPTimeout)
|
||||
if err != nil {
|
||||
return "", &OAuthError{
|
||||
// This might happen if /api was asleep. Assume the user will just retry
|
||||
|
@ -81,12 +88,12 @@ func (o *OAuthRequest) refreshToken(refreshToken string) (string, error) {
|
|||
}
|
||||
}
|
||||
// add tokens to cache
|
||||
o.env.Cache().Set(o.AccessTokenKey, tokens.AccessToken, tokens.ExpiresIn/60)
|
||||
o.env.Cache().Set(o.RefreshTokenKey, tokens.RefreshToken, 2*525960) // it should never expire unless revoked, default to 2 year
|
||||
o.Env.Cache().Set(o.AccessTokenKey, tokens.AccessToken, tokens.ExpiresIn/60)
|
||||
o.Env.Cache().Set(o.RefreshTokenKey, tokens.RefreshToken, 2*525960) // it should never expire unless revoked, default to 2 year
|
||||
return tokens.AccessToken, nil
|
||||
}
|
||||
|
||||
func OauthResult[a any](o *OAuthRequest, url string, body io.Reader, requestModifiers ...runtime.HTTPRequestModifier) (a, error) {
|
||||
func OauthResult[a any](o *OAuthRequest, url string, body io.Reader, requestModifiers ...RequestModifier) (a, error) {
|
||||
if data, err := getCacheValue[a](&o.Request, url); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
|
@ -98,12 +105,12 @@ func OauthResult[a any](o *OAuthRequest, url string, body io.Reader, requestModi
|
|||
}
|
||||
|
||||
// add token to header for authentication
|
||||
addAuthHeader := func(request *http.Request) {
|
||||
addAuthHeader := func(request *httplib.Request) {
|
||||
request.Header.Add("Authorization", "Bearer "+accessToken)
|
||||
}
|
||||
|
||||
if requestModifiers == nil {
|
||||
requestModifiers = []runtime.HTTPRequestModifier{}
|
||||
requestModifiers = []RequestModifier{}
|
||||
}
|
||||
|
||||
requestModifiers = append(requestModifiers, addAuthHeader)
|
|
@ -4,11 +4,9 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type data struct {
|
||||
|
@ -143,32 +141,32 @@ func TestOauthResult(t *testing.T) {
|
|||
url := "https://www.strava.com/api/v3/athlete/activities?page=1&per_page=1"
|
||||
tokenURL := fmt.Sprintf("https://ohmyposh.dev/api/refresh?segment=test&token=%s", tc.RefreshToken)
|
||||
|
||||
var props properties.Map = map[properties.Property]any{
|
||||
properties.CacheTimeout: tc.CacheTimeout,
|
||||
properties.AccessToken: tc.AccessToken,
|
||||
properties.RefreshToken: tc.RefreshToken,
|
||||
}
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache := &mock.Cache{}
|
||||
|
||||
cache.On("Get", url).Return(tc.CacheJSONResponse, !tc.ResponseCacheMiss)
|
||||
cache.On("Get", accessTokenKey).Return(tc.AccessToken, tc.AccessTokenFromCache)
|
||||
cache.On("Get", refreshTokenKey).Return(tc.RefreshToken, tc.RefreshTokenFromCache)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
cache.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &MockedEnvironment{}
|
||||
|
||||
env.On("Cache").Return(cache)
|
||||
env.On("HTTPRequest", url).Return([]byte(tc.JSONResponse), tc.Error)
|
||||
env.On("HTTPRequest", tokenURL).Return([]byte(tc.TokenResponse), tc.Error)
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
|
||||
oauth := &OAuthRequest{
|
||||
AccessTokenKey: accessTokenKey,
|
||||
RefreshTokenKey: refreshTokenKey,
|
||||
SegmentName: "test",
|
||||
AccessToken: tc.AccessToken,
|
||||
RefreshToken: tc.RefreshToken,
|
||||
Request: Request{
|
||||
Env: env,
|
||||
CacheTimeout: tc.CacheTimeout,
|
||||
HTTPTimeout: 20,
|
||||
},
|
||||
}
|
||||
oauth.Init(env, props)
|
||||
|
||||
got, err := OauthResult[*data](oauth, url, nil)
|
||||
assert.Equal(t, tc.ExpectedData, got, tc.Case)
|
78
src/runtime/http/request.go
Normal file
78
src/runtime/http/request.go
Normal file
|
@ -0,0 +1,78 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/log"
|
||||
)
|
||||
|
||||
type RequestModifier func(request *http.Request)
|
||||
|
||||
type Request struct {
|
||||
Env Environment
|
||||
CacheTimeout int
|
||||
HTTPTimeout int
|
||||
}
|
||||
|
||||
type Environment interface {
|
||||
HTTPRequest(url string, body io.Reader, timeout int, requestModifiers ...RequestModifier) ([]byte, error)
|
||||
Cache() cache.Cache
|
||||
}
|
||||
|
||||
func Do[a any](r *Request, url string, requestModifiers ...RequestModifier) (a, error) {
|
||||
if data, err := getCacheValue[a](r, url); err == nil {
|
||||
return data, nil
|
||||
}
|
||||
return do[a](r, url, nil, requestModifiers...)
|
||||
}
|
||||
|
||||
func getCacheValue[a any](r *Request, key string) (a, error) {
|
||||
var data a
|
||||
cacheTimeout := r.CacheTimeout // r.props.GetInt(properties.CacheTimeout, 30)
|
||||
if cacheTimeout <= 0 {
|
||||
return data, errors.New("no cache needed")
|
||||
}
|
||||
|
||||
if val, found := r.Env.Cache().Get(key); found {
|
||||
err := json.Unmarshal([]byte(val), &data)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return data, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
err := errors.New("no data in cache")
|
||||
log.Error(err)
|
||||
|
||||
return data, err
|
||||
}
|
||||
|
||||
func do[a any](r *Request, url string, body io.Reader, requestModifiers ...RequestModifier) (a, error) {
|
||||
var data a
|
||||
httpTimeout := r.HTTPTimeout // r.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
|
||||
responseBody, err := r.Env.HTTPRequest(url, body, httpTimeout, requestModifiers...)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return data, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(responseBody, &data)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return data, err
|
||||
}
|
||||
|
||||
cacheTimeout := r.CacheTimeout // r.props.GetInt(properties.CacheTimeout, 30)
|
||||
if cacheTimeout > 0 {
|
||||
r.Env.Cache().Set(url, string(responseBody), cacheTimeout)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
|
@ -1,16 +1,31 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type MockedEnvironment struct {
|
||||
testify_.Mock
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) Cache() cache.Cache {
|
||||
args := env.Called()
|
||||
return args.Get(0).(cache.Cache)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) HTTPRequest(url string, _ io.Reader, _ int, _ ...RequestModifier) ([]byte, error) {
|
||||
args := env.Called(url)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func TestRequestResult(t *testing.T) {
|
||||
successData := &data{Hello: "world"}
|
||||
jsonResponse := `{ "hello":"world" }`
|
||||
|
@ -62,23 +77,21 @@ func TestRequestResult(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var props properties.Map = map[properties.Property]any{
|
||||
properties.CacheTimeout: tc.CacheTimeout,
|
||||
}
|
||||
c := &mock.Cache{}
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
c.On("Get", url).Return(tc.CacheJSONResponse, !tc.ResponseCacheMiss)
|
||||
c.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
|
||||
cache.On("Get", url).Return(tc.CacheJSONResponse, !tc.ResponseCacheMiss)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
env := &MockedEnvironment{}
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
|
||||
env.On("Cache").Return(cache)
|
||||
env.On("Cache").Return(c)
|
||||
env.On("HTTPRequest", url).Return([]byte(tc.JSONResponse), tc.Error)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
|
||||
request := &Request{}
|
||||
request.Init(env, props)
|
||||
request := &Request{
|
||||
Env: env,
|
||||
CacheTimeout: tc.CacheTimeout,
|
||||
HTTPTimeout: 0,
|
||||
}
|
||||
|
||||
got, err := Do[*data](request, url, nil)
|
||||
assert.Equal(t, tc.ExpectedData, got, tc.Case)
|
|
@ -1,46 +0,0 @@
|
|||
package runtime
|
||||
|
||||
import "sync"
|
||||
|
||||
func NewConcurrentMap() *ConcurrentMap {
|
||||
var cm ConcurrentMap
|
||||
return &cm
|
||||
}
|
||||
|
||||
type ConcurrentMap sync.Map
|
||||
|
||||
func (cm *ConcurrentMap) Set(key string, value any) {
|
||||
(*sync.Map)(cm).Store(key, value)
|
||||
}
|
||||
|
||||
func (cm *ConcurrentMap) Get(key string) (any, bool) {
|
||||
return (*sync.Map)(cm).Load(key)
|
||||
}
|
||||
|
||||
func (cm *ConcurrentMap) Delete(key string) {
|
||||
(*sync.Map)(cm).Delete(key)
|
||||
}
|
||||
|
||||
func (cm *ConcurrentMap) Contains(key string) bool {
|
||||
_, ok := (*sync.Map)(cm).Load(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (cm *ConcurrentMap) SimpleMap() SimpleMap {
|
||||
list := make(map[string]any)
|
||||
(*sync.Map)(cm).Range(func(key, value any) bool {
|
||||
list[key.(string)] = value
|
||||
return true
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
type SimpleMap map[string]any
|
||||
|
||||
func (m SimpleMap) ConcurrentMap() *ConcurrentMap {
|
||||
var cm ConcurrentMap
|
||||
for k, v := range m {
|
||||
cm.Set(k, v)
|
||||
}
|
||||
return &cm
|
||||
}
|
295
src/runtime/mock/environment.go
Normal file
295
src/runtime/mock/environment.go
Normal file
|
@ -0,0 +1,295 @@
|
|||
package mock
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/battery"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type Environment struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (env *Environment) Getenv(key string) string {
|
||||
args := env.Called(key)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Pwd() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Home() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) HasFiles(pattern string) bool {
|
||||
args := env.Called(pattern)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) HasFilesInDir(dir, pattern string) bool {
|
||||
args := env.Called(dir, pattern)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) HasFolder(folder string) bool {
|
||||
args := env.Called(folder)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) ResolveSymlink(path string) (string, error) {
|
||||
args := env.Called(path)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) FileContent(file string) string {
|
||||
args := env.Called(file)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) LsDir(path string) []fs.DirEntry {
|
||||
args := env.Called(path)
|
||||
return args.Get(0).([]fs.DirEntry)
|
||||
}
|
||||
|
||||
func (env *Environment) PathSeparator() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) User() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Host() (string, error) {
|
||||
args := env.Called()
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) GOOS() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Platform() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) CommandPath(command string) string {
|
||||
args := env.Called(command)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) HasCommand(command string) bool {
|
||||
args := env.Called(command)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) RunCommand(command string, args ...string) (string, error) {
|
||||
arguments := env.Called(command, args)
|
||||
return arguments.String(0), arguments.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) RunShellCommand(shell, command string) string {
|
||||
args := env.Called(shell, command)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) StatusCodes() (int, string) {
|
||||
args := env.Called()
|
||||
return args.Int(0), args.String(1)
|
||||
}
|
||||
|
||||
func (env *Environment) ExecutionTime() float64 {
|
||||
args := env.Called()
|
||||
return float64(args.Int(0))
|
||||
}
|
||||
|
||||
func (env *Environment) Root() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Flags() *runtime.Flags {
|
||||
arguments := env.Called()
|
||||
return arguments.Get(0).(*runtime.Flags)
|
||||
}
|
||||
|
||||
func (env *Environment) BatteryState() (*battery.Info, error) {
|
||||
args := env.Called()
|
||||
return args.Get(0).(*battery.Info), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) Shell() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) QueryWindowTitles(processName, windowTitleRegex string) (string, error) {
|
||||
args := env.Called(processName, windowTitleRegex)
|
||||
return args.String(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) WindowsRegistryKeyValue(path string) (*runtime.WindowsRegistryValue, error) {
|
||||
args := env.Called(path)
|
||||
return args.Get(0).(*runtime.WindowsRegistryValue), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) HTTPRequest(url string, _ io.Reader, _ int, _ ...http.RequestModifier) ([]byte, error) {
|
||||
args := env.Called(url)
|
||||
return args.Get(0).([]byte), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) HasParentFilePath(path string) (*runtime.FileInfo, error) {
|
||||
args := env.Called(path)
|
||||
return args.Get(0).(*runtime.FileInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) StackCount() int {
|
||||
args := env.Called()
|
||||
return args.Int(0)
|
||||
}
|
||||
|
||||
func (env *Environment) IsWsl() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) IsWsl2() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) TerminalWidth() (int, error) {
|
||||
args := env.Called()
|
||||
return args.Int(0), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) CachePath() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Cache() cache.Cache {
|
||||
args := env.Called()
|
||||
return args.Get(0).(cache.Cache)
|
||||
}
|
||||
|
||||
func (env *Environment) Close() {
|
||||
_ = env.Called()
|
||||
}
|
||||
|
||||
func (env *Environment) Logs() string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) InWSLSharedDrive() bool {
|
||||
args := env.Called()
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) ConvertToWindowsPath(_ string) string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) ConvertToLinuxPath(_ string) string {
|
||||
args := env.Called()
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Connection(connectionType runtime.ConnectionType) (*runtime.Connection, error) {
|
||||
args := env.Called(connectionType)
|
||||
return args.Get(0).(*runtime.Connection), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) TemplateCache() *cache.Template {
|
||||
args := env.Called()
|
||||
return args.Get(0).(*cache.Template)
|
||||
}
|
||||
|
||||
func (env *Environment) LoadTemplateCache() {
|
||||
_ = env.Called()
|
||||
}
|
||||
|
||||
func (env *Environment) MockGitCommand(dir, returnValue string, args ...string) {
|
||||
args = append([]string{"-C", dir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
||||
env.On("RunCommand", "git", args).Return(returnValue, nil)
|
||||
}
|
||||
|
||||
func (env *Environment) MockHgCommand(dir, returnValue string, args ...string) {
|
||||
args = append([]string{"-R", dir}, args...)
|
||||
env.On("RunCommand", "hg", args).Return(returnValue, nil)
|
||||
}
|
||||
|
||||
func (env *Environment) MockSvnCommand(dir, returnValue string, args ...string) {
|
||||
args = append([]string{"-C", dir, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
||||
env.On("RunCommand", "svn", args).Return(returnValue, nil)
|
||||
}
|
||||
|
||||
func (env *Environment) HasFileInParentDirs(pattern string, depth uint) bool {
|
||||
args := env.Called(pattern, depth)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) DirMatchesOneOf(dir string, regexes []string) bool {
|
||||
args := env.Called(dir, regexes)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) Trace(start time.Time, args ...string) {
|
||||
_ = env.Called(start, args)
|
||||
}
|
||||
|
||||
func (env *Environment) Debug(message string) {
|
||||
_ = env.Called(message)
|
||||
}
|
||||
|
||||
func (env *Environment) DebugF(format string, a ...any) {
|
||||
_ = env.Called(format, a)
|
||||
}
|
||||
|
||||
func (env *Environment) Error(err error) {
|
||||
_ = env.Called(err)
|
||||
}
|
||||
|
||||
func (env *Environment) DirIsWritable(path string) bool {
|
||||
args := env.Called(path)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (env *Environment) SetPromptCount() {
|
||||
_ = env.Called()
|
||||
}
|
||||
|
||||
func (env *Environment) CursorPosition() (int, int) {
|
||||
args := env.Called()
|
||||
return args.Int(0), args.Int(1)
|
||||
}
|
||||
|
||||
func (env *Environment) SystemInfo() (*runtime.SystemInfo, error) {
|
||||
args := env.Called()
|
||||
return args.Get(0).(*runtime.SystemInfo), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *Environment) Unset(name string) {
|
||||
for i := 0; i < len(env.ExpectedCalls); i++ {
|
||||
f := env.ExpectedCalls[i]
|
||||
if f.Method == name {
|
||||
f.Unset()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
httplib "net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -17,12 +17,14 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/concurrent"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/log"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/regex"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/battery"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/cmd"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/config"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/net"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
|
||||
disk "github.com/shirou/gopsutil/v3/disk"
|
||||
load "github.com/shirou/gopsutil/v3/load"
|
||||
|
@ -77,8 +79,6 @@ type FileInfo struct {
|
|||
IsDir bool
|
||||
}
|
||||
|
||||
type HTTPRequestModifier func(request *http.Request)
|
||||
|
||||
type WindowsRegistryValueType string
|
||||
|
||||
const (
|
||||
|
@ -170,20 +170,20 @@ type Environment interface {
|
|||
BatteryState() (*battery.Info, error)
|
||||
QueryWindowTitles(processName, windowTitleRegex string) (string, error)
|
||||
WindowsRegistryKeyValue(path string) (*WindowsRegistryValue, error)
|
||||
HTTPRequest(url string, body io.Reader, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error)
|
||||
HTTPRequest(url string, body io.Reader, timeout int, requestModifiers ...http.RequestModifier) ([]byte, error)
|
||||
IsWsl() bool
|
||||
IsWsl2() bool
|
||||
StackCount() int
|
||||
TerminalWidth() (int, error)
|
||||
CachePath() string
|
||||
Cache() Cache
|
||||
Cache() cache.Cache
|
||||
Close()
|
||||
Logs() string
|
||||
InWSLSharedDrive() bool
|
||||
ConvertToLinuxPath(path string) string
|
||||
ConvertToWindowsPath(path string) string
|
||||
Connection(connectionType ConnectionType) (*Connection, error)
|
||||
TemplateCache() *TemplateCache
|
||||
TemplateCache() *cache.Template
|
||||
LoadTemplateCache()
|
||||
SetPromptCount()
|
||||
CursorPosition() (row, col int)
|
||||
|
@ -196,18 +196,18 @@ type Environment interface {
|
|||
|
||||
type Terminal struct {
|
||||
CmdFlags *Flags
|
||||
Var SimpleMap
|
||||
Var concurrent.SimpleMap
|
||||
|
||||
cwd string
|
||||
host string
|
||||
cmdCache *commandCache
|
||||
fileCache *fileCache
|
||||
tmplCache *TemplateCache
|
||||
cmdCache *cache.Command
|
||||
fileCache *cache.File
|
||||
tmplCache *cache.Template
|
||||
networks []*Connection
|
||||
|
||||
sync.RWMutex
|
||||
|
||||
lsDirMap ConcurrentMap
|
||||
lsDirMap concurrent.Map
|
||||
}
|
||||
|
||||
func (term *Terminal) Init() {
|
||||
|
@ -224,14 +224,14 @@ func (term *Terminal) Init() {
|
|||
log.Plain()
|
||||
}
|
||||
|
||||
term.fileCache = &fileCache{}
|
||||
term.fileCache = &cache.File{}
|
||||
term.fileCache.Init(term.CachePath())
|
||||
term.resolveConfigPath()
|
||||
term.cmdCache = &commandCache{
|
||||
commands: NewConcurrentMap(),
|
||||
term.cmdCache = &cache.Command{
|
||||
Commands: concurrent.NewMap(),
|
||||
}
|
||||
|
||||
term.tmplCache = &TemplateCache{}
|
||||
term.tmplCache = &cache.Template{}
|
||||
|
||||
term.SetPromptCount()
|
||||
}
|
||||
|
@ -501,7 +501,7 @@ func (term *Terminal) GOOS() string {
|
|||
|
||||
func (term *Terminal) RunCommand(command string, args ...string) (string, error) {
|
||||
defer term.Trace(time.Now(), append([]string{command}, args...)...)
|
||||
if cacheCommand, ok := term.cmdCache.get(command); ok {
|
||||
if cacheCommand, ok := term.cmdCache.Get(command); ok {
|
||||
command = cacheCommand
|
||||
}
|
||||
output, err := cmd.Run(command, args...)
|
||||
|
@ -522,14 +522,14 @@ func (term *Terminal) RunShellCommand(shell, command string) string {
|
|||
|
||||
func (term *Terminal) CommandPath(command string) string {
|
||||
defer term.Trace(time.Now(), command)
|
||||
if path, ok := term.cmdCache.get(command); ok {
|
||||
if path, ok := term.cmdCache.Get(command); ok {
|
||||
term.Debug(path)
|
||||
return path
|
||||
}
|
||||
|
||||
path, err := term.LookPath(command)
|
||||
if err == nil {
|
||||
term.cmdCache.set(command, path)
|
||||
term.cmdCache.Set(command, path)
|
||||
term.Debug(path)
|
||||
return path
|
||||
}
|
||||
|
@ -617,13 +617,13 @@ func (term *Terminal) unWrapError(err error) error {
|
|||
return cause
|
||||
}
|
||||
|
||||
func (term *Terminal) HTTPRequest(targetURL string, body io.Reader, timeout int, requestModifiers ...HTTPRequestModifier) ([]byte, error) {
|
||||
func (term *Terminal) HTTPRequest(targetURL string, body io.Reader, timeout int, requestModifiers ...http.RequestModifier) ([]byte, error) {
|
||||
defer term.Trace(time.Now(), targetURL)
|
||||
|
||||
ctx, cncl := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(timeout))
|
||||
defer cncl()
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, targetURL, body)
|
||||
request, err := httplib.NewRequestWithContext(ctx, httplib.MethodGet, targetURL, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -637,7 +637,7 @@ func (term *Terminal) HTTPRequest(targetURL string, body io.Reader, timeout int,
|
|||
term.Debug(string(dump))
|
||||
}
|
||||
|
||||
response, err := net.HTTPClient.Do(request)
|
||||
response, err := http.HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
term.Error(err)
|
||||
return nil, term.unWrapError(err)
|
||||
|
@ -697,7 +697,7 @@ func (term *Terminal) StackCount() int {
|
|||
return term.CmdFlags.StackCount
|
||||
}
|
||||
|
||||
func (term *Terminal) Cache() Cache {
|
||||
func (term *Terminal) Cache() cache.Cache {
|
||||
return term.fileCache
|
||||
}
|
||||
|
||||
|
@ -708,11 +708,13 @@ func (term *Terminal) saveTemplateCache() {
|
|||
if !canSave {
|
||||
return
|
||||
}
|
||||
cache := term.TemplateCache()
|
||||
cache.SegmentsCache = cache.Segments.SimpleMap()
|
||||
templateCache, err := json.Marshal(cache)
|
||||
|
||||
tmplCache := term.TemplateCache()
|
||||
tmplCache.SegmentsCache = tmplCache.Segments.ToSimpleMap()
|
||||
|
||||
templateCache, err := json.Marshal(tmplCache)
|
||||
if err == nil {
|
||||
term.fileCache.Set(TEMPLATECACHE, string(templateCache), 1440)
|
||||
term.fileCache.Set(cache.TEMPLATECACHE, string(templateCache), 1440)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -724,18 +726,23 @@ func (term *Terminal) Close() {
|
|||
|
||||
func (term *Terminal) LoadTemplateCache() {
|
||||
defer term.Trace(time.Now())
|
||||
val, OK := term.fileCache.Get(TEMPLATECACHE)
|
||||
|
||||
val, OK := term.fileCache.Get(cache.TEMPLATECACHE)
|
||||
if !OK {
|
||||
return
|
||||
}
|
||||
var tmplCache TemplateCache
|
||||
|
||||
var tmplCache cache.Template
|
||||
|
||||
err := json.Unmarshal([]byte(val), &tmplCache)
|
||||
if err != nil {
|
||||
term.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
tmplCache.Segments = tmplCache.SegmentsCache.ConcurrentMap()
|
||||
tmplCache.initialized = true
|
||||
tmplCache.Initialized = true
|
||||
|
||||
term.tmplCache = &tmplCache
|
||||
}
|
||||
|
||||
|
@ -743,13 +750,13 @@ func (term *Terminal) Logs() string {
|
|||
return log.String()
|
||||
}
|
||||
|
||||
func (term *Terminal) TemplateCache() *TemplateCache {
|
||||
func (term *Terminal) TemplateCache() *cache.Template {
|
||||
defer term.Trace(time.Now())
|
||||
tmplCache := term.tmplCache
|
||||
tmplCache.Lock()
|
||||
defer tmplCache.Unlock()
|
||||
|
||||
if tmplCache.initialized {
|
||||
if tmplCache.Initialized {
|
||||
return tmplCache
|
||||
}
|
||||
|
||||
|
@ -758,7 +765,7 @@ func (term *Terminal) TemplateCache() *TemplateCache {
|
|||
tmplCache.ShellVersion = term.CmdFlags.ShellVersion
|
||||
tmplCache.Code, _ = term.StatusCodes()
|
||||
tmplCache.WSL = term.IsWsl()
|
||||
tmplCache.Segments = NewConcurrentMap()
|
||||
tmplCache.Segments = concurrent.NewMap()
|
||||
tmplCache.PromptCount = term.CmdFlags.PromptCount
|
||||
tmplCache.Env = make(map[string]string)
|
||||
tmplCache.Var = make(map[string]any)
|
||||
|
@ -807,7 +814,7 @@ func (term *Terminal) TemplateCache() *TemplateCache {
|
|||
tmplCache.SHLVL = shlvl
|
||||
}
|
||||
|
||||
tmplCache.initialized = true
|
||||
tmplCache.Initialized = true
|
||||
return tmplCache
|
||||
}
|
||||
|
||||
|
@ -863,13 +870,13 @@ func (term *Terminal) SetPromptCount() {
|
|||
}
|
||||
}
|
||||
var count int
|
||||
if val, found := term.Cache().Get(PROMPTCOUNTCACHE); found {
|
||||
if val, found := term.Cache().Get(cache.PROMPTCOUNTCACHE); found {
|
||||
count, _ = strconv.Atoi(val)
|
||||
}
|
||||
// only write to cache if we're the primary prompt
|
||||
if term.CmdFlags.Primary {
|
||||
count++
|
||||
term.Cache().Set(PROMPTCOUNTCACHE, strconv.Itoa(count), 1440)
|
||||
term.Cache().Set(cache.PROMPTCOUNTCACHE, strconv.Itoa(count), 1440)
|
||||
}
|
||||
term.CmdFlags.PromptCount = count
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -32,7 +32,7 @@ func TestArgocdGetConfigFromOpts(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Getenv", argocdOptsEnv).Return(tc.Opts)
|
||||
|
||||
argocd := &Argocd{
|
||||
|
@ -57,7 +57,7 @@ func TestArgocdGetConfigPath(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(poshHome)
|
||||
env.On("Getenv", argocdOptsEnv).Return(tc.Opts)
|
||||
|
||||
|
@ -153,9 +153,9 @@ users:
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", configFile).Return(tc.Config)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
|
||||
argocd := &Argocd{
|
||||
env: env,
|
||||
|
@ -246,11 +246,11 @@ servers:
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(poshHome)
|
||||
env.On("Getenv", argocdOptsEnv).Return(tc.Opts)
|
||||
env.On("FileContent", configFile).Return(tc.Config)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
|
||||
argocd := &Argocd{
|
||||
env: env,
|
||||
|
|
|
@ -3,8 +3,8 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -51,7 +51,7 @@ func TestAWSSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Getenv", "AWS_VAULT").Return(tc.Vault)
|
||||
env.On("Getenv", "AWS_PROFILE").Return(tc.Profile)
|
||||
env.On("Getenv", "AWS_DEFAULT_PROFILE").Return(tc.DefaultProfile)
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -108,7 +108,7 @@ func TestAzSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(poshHome)
|
||||
var azureProfile, azureRmContext string
|
||||
|
||||
|
|
|
@ -6,11 +6,11 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestAzdSegment(t *testing.T) {
|
||||
|
@ -35,8 +35,8 @@ func TestAzdSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env := new(mock.Environment)
|
||||
env.On("Debug", testify_.Anything)
|
||||
|
||||
if tc.IsInited {
|
||||
fileInfo := &runtime.FileInfo{
|
||||
|
|
|
@ -5,8 +5,9 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -144,7 +145,7 @@ func TestBrewfatherSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
props := properties.Map{
|
||||
properties.CacheTimeout: tc.CacheTimeout,
|
||||
BFBatchID: BFFakeBatchID,
|
||||
|
@ -152,7 +153,7 @@ func TestBrewfatherSegment(t *testing.T) {
|
|||
BFUserID: "FAKE",
|
||||
}
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache := &cache_.Cache{}
|
||||
cache.On("Get", BFCacheKey).Return(nil, false) // cache testing later because cache is a little more complicated than just the single response.
|
||||
// cache.On("Set", BFCacheKey, tc.JSONResponse, tc.CacheTimeout).Return()
|
||||
|
||||
|
|
|
@ -5,11 +5,12 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -201,7 +202,7 @@ func TestCarbonIntensitySegmentSingle(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
var props = properties.Map{
|
||||
properties.HTTPTimeout: 5000,
|
||||
properties.CacheTimeout: 0,
|
||||
|
@ -226,7 +227,7 @@ func TestCarbonIntensitySegmentSingle(t *testing.T) {
|
|||
}
|
||||
|
||||
env.On("HTTPRequest", CARBONINTENSITYURL).Return([]byte(jsonResponse), responseError)
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
|
||||
d := &CarbonIntensity{
|
||||
props: props,
|
||||
|
@ -250,13 +251,14 @@ func TestCarbonIntensitySegmentFromCache(t *testing.T) {
|
|||
response := `{ "data": [ { "from": "2023-10-27T12:30Z", "to": "2023-10-27T13:00Z", "intensity": { "forecast": 199, "actual": 193, "index": "moderate" } } ] }`
|
||||
expectedString := "CO₂ •193 ↗ 199"
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
cache := &mock.MockedCache{}
|
||||
env := &mock.Environment{}
|
||||
cache := &cache_.Cache{}
|
||||
|
||||
d := &CarbonIntensity{
|
||||
props: properties.Map{},
|
||||
env: env,
|
||||
}
|
||||
|
||||
cache.On("Get", CARBONINTENSITYURL).Return(response, true)
|
||||
cache.On("Set").Return()
|
||||
env.On("Cache").Return(cache)
|
||||
|
|
|
@ -6,9 +6,9 @@ import (
|
|||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -64,7 +64,7 @@ func TestCFTargetSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var env = new(mock.MockedEnvironment)
|
||||
var env = new(mock.Environment)
|
||||
env.On("HasCommand", "cf").Return(true)
|
||||
env.On("RunCommand", "cf", []string{"target"}).Return(tc.TargetOutput, tc.CommandError)
|
||||
env.On("Pwd", nil).Return("/usr/home/dev/my-app")
|
||||
|
|
|
@ -3,14 +3,14 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExecuteCommand(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "echo hello").Return("hello")
|
||||
props := properties.Map{
|
||||
|
@ -26,7 +26,7 @@ func TestExecuteCommand(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteMultipleCommandsOrFirst(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "exit 1").Return("")
|
||||
env.On("RunShellCommand", "bash", "echo hello").Return("hello")
|
||||
|
@ -44,7 +44,7 @@ func TestExecuteMultipleCommandsOrFirst(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteMultipleCommandsOrSecond(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "echo hello").Return("hello")
|
||||
env.On("RunShellCommand", "bash", "echo world").Return("world")
|
||||
|
@ -61,7 +61,7 @@ func TestExecuteMultipleCommandsOrSecond(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteMultipleCommandsAnd(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "echo hello").Return("hello")
|
||||
env.On("RunShellCommand", "bash", "echo world").Return("world")
|
||||
|
@ -78,7 +78,7 @@ func TestExecuteMultipleCommandsAnd(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteSingleCommandEmpty(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "").Return("")
|
||||
props := properties.Map{
|
||||
|
@ -93,7 +93,7 @@ func TestExecuteSingleCommandEmpty(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteSingleCommandNoCommandProperty(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "").Return("")
|
||||
var props properties.Map
|
||||
|
@ -106,7 +106,7 @@ func TestExecuteSingleCommandNoCommandProperty(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteMultipleCommandsAndDisabled(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "echo").Return("")
|
||||
props := properties.Map{
|
||||
|
@ -121,7 +121,7 @@ func TestExecuteMultipleCommandsAndDisabled(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteMultipleCommandsOrDisabled(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "echo").Return("")
|
||||
env.On("RunShellCommand", "bash", "echo|| echo").Return("")
|
||||
|
@ -137,7 +137,7 @@ func TestExecuteMultipleCommandsOrDisabled(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecuteNonInterpretedCommand(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", "echo hello && echo world").Return("hello world")
|
||||
props := properties.Map{
|
||||
|
@ -174,7 +174,7 @@ func TestExecuteScript(t *testing.T) {
|
|||
}
|
||||
for _, tc := range cases {
|
||||
script := "../test/script.sh"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "bash").Return(true)
|
||||
env.On("RunShellCommand", "bash", script).Return(tc.Output)
|
||||
props := properties.Map{
|
||||
|
|
|
@ -4,9 +4,9 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -88,7 +88,7 @@ func TestConnection(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
for _, con := range tc.Connections {
|
||||
env.On("Connection", con.Connection.Type).Return(con.Connection, con.Error)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -34,7 +34,7 @@ func TestDockerSegment(t *testing.T) {
|
|||
|
||||
for _, tc := range cases {
|
||||
docker := &Docker{}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
docker.Init(properties.Map{}, env)
|
||||
|
||||
for _, v := range docker.envVars() {
|
||||
|
|
|
@ -5,14 +5,14 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExecutionTimeWriterDefaultThresholdEnabled(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("ExecutionTime").Return(1337)
|
||||
executionTime := &Executiontime{
|
||||
env: env,
|
||||
|
@ -22,7 +22,7 @@ func TestExecutionTimeWriterDefaultThresholdEnabled(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecutionTimeWriterDefaultThresholdDisabled(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("ExecutionTime").Return(1)
|
||||
executionTime := &Executiontime{
|
||||
env: env,
|
||||
|
@ -32,7 +32,7 @@ func TestExecutionTimeWriterDefaultThresholdDisabled(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecutionTimeWriterCustomThresholdEnabled(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("ExecutionTime").Return(99)
|
||||
props := properties.Map{
|
||||
ThresholdProperty: float64(10),
|
||||
|
@ -45,7 +45,7 @@ func TestExecutionTimeWriterCustomThresholdEnabled(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestExecutionTimeWriterCustomThresholdDisabled(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("ExecutionTime").Return(99)
|
||||
props := properties.Map{
|
||||
ThresholdProperty: float64(100),
|
||||
|
@ -60,7 +60,7 @@ func TestExecutionTimeWriterCustomThresholdDisabled(t *testing.T) {
|
|||
func TestExecutionTimeWriterDuration(t *testing.T) {
|
||||
input := 1337
|
||||
expected := "1.337s"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("ExecutionTime").Return(input)
|
||||
executionTime := &Executiontime{
|
||||
env: env,
|
||||
|
@ -73,7 +73,7 @@ func TestExecutionTimeWriterDuration(t *testing.T) {
|
|||
func TestExecutionTimeWriterDuration2(t *testing.T) {
|
||||
input := 13371337
|
||||
expected := "3h 42m 51.337s"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("ExecutionTime").Return(input)
|
||||
executionTime := &Executiontime{
|
||||
env: env,
|
||||
|
|
|
@ -4,9 +4,9 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestFirebaseSegment(t *testing.T) {
|
||||
|
@ -53,12 +53,12 @@ func TestFirebaseSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return("home")
|
||||
env.On("Pwd").Return(tc.ActivePath)
|
||||
fcPath := filepath.Join("home", ".config", "configstore", "firebase-tools.json")
|
||||
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
f := Firebase{
|
||||
env: env,
|
||||
}
|
||||
|
@ -96,12 +96,12 @@ func TestGetFirebaseActiveConfig(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return("home")
|
||||
configPath := filepath.Join("home", ".config", "configstore")
|
||||
contentPath := filepath.Join(configPath, "firebase-tools.json")
|
||||
env.On("FileContent", contentPath).Return(tc.ActiveConfig)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
f := Firebase{
|
||||
env: env,
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -52,7 +52,7 @@ func TestFossilStatus(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return("unix")
|
||||
env.On("IsWsl").Return(false)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
|
|
|
@ -4,11 +4,11 @@ import (
|
|||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestGcpSegment(t *testing.T) {
|
||||
|
@ -51,13 +51,13 @@ func TestGcpSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Getenv", "CLOUDSDK_CONFIG").Return("config")
|
||||
fcPath := path.Join("config", "active_config")
|
||||
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
|
||||
cfgpath := path.Join("config", "configurations", "config_production")
|
||||
env.On("FileContent", cfgpath).Return(tc.CfgData)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
g := &Gcp{
|
||||
env: env,
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func TestGetConfigDirectory(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.CloudSDKConfig)
|
||||
env.On("Getenv", "APPDATA").Return(tc.AppData)
|
||||
env.On("Home").Return(tc.Home)
|
||||
|
@ -127,7 +127,7 @@ func TestGetActiveConfig(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", "active_config").Return(tc.ActiveConfig)
|
||||
g := &Gcp{
|
||||
env: env,
|
||||
|
|
|
@ -9,12 +9,12 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -24,7 +24,7 @@ const (
|
|||
)
|
||||
|
||||
func TestEnabledGitNotFound(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "git").Return(false)
|
||||
env.On("GOOS").Return("")
|
||||
|
@ -44,7 +44,7 @@ func TestEnabledInWorkingDirectory(t *testing.T) {
|
|||
ParentFolder: "/dir",
|
||||
IsDir: true,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "git").Return(true)
|
||||
env.On("GOOS").Return("")
|
||||
|
@ -55,7 +55,7 @@ func TestEnabledInWorkingDirectory(t *testing.T) {
|
|||
env.On("PathSeparator").Return("/")
|
||||
env.On("Home").Return(poshHome)
|
||||
env.On("Getenv", poshGitEnv).Return("")
|
||||
env.On("DirMatchesOneOf", mock2.Anything, mock2.Anything).Return(false)
|
||||
env.On("DirMatchesOneOf", testify_.Anything, testify_.Anything).Return(false)
|
||||
g := &Git{
|
||||
scm: scm{
|
||||
env: env,
|
||||
|
@ -132,7 +132,7 @@ func TestEnabledInWorktree(t *testing.T) {
|
|||
ParentFolder: TestRootPath + "dev",
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", TestRootPath+dotGit).Return(fmt.Sprintf("gitdir: %s", tc.WorkingFolder))
|
||||
env.On("FileContent", filepath.Join(tc.WorkingFolder, tc.WorkingFolderAddon)).Return(tc.WorkingFolderContent)
|
||||
env.On("HasFilesInDir", tc.WorkingFolder, tc.WorkingFolderAddon).Return(true)
|
||||
|
@ -188,7 +188,7 @@ func TestEnabledInBareRepo(t *testing.T) {
|
|||
}
|
||||
for _, tc := range cases {
|
||||
pwd := "/home/user/bare.git"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("GOOS").Return("")
|
||||
env.On("HasCommand", "git").Return(true)
|
||||
|
@ -217,7 +217,7 @@ func TestGetGitOutputForCommand(t *testing.T) {
|
|||
args := []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}
|
||||
commandArgs := []string{"symbolic-ref", "--short", "HEAD"}
|
||||
want := "je suis le output"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("IsWsl").Return(false)
|
||||
env.On("RunCommand", "git", append(args, commandArgs...)).Return(want, nil)
|
||||
env.On("GOOS").Return("unix")
|
||||
|
@ -335,7 +335,7 @@ func TestSetGitHEADContextClean(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("GOOS").Return("unix")
|
||||
env.On("IsWsl").Return(false)
|
||||
|
@ -404,7 +404,7 @@ func TestSetPrettyHEADName(t *testing.T) {
|
|||
{Case: "no hash on commit", Expected: "commit 1234567", HEAD: "12345678910"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", "/HEAD").Return(tc.HEAD)
|
||||
env.On("GOOS").Return("unix")
|
||||
env.On("IsWsl").Return(false)
|
||||
|
@ -575,7 +575,7 @@ func TestSetGitStatus(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return("unix")
|
||||
env.On("IsWsl").Return(false)
|
||||
env.MockGitCommand("", strings.ReplaceAll(tc.Output, "\t", ""), "status", "-unormal", "--branch", "--porcelain=2")
|
||||
|
@ -618,7 +618,7 @@ func TestGetStashContextZeroEntries(t *testing.T) {
|
|||
{Expected: 4, StashContent: "1\n2\n3\n4\n\n"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", "/logs/refs/stash").Return(tc.StashContent)
|
||||
g := &Git{
|
||||
scm: scm{
|
||||
|
@ -677,7 +677,7 @@ func TestGitUpstream(t *testing.T) {
|
|||
{Case: "My custom server", Expected: "CU", Upstream: "mycustom.server/test"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
env.On("IsWsl").Return(false)
|
||||
env.On("RunCommand", "git", []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false",
|
||||
"-c", "color.status=false", "remote", "get-url", "origin"}).Return(tc.Upstream, nil)
|
||||
|
@ -875,7 +875,7 @@ func TestGitTemplateString(t *testing.T) {
|
|||
props := properties.Map{
|
||||
FetchStatus: true,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
tc.Git.env = env
|
||||
tc.Git.props = props
|
||||
assert.Equal(t, tc.Expected, renderTemplate(env, tc.Template, tc.Git), tc.Case)
|
||||
|
@ -1043,7 +1043,7 @@ func TestGitCommit(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.MockGitCommand("", tc.Output, "log", "-1", "--pretty=format:an:%an%nae:%ae%ncn:%cn%nce:%ce%nat:%at%nsu:%s%nha:%H")
|
||||
g := &Git{
|
||||
scm: scm{
|
||||
|
@ -1095,7 +1095,7 @@ func TestGitRemotes(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", "config").Return(tc.Config)
|
||||
|
||||
g := &Git{
|
||||
|
@ -1140,7 +1140,7 @@ func TestGitRepoName(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("GOOS").Return(runtime.LINUX)
|
||||
|
||||
|
|
|
@ -4,11 +4,12 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestGitversion(t *testing.T) {
|
||||
|
@ -70,14 +71,14 @@ func TestGitversion(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
cache := &mock.MockedCache{}
|
||||
env := new(mock.Environment)
|
||||
cache := &cache_.Cache{}
|
||||
|
||||
env.On("HasCommand", "gitversion").Return(tc.HasGitversion)
|
||||
env.On("Pwd").Return("test-dir")
|
||||
env.On("Cache").Return(cache)
|
||||
cache.On("Get", "test-dir").Return(tc.CacheResponse, len(tc.CacheResponse) != 0)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
cache.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
|
||||
env.On("RunCommand", "gitversion", []string{"-output", "json"}).Return(tc.Response, tc.CommandError)
|
||||
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
testify_mock "github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
)
|
||||
|
||||
func TestHelmSegment(t *testing.T) {
|
||||
|
@ -82,7 +82,7 @@ func TestHelmSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "helm").Return(tc.HelmExists)
|
||||
env.On("RunCommand", "helm", []string{"version", "--short", "--template={{.Version}}"}).Return("v3.12.3", nil)
|
||||
|
||||
|
|
|
@ -3,9 +3,9 @@ package segments
|
|||
import (
|
||||
"net"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/http"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
)
|
||||
|
||||
type ipData struct {
|
||||
|
@ -63,8 +63,11 @@ func (i *IPify) getResult() (string, error) {
|
|||
}
|
||||
|
||||
func (i *IPify) Init(props properties.Properties, env runtime.Environment) {
|
||||
request := &http.Request{}
|
||||
request.Init(env, props)
|
||||
request := &http.Request{
|
||||
Env: env,
|
||||
CacheTimeout: props.GetInt(properties.CacheTimeout, 30),
|
||||
HTTPTimeout: props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout),
|
||||
}
|
||||
|
||||
i.api = &ipAPI{
|
||||
Request: *request,
|
||||
|
|
|
@ -5,10 +5,10 @@ import (
|
|||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -16,7 +16,7 @@ const (
|
|||
)
|
||||
|
||||
type mockedipAPI struct {
|
||||
mock2.Mock
|
||||
testify_.Mock
|
||||
}
|
||||
|
||||
func (s *mockedipAPI) Get() (*ipData, error) {
|
||||
|
@ -66,6 +66,6 @@ func TestIpifySegment(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
|
||||
assert.Equal(t, tc.ExpectedString, renderTemplate(&mock.MockedEnvironment{}, ipify.Template(), ipify), tc.Case)
|
||||
assert.Equal(t, tc.ExpectedString, renderTemplate(&mock.Environment{}, ipify.Template(), ipify), tc.Case)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,9 +6,9 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -132,7 +132,7 @@ func TestKubectlSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "kubectl").Return(tc.KubectlExists)
|
||||
var kubeconfig string
|
||||
content, err := os.ReadFile("../test/kubectl.yml")
|
||||
|
|
|
@ -3,12 +3,14 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -41,7 +43,7 @@ func (l *languageArgs) hasvalue(value string, list []string) bool {
|
|||
}
|
||||
|
||||
func bootStrapLanguageTest(args *languageArgs) *language {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
|
||||
for _, command := range args.commands {
|
||||
env.On("HasCommand", command.executable).Return(args.hasvalue(command.executable, args.enabledCommands))
|
||||
|
@ -60,15 +62,15 @@ func bootStrapLanguageTest(args *languageArgs) *language {
|
|||
|
||||
env.On("Pwd").Return(cwd)
|
||||
env.On("Home").Return(home)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache.On("Get", mock2.Anything).Return(args.cachedVersion, len(args.cachedVersion) > 0)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
env.On("Cache").Return(cache)
|
||||
c := &cache_.Cache{}
|
||||
c.On("Get", testify_.Anything).Return(args.cachedVersion, len(args.cachedVersion) > 0)
|
||||
c.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
env.On("Cache").Return(c)
|
||||
|
||||
if args.properties == nil {
|
||||
args.properties = properties.Map{}
|
||||
|
@ -570,25 +572,25 @@ type mockedLanguageParams struct {
|
|||
extension string
|
||||
}
|
||||
|
||||
func getMockedLanguageEnv(params *mockedLanguageParams) (*mock.MockedEnvironment, properties.Map) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
func getMockedLanguageEnv(params *mockedLanguageParams) (*mock.Environment, properties.Map) {
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", params.cmd).Return(true)
|
||||
env.On("RunCommand", params.cmd, []string{params.versionParam}).Return(params.versionOutput, nil)
|
||||
env.On("HasFiles", params.extension).Return(true)
|
||||
env.On("Pwd").Return("/usr/home/project")
|
||||
env.On("Home").Return("/usr/home")
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
props := properties.Map{
|
||||
properties.FetchVersion: true,
|
||||
}
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache.On("Get", mock2.Anything).Return("", false)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
env.On("Cache").Return(cache)
|
||||
c := &cache_.Cache{}
|
||||
c.On("Get", testify_.Anything).Return("", false)
|
||||
c.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
env.On("Cache").Return(c)
|
||||
|
||||
return env, props
|
||||
}
|
||||
|
|
|
@ -4,11 +4,12 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -53,7 +54,7 @@ func TestLFMSegmentSingle(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
var props properties.Map = properties.Map{
|
||||
APIKey: "key",
|
||||
Username: "KibbeWater",
|
||||
|
@ -62,7 +63,7 @@ func TestLFMSegmentSingle(t *testing.T) {
|
|||
}
|
||||
|
||||
env.On("HTTPRequest", LFMAPIURL).Return([]byte(tc.APIJSONResponse), tc.Error)
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
|
||||
o := &LastFM{
|
||||
props: props,
|
||||
|
@ -86,8 +87,8 @@ func TestLFMSegmentFromCache(t *testing.T) {
|
|||
response := `{"recenttracks":{"track":[{"artist":{"mbid":"","#text":"C.Gambino"},"streamable":"0","name":"Automatic","date":{"uts":"1699350223","#text":"07 Nov 2023, 09:43"}}]}}`
|
||||
expectedString := "\uF04D"
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
cache := &mock.MockedCache{}
|
||||
env := &mock.Environment{}
|
||||
cache := &cache_.Cache{}
|
||||
o := &LastFM{
|
||||
props: properties.Map{
|
||||
APIKey: "key",
|
||||
|
|
|
@ -3,14 +3,14 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMercurialEnabledToolNotFound(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "hg").Return(false)
|
||||
env.On("GOOS").Return("")
|
||||
|
@ -32,7 +32,7 @@ func TestMercurialEnabledInWorkingDirectory(t *testing.T) {
|
|||
ParentFolder: "/dir",
|
||||
IsDir: true,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "hg").Return(true)
|
||||
env.On("GOOS").Return("")
|
||||
|
@ -146,7 +146,7 @@ A Added.File
|
|||
FetchStatus: true,
|
||||
}
|
||||
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "hg").Return(true)
|
||||
env.On("GOOS").Return("")
|
||||
|
|
|
@ -6,11 +6,12 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func getTestData(file string) string {
|
||||
|
@ -74,15 +75,15 @@ func TestNBASegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
props := properties.Map{
|
||||
properties.CacheTimeout: tc.CacheTimeout,
|
||||
TeamName: tc.TeamName,
|
||||
DaysOffset: tc.DaysOffset,
|
||||
}
|
||||
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
env.On("HTTPRequest", NBAScoreURL).Return([]byte(tc.JSONResponse), tc.Error)
|
||||
|
||||
// Add all the daysOffset to the http request responses
|
||||
|
@ -103,7 +104,7 @@ func TestNBASegment(t *testing.T) {
|
|||
cachedScheduleKey := fmt.Sprintf("%s%s", tc.TeamName, "schedule")
|
||||
cachedScoreKey := fmt.Sprintf("%s%s", tc.TeamName, "score")
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache := &cache_.Cache{}
|
||||
cache.On("Get", cachedScheduleKey).Return(nba.getGameNotFoundData(), tc.CacheFoundFail)
|
||||
cache.On("Get", cachedScoreKey).Return(nba.getGameNotFoundData(), tc.CacheFoundFail)
|
||||
cache.On("Set", cachedScheduleKey, nba.getGameNotFoundData(), tc.CacheTimeout).Return()
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
)
|
||||
|
@ -60,7 +60,7 @@ func TestNbgv(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "nbgv").Return(tc.HasNbgv)
|
||||
env.On("RunCommand", "nbgv", []string{"get-version", "--format=json"}).Return(tc.Response, tc.Error)
|
||||
nbgv := &Nbgv{
|
||||
|
|
|
@ -4,8 +4,9 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -132,14 +133,14 @@ func TestNSSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
props := properties.Map{
|
||||
properties.CacheTimeout: tc.CacheTimeout,
|
||||
URL: "FAKE",
|
||||
Headers: map[string]string{"Fake-Header": "xxxxx"},
|
||||
}
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache := &cache_.Cache{}
|
||||
cache.On("Get", FAKEAPIURL).Return(tc.JSONResponse, !tc.CacheFoundFail)
|
||||
cache.On("Set", FAKEAPIURL, tc.JSONResponse, tc.CacheTimeout).Return()
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
)
|
||||
|
@ -37,7 +37,7 @@ func TestNodeMatchesVersionFile(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("FileContent", ".nvmrc").Return(tc.RCVersion)
|
||||
|
||||
node := &Node{
|
||||
|
@ -75,7 +75,7 @@ func TestNodeInContext(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasFiles", "pnpm-lock.yaml").Return(tc.hasPNPM)
|
||||
env.On("HasFiles", "yarn.lock").Return(tc.hasYarn)
|
||||
env.On("HasFiles", "package-lock.json").Return(tc.hasNPM)
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -25,13 +25,13 @@ func TestGetNodePackageVersion(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var env = new(mock.MockedEnvironment)
|
||||
var env = new(mock.Environment)
|
||||
// mock getVersion methods
|
||||
env.On("Pwd").Return("posh")
|
||||
path := filepath.Join("posh", "node_modules", "nx")
|
||||
env.On("HasFilesInDir", path, "package.json").Return(!tc.NoFiles)
|
||||
env.On("FileContent", filepath.Join(path, "package.json")).Return(tc.PackageJSON)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
got, err := getNodePackageVersion(env, "nx")
|
||||
|
|
|
@ -3,9 +3,9 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -86,10 +86,10 @@ func TestOSInfo(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return(tc.GOOS)
|
||||
env.On("Platform").Return(tc.Platform)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
WSL: tc.IsWSL,
|
||||
})
|
||||
|
|
|
@ -6,11 +6,12 @@ import (
|
|||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -72,7 +73,7 @@ func TestOWMSegmentSingle(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
props := properties.Map{
|
||||
APIKey: "key",
|
||||
Location: tc.Location,
|
||||
|
@ -83,7 +84,7 @@ func TestOWMSegmentSingle(t *testing.T) {
|
|||
location := url.QueryEscape(tc.Location)
|
||||
testURL := fmt.Sprintf(OWMWEATHERAPIURL, location)
|
||||
env.On("HTTPRequest", testURL).Return([]byte(tc.WeatherJSONResponse), tc.Error)
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
|
||||
o := &Owm{
|
||||
props: props,
|
||||
|
@ -206,7 +207,7 @@ func TestOWMSegmentIcons(t *testing.T) {
|
|||
testURL := fmt.Sprintf(OWMWEATHERAPIURL, location)
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
|
||||
weatherResponse := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20.3}}`, tc.IconID)
|
||||
expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString)
|
||||
|
@ -229,7 +230,7 @@ func TestOWMSegmentIcons(t *testing.T) {
|
|||
|
||||
// test with hyperlink enabled
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
|
||||
weatherResponse := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20.3}}`, tc.IconID)
|
||||
expectedString := fmt.Sprintf("«%s (20°C)»(%s)", tc.ExpectedIconString, testURL)
|
||||
|
@ -255,8 +256,8 @@ func TestOWMSegmentFromCacheByGeoName(t *testing.T) {
|
|||
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
|
||||
expectedString := fmt.Sprintf("%s (20°C)", "\ue30d")
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
cache := &mock.MockedCache{}
|
||||
env := &mock.Environment{}
|
||||
cache := &cache_.Cache{}
|
||||
o := &Owm{
|
||||
props: properties.Map{
|
||||
APIKey: "key",
|
||||
|
@ -278,8 +279,8 @@ func TestOWMSegmentFromCacheWithHyperlinkByGeoName(t *testing.T) {
|
|||
response := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20}}`, "01d")
|
||||
expectedString := fmt.Sprintf("«%s (20°C)»(http://api.openweathermap.org/data/2.5/weather?q=AMSTERDAM,NL&units=metric&appid=key)", "\ue30d")
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
cache := &mock.MockedCache{}
|
||||
env := &mock.Environment{}
|
||||
cache := &cache_.Cache{}
|
||||
|
||||
o := &Owm{
|
||||
props: properties.Map{
|
||||
|
|
|
@ -5,14 +5,15 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/shell"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/template"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -24,7 +25,7 @@ const (
|
|||
cdefg = "/c/d/e/f/g"
|
||||
)
|
||||
|
||||
func renderTemplateNoTrimSpace(env *mock.MockedEnvironment, segmentTemplate string, context any) string {
|
||||
func renderTemplateNoTrimSpace(env *mock.Environment, segmentTemplate string, context any) string {
|
||||
found := false
|
||||
for _, call := range env.Mock.ExpectedCalls {
|
||||
if call.Method == "TemplateCache" {
|
||||
|
@ -33,13 +34,13 @@ func renderTemplateNoTrimSpace(env *mock.MockedEnvironment, segmentTemplate stri
|
|||
}
|
||||
}
|
||||
if !found {
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
}
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
tmpl := &template.Text{
|
||||
Template: segmentTemplate,
|
||||
Context: context,
|
||||
|
@ -52,7 +53,7 @@ func renderTemplateNoTrimSpace(env *mock.MockedEnvironment, segmentTemplate stri
|
|||
return text
|
||||
}
|
||||
|
||||
func renderTemplate(env *mock.MockedEnvironment, segmentTemplate string, context any) string {
|
||||
func renderTemplate(env *mock.Environment, segmentTemplate string, context any) string {
|
||||
return strings.TrimSpace(renderTemplateNoTrimSpace(env, segmentTemplate, context))
|
||||
}
|
||||
|
||||
|
@ -141,7 +142,7 @@ func TestParent(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(tc.HomePath)
|
||||
env.On("Pwd").Return(tc.Pwd)
|
||||
env.On("Flags").Return(&runtime.Flags{})
|
||||
|
@ -758,7 +759,7 @@ func TestAgnosterPathStyles(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("PathSeparator").Return(tc.PathSeparator)
|
||||
env.On("Home").Return(tc.HomePath)
|
||||
env.On("Pwd").Return(tc.Pwd)
|
||||
|
@ -775,12 +776,12 @@ func TestAgnosterPathStyles(t *testing.T) {
|
|||
}
|
||||
env.On("Shell").Return(tc.Shell)
|
||||
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
|
||||
displayCygpath := tc.GOOS == runtime.WINDOWS && tc.Shell == shell.BASH
|
||||
if displayCygpath {
|
||||
env.On("RunCommand", "cygpath", []string{"-u", tc.Pwd}).Return(tc.Cygpath, tc.CygpathError)
|
||||
env.On("RunCommand", "cygpath", mock2.Anything).Return("brrrr", nil)
|
||||
env.On("RunCommand", "cygpath", testify_.Anything).Return("brrrr", nil)
|
||||
}
|
||||
|
||||
path := &Path{
|
||||
|
@ -889,7 +890,7 @@ func TestFullAndFolderPath(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
if len(tc.PathSeparator) == 0 {
|
||||
tc.PathSeparator = "/"
|
||||
}
|
||||
|
@ -951,7 +952,7 @@ func TestFullPathCustomMappedLocations(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(homeDir)
|
||||
env.On("Pwd").Return(tc.Pwd)
|
||||
|
||||
|
@ -972,8 +973,8 @@ func TestFullPathCustomMappedLocations(t *testing.T) {
|
|||
|
||||
env.On("Flags").Return(args)
|
||||
env.On("Shell").Return(shell.GENERIC)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{
|
||||
"HOME": "/a/b/c",
|
||||
},
|
||||
|
@ -1005,13 +1006,13 @@ func TestPowerlevelMappedLocations(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return("/Users/michal")
|
||||
env.On("Pwd").Return(tc.Pwd)
|
||||
env.On("GOOS").Return(runtime.DARWIN)
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("Shell").Return(shell.GENERIC)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
path := &Path{
|
||||
env: env,
|
||||
props: properties.Map{
|
||||
|
@ -1028,7 +1029,7 @@ func TestPowerlevelMappedLocations(t *testing.T) {
|
|||
|
||||
func TestFolderPathCustomMappedLocations(t *testing.T) {
|
||||
pwd := abcd
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("Home").Return(homeDir)
|
||||
env.On("Pwd").Return(pwd)
|
||||
|
@ -1038,7 +1039,7 @@ func TestFolderPathCustomMappedLocations(t *testing.T) {
|
|||
}
|
||||
env.On("Flags").Return(args)
|
||||
env.On("Shell").Return(shell.GENERIC)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
path := &Path{
|
||||
env: env,
|
||||
props: properties.Map{
|
||||
|
@ -1208,7 +1209,7 @@ func TestAgnosterPath(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(tc.Home)
|
||||
env.On("PathSeparator").Return(tc.PathSeparator)
|
||||
env.On("Pwd").Return(tc.PWD)
|
||||
|
@ -1364,7 +1365,7 @@ func TestAgnosterLeftPath(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(tc.Home)
|
||||
env.On("PathSeparator").Return(tc.PathSeparator)
|
||||
env.On("Pwd").Return(tc.PWD)
|
||||
|
@ -1417,7 +1418,7 @@ func TestGetPwd(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("Home").Return(homeDir)
|
||||
env.On("Pwd").Return(tc.Pwd)
|
||||
|
@ -1427,7 +1428,7 @@ func TestGetPwd(t *testing.T) {
|
|||
}
|
||||
env.On("Flags").Return(args)
|
||||
env.On("Shell").Return(shell.PWSH)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
path := &Path{
|
||||
env: env,
|
||||
props: properties.Map{
|
||||
|
@ -1457,10 +1458,10 @@ func TestGetFolderSeparator(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env := new(mock.Environment)
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
path := &Path{
|
||||
env: env,
|
||||
pathSeparator: "/",
|
||||
|
@ -1476,7 +1477,7 @@ func TestGetFolderSeparator(t *testing.T) {
|
|||
props[FolderSeparatorIcon] = tc.FolderSeparatorIcon
|
||||
}
|
||||
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
Shell: "bash",
|
||||
})
|
||||
|
@ -1505,7 +1506,7 @@ func TestNormalizePath(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return(tc.HomeDir)
|
||||
env.On("GOOS").Return(tc.GOOS)
|
||||
pt := &Path{
|
||||
|
@ -1532,13 +1533,13 @@ func TestReplaceMappedLocations(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("Pwd").Return(tc.Pwd)
|
||||
env.On("Shell").Return(shell.FISH)
|
||||
env.On("GOOS").Return(runtime.DARWIN)
|
||||
env.On("Home").Return("/a/b/k")
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
path := &Path{
|
||||
env: env,
|
||||
props: properties.Map{
|
||||
|
@ -1606,7 +1607,7 @@ func TestSplitPath(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return("/a/b")
|
||||
env.On("HasParentFilePath", ".git").Return(tc.GitDir, nil)
|
||||
env.On("GOOS").Return(tc.GOOS)
|
||||
|
@ -1655,10 +1656,10 @@ func TestGetMaxWidth(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("Error", mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := new(mock.Environment)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("Error", testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{
|
||||
"MAX_WIDTH": "120",
|
||||
},
|
||||
|
|
|
@ -3,15 +3,15 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPlasticEnabledNotFound(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "cm").Return(false)
|
||||
env.On("GOOS").Return("")
|
||||
env.On("IsWsl").Return(false)
|
||||
|
@ -25,7 +25,7 @@ func TestPlasticEnabledNotFound(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestPlasticEnabledInWorkspaceDirectory(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "cm").Return(true)
|
||||
env.On("GOOS").Return("")
|
||||
env.On("IsWsl").Return(false)
|
||||
|
@ -47,7 +47,7 @@ func TestPlasticEnabledInWorkspaceDirectory(t *testing.T) {
|
|||
}
|
||||
|
||||
func setupCmStatusEnv(status, headStatus string) *Plastic {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("RunCommand", "cm", []string{"status", "--all", "--machinereadable"}).Return(status, nil)
|
||||
env.On("RunCommand", "cm", []string{"status", "--head", "--machinereadable"}).Return(headStatus, nil)
|
||||
p := &Plastic{
|
||||
|
@ -333,7 +333,7 @@ func TestPlasticTemplateString(t *testing.T) {
|
|||
FetchStatus: true,
|
||||
}
|
||||
tc.Plastic.props = props
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
tc.Plastic.env = env
|
||||
assert.Equal(t, tc.Expected, renderTemplate(env, tc.Template, tc.Plastic), tc.Case)
|
||||
}
|
||||
|
|
|
@ -3,9 +3,9 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -182,7 +182,7 @@ func TestPoshGitSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Getenv", poshGitEnv).Return(tc.PoshGitJSON)
|
||||
env.On("Home").Return("/Users/bill")
|
||||
env.On("GOOS").Return(runtime.LINUX)
|
||||
|
|
|
@ -6,13 +6,12 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_mock "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -256,11 +255,11 @@ func TestPackage(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On(hasFiles, testify_mock.Anything).Run(func(args testify_mock.Arguments) {
|
||||
env := new(mock.Environment)
|
||||
env.On(hasFiles, testify_.Anything).Run(func(args testify_.Arguments) {
|
||||
for _, c := range env.ExpectedCalls {
|
||||
if c.Method == hasFiles {
|
||||
c.ReturnArguments = testify_mock.Arguments{args.Get(0).(string) == tc.File}
|
||||
c.ReturnArguments = testify_.Arguments{args.Get(0).(string) == tc.File}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -309,17 +308,17 @@ func TestNuspecPackage(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On(hasFiles, testify_mock.Anything).Run(func(args testify_mock.Arguments) {
|
||||
env := new(mock.Environment)
|
||||
env.On(hasFiles, testify_.Anything).Run(func(args testify_.Arguments) {
|
||||
for _, c := range env.ExpectedCalls {
|
||||
if c.Method != hasFiles {
|
||||
continue
|
||||
}
|
||||
if args.Get(0).(string) == "*.nuspec" {
|
||||
c.ReturnArguments = testify_mock.Arguments{tc.HasFiles}
|
||||
c.ReturnArguments = testify_.Arguments{tc.HasFiles}
|
||||
continue
|
||||
}
|
||||
c.ReturnArguments = testify_mock.Arguments{false}
|
||||
c.ReturnArguments = testify_.Arguments{false}
|
||||
}
|
||||
})
|
||||
env.On("Pwd").Return("posh")
|
||||
|
@ -387,12 +386,12 @@ func TestDotnetProject(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On(hasFiles, testify_mock.Anything).Run(func(args testify_mock.Arguments) {
|
||||
env := new(mock.Environment)
|
||||
env.On(hasFiles, testify_.Anything).Run(func(args testify_.Arguments) {
|
||||
for _, c := range env.ExpectedCalls {
|
||||
if c.Method == hasFiles {
|
||||
pattern := "*" + filepath.Ext(tc.FileName)
|
||||
c.ReturnArguments = testify_mock.Arguments{args.Get(0).(string) == pattern}
|
||||
c.ReturnArguments = testify_.Arguments{args.Get(0).(string) == pattern}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -403,7 +402,7 @@ func TestDotnetProject(t *testing.T) {
|
|||
},
|
||||
})
|
||||
env.On("FileContent", tc.FileName).Return(tc.ProjectContents)
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
pkg := &Project{}
|
||||
pkg.Init(properties.Map{}, env)
|
||||
assert.Equal(t, tc.ExpectedEnabled, pkg.Enabled(), tc.Case)
|
||||
|
@ -429,11 +428,11 @@ func TestPowerShellModuleProject(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On(hasFiles, testify_mock.Anything).Run(func(args testify_mock.Arguments) {
|
||||
env := new(mock.Environment)
|
||||
env.On(hasFiles, testify_.Anything).Run(func(args testify_.Arguments) {
|
||||
for _, c := range env.ExpectedCalls {
|
||||
if c.Method == hasFiles {
|
||||
c.ReturnArguments = testify_mock.Arguments{args.Get(0).(string) == "*.psd1"}
|
||||
c.ReturnArguments = testify_.Arguments{args.Get(0).(string) == "*.psd1"}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -5,10 +5,11 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestPulumi(t *testing.T) {
|
||||
|
@ -185,7 +186,7 @@ description: A Console App
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
|
||||
env.On("HasCommand", "pulumi").Return(tc.HasCommand)
|
||||
env.On("RunCommand", "pulumi", []string{"stack", "ls", "--json"}).Return(tc.Stack, tc.StackError)
|
||||
|
@ -193,9 +194,9 @@ description: A Console App
|
|||
|
||||
env.On("Pwd").Return("/home/foobar/Work/oh-my-posh/pulumi/projects/awesome-project")
|
||||
env.On("Home").Return(filepath.Clean("/home/foobar"))
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything)
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything)
|
||||
|
||||
env.On("HasFiles", pulumiYAML).Return(len(tc.YAMLConfig) > 0)
|
||||
env.On("FileContent", pulumiYAML).Return(tc.YAMLConfig, nil)
|
||||
|
@ -210,9 +211,9 @@ description: A Console App
|
|||
env.On("HasFilesInDir", filepath.Clean("/home/foobar/.pulumi/workspaces"), workspaceFile).Return(len(tc.WorkSpaceFile) > 0)
|
||||
env.On("FileContent", filepath.Clean("/home/foobar/.pulumi/workspaces/"+workspaceFile)).Return(tc.WorkSpaceFile, nil)
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache := &cache_.Cache{}
|
||||
cache.On("Get", "pulumi-oh-my-posh-1337-c62b7b6786c5c5a85896576e46a25d7c9f888e92-about").Return(tc.AboutCache, len(tc.AboutCache) > 0)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
cache.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
|
||||
env.On("Cache").Return(cache)
|
||||
|
||||
|
|
|
@ -5,12 +5,12 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestPythonTemplate(t *testing.T) {
|
||||
|
@ -99,16 +99,16 @@ func TestPythonTemplate(t *testing.T) {
|
|||
env, props := getMockedLanguageEnv(params)
|
||||
|
||||
env.On("GOOS").Return("")
|
||||
env.On("CommandPath", mock2.Anything).Return(tc.PythonPath)
|
||||
env.On("CommandPath", testify_.Anything).Return(tc.PythonPath)
|
||||
env.On("RunCommand", "pyenv", []string{"version-name"}).Return(tc.VirtualEnvName, nil)
|
||||
env.On("HasFilesInDir", mock2.Anything, "pyvenv.cfg").Return(len(tc.PyvenvCfg) > 0)
|
||||
env.On("HasFilesInDir", testify_.Anything, "pyvenv.cfg").Return(len(tc.PyvenvCfg) > 0)
|
||||
env.On("FileContent", filepath.Join(filepath.Dir(tc.PythonPath), "pyvenv.cfg")).Return(tc.PyvenvCfg)
|
||||
env.On("Getenv", "VIRTUAL_ENV").Return(tc.VirtualEnvName)
|
||||
env.On("Getenv", "CONDA_ENV_PATH").Return(tc.VirtualEnvName)
|
||||
env.On("Getenv", "CONDA_DEFAULT_ENV").Return(tc.VirtualEnvName)
|
||||
env.On("Getenv", "PYENV_ROOT").Return("/home/user/.pyenv")
|
||||
env.On("PathSeparator").Return("")
|
||||
env.On("ResolveSymlink", mock2.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
|
||||
env.On("ResolveSymlink", testify_.Anything).Return(tc.ResolveSymlink.Path, tc.ResolveSymlink.Err)
|
||||
|
||||
props[properties.FetchVersion] = tc.FetchVersion
|
||||
props[UsePythonVersionFile] = true
|
||||
|
@ -131,11 +131,11 @@ func TestPythonPythonInContext(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return("")
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("CommandPath", mock2.Anything).Return("")
|
||||
env.On("HasFilesInDir", mock2.Anything, "pyvenv.cfg").Return(false)
|
||||
env.On("CommandPath", testify_.Anything).Return("")
|
||||
env.On("HasFilesInDir", testify_.Anything, "pyvenv.cfg").Return(false)
|
||||
env.On("Getenv", "VIRTUAL_ENV").Return(tc.VirtualEnvName)
|
||||
env.On("Getenv", "CONDA_ENV_PATH").Return("")
|
||||
env.On("Getenv", "CONDA_DEFAULT_ENV").Return("")
|
||||
|
@ -182,8 +182,8 @@ func TestPythonVirtualEnvIgnoreDefaultVenvNames(t *testing.T) {
|
|||
|
||||
env.On("GOOS").Return("")
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("CommandPath", mock2.Anything).Return("")
|
||||
env.On("HasFilesInDir", mock2.Anything, "pyvenv.cfg").Return(false)
|
||||
env.On("CommandPath", testify_.Anything).Return("")
|
||||
env.On("HasFilesInDir", testify_.Anything, "pyvenv.cfg").Return(false)
|
||||
env.On("Getenv", "VIRTUAL_ENV").Return(tc.VirtualEnvName)
|
||||
env.On("Getenv", "CONDA_ENV_PATH").Return("")
|
||||
env.On("Getenv", "CONDA_DEFAULT_ENV").Return("")
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
)
|
||||
|
||||
func TestSetDir(t *testing.T) {
|
||||
|
@ -43,7 +43,7 @@ func TestSetDir(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return(tc.GOOS)
|
||||
home := "/usr/home"
|
||||
if tc.GOOS == runtime.WINDOWS {
|
||||
|
@ -100,7 +100,7 @@ func TestSetCommitContext(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("RunCommand", "sl", []string{"log", "--limit", "1", "--template", SLCOMMITTEMPLATE}).Return(tc.Output, tc.Error)
|
||||
sl := &Sapling{
|
||||
scm: scm{
|
||||
|
@ -151,7 +151,7 @@ func TestShouldDisplay(t *testing.T) {
|
|||
IsDir: true,
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasCommand", "sl").Return(tc.HasSapling)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("GOOS").Return(runtime.LINUX)
|
||||
|
@ -229,7 +229,7 @@ func TestSetHeadContext(t *testing.T) {
|
|||
bm:sapling-segment
|
||||
`
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("RunCommand", "sl", []string{"log", "--limit", "1", "--template", SLCOMMITTEMPLATE}).Return(output, nil)
|
||||
env.On("RunCommand", "sl", []string{"status"}).Return(tc.Output, nil)
|
||||
sl := &Sapling{
|
||||
|
|
|
@ -3,9 +3,9 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -195,7 +195,7 @@ func TestHasCommand(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return(tc.GOOS)
|
||||
env.On("InWSLSharedDrive").Return(tc.IsWslSharedPath)
|
||||
env.On("HasCommand", "git").Return(true)
|
||||
|
|
|
@ -4,9 +4,10 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -112,7 +113,7 @@ func TestSessionSegmentTemplate(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("User").Return(tc.UserName)
|
||||
env.On("GOOS").Return("burp")
|
||||
env.On("Host").Return(tc.ComputerName, nil)
|
||||
|
@ -122,7 +123,7 @@ func TestSessionSegmentTemplate(t *testing.T) {
|
|||
}
|
||||
env.On("Getenv", "SSH_CONNECTION").Return(SSHSession)
|
||||
env.On("Getenv", "SSH_CLIENT").Return(SSHSession)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
UserName: tc.UserName,
|
||||
HostName: tc.ComputerName,
|
||||
Env: map[string]string{
|
||||
|
|
|
@ -3,16 +3,16 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWriteCurrentShell(t *testing.T) {
|
||||
expected := "zsh"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Shell").Return(expected, nil)
|
||||
env.On("Flags").Return(&runtime.Flags{ShellVersion: "1.2.3"})
|
||||
s := &Shell{
|
||||
|
@ -33,7 +33,7 @@ func TestUseMappedShellNames(t *testing.T) {
|
|||
{Shell: "PWSH", Expected: "PS"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Shell").Return(tc.Expected, nil)
|
||||
env.On("Flags").Return(&runtime.Flags{ShellVersion: "1.2.3"})
|
||||
s := &Shell{
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -84,7 +84,7 @@ func TestSitecoreSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HasFiles", "sitecore.json").Return(tc.SitecoreFileExists)
|
||||
env.On("HasFiles", path.Join(".sitecore", "user.json")).Return(tc.UserFileExists)
|
||||
env.On("FileContent", path.Join(".sitecore", "user.json")).Return(tc.UserFileContent)
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -27,7 +27,7 @@ func TestSpotifyDarwinEnabledAndSpotifyPlaying(t *testing.T) {
|
|||
{Running: "true", Expected: "\uF8E3 Candlemass - Spellbreaker", Status: "paused", Artist: "Candlemass", Track: "Spellbreaker"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("RunCommand", "osascript", []string{"-e", "application \"Spotify\" is running"}).Return(tc.Running, tc.Error)
|
||||
env.On("RunCommand", "osascript", []string{"-e", "tell application \"Spotify\" to player state as string"}).Return(tc.Status, nil)
|
||||
env.On("RunCommand", "osascript", []string{"-e", "tell application \"Spotify\" to artist of current track as string"}).Return(tc.Artist, nil)
|
||||
|
|
|
@ -3,15 +3,15 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSpotifyStringPlayingSong(t *testing.T) {
|
||||
expected := "\ue602 Candlemass - Spellbreaker"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
s := &Spotify{
|
||||
MusicPlayer: MusicPlayer{
|
||||
Artist: "Candlemass",
|
||||
|
@ -27,7 +27,7 @@ func TestSpotifyStringPlayingSong(t *testing.T) {
|
|||
|
||||
func TestSpotifyStringStoppedSong(t *testing.T) {
|
||||
expected := "\uf04d"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
s := &Spotify{
|
||||
MusicPlayer: MusicPlayer{
|
||||
Artist: "Candlemass",
|
||||
|
|
|
@ -5,9 +5,9 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -39,7 +39,7 @@ func TestSpotifyWindowsNative(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("QueryWindowTitles", "spotify.exe", `^(Spotify.*)|(.*\s-\s.*)$`).Return(tc.Title, tc.Error)
|
||||
env.On("QueryWindowTitles", "msedge.exe", `^(Spotify.*)`).Return("", &runtime.NotImplemented{})
|
||||
s := &Spotify{
|
||||
|
@ -80,7 +80,7 @@ func TestSpotifyWindowsPWA(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("QueryWindowTitles", "spotify.exe", "^(Spotify.*)|(.*\\s-\\s.*)$").Return("", &runtime.NotImplemented{})
|
||||
env.On("QueryWindowTitles", "msedge.exe", "^(Spotify.*)").Return(tc.Title, tc.Error)
|
||||
s := &Spotify{
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -66,7 +66,7 @@ func TestSpotifyWsl(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("IsWsl").Return(true)
|
||||
env.On("RunCommand", "tasklist.exe", []string{"/V", "/FI", "Imagename eq Spotify.exe", "/FO", "CSV", "/NH"}).Return(tc.ExecOutput, nil)
|
||||
s := &Spotify{
|
||||
|
|
|
@ -3,12 +3,12 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestStatusWriterEnabled(t *testing.T) {
|
||||
|
@ -24,13 +24,13 @@ func TestStatusWriterEnabled(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("StatusCodes").Return(tc.Status, "")
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Code: 133,
|
||||
})
|
||||
env.On("Error", mock2.Anything).Return(nil)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("Error", testify_.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
|
||||
props := properties.Map{}
|
||||
if len(tc.Template) > 0 {
|
||||
|
@ -91,12 +91,12 @@ func TestFormatStatus(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := new(mock.Environment)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Code: 133,
|
||||
})
|
||||
env.On("Error", mock2.Anything).Return(nil)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("Error", testify_.Anything).Return(nil)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
props := properties.Map{
|
||||
StatusTemplate: tc.Template,
|
||||
StatusSeparator: tc.Separator,
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/http"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
)
|
||||
|
||||
// StravaAPI is a wrapper around http.Oauth
|
||||
|
@ -133,8 +133,14 @@ func (s *Strava) Init(props properties.Properties, env runtime.Environment) {
|
|||
AccessTokenKey: StravaAccessTokenKey,
|
||||
RefreshTokenKey: StravaRefreshTokenKey,
|
||||
SegmentName: "strava",
|
||||
AccessToken: s.props.GetString(properties.AccessToken, ""),
|
||||
RefreshToken: s.props.GetString(properties.RefreshToken, ""),
|
||||
Request: http.Request{
|
||||
Env: env,
|
||||
CacheTimeout: s.props.GetInt(properties.CacheTimeout, 30),
|
||||
HTTPTimeout: s.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout),
|
||||
},
|
||||
}
|
||||
oauth.Init(env, props)
|
||||
|
||||
s.api = &stravaAPI{
|
||||
OAuthRequest: *oauth,
|
||||
|
|
|
@ -5,15 +5,15 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type mockedStravaAPI struct {
|
||||
mock2.Mock
|
||||
testify_.Mock
|
||||
}
|
||||
|
||||
func (s *mockedStravaAPI) GetActivities() ([]*StravaData, error) {
|
||||
|
@ -109,7 +109,7 @@ func TestStravaSegment(t *testing.T) {
|
|||
tc.Template = strava.Template()
|
||||
}
|
||||
|
||||
var got = renderTemplate(&mock.MockedEnvironment{}, tc.Template, strava)
|
||||
var got = renderTemplate(&mock.Environment{}, tc.Template, strava)
|
||||
assert.Equal(t, tc.ExpectedString, got, tc.Case)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,15 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSvnEnabledToolNotFound(t *testing.T) {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "svn").Return(false)
|
||||
env.On("GOOS").Return("")
|
||||
|
@ -31,7 +31,7 @@ func TestSvnEnabledInWorkingDirectory(t *testing.T) {
|
|||
ParentFolder: "/dir",
|
||||
IsDir: true,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("HasCommand", "svn").Return(true)
|
||||
env.On("GOOS").Return("")
|
||||
|
@ -157,7 +157,7 @@ func TestSvnTemplateString(t *testing.T) {
|
|||
props := properties.Map{
|
||||
FetchStatus: true,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
tc.Svn.env = env
|
||||
tc.Svn.props = props
|
||||
assert.Equal(t, tc.Expected, renderTemplate(env, tc.Template, tc.Svn), tc.Case)
|
||||
|
@ -230,7 +230,7 @@ R Moved.File`,
|
|||
ParentFolder: "/dir",
|
||||
IsDir: true,
|
||||
}
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("InWSLSharedDrive").Return(false)
|
||||
env.On("IsWsl").Return(false)
|
||||
env.On("HasCommand", "svn").Return(true)
|
||||
|
@ -291,7 +291,7 @@ func TestRepo(t *testing.T) {
|
|||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("RunCommand", "svn", []string{"info", "", "--show-item", "repos-root-url"}).Return(tc.Repo, nil)
|
||||
s := &Svn{
|
||||
scm: scm{
|
||||
|
|
|
@ -4,9 +4,9 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -77,7 +77,7 @@ func TestSysInfo(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("SystemInfo").Return(&tc.SysInfo, tc.Error)
|
||||
sysInfo := &SystemInfo{}
|
||||
props := properties.Map{
|
||||
|
|
|
@ -4,9 +4,9 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestTalosctlSegment(t *testing.T) {
|
||||
|
@ -39,11 +39,11 @@ func TestTalosctlSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return("home")
|
||||
fcPath := filepath.Join("home", ".talos", "config")
|
||||
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
talos := TalosCTL{
|
||||
env: env,
|
||||
}
|
||||
|
@ -75,12 +75,12 @@ func TestGetTalosctlActiveConfig(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("Home").Return("home")
|
||||
configPath := filepath.Join("home", ".talos")
|
||||
contentPath := filepath.Join(configPath, "config")
|
||||
env.On("FileContent", contentPath).Return(tc.ActiveConfig)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
talos := TalosCTL{
|
||||
env: env,
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -82,7 +82,7 @@ func TestTerraform(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
|
||||
env.On("HasCommand", "terraform").Return(tc.HasTfCommand)
|
||||
env.On("HasFolder", ".terraform").Return(tc.HasTfFolder)
|
||||
|
|
|
@ -3,8 +3,8 @@ package segments
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -26,9 +26,9 @@ func TestTextSegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("PathSeparator").Return("/")
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
UserName: "Posh",
|
||||
Env: map[string]string{
|
||||
"HELLO": "hello",
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -41,7 +41,7 @@ func TestTimeSegmentTemplate(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
tempus := &Time{
|
||||
env: env,
|
||||
props: properties.Map{},
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -96,7 +96,7 @@ func TestUI5Tooling(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func mockFilePresence(tc *testCase, ui5tooling *UI5Tooling, env *mock.MockedEnvironment) error {
|
||||
func mockFilePresence(tc *testCase, ui5tooling *UI5Tooling, env *mock.Environment) error {
|
||||
for _, f := range ui5tooling.language.extensions {
|
||||
match, err := filepath.Match(f, tc.UI5YamlFilename)
|
||||
|
||||
|
|
|
@ -7,11 +7,11 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestUmbracoSegment(t *testing.T) {
|
||||
|
@ -108,7 +108,7 @@ func TestUmbracoSegment(t *testing.T) {
|
|||
|
||||
for _, tc := range cases {
|
||||
// Prepare/arrange the test
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
var sampleCSProj, sampleWebConfig, sampleNonUmbracoCSProj string
|
||||
|
||||
if tc.HasCsproj {
|
||||
|
@ -136,7 +136,7 @@ func TestUmbracoSegment(t *testing.T) {
|
|||
env.On("FileContent", filepath.Join(umbracoProjectDirectory, "MyProject.csproj")).Return(sampleCSProj)
|
||||
env.On("FileContent", filepath.Join(umbracoProjectDirectory, "ANonUmbracoProject.csproj")).Return(sampleNonUmbracoCSProj)
|
||||
env.On("FileContent", filepath.Join(umbracoProjectDirectory, "web.config")).Return(sampleWebConfig)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
|
||||
if tc.HasUmbracoFolder {
|
||||
fileInfo := &runtime.FileInfo{
|
||||
|
|
|
@ -6,11 +6,12 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -80,9 +81,9 @@ func TestUnitySegment(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Debug", mock2.Anything)
|
||||
env := new(mock.Environment)
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
env.On("Debug", testify_.Anything)
|
||||
|
||||
err := errors.New("no match at root level")
|
||||
var projectDir *runtime.FileInfo
|
||||
|
@ -215,9 +216,9 @@ func TestUnitySegmentCSharpWebRequest(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Error", mock2.Anything).Return()
|
||||
env.On("Debug", mock2.Anything)
|
||||
env := new(mock.Environment)
|
||||
env.On("Error", testify_.Anything).Return()
|
||||
env.On("Debug", testify_.Anything)
|
||||
|
||||
err := errors.New("no match at root level")
|
||||
var projectDir *runtime.FileInfo
|
||||
|
@ -234,7 +235,7 @@ func TestUnitySegmentCSharpWebRequest(t *testing.T) {
|
|||
}
|
||||
env.On("HasParentFilePath", "ProjectSettings").Return(projectDir, err)
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache := &cache_.Cache{}
|
||||
cache.On("Get", tc.CacheGet.key).Return(tc.CacheGet.val, tc.CacheGet.found)
|
||||
cache.On("Set", tc.CacheSet.key, tc.CacheSet.val, -1).Return()
|
||||
env.On("Cache").Return(cache)
|
||||
|
|
|
@ -6,12 +6,13 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/build"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
|
||||
|
||||
"github.com/alecthomas/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestUpgrade(t *testing.T) {
|
||||
|
@ -65,8 +66,8 @@ func TestUpgrade(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
cache := &mock.MockedCache{}
|
||||
env := new(mock.Environment)
|
||||
cache := &cache_.Cache{}
|
||||
|
||||
env.On("Cache").Return(cache)
|
||||
if len(tc.CachedVersion) == 0 {
|
||||
|
@ -74,7 +75,7 @@ func TestUpgrade(t *testing.T) {
|
|||
}
|
||||
cacheData := fmt.Sprintf(`{"latest":"%s", "current": "%s"}`, tc.LatestVersion, tc.CachedVersion)
|
||||
cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
cache.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)
|
||||
|
||||
build.Version = tc.CurrentVersion
|
||||
|
||||
|
|
|
@ -5,12 +5,13 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestWTTrackedTime(t *testing.T) {
|
||||
|
@ -72,18 +73,18 @@ func TestWTTrackedTime(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
response := fmt.Sprintf(`{"cumulative_total": {"seconds": %.2f, "text": "x"}}`, float64(tc.Seconds))
|
||||
|
||||
env.On("HTTPRequest", FAKEAPIURL).Return([]byte(response), tc.Error)
|
||||
|
||||
cache := &mock.MockedCache{}
|
||||
cache.On("Get", FAKEAPIURL).Return(response, !tc.CacheFoundFail)
|
||||
cache.On("Set", FAKEAPIURL, response, tc.CacheTimeout).Return()
|
||||
env.On("Cache").Return(cache)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
mockedCache := &cache_.Cache{}
|
||||
mockedCache.On("Get", FAKEAPIURL).Return(response, !tc.CacheFoundFail)
|
||||
mockedCache.On("Set", FAKEAPIURL, response, tc.CacheTimeout).Return()
|
||||
env.On("Cache").Return(mockedCache)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{"HELLO": "hello"},
|
||||
})
|
||||
|
||||
|
@ -125,11 +126,11 @@ func TestWTGetUrl(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := &mock.MockedEnvironment{}
|
||||
env := &mock.Environment{}
|
||||
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{"HELLO": "hello"},
|
||||
})
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -60,7 +60,7 @@ func TestWinReg(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("GOOS").Return(runtime.WINDOWS)
|
||||
env.On("WindowsRegistryKeyValue", tc.Path).Return(tc.getWRKVOutput, tc.Err)
|
||||
r := &WindowsRegistry{
|
||||
|
|
|
@ -8,11 +8,11 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/http"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/http"
|
||||
|
||||
http2 "net/http"
|
||||
httplib "net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
|
@ -120,15 +120,18 @@ func (w *withingsAPI) GetSleep() (*WithingsData, error) {
|
|||
}
|
||||
|
||||
func (w *withingsAPI) getWithingsData(endpoint string, formData url.Values) (*WithingsData, error) {
|
||||
modifiers := func(request *http2.Request) {
|
||||
request.Method = http2.MethodPost
|
||||
modifiers := func(request *httplib.Request) {
|
||||
request.Method = httplib.MethodPost
|
||||
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
|
||||
body := strings.NewReader(formData.Encode())
|
||||
|
||||
data, err := http.OauthResult[*WithingsData](w.OAuthRequest, endpoint, body, modifiers)
|
||||
if data != nil && data.Status != 0 {
|
||||
return nil, errors.New("Withings API error: " + strconv.Itoa(data.Status))
|
||||
}
|
||||
|
||||
return data, err
|
||||
}
|
||||
|
||||
|
@ -207,13 +210,16 @@ func (w *Withings) getSleep() bool {
|
|||
if sleepStart.IsZero() || start.Before(sleepStart) {
|
||||
sleepStart = start
|
||||
}
|
||||
|
||||
end := time.Unix(series.Enddate, 0)
|
||||
if sleepStart.IsZero() || start.After(sleepEnd) {
|
||||
sleepEnd = end
|
||||
}
|
||||
}
|
||||
|
||||
sleepHours := sleepEnd.Sub(sleepStart).Hours()
|
||||
w.SleepHours = fmt.Sprintf("%0.1f", sleepHours)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -224,8 +230,14 @@ func (w *Withings) Init(props properties.Properties, env runtime.Environment) {
|
|||
AccessTokenKey: WithingsAccessTokenKey,
|
||||
RefreshTokenKey: WithingsRefreshTokenKey,
|
||||
SegmentName: "withings",
|
||||
AccessToken: w.props.GetString(properties.AccessToken, ""),
|
||||
RefreshToken: w.props.GetString(properties.RefreshToken, ""),
|
||||
Request: http.Request{
|
||||
Env: env,
|
||||
CacheTimeout: w.props.GetInt(properties.CacheTimeout, 30),
|
||||
HTTPTimeout: w.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout),
|
||||
},
|
||||
}
|
||||
oauth.Init(env, props)
|
||||
|
||||
w.api = &withingsAPI{
|
||||
OAuthRequest: oauth,
|
||||
|
|
|
@ -5,15 +5,15 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type mockedWithingsAPI struct {
|
||||
mock2.Mock
|
||||
testify_.Mock
|
||||
}
|
||||
|
||||
func (s *mockedWithingsAPI) GetMeasures(meastypes string) (*WithingsData, error) {
|
||||
|
@ -165,7 +165,7 @@ func TestWithingsSegment(t *testing.T) {
|
|||
tc.Template = withings.Template()
|
||||
}
|
||||
|
||||
var got = renderTemplate(&mock.MockedEnvironment{}, tc.Template, withings)
|
||||
var got = renderTemplate(&mock.Environment{}, tc.Template, withings)
|
||||
assert.Equal(t, tc.ExpectedString, got, tc.Case)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,15 +4,15 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func bootstrapYTMDATest(json string, err error) *Ytm {
|
||||
url := "http://127.0.0.1:9863"
|
||||
env := new(mock.MockedEnvironment)
|
||||
env := new(mock.Environment)
|
||||
env.On("HTTPRequest", url+"/query").Return([]byte(json), err)
|
||||
ytm := &Ytm{
|
||||
env: env,
|
||||
|
|
|
@ -4,12 +4,12 @@ import (
|
|||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/color"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestConsoleBackgroundColorTemplate(t *testing.T) {
|
||||
|
@ -23,9 +23,9 @@ func TestConsoleBackgroundColorTemplate(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := new(mock.Environment)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: map[string]string{
|
||||
"TERM_PROGRAM": tc.Term,
|
||||
},
|
||||
|
|
|
@ -3,11 +3,11 @@ package template
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestGlob(t *testing.T) {
|
||||
|
@ -22,9 +22,9 @@ func TestGlob(t *testing.T) {
|
|||
{Case: "multiple glob", Expected: "NOK", Template: `{{ if or (glob "package.json") (glob "node_modules") }}OK{{ else }}NOK{{ end }}`},
|
||||
}
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := &mock.Environment{}
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
for _, tc := range cases {
|
||||
|
|
|
@ -3,11 +3,11 @@ package template
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestUrl(t *testing.T) {
|
||||
|
@ -21,13 +21,13 @@ func TestUrl(t *testing.T) {
|
|||
{Case: "invalid url", Expected: "", Template: `{{ url "link" "Foo" }}`, ShouldError: true},
|
||||
}
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := &mock.Environment{}
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
for _, tc := range cases {
|
||||
tmpl := &Text{
|
||||
Template: tc.Template,
|
||||
|
@ -52,9 +52,9 @@ func TestPath(t *testing.T) {
|
|||
{Case: "valid path", Expected: "<LINK>file:/test/test<TEXT>link</TEXT></LINK>", Template: `{{ path "link" "/test/test" }}`},
|
||||
}
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := &mock.Environment{}
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
for _, tc := range cases {
|
||||
|
|
|
@ -3,11 +3,11 @@ package template
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
testify_ "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestHResult(t *testing.T) {
|
||||
|
@ -21,13 +21,13 @@ func TestHResult(t *testing.T) {
|
|||
{Case: "Not a number", Template: `{{ hresult "no number" }}`, ShouldError: true},
|
||||
}
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("TemplateCache").Return(&runtime.TemplateCache{
|
||||
env := &mock.Environment{}
|
||||
env.On("TemplateCache").Return(&cache.Template{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
|
||||
env.On("Error", testify_.Anything)
|
||||
env.On("Debug", testify_.Anything)
|
||||
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
|
||||
for _, tc := range cases {
|
||||
tmpl := &Text{
|
||||
Template: tc.Template,
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue