[PYTHON] Use watchdog (watchmedo) in test-driven development

For test-driven development, I think it's a must to run tests automatically when you save. Also, don't you think that even if you write a little script, it will be better if it is automatically executed when you save it?

At that time, watchdog (watchmedo)! !! (Although various OSs have API commands for monitoring, it is recommended for those who want to follow the same procedure for multiple OSs.)

Addition </ b> </ font>: 2016/11/07 The command posted was tested in the terminal of MINGW32 that comes with Git for Windows, but it works at the command prompt. I was informed that there was no such thing. Probably because the shell doesn't work. (I haven't checked it. I'm sorry.)

Addition </ b> </ font>: 2016-12-14 For some reason, only my home PC was running twice, so I reviewed the option commands. It didn't work with --wait, and the -W option was for double execution prevention. Also, it seems that the description of [" $ {watch_event_type} "==" modified "] && echo hoge, which is limited to update events, is not necessary, so I deleted it from the article.

install

If you are using an OS that is easy to develop, such as Linux Mint or Mac, please see the following and install the atmosphere because it is an instant kill. I'm addicted to Windows, so I'll write it for Windows users.

python install

I need Python, so I'll download it. Either 2 or 3 system is fine, but be careful of 32bit or 64bit.

If you download on the top page, 32bit will be dropped, so if you are 64bit, please drop it from Download list here.

Pay attention to the folder when installing! !!

Although it is the folder at the time of install, be sure to save it directly under the C drive. The default C: \ Program Files (x86) will cause problems with path resolution or something and will not work properly. .. ..

#I installed it here
C:\python35-64

pip install watchdog

python comes with a module management software called "pip", so use that to install it.

  1. Start ~~ Command Prompt ~~ ** MINGW32 **.
  2. python -m pip install --upgrade pip --force-reinstall
  3. pip install watchdog

The second step is a pip upgrade so you can skip it.

How to use


watchmedo shell-command -W --recursive --pattern '*.php;*.txt' --command 'echo test'Monitoring folder (current directory if not specified)

--shell-command: Specified when executing a shell command. There is also log. --W: W in capital letters. Ignore events that occur while the command is being executed (concurrent event ignore setting) --recursive: Recursively go to the specified folder --pattern: Set the file to be monitored (in the above, the extensions are php and txt) --command: The command to execute. ~~ In the above example, only the update event is echoed. (When I used vim on windows, 3 events flew at the same time as saving ... 2 Netbeans also flew ...) ~~

When running PHPUnit

cd Monitored folder
watchmedo shell-command -W --recursive --pattern '*.php' --command 'Project full path/vendor/bin/phpunit full path/tests/'

After moving to the project folder, you can omit specifying the folder to be monitored and it will be colored with phpunit. (On the contrary, why isn't it colored when the monitored folder is specified?)

Postscript


Installation is a bit cumbersome, but enjoy a comfortable coding life.

Recommended Posts