Skip to content

Commit

Permalink
Merge pull request #920 from wakatime/bugfix/file-experts-required-fi…
Browse files Browse the repository at this point in the history
…elds

Validate required fields for fileexperts
  • Loading branch information
gandarez authored Aug 27, 2023
2 parents 5c6f222 + 09c6139 commit 8b2fb25
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/fileexperts/fileexperts.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func initHandleOptions(params paramscmd.Params) []heartbeat.HandleOption {
HideProjectFolder: params.Heartbeat.Sanitize.HideProjectFolder,
ProjectPatterns: params.Heartbeat.Sanitize.HideProjectNames,
}),
fileexperts.WithValidation(),
filter.WithLengthValidator(),
}
}
41 changes: 41 additions & 0 deletions pkg/fileexperts/validation.go
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
}
97 changes: 97 additions & 0 deletions pkg/fileexperts/validation_test.go
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)
}

0 comments on commit 8b2fb25

Please sign in to comment.