Skip to content

Commit 6f6d40e

Browse files
committed
Format unsaved entity file paths
1 parent 02eefc9 commit 6f6d40e

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

cmd/heartbeat/heartbeat_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func TestSendHeartbeats_NonExistingEntity(t *testing.T) {
598598
assert.Contains(t, string(output), "skipping because of non-existing file")
599599
}
600600

601-
func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
601+
func TestSendHeartbeats_ExtraHeartbeatsIsUnsavedEntity(t *testing.T) {
602602
resetSingleton(t)
603603

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

640640
expectedBodyStr := fmt.Sprintf(
641641
string(expectedBody),
642-
entities[0].Entity, userAgent,
643-
entities[1].Entity, userAgent,
642+
entities[0].Entity, subfolders, userAgent,
643+
entities[1].Entity, subfolders, userAgent,
644644
entities[2].Entity, subfolders, userAgent,
645645
)
646646

@@ -707,7 +707,6 @@ func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
707707
v.Set("lines-in-file", 91)
708708
v.Set("plugin", plugin)
709709
v.Set("time", 1585598051)
710-
v.Set("timeout", 5)
711710
v.Set("extra-heartbeats", true)
712711
v.Set("log-file", logFile.Name())
713712
v.Set("verbose", true)

cmd/heartbeat/testdata/api_heartbeats_request_is_unsaved_entity_template.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"lineno": 11,
88
"lines": 91,
99
"project": "wakatime-cli",
10+
"project_root_count": %d,
1011
"type": "file",
1112
"time": 1585598051,
1213
"user_agent": "%s"
@@ -20,6 +21,7 @@
2021
"lineno": 42,
2122
"lines": 45,
2223
"project": "wakatime-cli",
24+
"project_root_count": %d,
2325
"type": "file",
2426
"time": 1585598052,
2527
"user_agent": "%s"

pkg/heartbeat/format.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,19 @@ func WithFormatting() HandleOption {
3636
}
3737
}
3838

39-
// Format accepts a heartbeat formats it's filepath and returns the formatted version.
39+
// Format accepts a heartbeat to format its filepath and returns the formatted version.
4040
func Format(ctx context.Context, h Heartbeat) Heartbeat {
41-
if !h.IsUnsavedEntity && (runtime.GOOS != "windows" || !windows.IsWindowsNetworkMount(h.Entity)) {
42-
formatLinuxFilePath(ctx, &h)
41+
if h.EntityType != FileType {
42+
return h
4343
}
4444

4545
if runtime.GOOS == "windows" {
4646
formatWindowsFilePath(ctx, &h)
47+
return h
48+
}
49+
50+
if !windows.IsWindowsNetworkMount(h.Entity) {
51+
formatLinuxFilePath(ctx, &h)
4752
}
4853

4954
return h
@@ -92,9 +97,15 @@ func formatLinuxFilePath(ctx context.Context, h *Heartbeat) {
9297
func formatWindowsFilePath(ctx context.Context, h *Heartbeat) {
9398
logger := log.Extract(ctx)
9499

95-
h.Entity = windows.FormatFilePath(h.Entity)
100+
formatted, err := filepath.Abs(h.Entity)
101+
if err != nil {
102+
logger.Debugf("failed to resolve absolute path for %q: %s", h.Entity, err)
103+
return
104+
}
105+
106+
h.Entity = windows.FormatFilePath(formatted)
96107

97-
if !h.IsUnsavedEntity && !windows.IsWindowsNetworkMount(h.Entity) {
108+
if !windows.IsWindowsNetworkMount(h.Entity) {
98109
var err error
99110

100111
h.LocalFile, err = windows.FormatLocalFilePath(h.LocalFile, h.Entity)

pkg/heartbeat/format_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,43 @@ func TestWithFormatting(t *testing.T) {
5454
}, result)
5555
}
5656

57+
func TestFormat_NotFileType(t *testing.T) {
58+
tests := map[string]heartbeat.EntityType{
59+
"app": heartbeat.AppType,
60+
"domain": heartbeat.DomainType,
61+
"event": heartbeat.EventType,
62+
"url": heartbeat.URLType,
63+
}
64+
65+
for name, entityType := range tests {
66+
t.Run(name, func(t *testing.T) {
67+
h := heartbeat.Heartbeat{
68+
Entity: "/unmodified",
69+
EntityType: entityType,
70+
}
71+
72+
formatted := heartbeat.Format(context.Background(), h)
73+
74+
assert.Equal(t, "/unmodified", formatted.Entity)
75+
})
76+
}
77+
}
78+
79+
func TestFormat_WindowsPath(t *testing.T) {
80+
if runtime.GOOS != "windows" {
81+
t.Skip("Skipping because OS is not windows.")
82+
}
83+
84+
h := heartbeat.Heartbeat{
85+
Entity: `C:\Users\project\main.go`,
86+
EntityType: heartbeat.FileType,
87+
}
88+
89+
formatted := heartbeat.Format(context.Background(), h)
90+
91+
assert.Equal(t, "C:/Users/project/main.go", formatted.Entity)
92+
}
93+
5794
func TestFormat_NetworkMount(t *testing.T) {
5895
if runtime.GOOS != "windows" {
5996
t.Skip("Skipping because OS is not windows.")

pkg/windows/windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ var (
3939
// FormatFilePath formats a windows filepath by converting backslash to
4040
// frontslash and ensuring that drive letter is upper case.
4141
func FormatFilePath(fp string) string {
42-
isWindowsNetworkMount := windowsNetworkMountRegex.MatchString(fp)
42+
isWindowsNetworkMount := IsWindowsNetworkMount(fp)
4343

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

0 commit comments

Comments
 (0)