mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-13 12:17:26 -08:00
fix(windows): consistent casing for drive and only drive letters
resolves #1173
This commit is contained in:
parent
8b20d8fd81
commit
a01f4faa1e
|
@ -107,12 +107,19 @@ func (c *commandCache) get(command string) (string, bool) {
|
|||
return command, ok
|
||||
}
|
||||
|
||||
type tracer struct {
|
||||
type tracer interface {
|
||||
init(home string)
|
||||
close()
|
||||
trace(start time.Time, function string, args ...string)
|
||||
error(message string)
|
||||
}
|
||||
|
||||
type fileTracer struct {
|
||||
file *os.File
|
||||
debug bool
|
||||
}
|
||||
|
||||
func (t *tracer) init(home string) {
|
||||
func (t *fileTracer) init(home string) {
|
||||
if !t.debug {
|
||||
return
|
||||
}
|
||||
|
@ -126,7 +133,7 @@ func (t *tracer) init(home string) {
|
|||
log.Println("#### start oh-my-posh run ####")
|
||||
}
|
||||
|
||||
func (t *tracer) close() {
|
||||
func (t *fileTracer) close() {
|
||||
if !t.debug {
|
||||
return
|
||||
}
|
||||
|
@ -134,7 +141,7 @@ func (t *tracer) close() {
|
|||
_ = t.file.Close()
|
||||
}
|
||||
|
||||
func (t *tracer) trace(start time.Time, function string, args ...string) {
|
||||
func (t *fileTracer) trace(start time.Time, function string, args ...string) {
|
||||
if !t.debug {
|
||||
return
|
||||
}
|
||||
|
@ -143,7 +150,7 @@ func (t *tracer) trace(start time.Time, function string, args ...string) {
|
|||
log.Println(trace)
|
||||
}
|
||||
|
||||
func (t *tracer) error(message string) {
|
||||
func (t *fileTracer) error(message string) {
|
||||
if !t.debug {
|
||||
return
|
||||
}
|
||||
|
@ -156,7 +163,7 @@ type environment struct {
|
|||
cwd string
|
||||
cmdCache *commandCache
|
||||
fileCache *fileCache
|
||||
tracer *tracer
|
||||
tracer tracer
|
||||
}
|
||||
|
||||
func (env *environment) init(args *args) {
|
||||
|
@ -164,7 +171,7 @@ func (env *environment) init(args *args) {
|
|||
env.cmdCache = &commandCache{
|
||||
commands: newConcurrentMap(),
|
||||
}
|
||||
tracer := &tracer{
|
||||
tracer := &fileTracer{
|
||||
debug: *args.Debug,
|
||||
}
|
||||
tracer.init(env.homeDir())
|
||||
|
@ -185,7 +192,8 @@ func (env *environment) getcwd() string {
|
|||
}
|
||||
correctPath := func(pwd string) string {
|
||||
// on Windows, and being case sensitive and not consistent and all, this gives silly issues
|
||||
return strings.Replace(pwd, "c:", "C:", 1)
|
||||
driveLetter := getCompiledRegex(`^[a-z]:`)
|
||||
return driveLetter.ReplaceAllStringFunc(pwd, strings.ToUpper)
|
||||
}
|
||||
if env.args != nil && *env.args.PWD != "" {
|
||||
env.cwd = correctPath(*env.args.PWD)
|
||||
|
|
|
@ -2,10 +2,44 @@ package main
|
|||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockedTracer is an autogenerated mock type for the tracer type
|
||||
type MockedTracer struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// close provides a mock function with given fields:
|
||||
func (_m *MockedTracer) close() {
|
||||
_m.Called()
|
||||
}
|
||||
|
||||
// error provides a mock function with given fields: message
|
||||
func (_m *MockedTracer) error(message string) {
|
||||
_m.Called(message)
|
||||
}
|
||||
|
||||
// init provides a mock function with given fields: home
|
||||
func (_m *MockedTracer) init(home string) {
|
||||
_m.Called(home)
|
||||
}
|
||||
|
||||
// trace provides a mock function with given fields: start, function, args
|
||||
func (_m *MockedTracer) trace(start time.Time, function string, args ...string) {
|
||||
_va := make([]interface{}, len(args))
|
||||
for _i := range args {
|
||||
_va[_i] = args[_i]
|
||||
}
|
||||
var _ca []interface{}
|
||||
_ca = append(_ca, start, function)
|
||||
_ca = append(_ca, _va...)
|
||||
_m.Called(_ca...)
|
||||
}
|
||||
|
||||
func TestNormalHostName(t *testing.T) {
|
||||
hostName := "hello"
|
||||
assert.Equal(t, hostName, cleanHostName(hostName))
|
||||
|
@ -21,3 +55,29 @@ func TestHostNameWithLan(t *testing.T) {
|
|||
cleanHostName := cleanHostName(hostName)
|
||||
assert.Equal(t, "hello", cleanHostName)
|
||||
}
|
||||
|
||||
func TestWindowsPathWithDriveLetter(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
CWD string
|
||||
Expected string
|
||||
}{
|
||||
{Case: "C drive", CWD: `C:\Windows\`, Expected: `C:\Windows\`},
|
||||
{Case: "C drive lower case", CWD: `c:\Windows\`, Expected: `C:\Windows\`},
|
||||
{Case: "P drive lower case", CWD: `p:\some\`, Expected: `P:\some\`},
|
||||
{Case: "some drive lower case", CWD: `some:\some\`, Expected: `some:\some\`},
|
||||
{Case: "drive ending in c:", CWD: `src:\source\`, Expected: `src:\source\`},
|
||||
{Case: "registry drive", CWD: `HKLM:\SOFTWARE\magnetic:test\`, Expected: `HKLM:\SOFTWARE\magnetic:test\`},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
tracer := &MockedTracer{}
|
||||
tracer.On("trace", mock.Anything, mock.Anything, mock.Anything)
|
||||
env := &environment{
|
||||
tracer: tracer,
|
||||
args: &args{
|
||||
PWD: &tc.CWD,
|
||||
},
|
||||
}
|
||||
assert.Equal(t, env.getcwd(), tc.Expected)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -618,86 +618,55 @@ func TestGetFolderPathCustomMappedLocations(t *testing.T) {
|
|||
assert.Equal(t, "#", got)
|
||||
}
|
||||
|
||||
func testWritePathInfo(home, pwd, pathSeparator string) string {
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
FolderSeparatorIcon: " > ",
|
||||
FolderIcon: "f",
|
||||
HomeIcon: "~",
|
||||
},
|
||||
func TestAgnosterPath(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
Expected string
|
||||
Home string
|
||||
PWD string
|
||||
PathSeparator string
|
||||
}{
|
||||
{Case: "Windows outside home", Expected: "C: > f > f > location", Home: homeBillWindows, PWD: "C:\\Program Files\\Go\\location", PathSeparator: "\\"},
|
||||
{Case: "Windows oustide home", Expected: "~ > f > f > location", Home: homeBillWindows, PWD: homeBillWindows + "\\Documents\\Bill\\location", PathSeparator: "\\"},
|
||||
{Case: "Windows inside home zero levels", Expected: "C: > location", Home: homeBillWindows, PWD: "C:\\location", PathSeparator: "\\"},
|
||||
{Case: "Windows inside home one level", Expected: "C: > f > location", Home: homeBillWindows, PWD: "C:\\Program Files\\location", PathSeparator: "\\"},
|
||||
{Case: "Windows lower case drive letter", Expected: "C: > Windows", Home: homeBillWindows, PWD: "C:\\Windows\\", PathSeparator: "\\"},
|
||||
{Case: "Windows lower case drive letter (other)", Expected: "P: > Other", Home: homeBillWindows, PWD: "P:\\Other\\", PathSeparator: "\\"},
|
||||
{Case: "Windows lower word drive", Expected: "some: > some", Home: homeBillWindows, PWD: "some:\\some\\", PathSeparator: "\\"},
|
||||
{Case: "Windows lower word drive (ending with c)", Expected: "src: > source", Home: homeBillWindows, PWD: "src:\\source\\", PathSeparator: "\\"},
|
||||
{Case: "Windows lower word drive (arbitrary cases)", Expected: "sRc: > source", Home: homeBillWindows, PWD: "sRc:\\source\\", PathSeparator: "\\"},
|
||||
{Case: "Windows registry drive", Expected: "\uf013 > f > magnetic:test", Home: homeBillWindows, PWD: "HKLM:\\SOFTWARE\\magnetic:test\\", PathSeparator: "\\"},
|
||||
{Case: "Windows registry drive case sensitive", Expected: "\uf013 > f > magnetic:TOAST", Home: homeBillWindows, PWD: "HKLM:\\SOFTWARE\\magnetic:TOAST\\", PathSeparator: "\\"},
|
||||
{Case: "Unix outside home", Expected: "mnt > f > f > location", Home: homeJan, PWD: "/mnt/go/test/location", PathSeparator: "/"},
|
||||
{Case: "Unix inside home", Expected: "~ > f > f > location", Home: homeJan, PWD: homeJan + "/docs/jan/location", PathSeparator: "/"},
|
||||
{Case: "Unix outside home zero levels", Expected: "mnt > location", Home: homeJan, PWD: "/mnt/location", PathSeparator: "/"},
|
||||
{Case: "Unix outside home one level", Expected: "mnt > f > location", Home: homeJan, PWD: "/mnt/folder/location", PathSeparator: "/"},
|
||||
}
|
||||
env := new(MockedEnvironment)
|
||||
env.On("homeDir", nil).Return(home)
|
||||
env.On("getPathSeperator", nil).Return(pathSeparator)
|
||||
env.On("getcwd", nil).Return(pwd)
|
||||
env.On("getRuntimeGOOS", nil).Return("")
|
||||
args := &args{
|
||||
PSWD: &pwd,
|
||||
|
||||
for _, tc := range cases {
|
||||
props := &properties{
|
||||
values: map[Property]interface{}{
|
||||
FolderSeparatorIcon: " > ",
|
||||
FolderIcon: "f",
|
||||
HomeIcon: "~",
|
||||
},
|
||||
}
|
||||
env := new(MockedEnvironment)
|
||||
env.On("homeDir", nil).Return(tc.Home)
|
||||
env.On("getPathSeperator", nil).Return(tc.PathSeparator)
|
||||
env.On("getcwd", nil).Return(tc.PWD)
|
||||
env.On("getRuntimeGOOS", nil).Return("")
|
||||
args := &args{
|
||||
PSWD: &tc.PWD,
|
||||
}
|
||||
env.On("getArgs", nil).Return(args)
|
||||
path := &path{
|
||||
env: env,
|
||||
props: props,
|
||||
}
|
||||
got := path.getAgnosterPath()
|
||||
assert.Equal(t, tc.Expected, got, tc.Case)
|
||||
}
|
||||
env.On("getArgs", nil).Return(args)
|
||||
path := &path{
|
||||
env: env,
|
||||
props: props,
|
||||
}
|
||||
return path.getAgnosterPath()
|
||||
}
|
||||
|
||||
func TestWritePathInfoWindowsOutsideHome(t *testing.T) {
|
||||
home := homeBillWindows
|
||||
want := "C: > f > f > location"
|
||||
got := testWritePathInfo(home, "C:\\Program Files\\Go\\location", "\\")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoWindowsInsideHome(t *testing.T) {
|
||||
home := homeBillWindows
|
||||
location := home + "\\Documents\\Bill\\location"
|
||||
want := "~ > f > f > location"
|
||||
got := testWritePathInfo(home, location, "\\")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoWindowsOutsideHomeZeroLevels(t *testing.T) {
|
||||
home := homeBillWindows
|
||||
want := "C: > location"
|
||||
got := testWritePathInfo(home, "C:\\location", "\\")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoWindowsOutsideHomeOneLevels(t *testing.T) {
|
||||
home := homeBillWindows
|
||||
want := "C: > f > location"
|
||||
got := testWritePathInfo(home, "C:\\Program Files\\location", "\\")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoUnixOutsideHome(t *testing.T) {
|
||||
home := homeJan
|
||||
want := "mnt > f > f > location"
|
||||
got := testWritePathInfo(home, "/mnt/go/test/location", "/")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoUnixInsideHome(t *testing.T) {
|
||||
home := homeJan
|
||||
location := home + "/docs/jan/location"
|
||||
want := "~ > f > f > location"
|
||||
got := testWritePathInfo(home, location, "/")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoUnixOutsideHomeZeroLevels(t *testing.T) {
|
||||
home := homeJan
|
||||
want := "mnt > location"
|
||||
got := testWritePathInfo(home, "/mnt/location", "/")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestWritePathInfoUnixOutsideHomeOneLevels(t *testing.T) {
|
||||
home := homeJan
|
||||
want := "mnt > f > location"
|
||||
got := testWritePathInfo(home, "/mnt/folder/location", "/")
|
||||
assert.Equal(t, want, got)
|
||||
}
|
||||
|
||||
func TestGetPwd(t *testing.T) {
|
||||
|
|
Loading…
Reference in a new issue