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 6f6d40e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 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
21 changes: 16 additions & 5 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
}

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 @@ -92,9 +97,15 @@ func formatLinuxFilePath(ctx context.Context, h *Heartbeat) {
func formatWindowsFilePath(ctx context.Context, h *Heartbeat) {
logger := log.Extract(ctx)

h.Entity = windows.FormatFilePath(h.Entity)
formatted, err := filepath.Abs(h.Entity)
if err != nil {
logger.Debugf("failed to resolve absolute path for %q: %s", h.Entity, err)
return
}

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

View check run for this annotation

Codecov / codecov/patch

pkg/heartbeat/format.go#L100-L104

Added lines #L100 - L104 were not covered by tests

h.Entity = windows.FormatFilePath(formatted)

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

View check run for this annotation

Codecov / codecov/patch

pkg/heartbeat/format.go#L106

Added line #L106 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/heartbeat/format.go#L108

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

h.LocalFile, err = windows.FormatLocalFilePath(h.LocalFile, h.Entity)
Expand Down
37 changes: 37 additions & 0 deletions pkg/heartbeat/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,43 @@ func TestWithFormatting(t *testing.T) {
}, result)
}

func TestFormat_NotFileType(t *testing.T) {
tests := map[string]heartbeat.EntityType{
"app": heartbeat.AppType,
"domain": heartbeat.DomainType,
"event": heartbeat.EventType,
"url": heartbeat.URLType,
}

for name, entityType := range tests {
t.Run(name, func(t *testing.T) {
h := heartbeat.Heartbeat{
Entity: "/unmodified",
EntityType: entityType,
}

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

assert.Equal(t, "/unmodified", formatted.Entity)
})
}
}

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 6f6d40e

Please sign in to comment.