Skip to content
Snippets Groups Projects
Commit abfc5ef0 authored by Nick Thomas's avatar Nick Thomas
Browse files

Test blob indexing

parent ba45ea9a
No related branches found
No related tags found
1 merge request!1Initial implementation of an elasticsearch indexer in Go
Loading
Loading
@@ -10,8 +10,8 @@ import (
)
 
var (
skipTooLargeBlob = fmt.Errorf("Blob should be skipped: Too large")
skipBinaryBlob = fmt.Errorf("Blob should be skipped: binary")
SkipTooLargeBlob = fmt.Errorf("Blob should be skipped: Too large")
SkipBinaryBlob = fmt.Errorf("Blob should be skipped: binary")
)
 
const (
Loading
Loading
@@ -21,9 +21,9 @@ const (
 
func isSkipBlobErr(err error) bool {
switch err {
case skipTooLargeBlob:
case SkipTooLargeBlob:
return true
case skipBinaryBlob:
case SkipBinaryBlob:
return true
}
 
Loading
Loading
@@ -60,7 +60,7 @@ func GenerateBlobID(parentID, filename string) string {
 
func BuildBlob(file *git.File, parentID, commitSHA string) (*Blob, error) {
if file.Size > maxBlobSize {
return nil, skipTooLargeBlob
return nil, SkipTooLargeBlob
}
 
reader, err := file.Blob()
Loading
Loading
@@ -78,7 +78,7 @@ func BuildBlob(file *git.File, parentID, commitSHA string) (*Blob, error) {
}
 
if DetectBinary(b) {
return nil, skipBinaryBlob
return nil, SkipBinaryBlob
}
 
content := tryEncodeBytes(b)
Loading
Loading
package indexer_test
import (
"encoding/json"
"io"
"io/ioutil"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/es-git-go/git"
"gitlab.com/gitlab-org/es-git-go/indexer"
)
const (
sha = "9876543210987654321098765432109876543210"
oid = "0123456789012345678901234567890123456789"
)
func readerFunc(data string, err error) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader(data)), err
}
}
func validFile(path, content string) *git.File {
return &git.File{
Path: path,
Blob: readerFunc(content, nil),
Size: int64(len(content)),
Oid: oid,
}
}
func validBlob(file *git.File, content, language string) *indexer.Blob {
return &indexer.Blob{
Type: "blob",
ID: indexer.GenerateBlobID(parentID, file.Path),
OID: oid,
RepoID: parentID,
CommitSHA: sha,
Content: content,
Path: file.Path,
Filename: file.Path,
Language: language,
}
}
func TestBuildBlob(t *testing.T) {
file := validFile("foo/bar", "foo")
expected := validBlob(file, "foo", "Text")
actual, err := indexer.BuildBlob(file, expected.RepoID, expected.CommitSHA)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
expectedJSON := `{
"commit_sha" : "` + expected.CommitSHA + `",
"content" : "` + expected.Content + `",
"file_name" : "` + expected.Filename + `",
"language" : "` + expected.Language + `",
"oid" : "` + expected.OID + `",
"path" : "` + expected.Path + `",
"rid" : "` + expected.RepoID + `",
"type" : "blob"
}`
actualJSON, err := json.Marshal(actual)
assert.NoError(t, err)
assert.JSONEq(t, expectedJSON, string(actualJSON))
}
func TestBuildBlobSkipsLargeBlobs(t *testing.T) {
file := validFile("foo/bar", "foo")
file.Size = 1024*1024 + 1
blob, err := indexer.BuildBlob(file, parentID, sha)
assert.Error(t, err, indexer.SkipTooLargeBlob)
assert.Nil(t, blob)
}
func TestBuildBlobSkipsBinaryBlobs(t *testing.T) {
file := validFile("foo/bar", "foo\x00")
blob, err := indexer.BuildBlob(file, parentID, sha)
assert.Equal(t, err, indexer.SkipBinaryBlob)
assert.Nil(t, blob)
}
func TestBuildBlobDetectsLanguageByFilename(t *testing.T) {
file := validFile("Makefile.am", "foo")
blob, err := indexer.BuildBlob(file, parentID, sha)
assert.NoError(t, err)
assert.Equal(t, "Makefile", blob.Language)
}
func TestBuildBlobDetectsLanguageByExtension(t *testing.T) {
file := validFile("foo.rb", "foo")
blob, err := indexer.BuildBlob(file, parentID, sha)
assert.NoError(t, err)
assert.Equal(t, "Ruby", blob.Language)
}
func TestGenerateBlobID(t *testing.T) {
assert.Equal(t, "projectID_path", indexer.GenerateBlobID("projectID", "path"))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment