nox icon indicating copy to clipboard operation
nox copied to clipboard

how about a `--watch` option ?

Open asmodehn opened this issue 3 years ago • 4 comments

How would this feature be useful? Having nox running in the background would provide immediate feedback while coding.

Describe the solution you'd like pytest-watch implements this for pytest. The point is to implement this feature at the session level so any session can be run in the background... it might make sense for some sessions, but not others... So maybe have a combination of a command-line flag to 'enable watch', and a session parameter declaring it 'watchable' or not

Describe alternatives you've considered watch --color nox breaks colors on my setup (ubuntu18) session.run('ptw') only works for pytest

asmodehn avatar Oct 22 '20 14:10 asmodehn

watch --color nox breaks colors on my setup (ubuntu18)

I wonder why this is? I would definitely prefer not to add more dependencies and complexity to Nox if it can be helped.

theacodes avatar Nov 19 '20 16:11 theacodes

I tried to find a bit more information about this. As a starting point, testing watch --color with ls :

  • watch --color ls -ahl --color is colorized
  • watch --color ls -ahl is not colorized
  • ls -ahl is colorized
  • ls -ahl --color is colorized

This makes sense, because it turns out my .bashrc has aliases by default (ubuntu) : alias ls='ls --color=auto' which are not in effect when the command is run by watch instead. Just mentioning it for other who might trip up on this as well.

So redirecting the output of ls into a file, and viewing it via vim allows to see the color codes :

drwxrwxr-x 5 alexv alexv 4,0K nov.  20 11:52 ^[[0m^[[01;34m.^[[0m
drwxrwxr-x 5 alexv alexv 4,0K nov.  20 11:44 ^[[01;34m..^[[0m

I tried to redirect the nox output, and I cannot see any color code in there... But it turns out if I run nox --force-color > nox.out 2>&1 I can see the color codes in the nox.out file output. So running watch --color nox --force-color has colors for me now ;-)

There is however a few caveats with this solution :

  • watch will kill the program to re-run it at regular interval (possibly triggering surprising errors) So for long test suites, you should have a large enough watch interval: watch -n 10 --color nox --force-color
  • the first few times the test runs will be longer than one might think, to account for the time downloading dependencies and populating the virtualenv. It seems the test timer displayed on the last line == 1 passed in 0.00s == doesn't seem to count the virtualenv creation time ? In any case, configuring nox to reuse virtualenvs helps here, and is likely the proper setup for such a usecase.

Anyway I'm happy to close this if you don't see anything to address here. As far as I am concerned, I can have a working TDD setup with nox for small projects now ;-) Thanks !

asmodehn avatar Nov 20 '20 11:11 asmodehn

@asmodehn You could also try to run Nox when source files have changed. There are many general-purpose (but somewhat platform dependent) programs out there that help you do this. One example would be entr:

while true; do find src -name '*.py' | entr -d nox -r; done

cjolowicz avatar Nov 20 '20 11:11 cjolowicz

@cjolowicz Thanks for the mention of entr, I didn't know about it.

But if I were to go that route, I'd rather write a small nox-watch wrapper python script, relying on https://github.com/gorakhargosh/watchdog or https://github.com/dsoprea/PyInotify Currently not really needed for my usecase, but who knows, that day might eventually come ;-)

asmodehn avatar Nov 21 '20 10:11 asmodehn