Skip to content

Commit

Permalink
fix(tags): slice access out of range (#693)
Browse files Browse the repository at this point in the history
* fix(tags): slice access out of range

* test: add test for case when there are no tags defined
  • Loading branch information
nicufk authored Jan 6, 2022
1 parent 055c94e commit f3d22cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions internal/pkg/api/repository/result/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,12 @@ func (r *MongoRepository) GetTags(ctx context.Context) (tags []string, err error
if err != nil {
return nil, err
}
sortedResult := result[0].Tags
sort.Sort(sort.StringSlice(sortedResult))
return sortedResult, nil
tags = []string{}
if len(result) > 0 {
tags = result[0].Tags
sort.Sort(sort.StringSlice(tags))
}
return tags, nil
}

func (r *MongoRepository) Insert(ctx context.Context, result testkube.Execution) (err error) {
Expand Down
19 changes: 18 additions & 1 deletion internal/pkg/api/repository/result/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
mongoDbName = "testkube"
)

func TestFilters(t *testing.T) {
func TestStorage(t *testing.T) {
assert := require.New(t)

repository, err := getRepository()
Expand Down Expand Up @@ -235,6 +235,23 @@ func TestFilters(t *testing.T) {
assert.NoError(err)
assert.Len(tags, numberOfTags)
})

}

func TestTags(t *testing.T) {
assert := require.New(t)

repository, err := getRepository()
assert.NoError(err)

err = repository.Coll.Drop(context.TODO())
assert.NoError(err)

t.Run("getting tags when there are no tags should return empty slice", func(t *testing.T) {
tags, err := repository.GetTags(context.Background())
assert.NoError(err)
assert.Len(tags, 0)
})
}

func getRepository() (*MongoRepository, error) {
Expand Down

0 comments on commit f3d22cb

Please sign in to comment.