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

Defer examples to last for cleaning up tasks (like the Go feature defer) #127

Open
eldipa opened this issue Aug 2, 2020 · 0 comments
Open
Labels
enhancement something nice to have but it is not neither critical nor urgent

Comments

@eldipa
Copy link
Collaborator

eldipa commented Aug 2, 2020

Describe the feature you'd like

You can tag an example with +defer and its execution will be deferred at the end. If multiple examples have +defer, the execution order will be reversed as they were seen: first example seen last executed.

This is based on the Go lang's defer feature

For example:

>>> f = open()
>>> f.close()   # byexample: +defer
>>> c = connect()                                                                                                              
>>> c.close()   # byexample: +defer

>>> do_something(f, c)

In the above example the examples will be executed like if they were written like this:

>>> f = open()
>>> c = connect()

>>> do_something(f, c)

>>> c.close()
>>> f.close()

On failures, the fail fast mode enters in action (examples are skipped). For example if c = connect() fails, the equivalent execution would be:

>>> f = open()
>>> c = connect()   # <-- Imagine that this example fails

>>> do_something(f, c)  # <-- This will be skipped

>>> c.close()  # <-- This will be skipped
>>> f.close()  # <-- This will NOT be skipped                                                                                  

The reasoning is that c.close() # byexample: +defer was seen after one example failed and therefore it should be skipped while f.close() # byexample: +defer was seen before the failing example so it should be executed as usual.

The equivalent file would be (note the skip):

>>> f = open()
>>> c = connect()   # <-- Imagine that this example fails

>>> do_something(f, c)  # <-- This will be skipped

>>> c.close()   # byexample: +skip  # <-- This will be skipped
>>> f.close()   # byexample: -skip  # <-- This will NOT be skipped

+defer not only "reschedule" the example but also adds -skip to it if it was seen before a failing test or +skip it it was seen after.

An explicit +skip or -skip could be used to ensure a particular flag and it should be honored regardless of the default behavior of +defer: if +skip +defer is used, the example is deferred but always is skipped for example.

Additional links (experience from people using golang)

The good, the bad and the ugly of defer:

Not related, but interesting articles:

@eldipa eldipa added the enhancement something nice to have but it is not neither critical nor urgent label Aug 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement something nice to have but it is not neither critical nor urgent
Projects
None yet
Development

No branches or pull requests

1 participant