Skip to content

Commit

Permalink
v2: Pass BuildTags in an Options struct
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Feb 24, 2024
1 parent 483edb0 commit e7d5fc5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion v2/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Execute(nameSystems namer.NameSystems, defaultSystem string, getTargets fun
buildTags = append(buildTags, buildTag)
}

p := parser.New(buildTags)
p := parser.NewWithOptions(parser.Options{BuildTags: buildTags})
if err := p.LoadPackages(patterns...); err != nil {
return fmt.Errorf("failed making a parser: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion v2/generator/snippet_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func construct(t *testing.T, patterns ...string) *generator.Context {
b := parser.New(nil)
b := parser.New()

if err := b.LoadPackages(patterns...); err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
17 changes: 14 additions & 3 deletions v2/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,29 @@ type fileLine struct {
line int
}

// New constructs a new builder.
func New(buildTags []string) *Parser {
// New constructs a new Parser.
func New() *Parser {
return NewWithOptions(Options{})
}

func NewWithOptions(opts Options) *Parser {
return &Parser{
goPkgs: map[string]*packages.Package{},
userRequested: map[string]bool{},
fullyProcessed: map[string]bool{},
fset: token.NewFileSet(),
endLineToCommentGroup: map[fileLine]*ast.CommentGroup{},
buildTags: buildTags,
buildTags: opts.BuildTags,
}
}

// Options holds optional settings for the Parser.
type Options struct {
// BuildTags is a list of optional tags to be specified when loading
// packages.
BuildTags []string
}

// FindPackages expands the provided patterns into a list of Go import-paths,
// much like `go list -find`.
func (p *Parser) FindPackages(patterns ...string) ([]string, error) {
Expand Down
20 changes: 10 additions & 10 deletions v2/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestNew(t *testing.T) {
parser := New(nil)
parser := New()
if parser.goPkgs == nil {
t.Errorf("expected .goPkgs to be initialized")
}
Expand Down Expand Up @@ -123,22 +123,22 @@ func keys[T any](m map[string]T) []string {
func TestAddBuildTags(t *testing.T) {
testTags := []string{"foo", "bar", "qux"}

parser := New(nil)
parser := New()
if len(parser.buildTags) != 0 {
t.Errorf("expected no default build tags, got %v", parser.buildTags)
}
parser = New(testTags[0:1])
parser = NewWithOptions(Options{BuildTags: testTags[0:1]})
if want, got := testTags[0:1], parser.buildTags; !sliceEq(want, got) {
t.Errorf("wrong build tags:\nwant: %v\ngot: %v", pretty(want), pretty(got))
}
parser = New(testTags)
parser = NewWithOptions(Options{BuildTags: testTags})
if want, got := testTags, parser.buildTags; !sliceEq(want, got) {
t.Errorf("wrong build tags:\nwant: %v\ngot: %v", pretty(want), pretty(got))
}
}

func TestFindPackages(t *testing.T) {
parser := New(nil)
parser := New()

// Proper packages with deps.
if pkgs, err := parser.FindPackages("./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestAlreadyLoaded(t *testing.T) {
}
}

parser := New(nil)
parser := New()

// Test loading something we don't have.
if existing, netNew, err := parser.alreadyLoaded("./testdata/root1"); err != nil {
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestAlreadyLoaded(t *testing.T) {
}

func TestLoadPackagesInternal(t *testing.T) {
parser := New(nil)
parser := New()

// Proper packages with deps.
if pkgs, err := parser.loadPackages("./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestLoadPackagesInternal(t *testing.T) {
}

func TestLoadPackagesTo(t *testing.T) {
parser := New(nil)
parser := New()
u := types.Universe{}

// Proper packages with deps.
Expand Down Expand Up @@ -444,7 +444,7 @@ func TestForEachPackageRecursive(t *testing.T) {
}

func TestUserRequestedPackages(t *testing.T) {
parser := New(nil)
parser := New()

// Proper packages with deps.
if err := parser.LoadPackages("./testdata/root1", "./testdata/root2", "./testdata/roots345/..."); err != nil {
Expand All @@ -469,7 +469,7 @@ func TestUserRequestedPackages(t *testing.T) {
}

func TestAddOnePkgToUniverse(t *testing.T) {
parser := New(nil)
parser := New()

// Proper packages with deps.
if pkgs, err := parser.loadPackages("./testdata/root2"); err != nil {
Expand Down

0 comments on commit e7d5fc5

Please sign in to comment.