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

Feature: allow buck2 test to test pure Starlark logic #796

Open
thoughtpolice opened this issue Oct 20, 2024 · 1 comment
Open

Feature: allow buck2 test to test pure Starlark logic #796

thoughtpolice opened this issue Oct 20, 2024 · 1 comment

Comments

@thoughtpolice
Copy link
Contributor

thoughtpolice commented Oct 20, 2024

I just had to implement this function in Starlark:

# 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:

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:

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.

@ndmitchell
Copy link
Contributor

I just do:

def _test():
    assert_eq(rjust('3', '0', 4), '0003')

_test()

At the bottom of the file. Means these tests run in every single run, but they are typically millisecond duration, and if they fail, the consequences are often serious. As an example,

def _test_tree_functions():

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants