Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format unsaved entity file paths #1158

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
}

// 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 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
Loading