Skip to content

Commit

Permalink
fix: validate doc string
Browse files Browse the repository at this point in the history
  • Loading branch information
x1unix committed Jul 16, 2024
1 parent 0a6c543 commit a57c5c6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
15 changes: 13 additions & 2 deletions internal/pkgindex/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ func ParseImportCompletionItem(ctx context.Context, params PackageParseParams) (
continue
}

// Found a doc string, exit.
result.Detail = src.Name.String()
docComment := strings.TrimSpace(analyzer.FormatDocString(doc.Text()).Value)

// Package doc should start with "Package xxx", see issue #367.
docComment := strings.TrimSpace(doc.Text())
if !validatePackageDoc(docComment) {
continue
}

docComment = strings.TrimSpace(analyzer.FormatDocString(docComment).Value)
docString = docComment + "\n\n" + docString
break
}
Expand All @@ -71,3 +77,8 @@ func ParseImportCompletionItem(ctx context.Context, params PackageParseParams) (
})
return result, nil
}

func validatePackageDoc(str string) bool {
normalizedStr := strings.ToLower(str)
return strings.HasPrefix(normalizedStr, "package ")
}
59 changes: 50 additions & 9 deletions internal/pkgindex/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,10 +14,13 @@ import (

func TestParseImportCompletionItem(t *testing.T) {
cases := map[string]struct {
pkgName string
expectErr string
getContext func() context.Context
expect func(importPath string) monaco.CompletionItem
pkgName string
customGoRoot string
expectErr string
filterInputFiles bool
getContext func() context.Context
expect func(importPath string) monaco.CompletionItem
compareFunc func(t *testing.T, got monaco.CompletionItem)
}{
"package with documentation": {
pkgName: "foopkg/pkgbar",
Expand Down Expand Up @@ -67,6 +71,15 @@ func TestParseImportCompletionItem(t *testing.T) {
return ctx
},
},
"367-syscall-invalid-description": {
customGoRoot: mustGetGoRoot(t) + "/src",
pkgName: "syscall",
filterInputFiles: true,
compareFunc: func(t *testing.T, got monaco.CompletionItem) {
expectPfx := "Package syscall contains an interface to the low-level"
expectStartWith(t, got.Documentation.Value.Value, expectPfx)
},
},
}

const rootDir = "testdata"
Expand All @@ -77,25 +90,35 @@ func TestParseImportCompletionItem(t *testing.T) {
ctx = c.getContext()
}

goRoot := rootDir
if c.customGoRoot != "" {
goRoot = c.customGoRoot
}

result, err := ParseImportCompletionItem(ctx, PackageParseParams{
RootDir: rootDir,
RootDir: goRoot,
ImportPath: c.pkgName,
Files: findDirFiles(t, rootDir, c.pkgName),
Files: findDirFiles(t, goRoot, c.pkgName, c.filterInputFiles),
})
if c.expectErr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), c.expectErr)
return
}

expect := c.expect(c.pkgName)
require.NoError(t, err)
if c.compareFunc != nil {
c.compareFunc(t, result)
return
}

expect := c.expect(c.pkgName)
require.Equal(t, expect, result)
})
}
}

func findDirFiles(t *testing.T, dir, pkgName string) []string {
func findDirFiles(t *testing.T, dir, pkgName string, filterFiles bool) []string {
t.Helper()
entries, err := os.ReadDir(filepath.Join(dir, pkgName))
require.NoError(t, err)
Expand All @@ -106,8 +129,26 @@ func findDirFiles(t *testing.T, dir, pkgName string) []string {
continue
}

files = append(files, entry.Name())
name := entry.Name()
if filterFiles {
if strings.HasSuffix(name, "_test.go") || !strings.HasSuffix(name, ".go") {
continue
}
}

files = append(files, name)
}

return files
}

func mustGetGoRoot(t *testing.T) string {
t.Helper()
v, err := ResolveGoRoot()
require.NoError(t, err)
return v
}

func expectStartWith(t *testing.T, str string, pfx string) {
require.Truef(t, strings.HasPrefix(str, pfx), "string %q should start with %q", str, pfx)
}

0 comments on commit a57c5c6

Please sign in to comment.