+
Skip to content

Conversation

justinmk
Copy link
Contributor

@justinmk justinmk commented Sep 14, 2025

Problem

Tests have no way to retry without manually managing before_each and after_each setup/teardown. This is painful. And busted does a good job of guarding its internals and makes it impossible to extend it: AFAICT, consumers have no way to access the current "test context", or the current list of before_each/after_each hooks.

Solution

Introduce env.set_retries(), which allows test authors to optionally retry a test:

it('...', function()
  set_retries(2) -- If the test fails, it will be retried up to 2 times.
end)

Todo

  • On a fresh clone of this repo there are 91 failures (and 1583 successes).
    • I have only checked that this PR doesn't add new failures.
  • Haven't added docs/tests, want to see if this will be acceptable first.
  • Should the test have access to the current retry number? I don't get why busted gives zero context to tests.
  • Also send to https://github.com/lewis6991/nvim-test

@alerque
Copy link
Member

alerque commented Sep 17, 2025

* Haven't added docs/tests, want to see if this will be acceptable first.

Yes, I think I'm up for having this added.

* Should the test have access to the current retry number? I don't get why busted gives zero context to tests.

Yes I think it probably should be exposed.

Historically I think (speculating here) the reason zero context has been given is just because we're clearing out the environment and isolating tests and exposing any context would mean adding a new API surface to explicitly start making some things available inside the isolated test runner scope.

@justinmk
Copy link
Contributor Author

Thanks, I'll follow up on this then.

Any guidance on the interface? Defining new magic globals like set_retries feels a bit weird but that seems to be the pattern. Is set_retries the right name?

@alerque
Copy link
Member

alerque commented Sep 17, 2025

That sounds okay, but the code you used is a little confusing. We should either set the number of retries (zero being don't attempt any retries after failure) and default it to zero or set the max attempts and default it to one, but preferably not mix and match and have to calculate one from the other in code. Not that the math is hard, just that the code will be neater if the logic aligns with the interface naming the end user is seeing.

@justinmk
Copy link
Contributor Author

Don't look at the code yet, it's a broken mess :)

assert(type(match) == 'table')
assert(type(assert) == 'table')

describe('retry test', function()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the set_retries tests go in a new _spec.lua file?

Comment on lines 123 to +127
if status == 'success' then
-- Remove any failures for this test since it succeeded
for i = #handler.failures, 1, -1 do
if handler.failures[i].id == tostring(element) then
table.remove(handler.failures, i)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit hacky. Is there a better way for the it() implementation in busted/init.lua to avoid reporting "failures" until all retries are exhausted?

Problem:
Tests have no way to retry without manually managing `before_each` and
`after_each` setup/teardown. This is painful. And busted does a good job
of guarding its internals and makes it impossible to extend it: AFAICT,
consumers have no way to access the current "test context", or the
current list of `before_each`/`after_each` hooks.

Solution:
Introduce `env.set_retries()`, which allows test authors to optionally retry a test:

```lua
it('...', function()
  set_retries(2) -- If the test fails, it will be retried up to 2 times.
end)
```

Testing:

    luarocks --local remove busted --force && luarocks --local make && ~/.luarocks/bin/busted --pattern=core
@justinmk
Copy link
Contributor Author

Still needs cleanup (and more tests), but the functionality (AI-generated) seems to work now.

Note: On a fresh clone of this repo there are 91 failures (and 1583 successes). I have only checked that this PR doesn't increase the failures.

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

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载