Skip to content

Commit

Permalink
Format unsaved entity file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Jan 10, 2025
1 parent 02eefc9 commit 0e366a3
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
7 changes: 3 additions & 4 deletions cmd/heartbeat/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func TestSendHeartbeats_NonExistingEntity(t *testing.T) {
assert.Contains(t, string(output), "skipping because of non-existing file")
}

func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
func TestSendHeartbeats_ExtraHeartbeatsIsUnsavedEntity(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
Expand Down Expand Up @@ -639,8 +639,8 @@ func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {

expectedBodyStr := fmt.Sprintf(
string(expectedBody),
entities[0].Entity, userAgent,
entities[1].Entity, userAgent,
entities[0].Entity, subfolders, userAgent,
entities[1].Entity, subfolders, userAgent,
entities[2].Entity, subfolders, userAgent,
)

Expand Down Expand Up @@ -707,7 +707,6 @@ func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
v.Set("lines-in-file", 91)
v.Set("plugin", plugin)
v.Set("time", 1585598051)
v.Set("timeout", 5)
v.Set("extra-heartbeats", true)
v.Set("log-file", logFile.Name())
v.Set("verbose", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"lineno": 11,
"lines": 91,
"project": "wakatime-cli",
"project_root_count": %d,
"type": "file",
"time": 1585598051,
"user_agent": "%s"
Expand All @@ -20,6 +21,7 @@
"lineno": 42,
"lines": 45,
"project": "wakatime-cli",
"project_root_count": %d,
"type": "file",
"time": 1585598052,
"user_agent": "%s"
Expand Down
13 changes: 9 additions & 4 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.EntityType != FileType {
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 Expand Up @@ -94,7 +99,7 @@ func formatWindowsFilePath(ctx context.Context, h *Heartbeat) {

h.Entity = windows.FormatFilePath(h.Entity)

if !h.IsUnsavedEntity && !windows.IsWindowsNetworkMount(h.Entity) {
if !windows.IsWindowsNetworkMount(h.Entity) {

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

View check run for this annotation

Codecov / codecov/patch

pkg/heartbeat/format.go#L102

Added line #L102 was not covered by tests
var err error

h.LocalFile, err = windows.FormatLocalFilePath(h.LocalFile, h.Entity)
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_WindowsPath(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 0e366a3

Please sign in to comment.