Skip to content
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

Add example of how to depend one pkg_deb .changes file #484

Merged
merged 19 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
15 changes: 15 additions & 0 deletions examples/where_is_my_output/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ pkg_deb(
package = "mwp",
version = "3.14",
)

# We can also depend just on the .changes file

filegroup(
name = "the_changes_file",
srcs = [":deb"],
output_group = "changes",
)
aiuto marked this conversation as resolved.
Show resolved Hide resolved

genrule(
name = "use_changes_file",
srcs = [":the_changes_file"],
outs = ["copy_of_changes.txt"],
cmd = "cp $(location :the_changes_file) $@",
)
37 changes: 34 additions & 3 deletions examples/where_is_my_output/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ to inspect a target and print exactly what we need. Let's try it:

```shell
bazel build :deb
bazel cquery :deb --output=starlark --starlark:file=show_deb_outputs.bzl 2>/dev/null
bazel cquery :deb --output=starlark --starlark:file=show_all_outputs.bzl 2>/dev/null
```

That should produce something like
Expand All @@ -35,15 +35,15 @@ changes: bazel-out/k8-fastbuild/bin/mwp_3.changes

### How it works

show_deb_outputs.bzl is a Starlark script that must contain a function with the
show_all_outputs.bzl is a Starlark script that must contain a function with the
name `format`, that takes a single argument. The argument is typically named
target, and is a configured Bazel target, as you might have access to while
writing a custom rule. We can inspect its providers and print them in a useful
way.

For pkg_deb, there are two files, the .deb file and the .changes, and both are
passed along in the rule's OutputGroupInfo provider. This snippet below (from
show_deb_outputs.bzl) prints them.
show_all_outputs.bzl) prints them.

```python
def format(target):
Expand All @@ -64,3 +64,34 @@ def format(target):
A full explanation of why this works is beyond the scope of this example. It
requires some knowledge of how to write custom Bazel rules. See the Bazel
documentation for more information.

## Using an implicit output as input to another rule.

Sometimes a rule will create an implicit output that the user does not
explicitly specify as an attribute of the target. The .changes file
from pkg_deb is an example. If we want another rule to depend on an
aiuto marked this conversation as resolved.
Show resolved Hide resolved
implicitly created file, we can do that with a filegroup that
specifies the specific output group containing that file.
aiuto marked this conversation as resolved.
Show resolved Hide resolved

In the example below, `:deb` is a rule producing an explicit .deb output
and an implict .changes output. We refer to the .changes file using
the `filegroup` and specifying the desired output group name. Then,
any rule can use this `filegroup` as an input.

```python
aiuto marked this conversation as resolved.
Show resolved Hide resolved

pkg_deb(name = "deb", ...)

filegroup(
name = "the_changes_file",
srcs = [":deb"],
output_group = "changes",
)

genrule(
name = "use_changes_file",
srcs = [":the_changes_file"],
outs = ["copy_of_changes.txt"],
cmd = "cp $(location :the_changes_file) $@",
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Extract the paths to the various outputs of pkg_deb
#
# Usage:
# bazel cquery //:debian --output=starlark --starlark:file=show_deb_outputs.bzl
# bazel cquery //:deb --output=starlark --starlark:file=show_all_outputs.bzl
#

def format(target):
Expand Down
2 changes: 1 addition & 1 deletion pkg/private/deb/deb.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def _pkg_deb_impl(ctx):
return [
OutputGroupInfo(**output_groups),
DefaultInfo(
files = depset([output_file]),
files = depset(outputs),
runfiles = ctx.runfiles(files = outputs),
),
PackageArtifactInfo(
Expand Down
10 changes: 8 additions & 2 deletions tests/deb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Tests for pkg_deb specific behavior

load("@rules_python//python:defs.bzl", "py_test")
load(":deb_tests.bzl", "package_naming_test")
load(":deb_tests.bzl", "all_files_in_default_info_test", "package_naming_test")
load("//pkg:mappings.bzl", "pkg_mklink")
load("//pkg:pkg.bzl", "pkg_tar")
load("//pkg:deb.bzl", "pkg_deb")
Expand Down Expand Up @@ -104,8 +104,14 @@ py_test(
],
)

all_files_in_default_info_test(
name = "default_info_test",
expected_basename = "fizzbuzz_test_all",
target_under_test = ":test_deb",
)

package_naming_test(
name = "naming_test",
target_under_test = ":test_deb",
expected_name = "fizzbuzz_test_all.deb",
target_under_test = ":test_deb",
)
65 changes: 63 additions & 2 deletions tests/deb/deb_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@

load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

def assert_contains(env, expected, got, msg = None):
"""Asserts that `expected` occurs in the iterable (`list`, `dict`) `got`.

Args:
env: The test environment returned by `unittest.begin`.
expected: An expected value.
got: The actual set returned by some computation.
msg: An optional message that will be printed that describes the failure.
If omitted, a default will be used.
"""
if expected in got:
return
expectation_msg = "Expected %s in (%s)" % (expected, got)
if msg:
full_msg = "%s (%s)" % (msg, expectation_msg)
else:
full_msg = expectation_msg
analysistest.fail(env, full_msg)

def _package_naming_test_impl(ctx):
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
Expand All @@ -37,16 +56,58 @@ def _package_naming_test_impl(ctx):
if ctx.attr.expected_name:
asserts.equals(
env,
deb_path.split("/")[-1], # basename(path)
ctx.attr.expected_name,
deb_path.split("/")[-1], # basename(path)
"Deb package file name is not correct",
)
return analysistest.end(env)


package_naming_test = analysistest.make(
_package_naming_test_impl,
attrs = {
"expected_name": attr.string(),
},
)

def _all_files_in_default_info_impl(ctx):
aiuto marked this conversation as resolved.
Show resolved Hide resolved
env = analysistest.begin(ctx)
target_under_test = analysistest.target_under_test(env)
di = target_under_test[DefaultInfo]
files = di.files.to_list()
file_names = [f.basename for f in files]
asserts.equals(
env,
3,
len(files),
file_names,
)

expect_output = target_under_test.label.name + ".deb"
assert_contains(
env,
expect_output,
file_names,
)

expect_output = ctx.attr.expected_basename + ".deb"
assert_contains(
env,
expect_output,
file_names,
)

expect_output = ctx.attr.expected_basename + ".changes"
assert_contains(
env,
expect_output,
file_names,
)

return analysistest.end(env)

all_files_in_default_info_test = analysistest.make(
_all_files_in_default_info_impl,
attrs = {
"expected_basename": attr.string(),
},
)