diff options
Diffstat (limited to 'workhorse/internal/lsif_transformer/parser')
-rw-r--r-- | workhorse/internal/lsif_transformer/parser/docs.go | 10 | ||||
-rw-r--r-- | workhorse/internal/lsif_transformer/parser/docs_test.go | 32 |
2 files changed, 32 insertions, 10 deletions
diff --git a/workhorse/internal/lsif_transformer/parser/docs.go b/workhorse/internal/lsif_transformer/parser/docs.go index f87bc7fd86c..9cdec4c8d42 100644 --- a/workhorse/internal/lsif_transformer/parser/docs.go +++ b/workhorse/internal/lsif_transformer/parser/docs.go @@ -5,6 +5,7 @@ import ( "bufio" "encoding/json" "io" + "path/filepath" "strings" ) @@ -116,7 +117,7 @@ func (d *Docs) addMetadata(line []byte) error { return err } - d.Root = strings.TrimSpace(metadata.Root) + "/" + d.Root = strings.TrimSpace(metadata.Root) return nil } @@ -127,7 +128,12 @@ func (d *Docs) addDocument(line []byte) error { return err } - d.Entries[doc.Id] = strings.TrimPrefix(doc.Uri, d.Root) + relativePath, err := filepath.Rel(d.Root, doc.Uri) + if err != nil { + relativePath = doc.Uri + } + + d.Entries[doc.Id] = relativePath return nil } diff --git a/workhorse/internal/lsif_transformer/parser/docs_test.go b/workhorse/internal/lsif_transformer/parser/docs_test.go index 24e3eba8ac5..805bc53c0b7 100644 --- a/workhorse/internal/lsif_transformer/parser/docs_test.go +++ b/workhorse/internal/lsif_transformer/parser/docs_test.go @@ -18,16 +18,32 @@ func TestParse(t *testing.T) { require.NoError(t, err) defer d.Close() - data := []byte(`{"id":"1","label":"metaData","projectRoot":"file:///Users/nested"}` + "\n") - data = append(data, createLine("2", "document", "file:///Users/nested/file.rb")...) - data = append(data, createLine("3", "document", "file:///Users/nested/folder/file.rb")...) - data = append(data, createLine("4", "document", "file:///Users/wrong/file.rb")...) + for _, root := range []string{ + "file:///Users/nested", + "file:///Users/nested/.", + "file:///Users/nested/", + } { + t.Run("Document with root: "+root, func(t *testing.T) { + data := []byte(`{"id":"1","label":"metaData","projectRoot":"` + root + `"}` + "\n") + data = append(data, createLine("2", "document", "file:///Users/nested/file.rb")...) + data = append(data, createLine("3", "document", "file:///Users/nested/folder/file.rb")...) - require.NoError(t, d.Parse(bytes.NewReader(data))) + require.NoError(t, d.Parse(bytes.NewReader(data))) + + require.Equal(t, "file.rb", d.Entries[2]) + require.Equal(t, "folder/file.rb", d.Entries[3]) + }) + } + + t.Run("Relative path cannot be calculated", func(t *testing.T) { + originalUri := "file:///Users/nested/folder/file.rb" + data := []byte(`{"id":"1","label":"metaData","projectRoot":"/a"}` + "\n") + data = append(data, createLine("2", "document", originalUri)...) + + require.NoError(t, d.Parse(bytes.NewReader(data))) - require.Equal(t, d.Entries[2], "file.rb") - require.Equal(t, d.Entries[3], "folder/file.rb") - require.Equal(t, d.Entries[4], "file:///Users/wrong/file.rb") + require.Equal(t, originalUri, d.Entries[2]) + }) } func TestParseContainsLine(t *testing.T) { |