-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gazelle): Add "python_visibility" directive that appends additio…
…nal visibility labels (#1784) Fixes #1783. Add a new gazelle directive, `python_visibility`, that allows users to add labels to the `visibility` attribute of generated targets. out by the way, hence this PR), I noticed that the docs were a little This directive acts similar to[^1] the [`go_visibility` directive](https://github.com/bazelbuild/bazel-gazelle#directives). The primary use case is for python projects that separate unit test files from the python packages/modules that they test, like so: ``` packaging_tutorial/ ├── LICENSE ├── pyproject.toml ├── README.md ├── src/ │ └── mypackage/ │ ├── __init__.py │ └── foo.py └── tests/ ├── __init__.py └── test_foo.py ``` A future PR will add an example to the `./examples` directory (issue #1775). [^1]: At least, similar based on docs. I haven't done any actual comparison.
- Loading branch information
1 parent
3f40e98
commit da10ac4
Showing
15 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,4 @@ | ||
# Directives can be added in any order. They will be ordered alphabetically | ||
# when added. | ||
# gazelle:python_visibility //tests:__pkg__ | ||
# gazelle:python_visibility //bar:baz |
16 changes: 16 additions & 0 deletions
16
gazelle/python/testdata/directive_python_visibility/BUILD.out
This file contains 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,16 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
# Directives can be added in any order. They will be ordered alphabetically | ||
# when added. | ||
# gazelle:python_visibility //tests:__pkg__ | ||
# gazelle:python_visibility //bar:baz | ||
|
||
py_library( | ||
name = "directive_python_visibility", | ||
srcs = ["foo.py"], | ||
visibility = [ | ||
"//:__subpackages__", | ||
"//bar:baz", | ||
"//tests:__pkg__", | ||
], | ||
) |
4 changes: 4 additions & 0 deletions
4
gazelle/python/testdata/directive_python_visibility/README.md
This file contains 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,4 @@ | ||
# Directive: `python_visibility` | ||
|
||
This test case asserts that the `# gazelle:python_visibility` directive correctly | ||
appends multiple labels to the target's `visibility` parameter. |
This file contains 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 @@ | ||
# This is a Bazel workspace for the Gazelle test data. |
This file contains 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,2 @@ | ||
def func(): | ||
print("library_func") |
4 changes: 4 additions & 0 deletions
4
gazelle/python/testdata/directive_python_visibility/subdir/BUILD.in
This file contains 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,4 @@ | ||
# python_visibilty directive applies to all child bazel packages. | ||
# Thus, the generated file for this package will also have vis for | ||
# //tests:__pkg__ and //bar:baz in addition to the default. | ||
# gazelle:python_visibility //tests:__subpackages__ |
20 changes: 20 additions & 0 deletions
20
gazelle/python/testdata/directive_python_visibility/subdir/BUILD.out
This file contains 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,20 @@ | ||
load("@rules_python//python:defs.bzl", "py_library") | ||
|
||
# python_visibilty directive applies to all child bazel packages. | ||
# Thus, the generated file for this package will also have vis for | ||
# //tests:__pkg__ and //bar:baz in addition to the default. | ||
# gazelle:python_visibility //tests:__subpackages__ | ||
|
||
py_library( | ||
name = "subdir", | ||
srcs = [ | ||
"__init__.py", | ||
"bar.py", | ||
], | ||
visibility = [ | ||
"//:__subpackages__", | ||
"//bar:baz", | ||
"//tests:__pkg__", | ||
"//tests:__subpackages__", | ||
], | ||
) |
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions
17
gazelle/python/testdata/directive_python_visibility/test.yaml
This file contains 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,17 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
--- | ||
expect: | ||
exit_code: 0 |
This file contains 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