Skip to content

Commit

Permalink
0.4.0 - Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Defty committed Apr 15, 2020
1 parent 7932c16 commit ac20820
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Desktop.ini
$RECYCLE.BIN/

# Project Specific
Dockerfile
docker-compose.yml
debug.log
config.json
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM php:7.3-apache

# install the PHP extensions we need
RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg default-mysql-client nano less && rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd \
&& docker-php-ext-install gd mysqli
Expand All @@ -13,7 +12,11 @@ RUN curl -o /bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/pha
&& chmod +x /bin/wp \
&& wp --info --allow-root

ENV WP_VERSION latest
RUN set -ex; \
curl -o wordpress.tar.gz https://wordpress.org/wordpress-5.3.2.tar.gz; \
tar -xzf wordpress.tar.gz -C /usr/src/; \
rm wordpress.tar.gz; \
chown -R www-data:www-data /usr/src/wordpress

COPY entrypoint.sh /entrypoint.sh

Expand Down
29 changes: 29 additions & 0 deletions Dockerfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM php:7.3-apache

RUN apt-get update && apt-get install -y wget libpng-dev libjpeg-dev gnupg default-mysql-client nano less && rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-configure gd \
&& docker-php-ext-install gd mysqli

RUN a2enmod rewrite

VOLUME /var/www/html

RUN curl -o /bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x /bin/wp \
&& wp --info --allow-root

RUN set -ex; \
curl -o wordpress.tar.gz https://wordpress.org/wordpress-WP_VERSION.tar.gz; \
tar -xzf wordpress.tar.gz -C /usr/src/; \
rm wordpress.tar.gz; \
chown -R www-data:www-data /usr/src/wordpress

COPY entrypoint.sh /entrypoint.sh

RUN chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 80

CMD ["apache2-foreground"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ This may take a while as it builds and starts a new docker container. However, y

> Having trouble? Use File sharing to allow local directories on the Mac to be shared with Linux containers. See more https://docs.docker.com/docker-for-mac/#file-sharing
Once it is running there is no need to re-start it every time you run cypress. WP Cypress will restore the database to its initial state between each suite of integration tests to ensure a clean slate between tests.
Once it is running there is no need to re-start it every time you run cypress. WP Cypress will start and restore the database to it's initial state between each suite of integration tests to ensure a clean slate between tests.

You can add environment variables to the `cypress.json` configuration file. You can use this to specify the version of WordPress and which plugins/themes to install. All plugins will be activated and the first theme in the list will be activated. If this file is changed, you will need to re-run `wp-cypress start` to see the changes take effect. Composer is recommended to manage plugins and themes to if they do not exist in your project directory.

Expand Down
5 changes: 3 additions & 2 deletions bin/createConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const createConfig = (userConfig, dir) => {
});
}

shell.cp(`${dir}/Dockerfile.template`, `${dir}/Dockerfile`);
shell.sed('-i', 'WP_VERSION', userConfig.version || 'latest', `${dir}/Dockerfile`);

