bazelbuild / bazelbuild/rules_testing

Make accessing ToolchainInfo easier

Open
#50 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Starlark
Stars
29
Forks
11
Avg merge
15h 15m
Merged PRs (30d)
3

Description

Today, if you're testing a rule that returns `platform_common.ToolchainInfo`, getting at the value is a bit awkward -- ToolchainInfo is a single provider type, but it carries toolchain-specific attributes.

The regular `TargetSubject.provider` function can work, but it means you have to manually unwrap the values to continue returning a Subject object. For example, if you have a toolchain rule such as:
```
def mylang_toolchain(ctx):
return [ToolchainInfo(my_lang_info=MyLangInfo(...))]
```

A naive rules-testing accessor would look like:

```
toolchain_info = target.provider(ToolchainInfo, factory=my_lang_toolchain_info_subject_new)
my_lang_info = toolchain_info.my_lang_info()
my_lang_info.whatever().equals(...)
```

I suppose `my_lang_toolchain_info` could be a lambda that did the unwrapping directly. In either case, this is kind of annoying.

A few API ideas:

```
# 1. special accessor on TargetSubject that accepts the attr name
target.toolchain_attr("my_lang_info", factory=my_lang_info_subject_new)

# 2. Generic ToolchainInfoSubject with attr-based accessor
target.provider(ToolchainInfo).attr("my_lang_info", factory=my_lang_info_subject_new)
```

(1) seems like a convenience API for (2). (2) seems like potential overkill -- ToolchainInfo is basically a plain struct and doesn't have any well known fields. So other than an `attr()` method, I don't know what else it would have.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.