Skip to content

Commit

Permalink
Test change windows drive letter to uppercase always
Browse files Browse the repository at this point in the history
  • Loading branch information
anjannath committed Sep 20, 2023
1 parent 9ccf0b8 commit 7e6b566
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/os/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,24 @@ func TestFileExists(t *testing.T) {
filename = filepath.Join(dirname, "nonexistent")
assert.False(t, FileExists(filename))
}

func TestUppercaseDriveLetter(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Test only works for windows")
}

tests := []struct {
got string
expected string
}{
{"c:\\crc\\crc-org", "C:\\crc\\crc-org"},
{"d:\\happy\\world", "D:\\happy\\world"},
{"D:\\happy\\world", "D:\\happy\\world"},
{":\\crc\\snc", ":\\crc\\snc"},
{"\\crc\\snc", "\\crc\\snc"},
}

for _, tt := range tests {
assert.Equal(t, tt.expected, uppercaseDriveLetterInPath(tt.got), "test failed")
}
}
8 changes: 8 additions & 0 deletions pkg/os/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ func GetCurrentUsername() (string, error) {
}
return u.Username, nil
}

func GetCurrentExecutablePath() (string, error) {
return os.Executable()
}

func uppercaseDriveLetterInPath(binPath string) string {
return binPath
}
21 changes: 21 additions & 0 deletions pkg/os/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"io"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/crc-org/crc/v2/pkg/crc/logging"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
Expand Down Expand Up @@ -44,3 +46,22 @@ func GetCurrentUsername() (string, error) {
}
return "", errors.New("unable to find the username of current user")
}

func GetCurrentExecutable() (string, error) {
binPath, err := os.Executable()
if err != nil {
return "", err
}
return uppercaseDriveLetterInPath(binPath), nil
}

func uppercaseDriveLetterInPath(binPath string) string {
drive := filepath.VolumeName(binPath)
if drive != "" {
driveUpperCase := strings.ToUpper(drive)
normalizedBinPath := strings.Replace(binPath, drive, driveUpperCase, 1)
logging.Debugf("Original path: %s. Normalized path: %s", binPath, normalizedBinPath)
return strings.Replace(binPath, drive, driveUpperCase, 1)
}
return binPath
}

0 comments on commit 7e6b566

Please sign in to comment.