const dockerComposeFile = shell.ShellString(`
version: '3.7'
services:
Expand All @@ -34,8 +37,6 @@ services:
build: .
ports:
- 80:80
environment:
WP_VERSION: ${userConfig.version || 'latest'}
volumes:
- wp:/var/www/html ${volumes.map((x) => `
- ${x}`).join('')}
Expand Down
29 changes: 21 additions & 8 deletions bin/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const shell = require('shelljs');

const retryCommand = require('./utils/retryCommand');
const createConfig = require('./createConfig');
const { exec, wpcli } = require('./utils/exec');
const { exec, cli, wpcli } = require('./utils/exec');
const run = require('./utils/run');
const configureWordPress = require('./configureWordPress');

const start = async (userConfig, packageDir, logFile) => {
const configFile = fs.createWriteStream(`${packageDir}/config.json`);
Expand All @@ -18,19 +17,33 @@ const start = async (userConfig, packageDir, logFile) => {
'docker-compose down --volumes && docker-compose build && docker-compose up -d',
logFile,
),
'Creating Test Environment',
'Test Environment running on port 80',
'Creating test container',
'Test container created',
logFile,
);

await run(
async () => retryCommand(() => wpcli('core is-installed', logFile), 2000, 30),
'Downloading & Installing WordPress',
'WordPress Installed',
async () => retryCommand(() => cli('mysqladmin ping -h"db"', logFile), 2000, 30),
'Waiting for database connection',
'Database connected',
logFile,
);

await configureWordPress(configFile.path, logFile);
await run(
async () => wpcli(`core config \
--dbhost=db \
--dbname=wordpress \
--dbuser=root \
--dbpass='' \
--locale=en_US \
--extra-php <<PHP
define( 'WP_DEBUG', true );
PHP
`, logFile),
'Creating config',
'Ready to start testing!',
logFile,
);
};

module.exports = start;
1 change: 1 addition & 0 deletions bin/utils/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ const exec = (cmd, logFile) => new Promise((resolve) => {

module.exports = {
exec,
cli: (command, logFile) => exec(`docker-compose exec -T wp ${command}`, logFile),
wpcli: (command, logFile) => exec(`docker-compose exec -T wp wp --allow-root ${command}`, logFile),
};
5 changes: 4 additions & 1 deletion bin/utils/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const ora = require('ora');
const shell = require('shelljs');

const run = async (command, start, succeed = false, logFile = false) => {
const spinner = ora(start).start();
const spinner = ora({
text: start,
prefixText: 'wp-cypress',
}).start();

const { code, stdout, stderr } = await command();

Expand Down
68 changes: 26 additions & 42 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
#!/bin/bash
set -eu
if [ -f wp-config.php ]; then
rm wp-config.php
fi

: ${WP_LOCALE:=${WP_LOCALE:-en_US}}
: ${WP_ADMIN_EMAIL:=${WP_ADMIN_EMAIL:-admin@example.com}}
: ${WP_DB_HOST:=db}
: ${WP_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}}
: ${WP_DB_PASSWORD:=''}
: ${WP_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-wordpress}}
user="$(id -u)"
group="$(id -g)"

wp core --allow-root download \
--version=${WP_VERSION} \
--force --debug
if [ "$(id -u)" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then
chown "$user:$group" .
fi

c=1
until mysqladmin ping -h"$WP_DB_HOST" --silent &> /dev/null
do
c=$((c + 1))
if [ $c -eq 60 ]
then
break
fi
sleep 2
done
sourceTarArgs=(
--create
--file -
--directory /usr/src/wordpress
--owner "$user" --group "$group"
)
targetTarArgs=(
--extract
--file -
)

# Generate the wp-config file for debugging.
wp core --allow-root config \
--dbhost="$WP_DB_HOST" \
--dbname="$WP_DB_NAME" \
--dbuser="$WP_DB_USER" \
--dbpass="$WP_DB_PASSWORD" \
--locale="$WP_LOCALE" \
--extra-php <<PHP
define( 'WP_DEBUG', true );
PHP
tar "${sourceTarArgs[@]}" . | tar "${targetTarArgs[@]}"

cat > .htaccess <<EOF
# BEGIN WordPress
Expand All @@ -55,15 +39,15 @@ EOF

chown "www-data:www-data" .htaccess

# Create the database.
wp db --allow-root create

wp --allow-root core install \
--url=http://localhost \
--title="WP Cypress" \
--admin_user=admin \
--admin_password=password \
--admin_email="[email protected]" \
--skip-email \
c=1
until mysqladmin ping -h"db" --silent &> /dev/null
do
c=$((c + 1))
if [ $c -eq 60 ]
then
break
fi
sleep 2
done

exec "$@"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bigbite/wp-cypress",
"version": "0.3.2",
"version": "0.4.0",
"description": "WordPress end to end testing with Cypress.io",
"main": "lib/index.js",
"repository": "https://github.com/bigbite/wp-cypress",
Expand Down

0 comments on commit ac20820

Please sign in to comment.