Skip to content

Commit

Permalink
Add --afterrun option instead of --oninterrupt:
Browse files Browse the repository at this point in the history
* Name chosen for consistency with --beforerun
* Pass the py.test exit code as a CLI parameter
* Always run this hook, even on KeyboardInterrupt
* Clarify that --onexit runs when pytest-watch exits
* Clarify that --runner overrides the literal "py.test" command
* Use subprocess.call instead of os.system in run_hook (and handle passing arguments)
  • Loading branch information
joeyespo committed Apr 6, 2016
1 parent 18a99b8 commit fc471a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
9 changes: 5 additions & 4 deletions pytest_watch/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
(if relative, starts from root of each watched dir).
-c --clear Automatically clear the screen before each run.
--beforerun=<cmd> Run arbitrary command before tests are run.
--afterrun <cmd> Run arbitrary command on completion or interruption.
The exit code of "py.test" is passed as an argument.
--onpass=<cmd> Run arbitrary command on pass.
--onfail=<cmd> Run arbitrary command on failure.
--onexit=<cmd> Run arbitrary command when exiting.
--oninterrupt=<cmd> Run arbitrary command on KeyboardInterrupt.
--runner=<cmd> Run a custom command instead of py.test.
--onexit=<cmd> Run arbitrary command when exiting pytest-watch.
--runner=<cmd> Run a custom command instead of "py.test".
--pdb Start the interactive Python debugger on errors.
This also enables --wait to prevent pdb interruption.
--nobeep Do not beep on failure.
Expand Down Expand Up @@ -105,8 +106,8 @@ def main(argv=None):
onfail=args['--onfail'],
runner=args['--runner'],
beforerun=args['--beforerun'],
afterrun=args['--afterrun'],
onexit=args['--onexit'],
oninterrupt=args['--oninterrupt'],
poll=args['--poll'],
extensions=extensions,
args=pytest_args,
Expand Down
22 changes: 11 additions & 11 deletions pytest_watch/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
# Exit codes from pytest
# http://pytest.org/latest/_modules/_pytest/main.html
EXIT_OK = 0
EXIT_INTERRUPTED = 2
EXIT_NOTESTSCOLLECTED = 5


Expand Down Expand Up @@ -170,17 +169,18 @@ def _split_recursive(directories, ignore):
return sorted(set(recursedirs)), sorted(set(norecursedirs))


def run_hook(cmd):
def run_hook(cmd, *args):
"""
Runs a command hook, if specified.
"""
if cmd:
os.system(cmd)
command = ' '.join(map(str, (cmd,) + args))
subprocess.call(command, shell=True)


def watch(directories=[], ignore=[], auto_clear=False, beep_on_failure=True,
onpass=None, onfail=None, runner=None, beforerun=None, onexit=None,
oninterrupt=None, poll=False, extensions=[], args=[], spool=None,
onpass=None, onfail=None, runner=None, beforerun=None, afterrun=None,
onexit=None, poll=False, extensions=[], args=[], spool=None,
wait=False, verbose=False, quiet=False):
argv = (runner or 'py.test').split(' ') + (args or [])

Expand Down Expand Up @@ -237,15 +237,15 @@ def watch(directories=[], ignore=[], auto_clear=False, beep_on_failure=True,
sleep(0.1)
except KeyboardInterrupt:
# Wait for current test run cleanup
if p.wait() == EXIT_INTERRUPTED:
run_hook(oninterrupt)
run_hook(afterrun, p.wait())
# Exit, since this keyboard interrupt was user-initiated
break

# Run custom commands
if exit_code == EXIT_INTERRUPTED:
run_hook(oninterrupt)
elif exit_code in [EXIT_OK, EXIT_NOTESTSCOLLECTED]:
# Run custom command
run_hook(afterrun, exit_code)

# Run dependent commands
if exit_code in [EXIT_OK, EXIT_NOTESTSCOLLECTED]:
run_hook(onpass)
else:
if beep_on_failure:
Expand Down

0 comments on commit fc471a4

Please sign in to comment.