Skip to content

Commit

Permalink
Prioritize windows when formatting filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Jan 9, 2025
1 parent 02eefc9 commit fabee29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
11 changes: 8 additions & 3 deletions pkg/heartbeat/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ 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 h.IsUnsavedEntity {
return h

Check warning on line 42 in pkg/heartbeat/format.go

View check run for this annotation

Codecov / codecov/patch

pkg/heartbeat/format.go#L42

Added line #L42 was not covered by tests
}

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

Check warning on line 48 in pkg/heartbeat/format.go

View check run for this annotation

Codecov / codecov/patch

pkg/heartbeat/format.go#L47-L48

Added lines #L47 - L48 were not covered by tests

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 fabee29

Please sign in to comment.