facebook / facebook/buck2

Feature: allow `buck2 test` to test pure Starlark logic

Open
#796 1 comment 0 reactions 0 assignees View on GitHub
test command
Dominant language
Rust
Stars
4.4k
Forks
394
PR merge metrics
No merged PRs in 30d

Description

I just had to implement this function in Starlark:

```python
# starlark has no 'rjust', and 'format' does not support padding with
# zeros, so we have to do this manually.
def rjust(s: str, pad: str, width: int) -> str:
if len(s) >= width:
return s
return pad * (width - len(s)) + s
```

What I'd like to do is write a test for this in terms of buck2 test, but I don't really think I can. I don't see why it shouldn't be possible, though?

For example you could imagine a BUILD file that does something like this:

```python
test_starlark_assert(
name = 'test-example',
expected = '0003',
actual = rjust('3', '0', 4),
)
```

`test_starlark_assert` could then just be a rule that returns some provider containing a pass/fail status, and the logic itself can actually be in the rule (this would allow you to have things like expected failures and write them on your own). Because these tests are actually pure you could have a family of providers you return:

```python
def _test_starlark_assert_impl(...):
# ...
if expected == actual:
pass_fail_info = PureTestPassResult('passed')
else:
pass_fail_info = PureTestFailureResult(f'expected value {expected} but got value {actual}')
return [ DefaultInfo(), pass_fail_info ]
```

I understand the TestInfo v2 thing won't be done for a while now, but this seems like a rather independent addition to the API.

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.