-
-
Notifications
You must be signed in to change notification settings - Fork 668
feat(gazelle): Directive controlling pytest ancestor dependencies #3596
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
Changes from all commits
539f391
ff30dd0
1091a17
3346dc9
279e6cb
c992c87
ee5ab98
93d81f8
4b183b4
4e3fde3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,7 +68,7 @@ func matchesAnyGlob(s string, globs []string) bool { | |
|
|
||
| // findConftestPaths returns package paths containing conftest.py, from currentPkg | ||
| // up through ancestors, stopping at module root. | ||
| func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string { | ||
| func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string, includeAncestorConftest bool) []string { | ||
| var result []string | ||
| for pkg := currentPkg; ; pkg = filepath.Dir(pkg) { | ||
| if pkg == "." { | ||
|
|
@@ -77,6 +77,12 @@ func findConftestPaths(repoRoot, currentPkg, pythonProjectRoot string) []string | |
| if _, err := os.Stat(filepath.Join(repoRoot, pkg, conftestFilename)); err == nil { | ||
| result = append(result, pkg) | ||
| } | ||
| // We traverse up the tree to find conftest files and we start in | ||
| // the current package. Thus if we find one in the current package | ||
| // and do not want ancestors, we break early. | ||
| if !includeAncestorConftest { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. N.B.: I opted to keep this separate from the next if pkg == "" or !includeAncestorConftest {
break
} |
||
| break | ||
| } | ||
| if pkg == "" { | ||
| break | ||
| } | ||
|
|
@@ -402,7 +408,6 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes | |
| setAnnotations(*annotations). | ||
| generateImportsAttribute() | ||
|
|
||
|
|
||
| pyBinary := pyBinaryTarget.build() | ||
|
|
||
| result.Gen = append(result.Gen, pyBinary) | ||
|
|
@@ -526,13 +531,13 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes | |
|
|
||
| for _, pyTestTarget := range pyTestTargets { | ||
| shouldAddConftest := pyTestTarget.annotations.includePytestConftest == nil || | ||
| *pyTestTarget.annotations.includePytestConftest | ||
| *pyTestTarget.annotations.includePytestConftest | ||
|
|
||
| if shouldAddConftest { | ||
| for _, conftestPkg := range findConftestPaths(args.Config.RepoRoot, args.Rel, pythonProjectRoot) { | ||
| for _, conftestPkg := range findConftestPaths(args.Config.RepoRoot, args.Rel, pythonProjectRoot, cfg.IncludeAncestorConftest()) { | ||
| pyTestTarget.addModuleDependency( | ||
| Module{ | ||
| Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, | ||
| Name: importSpecFromSrc(pythonProjectRoot, conftestPkg, conftestFilename).Imp, | ||
| Filepath: filepath.Join(conftestPkg, conftestFilename), | ||
| }, | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library") | ||
|
|
||
| py_library( | ||
| name = "conftest", | ||
| testonly = True, | ||
| srcs = ["conftest.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Directive: `python_include_ancestor_conftest` | ||
|
|
||
| This test case asserts that the `# gazelle:python_include_ancestor_conftest` | ||
| directive correctly includes or excludes ancestor `conftest` targets in | ||
| `py_test` target dependencies. | ||
|
|
||
| The test also asserts that the directive can be applied at any level and that | ||
| child levels will inherit the value: | ||
|
|
||
| + The root level does not set the directive (it defaults to True). | ||
| + The next level, `one/`, inherits that value. | ||
| + The next level, `one/two/`, sets the directive to False; consequently the | ||
| `py_test` target only includes the sibling `:conftest` target. | ||
| + The `one/two/no_conftest/` directory does not contain a `conftest.py` file | ||
| thereby asserting that we correctly do not include any `conftest` targets | ||
| whatsoever. | ||
| + The final level, `one/two/three/`, sets the directive back to True, meaning | ||
| the `py_test` target includes a total of 4 `conftest` targets. | ||
| + The `one/two/three/no_conftest/` directory does not contain a `conftest.py` | ||
| file and thus asserts that the code includes _only_ ancestor `conftest` | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Man, I really like the phrase "and thus" ...
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had gemini provide some synonym phrases. |
||
| targets. | ||
|
|
||
| See [Issue #3595](https://github.com/bazel-contrib/rules_python/issues/3595). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
|
||
| py_library( | ||
| name = "conftest", | ||
| testonly = True, | ||
| srcs = ["conftest.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "my_test", | ||
| srcs = ["my_test.py"], | ||
| deps = [ | ||
| ":conftest", | ||
| "//:conftest", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # gazelle:python_include_ancestor_conftest false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
|
||
| # gazelle:python_include_ancestor_conftest false | ||
|
|
||
| py_library( | ||
| name = "conftest", | ||
| testonly = True, | ||
| srcs = ["conftest.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "my_test", | ||
| srcs = ["my_test.py"], | ||
| deps = [":conftest"], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| load("@rules_python//python:defs.bzl", "py_test") | ||
|
|
||
| py_test( | ||
| name = "my_test", | ||
| srcs = ["my_test.py"], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # gazelle:python_include_ancestor_conftest true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| load("@rules_python//python:defs.bzl", "py_library", "py_test") | ||
|
|
||
| # gazelle:python_include_ancestor_conftest true | ||
|
|
||
| py_library( | ||
| name = "conftest", | ||
| testonly = True, | ||
| srcs = ["conftest.py"], | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| py_test( | ||
| name = "my_test", | ||
| srcs = ["my_test.py"], | ||
| deps = [ | ||
| ":conftest", | ||
| "//:conftest", | ||
| "//one:conftest", | ||
| "//one/two:conftest", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| load("@rules_python//python:defs.bzl", "py_test") | ||
|
|
||
| py_test( | ||
| name = "my_test", | ||
| srcs = ["my_test.py"], | ||
| deps = [ | ||
| "//:conftest", | ||
| "//one:conftest", | ||
| "//one/two:conftest", | ||
| "//one/two/three:conftest", | ||
| ], | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| --- | ||
| expect: | ||
| exit_code: 0 |
Uh oh!
There was an error while loading. Please reload this page.