Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gazelle/python/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (py *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
case "exclude":
// We record the exclude directive for coarse-grained packages
// since we do manual tree traversal in this mode.
config.AddExcludedPattern(strings.TrimSpace(d.Value))
config.AddExcludedPattern(filepath.Join(rel, strings.TrimSpace(d.Value)))
case pythonconfig.PythonExtensionDirective:
switch d.Value {
case "enabled":
Expand Down
9 changes: 5 additions & 4 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
if filepath.Ext(path) == ".py" {
if cfg.CoarseGrainedGeneration() || !isEntrypointFile(path) {
f, _ := filepath.Rel(args.Dir, path)
srcPath, _ := filepath.Rel(args.Dir, path)
repoPath := filepath.Join(args.Rel, srcPath)
excludedPatterns := cfg.ExcludedPatterns()
if excludedPatterns != nil {
it := excludedPatterns.Iterator()
for it.Next() {
excludedPattern := it.Value().(string)
isExcluded, err := doublestar.Match(excludedPattern, f)
isExcluded, err := doublestar.Match(excludedPattern, repoPath)
if err != nil {
return err
}
Expand All @@ -178,9 +179,9 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
}
baseName := filepath.Base(path)
if strings.HasSuffix(baseName, "_test.py") || strings.HasPrefix(baseName, "test_") {
pyTestFilenames.Add(f)
pyTestFilenames.Add(srcPath)
} else {
pyLibraryFilenames.Add(f)
pyLibraryFilenames.Add(srcPath)
}
}
}
Expand Down
26 changes: 8 additions & 18 deletions gazelle/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -33,7 +32,6 @@ import (

"github.com/bazelbuild/bazel-gazelle/testtools"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/emirpasic/gods/lists/singlylinkedlist"
"github.com/ghodss/yaml"
)

Expand Down Expand Up @@ -161,31 +159,23 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) {
t.Fatal(err)
}
}
errs := singlylinkedlist.New()

actualExitCode := cmd.ProcessState.ExitCode()
if config.Expect.ExitCode != actualExitCode {
errs.Add(fmt.Errorf("expected gazelle exit code: %d\ngot: %d",
config.Expect.ExitCode, actualExitCode,
))
t.Errorf("expected gazelle exit code: %d\ngot: %d",
config.Expect.ExitCode, actualExitCode)
}
actualStdout := stdout.String()
if strings.TrimSpace(config.Expect.Stdout) != strings.TrimSpace(actualStdout) {
errs.Add(fmt.Errorf("expected gazelle stdout: %s\ngot: %s",
config.Expect.Stdout, actualStdout,
))
t.Errorf("expected gazelle stdout: %s\ngot: %s",
config.Expect.Stdout, actualStdout)
}
actualStderr := stderr.String()
if strings.TrimSpace(config.Expect.Stderr) != strings.TrimSpace(actualStderr) {
errs.Add(fmt.Errorf("expected gazelle stderr: %s\ngot: %s",
config.Expect.Stderr, actualStderr,
))
t.Errorf("expected gazelle stderr: %s\ngot: %s",
config.Expect.Stderr, actualStderr)
}
if !errs.Empty() {
errsIt := errs.Iterator()
for errsIt.Next() {
err := errsIt.Value().(error)
t.Log(err)
}
if t.Failed() {
t.FailNow()
}

Expand Down
1 change: 1 addition & 0 deletions gazelle/python/testdata/monorepo/a/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:exclude bar/baz/hue.py
1 change: 1 addition & 0 deletions gazelle/python/testdata/monorepo/a/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:exclude bar/baz/hue.py
3 changes: 3 additions & 0 deletions gazelle/python/testdata/monorepo/a/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Exclusions
* Intentionally make the directory "a" so Gazelle visit this before "coarse_grained"
* Making sure that the exclusion here doesn't affect coarse_grained/bar/baz/hue.py