<commit_before>#!/usr/bin/env bash

if [ $# -lt 3 ]; then
	echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [no-cache]"
	exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
NO_CACHE=${6-false}
TRAVIS=${$TRAVIS:-false}

EXEC_DIR="$(pwd)"

# Formatting output to make it easier to function read 
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

set -ex

update_postmedia_test_config() {

	# pull down the custom files required to support wordpress and the testing configs
	cd $EXEC_DIR
	git clone --quiet https://github.com/Postmedia-Digital/CI_Config.git /tmp/ci_config
	# these three lines are temporary to get the "beta" changes
	cd /tmp/ci_config
	git checkout eslint
	cd $EXEC_DIR
	
	# Load the configuraiton files and get dependency versions
	source /tmp/ci_config/versions.cfg
	echo -e "${CYAN}Executing with WordPress ${WP_VERSION}, PHP Codesniffer ${PHP_CODESNIFFER_VERSION}, WordPress Coding Standards ${WP_CODING_STD_VERSION}, ESS Lint ${JS_LINT_VERSION}, and CSS Lint ${CSS_LINT_VERSION}${NC}"

}

remove_previous_temp_files() {
	# this function removes and reverts files before the installations, no errors are passed on if they don't exist
	# the deletion of wordpress, phpcodesniffer and related installations are handled based on cache status in those functions
	rm -rf /tmp/ci_config
}

remove_previous_temp_files
update_postmedia_test_config

# Export these into environment variables as other parts may rely on them
export WP_TESTS_DIR=${WP_TESTS_DIR:-${INSTALL_PATH}/wordpress-tests-lib/}
export WP_CORE_DIR="$INSTALL_PATH/wordpress/"
export CODE_SNIFFER_DIR="$INSTALL_PATH/php-codesniffer/"
export WP_CODING_STD_DIR="$INSTALL_PATH/wordpress-coding-standards/"

install_wp() {
	if [ "$NO_CACHE" == "false" ] && [ -d $WP_CORE_DIR ]; then
		# Directory exists, lets look for the version
		if [ -f $WP_CORE_DIR/.ci-$WP_VERSION.ver ]; then
			# current version in cache or installed is correct, skip
			echo -e "${CYAN}WordPress version ${WP_VERSION} is cached, skipping re-install.${NC}"
			return
		else
			# Not found or installed.
			echo -e "${RED}Installed WordPress version does not match version required ( ${WP_VERSION} ), beginning fresh install...${NC}"
		fi
	else
		# either not caching or directory doesnt exist
		echo -e "${RED}No cached copy present or requested for WordPress, beginning fresh install...${NC}"
	fi

	# Remove any files that may still be there before re-installing
	if [ -d $WP_CORE_DIR ]; then
		rm -rf $WP_CORE_DIR
	fi

	mkdir -p $WP_CORE_DIR

	local ARCHIVE_NAME="wordpress-$WP_VERSION"
	wget -nv -O /tmp/wordpress.tar.gz https://wordpress.org/${ARCHIVE_NAME}.tar.gz
	tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

	# mark the version
	touch ${WP_CORE_DIR}/.ci-${WP_VERSION}.ver
	cp /tmp/ci_config/db.php $WP_CORE_DIR/wp-content/db.php 
}

install_test_suite() {
	if [ "$NO_CACHE" == "false" ] && [ -d $WP_TESTS_DIR ]; then
		# Directory exists, and we are using cache if version is correct
		if [ -f $WP_TESTS_DIR/.ci-$WP_VERSION.ver ]; then
			# current version is in cach/installed.  Skip.
			echo -e "${CYAN}WordPress Test Librairies version ${WP_VERSION} is cached, skipping re-install.${NC}"
			# just update the configuration files as these change with each execution (potentially)
			update_test_configuration_files
			return
		else
			# Not installed or incorrect version
			echo -e "${RED}Installed WordPress Test Librairies version does not match the version required ( ${WP_VERSION} ), beginning fresh install...${NC}"
		fi
	else
		# either we are not caching or directory does not exist ( first run )
		echo -e "${RED}No cached copy present or requested for WordPress Test Library, beginning fresh install...${NC}"
	fi

	# portable in-place argument for both GNU sed and Mac OSX sed
	if [[ $(uname -s) == 'Darwin' ]]; then
		local ioption='-i .bak'
	else
		local ioption='-i'
	fi

	# Remove any files that may still be there before re-installing
	if [ -d $WP_TESTS_DIR ]; then
		rm -rf $WP_TESTS_DIR
	fi

	# set up testing suite
	mkdir -p $WP_TESTS_DIR
	cd $WP_TESTS_DIR
	svn co --quiet --non-interactive --trust-server-cert https://develop.svn.wordpress.org/tags/${WP_VERSION}/tests/phpunit/includes/ 
	svn co --quiet --non-interactive --trust-server-cert https://develop.svn.wordpress.org/tags/${WP_VERSION}/tests/phpunit/tests/
	svn co --quiet --non-interactive --trust-server-cert https://develop.svn.wordpress.org/tags/${WP_VERSION}/tests/phpunit/data/

	wget -nv -O wp-tests-config.php https://develop.svn.wordpress.org/tags/${WP_VERSION}/wp-tests-config-sample.php

	# Pull down wpcom_vip helper functionality - Developers should include these in the bootstrap file as needed only
	svn co --quiet --non-interactive --trust-server-cert https://vip-svn.wordpress.com/plugins/vip-do-not-include-on-wpcom
	wget -nv https://vip-svn.wordpress.com/plugins/vip-init.php
	wget -nv https://vip-svn.wordpress.com/plugins/vip-helper.php
	wget -nv https://vip-svn.wordpress.com/plugins/vip-helper-wpcom.php
	wget -nv https://vip-svn.wordpress.com/plugins/vip-helper-stats-wpcom.php

	# Update all the test configuration files from latest config or execution function values
	update_test_configuration_files
	
	# mark the version
	touch ${WP_TESTS_DIR}/.ci-${WP_VERSION}.ver
}

update_test_configuration_files() {
	
	# Remove the temp files for test configuration
	rm -f $EXEC_DIR/tests/phpunit.xml
	rm -f $EXEC_DIR/tests/phpunit.xml.bak
	rm -f $EXEC_DIR/tests/codesniffer.ruleset.xml

	# copy the codesniffer ruleset into the tests folder.
	cp /tmp/ci_config/codesniffer.ruleset.xml $EXEC_DIR/tests/

	# copy the phpunit test config into the tests folder.
	cp /tmp/ci_config/phpunit.xml $EXEC_DIR/tests/

	# modifies the wp-tests-config.php file for db access
	sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php
	sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php
	sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php
	sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php
	sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php

	# modify the path to reflect actual test folder path
	sed $ioption "s:replace/:$WP_TESTS_DIR/tests/:" $EXEC_DIR/tests/phpunit.xml
<commit_msg>opps missed changing to test lib dir<commit_after>#!/usr/bin/env bash

if [ $# -lt 3 ]; then
	echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [no-cache]"
	exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
NO_CACHE=${6-false}
TRAVIS=${$TRAVIS:-false}

EXEC_DIR="$(pwd)"

# Formatting output to make it easier to function read 
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

set -ex

update_postmedia_test_config() {

	# pull down the custom files required to support wordpress and the testing configs
	cd $EXEC_DIR
	git clone --quiet https://github.com/Postmedia-Digital/CI_Config.git /tmp/ci_config
	# these three lines are temporary to get the "beta" changes
	cd /tmp/ci_config
	git checkout eslint
	cd $EXEC_DIR
	
	# Load the configuraiton files and get dependency versions
	source /tmp/ci_config/versions.cfg
	echo -e "${CYAN}Executing with WordPress ${WP_VERSION}, PHP Codesniffer ${PHP_CODESNIFFER_VERSION}, WordPress Coding Standards ${WP_CODING_STD_VERSION}, ESS Lint ${JS_LINT_VERSION}, and CSS Lint ${CSS_LINT_VERSION}${NC}"

}

remove_previous_temp_files() {
	# this function removes and reverts files before the installations, no errors are passed on if they don't exist
	# the deletion of wordpress, phpcodesniffer and related installations are handled based on cache status in those functions
	rm -rf /tmp/ci_config
}

remove_previous_temp_files
update_postmedia_test_config

# Export these into environment variables as other parts may rely on them
export WP_TESTS_DIR=${WP_TESTS_DIR:-${INSTALL_PATH}/wordpress-tests-lib/}
export WP_CORE_DIR="$INSTALL_PATH/wordpress/"
export CODE_SNIFFER_DIR="$INSTALL_PATH/php-codesniffer/"
export WP_CODING_STD_DIR="$INSTALL_PATH/wordpress-coding-standards/"

install_wp() {
	if [ "$NO_CACHE" == "false" ] && [ -d $WP_CORE_DIR ]; then
		# Directory exists, lets look for the version
		if [ -f $WP_CORE_DIR/.ci-$WP_VERSION.ver ]; then
			# current version in cache or installed is correct, skip
			echo -e "${CYAN}WordPress version ${WP_VERSION} is cached, skipping re-install.${NC}"
			return
		else
			# Not found or installed.
			echo -e "${RED}Installed WordPress version does not match version required ( ${WP_VERSION} ), beginning fresh install...${NC}"
		fi
	else
		# either not caching or directory doesnt exist
		echo -e "${RED}No cached copy present or requested for WordPress, beginning fresh install...${NC}"
	fi

	# Remove any files that may still be there before re-installing
	if [ -d $WP_CORE_DIR ]; then
		rm -rf $WP_CORE_DIR
	fi

	mkdir -p $WP_CORE_DIR

	local ARCHIVE_NAME="wordpress-$WP_VERSION"
	wget -nv -O /tmp/wordpress.tar.gz https://wordpress.org/${ARCHIVE_NAME}.tar.gz
	tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR

	# mark the version
	touch ${WP_CORE_DIR}/.ci-${WP_VERSION}.ver
	cp /tmp/ci_config/db.php $WP_CORE_DIR/wp-content/db.php 
}

install_test_suite() {
	if [ "$NO_CACHE" == "false" ] && [ -d $WP_TESTS_DIR ]; then
		# Directory exists, and we are using cache if version is correct
		if [ -f $WP_TESTS_DIR/.ci-$WP_VERSION.ver ]; then
			# current version is in cach/installed.  Skip.
			echo -e "${CYAN}WordPress Test Librairies version ${WP_VERSION} is cached, skipping re-install.${NC}"
			# just update the configuration files as these change with each execution (potentially)
			update_test_configuration_files
			return
		else
			# Not installed or incorrect version
			echo -e "${RED}Installed WordPress Test Librairies version does not match the version required ( ${WP_VERSION} ), beginning fresh install...${NC}"
		fi
	else
		# either we are not caching or directory does not exist ( first run )
		echo -e "${RED}No cached copy present or requested for WordPress Test Library, beginning fresh install...${NC}"
	fi

	# portable in-place argument for both GNU sed and Mac OSX sed
	if [[ $(uname -s) == 'Darwin' ]]; then
		local ioption='-i .bak'
	else
		local ioption='-i'
	fi

	# Remove any files that may still be there before re-installing
	if [ -d $WP_TESTS_DIR ]; then
		rm -rf $WP_TESTS_DIR
	fi

	# set up testing suite
	mkdir -p $WP_TESTS_DIR
	cd $WP_TESTS_DIR
	svn co --quiet --non-interactive --trust-server-cert https://develop.svn.wordpress.org/tags/${WP_VERSION}/tests/phpunit/includes/ 
	svn co --quiet --non-interactive --trust-server-cert https://develop.svn.wordpress.org/tags/${WP_VERSION}/tests/phpunit/tests/
	svn co --quiet --non-interactive --trust-server-cert https://develop.svn.wordpress.org/tags/${WP_VERSION}/tests/phpunit/data/

	wget -nv -O wp-tests-config.php https://develop.svn.wordpress.org/tags/${WP_VERSION}/wp-tests-config-sample.php

	# Pull down wpcom_vip helper functionality - Developers should include these in the bootstrap file as needed only
	svn co --quiet --non-interactive --trust-server-cert https://vip-svn.wordpress.com/plugins/vip-do-not-include-on-wpcom
	wget -nv https://vip-svn.wordpress.com/plugins/vip-init.php
	wget -nv https://vip-svn.wordpress.com/plugins/vip-helper.php
	wget -nv https://vip-svn.wordpress.com/plugins/vip-helper-wpcom.php
	wget -nv https://vip-svn.wordpress.com/plugins/vip-helper-stats-wpcom.php

	# Update all the test configuration files from latest config or execution function values
	update_test_configuration_files
	
	# mark the version
	touch ${WP_TESTS_DIR}/.ci-${WP_VERSION}.ver
}

update_test_configuration_files() {
	
	# Remove the temp files for test configuration
	rm -f $EXEC_DIR/tests/phpunit.xml
	rm -f $EXEC_DIR/tests/phpunit.xml.bak
	rm -f $EXEC_DIR/tests/codesniffer.ruleset.xml

	# copy the codesniffer ruleset into the tests folder.
	cp /tmp/ci_config/codesniffer.ruleset.xml $EXEC_DIR/tests/

	# copy the phpunit test config into the tests folder.
	cp /tmp/ci_config/phpunit.xml $EXEC_DIR/tests/

	# modifies the wp-tests-config.php file for db access
	cd $WP_TESTS_DIR
	sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php
	sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php
	sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php
	sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php
	sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php

	# modify the path to reflect actual test folder path
	sed $ioption "s:replace/:$WP_TESTS_DIR/tests/:" $EXEC_DIR/tests/phpunit.xml

{'fraction_non_alphanumeric': 0.12069647803719825, 'fraction_numerical': 0.004115552037989711, 'mean_word_length': 4.2171758876961185, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 22, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2852521', 'n_tokens_mistral': 4500, 'n_tokens_neox': 4243, 'n_words': 1220}