Settings for running a test each time you save a file in the editor using watchmedo (watchdog)

If you want to execute a task every time you save a file with an editor, you can use Guard for Ruby, Grunt if you are writing JS, but that kind of tool when writing Python code I also want to use something made by Python. Here, watchdog is used.

Introduced

pip install watchdog

Then you can use the watchmedo command of the command line utility. If you want to monitor file system events and execute specific commands like this

watchmedo shell-command --patterns="*.py" --recursive --command='echo "${watch_src_path} is saved"' .

vim settings

As you can see in the watchdog Readme, I can't get filesystem events when using vim on MacOSX + terminal. It's a good idea to change the following vim settings so that you don't create a swap file. My .vimrc is as follows. Or if you monitor the . *. Swp file, it will work.

.vimrc


let OSTYPE = system('uname')
if OSTYPE == "Darwin\n"
    set noswapfile
    set nowritebackup
endif

Create a watch task to run the test

For example, if you have a test task in your Makefile and you want to add a watch task that runs the test task when you save the file

Makefile


watch:
	#Under the app directory*.Monitor py file events and run make test
	@echo Watch file changes and run test
	watchmedo shell-command --interval=5 --patterns="*.py" -R -W -D --command='make test' app

test:
	# (Example)Run Django tests
	python app/manage.py test;

Needs a double boot prevention option to prevent the test from running multiple times when multiple filesystem events are skipped.

Recommended Posts

Settings for running a test each time you save a file in the editor using watchmedo (watchdog)
A useful note when using Python for the first time in a while
Save the setting conditions as a CSV file using UDF Manager in OCTA
Get the file name in a folder using glob
Register a task in cron for the first time
[Python] You can save an object to a file by using the pickle module.
Save the pystan model and results in a pickle file
If you are a beginner in programming, why not make a "game" for the time being? The story
Try using FireBase Cloud Firestore in Python for the time being
Save the binary file in Python
Save a YAML-formatted file in PyYAML
Test & Debug Tips: Create a file of the specified size in Python
Until you win the silver medal (top 3%) in the competition you participated in within a month for the first time in data science!
How to save the feature point information of an image in a file and use it for matching
Write the test in a python docstring
Change the list in a for statement
MongoDB for the first time in Python
[IOS] Change the display time for each frame of GIF animation in Pythonista3.
Create a Python image in Django without a dummy image file and test the image upload
Test the number of times you have thrown a query (sql) in django
Save an array of numpy to a wav file using the wave module
Replace the named entity in the read text file with a label (using GiNZA)
Turn multiple lists with a for statement at the same time in Python
It's okay to participate for the first time! A hackathon starter kit that you want to prepare "before" participating in the hackathon!