Turn on debug logging for WordPress tests

Automated testing for WordPress plugins requires installation of a clean test environment. That test environment doesn’t have a debug log by default, so we need to edit the wp-tests-config.php file in the tests library.

Happily, we can automate that easily with sed — the stream editor. sed is useful for all sorts of automated text editing tasks, and I can’t think of an easier way to perform this task than sed.

We can use the option “-i” to ask sed to edit the file inline (i.e. not stream it somewhere else).

We then want somewhere safe to insert the new line, like the second line of the config file right after the opening PHP tag. That can be done with the command “2i” — at line 2, insert.

test: /tmp/wordpress-tests-lib
	vendor/bin/phpunit

/tmp/wordpress-tests-lib:
	bin/install-wp-tests.sh dbname dbuser dbpwd localhost nightly
	sed -i "2i define('WP_DEBUG_LOG', __DIR__ . '/debug.log');" /tmp/wordpress-tests-lib/wp-tests-config.php

Since I’m running my tests from a Makefile, I can add the sed command right after the script for installing the test environment, so that it runs every time I create a new environment.

Job is done, automatically so that I don’t forget to turn on debug logging.