-
Notifications
You must be signed in to change notification settings - Fork 62
expand gomod paths with glob patterns #1177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
t0rr3sp3dr0
wants to merge
1
commit into
project-dalec:main
Choose a base branch
from
t0rr3sp3dr0:pedrotorres/gomod-autodiscovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| package dalec | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/moby/buildkit/client/llb" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestPreprocessGomodPaths(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("bare wildcard globs from the source root", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{"*"}}) | ||
|
|
||
| var gotPattern string | ||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| gotPattern = pattern | ||
| return []string{"foo/module1", "foo/module2"}, nil | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
|
|
||
| gen := spec.Sources["foo"].Generate[0].Gomod | ||
| assert.DeepEqual(t, gen.Paths, []string{"./module1", "./module2"}) | ||
| assert.Equal(t, gotPattern, "foo/*") | ||
| }) | ||
|
|
||
| t.Run("every entry is globbed, including literal paths", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{".", "plugins/*"}}) | ||
|
|
||
| var gotPatterns []string | ||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| gotPatterns = append(gotPatterns, pattern) | ||
| switch pattern { | ||
| case "foo": | ||
| return []string{"foo"}, nil | ||
| case "foo/plugins/*": | ||
| return []string{"foo/plugins/v1"}, nil | ||
| default: | ||
| t.Fatalf("unexpected pattern %q", pattern) | ||
| return nil, nil | ||
| } | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
|
|
||
| gen := spec.Sources["foo"].Generate[0].Gomod | ||
| assert.DeepEqual(t, gen.Paths, []string{".", "./plugins/v1"}) | ||
| assert.DeepEqual(t, gotPatterns, []string{"foo", "foo/plugins/*"}) | ||
| }) | ||
|
|
||
| t.Run("results are fully sorted regardless of input order", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{"moduleB/*", "moduleA/*"}}) | ||
|
|
||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| switch pattern { | ||
| case "foo/moduleA/*": | ||
| return []string{"foo/moduleA/v2", "foo/moduleA/v1"}, nil | ||
| case "foo/moduleB/*": | ||
| return []string{"foo/moduleB/v1"}, nil | ||
| default: | ||
| t.Fatalf("unexpected pattern %q", pattern) | ||
| return nil, nil | ||
| } | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
|
|
||
| gen := spec.Sources["foo"].Generate[0].Gomod | ||
| assert.DeepEqual(t, gen.Paths, []string{"./moduleA/v1", "./moduleA/v2", "./moduleB/v1"}) | ||
| }) | ||
|
|
||
| t.Run("combines with the generator's Subpath", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{"plugins/*"}}) | ||
| spec.Sources["foo"].Generate[0].Subpath = "some/nested/dir" | ||
|
|
||
| var gotPattern string | ||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| gotPattern = pattern | ||
| return []string{"foo/some/nested/dir/plugins/v1"}, nil | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
|
|
||
| gen := spec.Sources["foo"].Generate[0].Gomod | ||
| assert.DeepEqual(t, gen.Paths, []string{"./plugins/v1"}) | ||
| assert.Equal(t, gotPattern, "foo/some/nested/dir/plugins/*") | ||
| }) | ||
|
|
||
| t.Run("does not touch or glob sources with no Paths", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{}) | ||
|
|
||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| t.Fatal("FSGlob should not be called when Paths is empty") | ||
| return nil, nil | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, spec.Sources["foo"].Generate[0].Gomod.Paths == nil) | ||
| }) | ||
|
|
||
| t.Run("is a no-op for specs without any gomod generator", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := &Spec{Sources: map[string]Source{"foo": {Git: &SourceGit{URL: "https://localhost/test.git", Commit: "deadbeef"}}}} | ||
|
|
||
| err := spec.preprocessGomodPaths(SourceOpts{}, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
| }) | ||
|
|
||
| t.Run("a pattern matching nothing is silently dropped", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{"module1/*", "typo/*"}}) | ||
|
|
||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| if pattern == "foo/module1/*" { | ||
| return []string{"foo/module1/v1"}, nil | ||
| } | ||
| return nil, nil | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
|
|
||
| gen := spec.Sources["foo"].Generate[0].Gomod | ||
| assert.DeepEqual(t, gen.Paths, []string{"./module1/v1"}) | ||
| }) | ||
|
|
||
| t.Run("Paths ends up a non-nil empty slice if every pattern matches nothing", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{"typo/*"}}) | ||
|
|
||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| return nil, nil | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.NilError(t, err) | ||
|
|
||
| gen := spec.Sources["foo"].Generate[0].Gomod | ||
| assert.Assert(t, gen.Paths != nil) | ||
| assert.DeepEqual(t, gen.Paths, []string{}) | ||
| }) | ||
|
|
||
| t.Run("propagates glob errors", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| spec := newSpecWithGomod(&GeneratorGomod{Paths: []string{"*"}}) | ||
|
|
||
| sOpt := newGlobSourceOpts(func(st llb.State, pattern string) ([]string, error) { | ||
| return nil, errors.New("boom") | ||
| }) | ||
|
|
||
| err := spec.preprocessGomodPaths(sOpt, llb.Scratch()) | ||
| assert.ErrorContains(t, err, "boom") | ||
| }) | ||
| } | ||
|
|
||
| func newSpecWithGomod(gen *GeneratorGomod) *Spec { | ||
| return &Spec{ | ||
| Sources: map[string]Source{ | ||
| "foo": { | ||
| Git: &SourceGit{ | ||
| URL: "https://localhost/bar.git", | ||
| Commit: "deadbeef", | ||
| }, | ||
| Generate: []*SourceGenerator{ | ||
| { | ||
| Gomod: gen, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func newGlobSourceOpts(glob func(st llb.State, pattern string) ([]string, error)) SourceOpts { | ||
| return SourceOpts{ | ||
| GetContext: func(name string, opts ...llb.LocalOption) (*llb.State, error) { | ||
| st := llb.Local(name, opts...) | ||
| return &st, nil | ||
| }, | ||
| Glob: glob, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is globging for all paths, forcing eager evaluation regardless of glob patterns being present.
I'm also thinking we could handle this inside our shell where these paths are handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no glob pattern is present, this will be a no-op. Are you worried about performance? I don't think there'll be a noticeable slowdown in paths without patterns. Since there's no function to check if a path has a glob pattern, we would have to implement one ourselves and maintain it. This wouldn't be easy, as support for new patterns could be added and given that on Windows the patterns behave differently (there's no escaping support in that OS).
If we don't handle this expansion here, things like gomod edits may not work as expected. Expanding the paths here ensures the rest of Dalec behaves exactly as before, and there's no special handling related to path expansion. The way I implemented it also makes it possible to easily expand this feature to other generators without having to worry too much about the specific package manager of other languages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a no-op because we have to do a full evaluation, including fetch the content immediately (actually the current implementation is doing a new solve for every path).
For using the posix shell to handle globs, I'm not sure its a problem to have to handle the globs in 2 places. It's just a prerequisite, same as how we have to tell it to traverse those paths in both places.