Using WP_UnitTestCase scaffolding in PHPStrom with composer - wordpress

I configured WP_UnitTestCase and the wp_cli and PHPUnit with composer in PHPStorm. I had an issue where it couldn’t find/use my WP DB username and password to create the DB so I created it manually in the terminal. (was this a mistake?) install-wp-tests.sh the installed, I was able to run some test did a bit of development and then shutdown my machine.
The next day, I started up my machine and attempted to continue where I left off. However PHPunit (should I be calling this WP_UnitTestCase instead?) Failed to run with the following errors.
Warning: require_once(/tmp/wordpress-tests-lib/includes/functions.php): failed to >open stream: No such file or directory in /thefullpathtowordpressprojectd/wp->content/plugins/myplugin/tests/bootstrap.php on line 8
Well /tmp/wordpress-tests-lib/ doesn’t exist
So I look into the install-wp-tests.sh and its installing itself to a tmp directory…okay?! (So ok I did notice that phpunit said Installing when I ran my tests the night before… so it reinstalls itself for every test!?)
Then I re-run ‘install-wp-tests.sh’, it fails when attempting to create the ‘wordpress-tests’ db since it already exist but I can now run tests again.
My question is even if the scaffolding/ WP_UnitTestCase reinstalls itself every time I run phpunit in the directory of my plugin folder where my phpunit.xml is. What do I have to change in the ‘install-wp-tests.sh’ or phpunit.xml so I don't have to manually reinstall for an existing project, how to clear the require_once error for the tmp empty directory?
Should I have to reinstall it everytime I reopen my project?
my install-we-test.sh is unchanged, but here it is:
#!/usr/bin/env bash
if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
exit 1
fi
DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
set -ex
download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
wget -nv -O "$2" "$1"
fi
}
install_wp() {
if [ -d $WP_CORE_DIR ]; then
return;
fi
mkdir -p $WP_CORE_DIR
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
else
local ARCHIVE_NAME="wordpress-$WP_VERSION"
fi
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}
install_test_suite() {
# 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
# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite
mkdir -p $WP_TESTS_DIR
svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/ $WP_TESTS_DIR/includes
fi
cd $WP_TESTS_DIR
if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
fi
}
install_db() {
# parse DB_HOST for port or socket references
local PARTS=(${DB_HOST//\:/ })
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""
if ! [ -z $DB_HOSTNAME ] ; then
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif ! [ -z $DB_HOSTNAME ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi
# create database
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
}
install_wp
install_test_suite
install_db
and my phpunit.xml
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>

Related

Cannot get sed to update a string [duplicate]

This question already has answers here:
Environment variable substitution in sed
(12 answers)
Closed 2 years ago.
It seems like sed is not replacing the string on my code.
What I intend for it to do is to change the property called "notification.enable" from false to true from server2 to activate, and, true to false in server1 to deactivate.
It works when I do it individually using below but when doesn't when being called from a function.
sed -i 's/notification.enable=false/notification.enable=true/g' /usr/config/activate.properties
function prompt_activate_server2_config {
activate="notification.enable=true"
deactivate="notification.enable=false"
dir=/usr/config
filename=$dir/activate.properties
output=$(grep "notification" $filename)
if [ -e $filename ];
then
sed -i 's/$deactivate/$activate/g' "$filename"
echo "Activated in Server 2"
echo $output;date
fi
}
function prompt_deactivate_server1_config {
activate="notification.enable=true"
deactivate="notification.enable=false"
dir=/usr/config
filename=$dir/activate.properties
output=$(grep "notification" $filename)
if [ -e $filename ];
then
sed -i 's/$activate/$deactivate/g' "$filename"
echo "Deactivated in Server 1"
echo $output;date
fi
}
I'm using function because I need to remotely execute the scripts from another server
So I call them using below. All the 'execute' stuff is on the above function. What am I missing?
if [ "$selection" == 'Activate Config' ]; then
#Activate server1/deactivate server2#
ssh user#server2 "$(typeset -f prompt_activate_server2_config); prompt_activate_server2_config"
sleep 2
ssh user#server1 "$(typeset -f prompt_deactivate_server1_config); prompt_deactivate_server1_config"
fi
You need to evaluate the two variables ($deactivate and $activate) and you do that by using double rather than single quotes:
sed -i "s/$deactivate/$activate/g" "$filename"
I would write it like this (and use sed rather than grep if you really need to print before/after):
update_config() {
local file=$1
local key=$2
local from=$3
local to=$4
[ -e "$file" ] && sed -i "s/\($key=\)$from/\\1$to/g" "$file"
}
update_config /usr/config/activate.properties 'notification\.enable' 'false' 'true'

/Users/username/.jenv/jenv.version: Permission denied

macOS Catalina, installed jenv 0.5.4 using homebrew, using zsh, followed all the steps listed in https://www.jenv.be/
In terminal I have the following error
Last login: Tue Dec 22 10:10:15 on ttys002
/usr/local/Cellar/jenv/0.5.4/libexec/libexec/jenv-refresh-plugins: line 14: /Users/username/.jenv/jenv.version: Permission denied
The below is the code from jenv-refresh-plugins
#!/usr/bin/env bash
# Summary: Refresh plugins links
resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}
set -e
[ -n "$JENV_DEBUG" ] && set -x
FORCE_REFRESH=0
if [ ! -f "${JENV_ROOT}/jenv.version" ]; then
echo "NONE" > ${JENV_ROOT}/jenv.version
fi
if [ "$1" = "--complete" ]; then
echo "--force"
exit
fi
if [ "$1" = "--force" ]; then
FORCE_REFRESH=1
fi
lastVersion=$(cat "${JENV_ROOT}/jenv.version" || echo "none")
currentVersion=$(jenv --version)
if [ ! "$lastVersion" == "$currentVersion" ] || [ $FORCE_REFRESH == "1" ]; then
echo "jenv has been updated, process to refresh plugin links"
for path in "${JENV_ROOT}/plugins/"*; do
if [ -L "$path" ]; then
pluginName=$(basename $path)
echo "Refresh plugin $pluginName"
ln -sfn "${JENV_INSTALL_DIR}/available-plugins/$pluginName" "${JENV_ROOT}/plugins/$pluginName"
fi
done
fi
echo "$currentVersion" > "${JENV_ROOT}/jenv.version"
jenv doctor
[OK] No JAVA_HOME set
[OK] Java binaries in path are jenv shims
[OK] Jenv is correctly loaded
Any help, much appreciated.
I just experienced this same issue on mac- it was caused because for some reason the folder /Users/username/.jenv had become locked.
I couldn't find a way to unlock it, so i just copied it to another directory, then ran sudo rm -rf /Users/username/.jenv , copied it back, and that has solved the problem.

How to tell script to look only into a specific folder

I'm trying to make a recycle bin for UNIX, so I have two scripts. 1 to delete the file and move it to the bin, the other script to restore the file back to its original location.
my restore script only works if the person gives the path to the deleted file.
ex: sh restore ~/trashbin/filename
How do I hardcode into my script so that I don't need to give the path to the deleted file it should already know to look in the trashbin for the file. My restore script works only when someone calls in the path to the file.
#!/bin/bash
rlink=$(readlink -e "$1")
rname=$(basename "$rlink")
function restoreFile() {
rlink=$(readlink -e "$1")
rname=$(basename "$rlink")
rorgpath=$(grep "$rname" ~/.restore.info | cut -d":" -f2)
rdirect=$(dirname "$rorgpath")
#echo $orgpath
if [ ! -d "$rdirect" ]
then
mkdir -p $rdirect
#echo $var
mv $rlink $rorgpath
else
mv $rlink $rorgpath
fi
}
if [ -z "$1" ]
then
echo "Error no filename provided."
exit 1
elif [ ! -f "$1" ]
then
echo "Error file does not exist."
exit 1
elif [ -f "$rorgpath" ]
then
echo "File already exists in original path."
read -p "Would you like to overwrite it? (y/n)" ovr
if [[ $ovr = y || $ovr = Y || $ovr = yes ]]
then
echo "Restoring File and overwriting."
restoreFile $1
grep -v "$rname" ~/.restore.info > ~/.restorebackup.info
mv ~/.restorebackup.info ~/.restore.info
fi
else
echo "Restoring file into original path."
restoreFile $1
grep -v "$rname" ~/.restore.info > ~/.restorebackup.info
mv ~/.restorebackup.info ~/.restore.info
fi
When you "remove" the file from the file-system to your trash-bin, move it so that its path is remembered. Example: removing file /home/user/file.txt should mean moving this file to ~/.trash/home/user/file.txt. That way, you'll be able to restore files to the original location, and you'll have auto-complete work, since you can do: sh restore ~/.trash/<TAB><TAB>

How do I deploy a artifact with maven layout using REST API?

I can do a normal deploy using the below command
curl -i -X PUT -u $artifactoryUser:$artifactoryPassword -T /path/to/file/file.zip http://localhost/artifactory/simple/repo/groupId/artifactId/version/file.zip
However, this will not resolve or update maven layout on the artifact. Is there a way I can upload without using the artifactory-maven plugin?
I found a solution to this question I had posted.
Syntax Used:
curl -i -X PUT -K $CURLPWD "http://localhost/artifactory/$REPO/$groupId/$artifactId/$versionId/$artifactId-$versionId.$fileExt"
Ended up writing a script so that md5 & sha1 values are uploaded with the file, or else, I had to go in Artifactory and fix it manually.
#!/bin/bash
usage() {
echo "Please check the Usage of the Script, there were no enough parameters supplied."
echo "Usage: ArtifactoryUpload.sh localFilePath Repo GroupID ArtifactID VersionID"
exit 1
}
if [ -z "$5" ]; then
usage
fi
localFilePath="$1"
REPO="$2"
groupId="$3"
artifactId="$4"
versionId="$5"
ARTIFAC=http://localhost/artifactory
if [ ! -f "$localFilePath" ]; then
echo "ERROR: local file $localFilePath does not exists!"
exit 1
fi
which md5sum || exit $?
which sha1sum || exit $?
md5Value="`md5sum "$localFilePath"`"
md5Value="${md5Value:0:32}"
sha1Value="`sha1sum "$localFilePath"`"
sha1Value="${sha1Value:0:40}"
fileName="`basename "$localFilePath"`"
fileExt="${fileName##*.}"
echo $md5Value $sha1Value $localFilePath
echo "INFO: Uploading $localFilePath to $targetFolder/$fileName"
curl -i -X PUT -K $CURLPWD \
-H "X-Checksum-Md5: $md5Value" \
-H "X-Checksum-Sha1: $sha1Value" \
-T "$localFilePath" \
"$ARTIFAC/$REPO/$groupId/$artifactId/$versionId/$artifactId-$versionId.$fileExt"

Unix troubleshooting, missing /etc/init.d file

I am working through this tutorial on daemonizing php scripts. When I run the following Unix command:
. /etc/init.d/functions
#startup values
log=/var/log/Daemon.log
#verify that the executable exists
test -x /home/godlikemouse/Daemon.php || exit 0RETVAL=0
prog="Daemon"
proc=/var/lock/subsys/Daemon
bin=/home/godlikemouse/Daemon.php
start() {
# Check if Daemon is already running
if [ ! -f $proc ]; then
echo -n $"Starting $prog: "
daemon $bin --log=$log
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $proc
echo
fi
return $RETVAL
}
I get the following output:
./Daemon: line 12: /etc/init.d/functions: No such file or directory
Starting Daemon: daemon: unrecognized option `--log=/var/log/Daemon.log'
I looked at my file system and there was no /etc/init.d file. Can anyone tell me what this is and where to obtain it? Also is the absence of that file what's causing the other error?
Separate your args within their own " " double-quotes:
args="--node $prog"
daemon "nohup ${exe}" "$args &" </dev/null 2>/dev/null
daemon "exe" "args"

Resources