# Truth Truth-style asserts for Bazel's Starlark. These asserts follow the Truth-style way of performing assertions. This basically means the actual value is wrapped in a type-specific object that provides type-specific assertion methods. This style provides several benefits: * A fluent API that more directly expresses the assertion * More egonomic assert functions * Error messages with more informative context * Promotes code reuses at the type-level. For more detailed documentation, see the docs on GitHub. ## Basic usage NOTE: This example assumes usage of [`rules_testing`]'s [`analysis_test`] framework, but that framework is not required. ``` def foo_test(env, target): subject = env.expect.that_target(target) subject.runfiles().contains_at_least(["foo.txt"]) subject.executable().equals("bar.exe") subject = env.expect.that_action(...) subject.contains_at_least_args(...) ``` {.starlark-object} ## matching.all {.starlark-signature} matching.all([matchers](#matching_all_matchers)) Match that all of multiple matchers match. {#matching_all_parameters} **PARAMETERS** [¶](#matching_all_parameters){.headerlink} {.params-box} :[matchers[¶](#matching_all_matchers){.headerlink}]{.span}: []{#matching_all_matchers} `list` of [`Matcher`]. If all match, then it is considered a match. {#matching_all_returns} RETURNS [¶](#matching_all_returns){.headerlink} : [`Matcher`] (see `_match_custom`) {.starlark-object} ## matching.any {.starlark-signature} matching.any([matchers](#matching_any_matchers)) Match that any of multiple matchers match. See also: `is_in` for checking if a value is one amongst multiple values. {#matching_any_parameters} **PARAMETERS** [¶](#matching_any_parameters){.headerlink} {.params-box} :[matchers[¶](#matching_any_matchers){.headerlink}]{.span}: []{#matching_any_matchers} `list` of [`Matcher`]. If any match, then it is considered a match. {#matching_any_returns} RETURNS [¶](#matching_any_returns){.headerlink} : [`Matcher`] (see `_match_custom`) {.starlark-object} ## matching.contains {.starlark-signature} matching.contains([contained](#matching_contains_contained)) Match that `contained` is within the to-be-matched value. This is equivalent to: `contained in to_be_matched`. See `_match_is_in` for the reversed operation. {#matching_contains_parameters} **PARAMETERS** [¶](#matching_contains_parameters){.headerlink} {.params-box} :[contained[¶](#matching_contains_contained){.headerlink}]{.span}: []{#matching_contains_contained} the value that to-be-matched value must contain. {#matching_contains_returns} RETURNS [¶](#matching_contains_returns){.headerlink} : [`Matcher`] (see `_match_custom`). {.starlark-object} ## matching.custom {.starlark-signature} matching.custom([desc](#matching_custom_desc), [func](#matching_custom_func)) Wrap an arbitrary function up as a Matcher. Method: Matcher.new `Matcher` struct attributes: * `desc`: ([`str`]) a human-friendly description * `match`: (callable) accepts 1 positional arg (the value to match) and returns [`bool`] (`True` if it matched, `False` if not). {#matching_custom_parameters} **PARAMETERS** [¶](#matching_custom_parameters){.headerlink} {.params-box} :[desc[¶](#matching_custom_desc){.headerlink}]{.span}: []{#matching_custom_desc} ([`str`]) a human-friendly string describing what is matched. :[func[¶](#matching_custom_func){.headerlink}]{.span}: []{#matching_custom_func} (callable) accepts 1 positional arg (the value to match) and returns [`bool`] (`True` if it matched, `False` if not). {#matching_custom_returns} RETURNS [¶](#matching_custom_returns){.headerlink} : [`Matcher`] (see above). {.starlark-object} ## matching.equals_wrapper {.starlark-signature} matching.equals_wrapper([value](#matching_equals_wrapper_value)) Match that a value equals `value`, but use `value` as the `desc`. This is a helper so that simple equality comparisons can re-use predicate based APIs. {#matching_equals_wrapper_parameters} **PARAMETERS** [¶](#matching_equals_wrapper_parameters){.headerlink} {.params-box} :[value[¶](#matching_equals_wrapper_value){.headerlink}]{.span}: []{#matching_equals_wrapper_value} object, the value that must be equal to. {#matching_equals_wrapper_returns} RETURNS [¶](#matching_equals_wrapper_returns){.headerlink} : [`Matcher`] (see `_match_custom()`), whose description is `value`. {.starlark-object} ## matching.file_basename_contains {.starlark-signature} matching.file_basename_contains([substr](#matching_file_basename_contains_substr)) Match that a a `File.basename` string contains a substring. {#matching_file_basename_contains_parameters} **PARAMETERS** [¶](#matching_file_basename_contains_parameters){.headerlink} {.params-box} :[substr[¶](#matching_file_basename_contains_substr){.headerlink}]{.span}: []{#matching_file_basename_contains_substr} ([`str`]) the substring to match. {#matching_file_basename_contains_returns} RETURNS [¶](#matching_file_basename_contains_returns){.headerlink} : [`Matcher`] (see `_match_custom()`). {.starlark-object} ## matching.file_basename_equals {.starlark-signature} matching.file_basename_equals([value](#matching_file_basename_equals_value)) Match that a `File.basename` string equals `value`. {#matching_file_basename_equals_parameters} **PARAMETERS** [¶](#matching_file_basename_equals_parameters){.headerlink} {.params-box} :[value[¶](#matching_file_basename_equals_value){.headerlink}]{.span}: []{#matching_file_basename_equals_value} ([`str`]) the basename to match. {#matching_file_basename_equals_returns} RETURNS [¶](#matching_file_basename_equals_returns){.headerlink} : [`Matcher`] instance {.starlark-object} ## matching.file_extension_in {.starlark-signature} matching.file_extension_in([values](#matching_file_extension_in_values)) Match that a `File.extension` string is any of `values`. See also: `file_path_matches` for matching extensions that have multiple parts, e.g. `*.tar.gz` or `*.so.*`. {#matching_file_extension_in_parameters} **PARAMETERS** [¶](#matching_file_extension_in_parameters){.headerlink} {.params-box} :[values[¶](#matching_file_extension_in_values){.headerlink}]{.span}: []{#matching_file_extension_in_values} ([`list`] of [`str`]) the extensions to match. {#matching_file_extension_in_returns} RETURNS [¶](#matching_file_extension_in_returns){.headerlink} : [`Matcher`] instance {.starlark-object} ## matching.file_path_matches {.starlark-signature} matching.file_path_matches([pattern](#matching_file_path_matches_pattern)) Match that a `File.path` string matches a glob-style pattern. {#matching_file_path_matches_parameters} **PARAMETERS** [¶](#matching_file_path_matches_parameters){.headerlink} {.params-box} :[pattern[¶](#matching_file_path_matches_pattern){.headerlink}]{.span}: []{#matching_file_path_matches_pattern} ([`str`]) the pattern to match. "*" can be used to denote "match anything". {#matching_file_path_matches_returns} RETURNS [¶](#matching_file_path_matches_returns){.headerlink} : [`Matcher`] (see `_match_custom`). {.starlark-object} ## matching.is_in {.starlark-signature} matching.is_in([values](#matching_is_in_values)) Match that the to-be-matched value is in a collection of other values. This is equivalent to: `to_be_matched in values`. See `_match_contains` for the reversed operation. See also: `matching.any` for matching amongst arbitrary other matchers. {#matching_is_in_parameters} **PARAMETERS** [¶](#matching_is_in_parameters){.headerlink} {.params-box} :[values[¶](#matching_is_in_values){.headerlink}]{.span}: []{#matching_is_in_values} The collection that the value must be within. {#matching_is_in_returns} RETURNS [¶](#matching_is_in_returns){.headerlink} : [`Matcher`] (see `_match_custom()`). {.starlark-object} ## matching.is_matcher {.starlark-signature} matching.is_matcher([obj](#matching_is_matcher_obj)) {#matching_is_matcher_parameters} **PARAMETERS** [¶](#matching_is_matcher_parameters){.headerlink} {.params-box} :[obj[¶](#matching_is_matcher_obj){.headerlink}]{.span}: []{#matching_is_matcher_obj} _undocumented_ {.starlark-object} ## matching.never {.starlark-signature} matching.never([desc](#matching_never_desc)) A matcher that never matches. This is mostly useful for testing, as it allows preventing any match while providing a custom description. {#matching_never_parameters} **PARAMETERS** [¶](#matching_never_parameters){.headerlink} {.params-box} :[desc[¶](#matching_never_desc){.headerlink}]{.span}: []{#matching_never_desc} ([`str`]) human-friendly string. {#matching_never_returns} RETURNS [¶](#matching_never_returns){.headerlink} : [`Matcher`] (see `_match_custom`). {.starlark-object} ## matching.str_endswith {.starlark-signature} matching.str_endswith([suffix](#matching_str_endswith_suffix)) Match that a string contains another string. {#matching_str_endswith_parameters} **PARAMETERS** [¶](#matching_str_endswith_parameters){.headerlink} {.params-box} :[suffix[¶](#matching_str_endswith_suffix){.headerlink}]{.span}: []{#matching_str_endswith_suffix} ([`str`]) the suffix that must be present {#matching_str_endswith_returns} RETURNS [¶](#matching_str_endswith_returns){.headerlink} : [`Matcher`] (see `_match_custom`). {.starlark-object} ## matching.str_matches {.starlark-signature} matching.str_matches([pattern](#matching_str_matches_pattern)) Match that a string matches a glob-style pattern. {#matching_str_matches_parameters} **PARAMETERS** [¶](#matching_str_matches_parameters){.headerlink} {.params-box} :[pattern[¶](#matching_str_matches_pattern){.headerlink}]{.span}: []{#matching_str_matches_pattern} ([`str`]) the pattern to match. `*` can be used to denote "match anything". There is an implicit `*` at the start and end of the pattern. {#matching_str_matches_returns} RETURNS [¶](#matching_str_matches_returns){.headerlink} : [`Matcher`] object. {.starlark-object} ## matching.str_startswith {.starlark-signature} matching.str_startswith([prefix](#matching_str_startswith_prefix)) Match that a string contains another string. {#matching_str_startswith_parameters} **PARAMETERS** [¶](#matching_str_startswith_parameters){.headerlink} {.params-box} :[prefix[¶](#matching_str_startswith_prefix){.headerlink}]{.span}: []{#matching_str_startswith_prefix} ([`str`]) the prefix that must be present {#matching_str_startswith_returns} RETURNS [¶](#matching_str_startswith_returns){.headerlink} : [`Matcher`] (see `_match_custom`). {.starlark-object} ## subjects.bool {.starlark-signature} subjects.bool([value](#subjects_bool_value), [meta](#subjects_bool_meta)) Creates a "BoolSubject" struct. Method: BoolSubject.new {#subjects_bool_parameters} **PARAMETERS** [¶](#subjects_bool_parameters){.headerlink} {.params-box} :[value[¶](#subjects_bool_value){.headerlink}]{.span}: []{#subjects_bool_value} ([`bool`]) the value to assert against. :[meta[¶](#subjects_bool_meta){.headerlink}]{.span}: []{#subjects_bool_meta} ([`ExpectMeta`]) the metadata about the call chain. {#subjects_bool_returns} RETURNS [¶](#subjects_bool_returns){.headerlink} : A [`BoolSubject`]. {.starlark-object} ## subjects.collection {.starlark-signature} subjects.collection([values](#subjects_collection_values), [meta](#subjects_collection_meta), [container_name](#subjects_collection_container_name)="values", [sortable](#subjects_collection_sortable)=True, [element_plural_name](#subjects_collection_element_plural_name)="elements") Creates a "CollectionSubject" struct. Method: CollectionSubject.new Public Attributes: * `actual`: The wrapped collection. {#subjects_collection_parameters} **PARAMETERS** [¶](#subjects_collection_parameters){.headerlink} {.params-box} :[values[¶](#subjects_collection_values){.headerlink}]{.span}: []{#subjects_collection_values} ([`collection`]) the values to assert against. :[meta[¶](#subjects_collection_meta){.headerlink}]{.span}: []{#subjects_collection_meta} ([`ExpectMeta`]) the metadata about the call chain. :[container_name[¶](#subjects_collection_container_name){.headerlink}]{.span}: []{#subjects_collection_container_name} (_default `"values"`_) ([`str`]) conceptual name of the container. :[sortable[¶](#subjects_collection_sortable){.headerlink}]{.span}: []{#subjects_collection_sortable} (_default `True`_) ([`bool`]) True if output should be sorted for display, False if not. :[element_plural_name[¶](#subjects_collection_element_plural_name){.headerlink}]{.span}: []{#subjects_collection_element_plural_name} (_default `"elements"`_) ([`str`]) the plural word for the values in the container. {#subjects_collection_returns} RETURNS [¶](#subjects_collection_returns){.headerlink} : [`CollectionSubject`]. {.starlark-object} ## subjects.default_info {.starlark-signature} subjects.default_info([info](#subjects_default_info_info), [meta](#subjects_default_info_meta)) Creates a `DefaultInfoSubject` {#subjects_default_info_parameters} **PARAMETERS** [¶](#subjects_default_info_parameters){.headerlink} {.params-box} :[info[¶](#subjects_default_info_info){.headerlink}]{.span}: []{#subjects_default_info_info} ([`DefaultInfo`]) the DefaultInfo object to wrap. :[meta[¶](#subjects_default_info_meta){.headerlink}]{.span}: []{#subjects_default_info_meta} ([`ExpectMeta`]) call chain information. {#subjects_default_info_returns} RETURNS [¶](#subjects_default_info_returns){.headerlink} : [`DefaultInfoSubject`] object. {.starlark-object} ## subjects.depset_file {.starlark-signature} subjects.depset_file([files](#subjects_depset_file_files), [meta](#subjects_depset_file_meta), [container_name](#subjects_depset_file_container_name)="depset", [element_plural_name](#subjects_depset_file_element_plural_name)="files") Creates a DepsetFileSubject asserting on `files`. Method: DepsetFileSubject.new {#subjects_depset_file_parameters} **PARAMETERS** [¶](#subjects_depset_file_parameters){.headerlink} {.params-box} :[files[¶](#subjects_depset_file_files){.headerlink}]{.span}: []{#subjects_depset_file_files} ([`depset`] of [`File`]) the values to assert on. :[meta[¶](#subjects_depset_file_meta){.headerlink}]{.span}: []{#subjects_depset_file_meta} ([`ExpectMeta`]) of call chain information. :[container_name[¶](#subjects_depset_file_container_name){.headerlink}]{.span}: []{#subjects_depset_file_container_name} (_default `"depset"`_) ([`str`]) conceptual name of the container. :[element_plural_name[¶](#subjects_depset_file_element_plural_name){.headerlink}]{.span}: []{#subjects_depset_file_element_plural_name} (_default `"files"`_) ([`str`]) the plural word for the values in the container. {#subjects_depset_file_returns} RETURNS [¶](#subjects_depset_file_returns){.headerlink} : [`DepsetFileSubject`] object. {.starlark-object} ## subjects.dict {.starlark-signature} subjects.dict([actual](#subjects_dict_actual), [meta](#subjects_dict_meta), [container_name](#subjects_dict_container_name)="dict", [key_plural_name](#subjects_dict_key_plural_name)="keys") Creates a new `DictSubject`. Method: DictSubject.new {#subjects_dict_parameters} **PARAMETERS** [¶](#subjects_dict_parameters){.headerlink} {.params-box} :[actual[¶](#subjects_dict_actual){.headerlink}]{.span}: []{#subjects_dict_actual} ([`dict`]) the dict to assert against. :[meta[¶](#subjects_dict_meta){.headerlink}]{.span}: []{#subjects_dict_meta} ([`ExpectMeta`]) of call chain information. :[container_name[¶](#subjects_dict_container_name){.headerlink}]{.span}: []{#subjects_dict_container_name} (_default `"dict"`_) ([`str`]) conceptual name of the dict. :[key_plural_name[¶](#subjects_dict_key_plural_name){.headerlink}]{.span}: []{#subjects_dict_key_plural_name} (_default `"keys"`_) ([`str`]) the plural word for the keys of the dict. {#subjects_dict_returns} RETURNS [¶](#subjects_dict_returns){.headerlink} : New `DictSubject` struct. {.starlark-object} ## subjects.file {.starlark-signature} subjects.file([file](#subjects_file_file), [meta](#subjects_file_meta)) Creates a FileSubject asserting against the given file. Method: FileSubject.new {#subjects_file_parameters} **PARAMETERS** [¶](#subjects_file_parameters){.headerlink} {.params-box} :[file[¶](#subjects_file_file){.headerlink}]{.span}: []{#subjects_file_file} ([`File`]) the file to assert against. :[meta[¶](#subjects_file_meta){.headerlink}]{.span}: []{#subjects_file_meta} ([`ExpectMeta`]) {#subjects_file_returns} RETURNS [¶](#subjects_file_returns){.headerlink} : [`FileSubject`] object. {.starlark-object} ## subjects.int {.starlark-signature} subjects.int([value](#subjects_int_value), [meta](#subjects_int_meta)) Create an "IntSubject" struct. Method: IntSubject.new {#subjects_int_parameters} **PARAMETERS** [¶](#subjects_int_parameters){.headerlink} {.params-box} :[value[¶](#subjects_int_value){.headerlink}]{.span}: []{#subjects_int_value} (optional [`int`]) the value to perform asserts against may be None. :[meta[¶](#subjects_int_meta){.headerlink}]{.span}: []{#subjects_int_meta} ([`ExpectMeta`]) the meta data about the call chain. {#subjects_int_returns} RETURNS [¶](#subjects_int_returns){.headerlink} : [`IntSubject`]. {.starlark-object} ## subjects.label {.starlark-signature} subjects.label([label](#subjects_label_label), [meta](#subjects_label_meta)) Creates a new `LabelSubject` for asserting `Label` objects. Method: LabelSubject.new {#subjects_label_parameters} **PARAMETERS** [¶](#subjects_label_parameters){.headerlink} {.params-box} :[label[¶](#subjects_label_label){.headerlink}]{.span}: []{#subjects_label_label} ([`Label`]) the label to check against. :[meta[¶](#subjects_label_meta){.headerlink}]{.span}: []{#subjects_label_meta} ([`ExpectMeta`]) the metadata about the call chain. {#subjects_label_returns} RETURNS [¶](#subjects_label_returns){.headerlink} : [`LabelSubject`]. {.starlark-object} ## subjects.runfiles {.starlark-signature} subjects.runfiles([runfiles](#subjects_runfiles_runfiles), [meta](#subjects_runfiles_meta), [kind](#subjects_runfiles_kind)=None) Creates a "RunfilesSubject" struct. Method: RunfilesSubject.new {#subjects_runfiles_parameters} **PARAMETERS** [¶](#subjects_runfiles_parameters){.headerlink} {.params-box} :[runfiles[¶](#subjects_runfiles_runfiles){.headerlink}]{.span}: []{#subjects_runfiles_runfiles} ([`runfiles`]) the runfiles to check against. :[meta[¶](#subjects_runfiles_meta){.headerlink}]{.span}: []{#subjects_runfiles_meta} ([`ExpectMeta`]) the metadata about the call chain. :[kind[¶](#subjects_runfiles_kind){.headerlink}]{.span}: []{#subjects_runfiles_kind} (_default `None`_) (optional [`str`]) what type of runfiles they are, usually "data" or "default". If not known or not applicable, use None. {#subjects_runfiles_returns} RETURNS [¶](#subjects_runfiles_returns){.headerlink} : [`RunfilesSubject`] object. {.starlark-object} ## subjects.str {.starlark-signature} subjects.str([actual](#subjects_str_actual), [meta](#subjects_str_meta)) Creates a subject for asserting strings. Method: StrSubject.new {#subjects_str_parameters} **PARAMETERS** [¶](#subjects_str_parameters){.headerlink} {.params-box} :[actual[¶](#subjects_str_actual){.headerlink}]{.span}: []{#subjects_str_actual} ([`str`]) the string to check against. :[meta[¶](#subjects_str_meta){.headerlink}]{.span}: []{#subjects_str_meta} ([`ExpectMeta`]) of call chain information. {#subjects_str_returns} RETURNS [¶](#subjects_str_returns){.headerlink} : [`StrSubject`] object. {.starlark-object} ## subjects.struct {.starlark-signature} subjects.struct([actual](#subjects_struct_actual), [meta](#subjects_struct_meta), [attrs](#subjects_struct_attrs)) Creates a `StructSubject`, which is a thin wrapper around a [`struct`]. {#subjects_struct_parameters} **PARAMETERS** [¶](#subjects_struct_parameters){.headerlink} {.params-box} :[actual[¶](#subjects_struct_actual){.headerlink}]{.span}: []{#subjects_struct_actual} ([`struct`]) the struct to wrap. :[meta[¶](#subjects_struct_meta){.headerlink}]{.span}: []{#subjects_struct_meta} ([`ExpectMeta`]) object of call context information. :[attrs[¶](#subjects_struct_attrs){.headerlink}]{.span}: []{#subjects_struct_attrs} ([`dict`] of [`str`] to [`callable`]) the functions to convert attributes to subjects. The keys are attribute names that must exist on `actual`. The values are functions with the signature `def factory(value, *, meta)`, where `value` is the actual attribute value of the struct, and `meta` is an [`ExpectMeta`] object. {#subjects_struct_returns} RETURNS [¶](#subjects_struct_returns){.headerlink} : [`StructSubject`] object, which is a struct with the following shape: * `actual` attribute, the underlying struct that was wrapped. * A callable attribute for each `attrs` entry; it takes no args and returns what the corresponding factory from `attrs` returns. {.starlark-object} ## subjects.target {.starlark-signature} subjects.target([target](#subjects_target_target), [meta](#subjects_target_meta)) Creates a subject for asserting Targets. Method: TargetSubject.new **Public attributes**: * `actual`: The wrapped [`Target`] object. {#subjects_target_parameters} **PARAMETERS** [¶](#subjects_target_parameters){.headerlink} {.params-box} :[target[¶](#subjects_target_target){.headerlink}]{.span}: []{#subjects_target_target} ([`Target`]) the target to check against. :[meta[¶](#subjects_target_meta){.headerlink}]{.span}: []{#subjects_target_meta} ([`ExpectMeta`]) metadata about the call chain. {#subjects_target_returns} RETURNS [¶](#subjects_target_returns){.headerlink} : [`TargetSubject`] object {.starlark-object} ## truth.expect {.starlark-signature} truth.expect([env](#truth_expect_env)) Wrapper around `env`. This is the entry point to the Truth-style assertions. Example usage: expect = expect(env) expect.that_action(action).contains_at_least_args(...) The passed in `env` object allows optional attributes to be set to customize behavior. Usually this is helpful for testing. See `_fake_env()` in truth_tests.bzl for examples. * `fail`: callable that takes a failure message. If present, it will be called instead of the regular `Expect.add_failure` logic. * `get_provider`: callable that takes 2 positional args (target and provider) and returns the found provider or fails. * `has_provider`: callable that takes 2 positional args (a [`Target`] and a [`provider`]) and returns [`bool`] (`True` if present, `False` otherwise) or fails. {#truth_expect_parameters} **PARAMETERS** [¶](#truth_expect_parameters){.headerlink} {.params-box} :[env[¶](#truth_expect_env){.headerlink}]{.span}: []{#truth_expect_env} unittest env struct, or some approximation. There are several attributes that override regular behavior; see above doc. {#truth_expect_returns} RETURNS [¶](#truth_expect_returns){.headerlink} : [`Expect`] object [`Action`]: https://bazel.build/rules/lib/Action [`ActionSubject`]: /api/action_subject [`bool`]: https://bazel.build/rules/lib/bool [`BoolSubject`]: /api/bool_subject [`CollectionSubject`]: /api/collection_subject [`depset`]: https://bazel.build/rules/lib/depset [`DepsetFileSubject`]: /api/depset_file_subject [`dict`]: https://bazel.build/rules/lib/dict [`DictSubject`]: /api/dict_subject [`Expect`]: /api/expect [`ExpectMeta`]: /api/expect_meta [`File`]: https://bazel.build/rules/lib/File [`FileSubject`]: /api/file_subject [`format_str`]: /api/expect_meta.html#expectmeta-format-str [`IntSubject`]: /api/int_subject [`Label`]: https://bazel.build/rules/lib/Label [`LabelSubject`]: /api/label_subject [`list`]: https://bazel.build/rules/lib/list [`Ordered`]: /api/ordered [`RunfilesSubject`]: /api/runfiles_subject [`str`]: https://bazel.build/rules/lib/string [`struct`]: https://bazel.build/rules/lib/builtins/struct [`StrSubject`]: /api/str_subject [`StructSubject`]: /api/struct_subject [`Target`]: https://bazel.build/rules/lib/Target [`TargetSubject`]: /api/target_subject [target-name]: https://bazel.build/concepts/labels#target-names [attr-label]: https://bazel.build/concepts/labels