Skip to content

Commit

Permalink
Format unsaved entity file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jan 10, 2025
1 parent 02eefc9 commit f75df94
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
11 changes: 6 additions & 5 deletions pkg/heartbeat/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ func WithFormatting() HandleOption {
}
}

// Format accepts a heartbeat formats it's filepath and returns the formatted version.
// Format accepts a heartbeat to format its filepath and returns the formatted version.
func Format(ctx context.Context, h Heartbeat) Heartbeat {
if !h.IsUnsavedEntity && (runtime.GOOS != "windows" || !windows.IsWindowsNetworkMount(h.Entity)) {
formatLinuxFilePath(ctx, &h)
}

if runtime.GOOS == "windows" {
formatWindowsFilePath(ctx, &h)
return h
}

if !windows.IsWindowsNetworkMount(h.Entity) {
formatLinuxFilePath(ctx, &h)
}

return h
Expand Down
15 changes: 15 additions & 0 deletions pkg/heartbeat/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func TestWithFormatting(t *testing.T) {
}, result)
}

func TestFormat_WindowsUnixProjectPathOverride(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping because OS is not windows.")
}

h := heartbeat.Heartbeat{
Entity: `C:\Users\project\main.go`,
EntityType: heartbeat.FileType,
}

formatted := heartbeat.Format(context.Background(), h)

assert.Equal(t, "C:/Users/project/main.go", formatted.Entity)
}

func TestFormat_NetworkMount(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping because OS is not windows.")
Expand Down
2 changes: 1 addition & 1 deletion pkg/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
// FormatFilePath formats a windows filepath by converting backslash to
// frontslash and ensuring that drive letter is upper case.
func FormatFilePath(fp string) string {
isWindowsNetworkMount := windowsNetworkMountRegex.MatchString(fp)
isWindowsNetworkMount := IsWindowsNetworkMount(fp)

fp = backslashReplaceRegex.ReplaceAllString(fp, "/")

Expand Down

0 comments on commit f75df94

Please sign in to comment.