-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #920 from wakatime/bugfix/file-experts-required-fi…
…elds Validate required fields for fileexperts
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package fileexperts | ||
|
||
import ( | ||
"github.com/wakatime/wakatime-cli/pkg/heartbeat" | ||
"github.com/wakatime/wakatime-cli/pkg/log" | ||
) | ||
|
||
// WithValidation initializes and returns a heartbeat handle option, which | ||
// can be used in a heartbeat processing pipeline to validate the heartbeat | ||
// before sending it to the API. | ||
func WithValidation() heartbeat.HandleOption { | ||
return func(next heartbeat.Handle) heartbeat.Handle { | ||
return func(hh []heartbeat.Heartbeat) ([]heartbeat.Result, error) { | ||
log.Debugln("execute fileexperts validation") | ||
|
||
var filtered []heartbeat.Heartbeat | ||
|
||
for _, h := range hh { | ||
if !Validate(h) { | ||
log.Debugf("missing required fields for fileexperts") | ||
continue | ||
} | ||
|
||
filtered = append(filtered, h) | ||
} | ||
|
||
return next(filtered) | ||
} | ||
} | ||
} | ||
|
||
// Validate validates if required fields are not empty. | ||
func Validate(h heartbeat.Heartbeat) bool { | ||
if h.Entity == "" || | ||
h.Project == nil || *h.Project == "" || | ||
h.ProjectRootCount == nil || *h.ProjectRootCount == 0 { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package fileexperts_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/wakatime/wakatime-cli/pkg/fileexperts" | ||
"github.com/wakatime/wakatime-cli/pkg/heartbeat" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWithValidation(t *testing.T) { | ||
opt := fileexperts.WithValidation() | ||
h := opt(func(hh []heartbeat.Heartbeat) ([]heartbeat.Result, error) { | ||
assert.Equal(t, []heartbeat.Heartbeat{ | ||
{ | ||
Entity: "/path/to/file", | ||
Project: heartbeat.PointerTo("wakatime"), | ||
ProjectRootCount: heartbeat.PointerTo(3), | ||
}, | ||
}, hh) | ||
|
||
return []heartbeat.Result{ | ||
{ | ||
Status: 201, | ||
}, | ||
}, nil | ||
}) | ||
|
||
result, err := h([]heartbeat.Heartbeat{ | ||
{ | ||
Entity: "/path/to/file", | ||
Project: heartbeat.PointerTo("wakatime"), | ||
ProjectRootCount: heartbeat.PointerTo(3), | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, []heartbeat.Result{ | ||
{ | ||
Status: 201, | ||
}, | ||
}, result) | ||
} | ||
|
||
func TestValidate_EmptyEntity(t *testing.T) { | ||
h := heartbeat.Heartbeat{} | ||
|
||
result := fileexperts.Validate(h) | ||
|
||
assert.False(t, result) | ||
} | ||
|
||
func TestValidate_NilProject(t *testing.T) { | ||
h := heartbeat.Heartbeat{ | ||
Entity: "/path/to/file", | ||
} | ||
|
||
result := fileexperts.Validate(h) | ||
|
||
assert.False(t, result) | ||
} | ||
|
||
func TestValidate_EmptyProject(t *testing.T) { | ||
h := heartbeat.Heartbeat{ | ||
Entity: "/path/to/file", | ||
Project: heartbeat.PointerTo(""), | ||
} | ||
|
||
result := fileexperts.Validate(h) | ||
|
||
assert.False(t, result) | ||
} | ||
|
||
func TestValidate_NilProjectRootCount(t *testing.T) { | ||
h := heartbeat.Heartbeat{ | ||
Entity: "/path/to/file", | ||
Project: heartbeat.PointerTo("waktime"), | ||
} | ||
|
||
result := fileexperts.Validate(h) | ||
|
||
assert.False(t, result) | ||
} | ||
|
||
func TestValidate_ZeroProjectRootCount(t *testing.T) { | ||
h := heartbeat.Heartbeat{ | ||
Entity: "/path/to/file", | ||
Project: heartbeat.PointerTo("waktime"), | ||
ProjectRootCount: heartbeat.PointerTo(0), | ||
} | ||
|
||
result := fileexperts.Validate(h) | ||
|
||
assert.False(t, result) | ||
} |