-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an attr to rust_binary for customization of the binary name
- Loading branch information
Matt Mackay
committed
Oct 29, 2024
1 parent
a6426e0
commit e527162
Showing
5 changed files
with
99 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
load(":rust_binary_name_suite.bzl", "binary_name_test_suite") | ||
|
||
binary_name_test_suite( | ||
name = "binary_name_test_suite", | ||
) |
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,73 @@ | ||
"""Starlark tests for `rust_binary.binary_name`""" | ||
|
||
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") | ||
load("@bazel_skylib//rules:write_file.bzl", "write_file") | ||
load("//rust:defs.bzl", "rust_binary") | ||
|
||
def _rust_binary_binary_name_test_impl(ctx): | ||
expected_basename = ctx.attr.expected_binary_name | ||
|
||
env = analysistest.begin(ctx) | ||
target = analysistest.target_under_test(env) | ||
|
||
action = target.actions[0] | ||
output = action.outputs.to_list()[0] | ||
|
||
asserts.equals(env, output.basename, expected_basename) | ||
|
||
return analysistest.end(env) | ||
|
||
_binary_name_test = analysistest.make( | ||
_rust_binary_binary_name_test_impl, | ||
attrs = { | ||
"expected_binary_name": attr.string(), | ||
}, | ||
) | ||
|
||
def binary_name_test_suite(name): | ||
"""Entry-point macro called from the BUILD file. | ||
Args: | ||
name (str): The name of the test suite. | ||
""" | ||
write_file( | ||
name = "main", | ||
out = "main.rs", | ||
content = [ | ||
"fn main() {}", | ||
"", | ||
], | ||
) | ||
|
||
rust_binary( | ||
name = "bin_unset", | ||
srcs = [":main.rs"], | ||
edition = "2021", | ||
) | ||
|
||
_binary_name_test( | ||
name = "unset_binary_name_test", | ||
target_under_test = ":bin_unset", | ||
expected_binary_name = "bin_unset", | ||
) | ||
|
||
rust_binary( | ||
name = "bin", | ||
binary_name = "some-binary", | ||
srcs = [":main.rs"], | ||
edition = "2021", | ||
) | ||
|
||
_binary_name_test( | ||
name = "set_binary_name_test", | ||
target_under_test = ":bin", | ||
expected_binary_name = "some-binary", | ||
) | ||
|
||
native.test_suite( | ||
name = name, | ||
tests = [ | ||
":unset_binary_name_test", | ||
":set_binary_name_test", | ||
], | ||
) |