1

I have two bash scripts. One runs perfectly fine till it reaches the call for another script, it then executes that script fine but never EVER continues in the original script and I am not sure how= to fix the issue.

This script, which runs the tests in each folder of a specific directory. Each sub folder contains a phpunit.xml file and when we enter each folder we run composer install and phpunit.

Should the directory be, Loader, Routes or Loop, we will then enter those and run the script in side that directory.

Lets look at the run-tests script:

#!/usr/bin/env bash

set -e

function run_tests() {
  if [[ -f "composer.json" ]] && [[ -f "phpunit.xml" ]]; then
    if [[ -d "vendor" ]]; then
      composer update
      phpunit > /dev/null
      phpunit
      cd ../
    else
      composer install
      phpunit > /dev/null
      phpunit
      cd ../
    fi
  else
    cd ../
  fi
}

for f in *; do
  if [[ -d $f ]]; then
    if [[ $f != ".git" ]] && [[ $f != "bin" ]] && [[ $f != "docs" ]]; then
      cd "$f/"

      if [[ $f == "Loader" ]]; then
        if [[ -d "Assets" ]]; then
          cd Assets/
          chmod +x asset-test
          ./asset-test
        fi
      fi

      if [[ $f == "Loop" ]]; then
        cd Loop/
        chmod +x loop-test
        ./loop-test
        cd ../
      fi

      if [[ $f == "Routes" ]]; then
        cd Routes/
        chmod +x routes-test
        ./routes-test
        cd ../
      fi

      run_tests
    fi
  fi
done

# Go Home.
cd /vagrant/Freya/

## Clean up time!
## Remove all vendor and composer.lock folders - just because.
for f in *; do
  if [[ -d $f ]]; then
    if [[ $f != ".git" ]] && [[ $f != "bin" ]] && [[ $f != "docs" ]]; then
      cd "$f/"
      rm -rf vendor composer.lock
      cd ../
    fi
  fi
done

We can see that once it enter the check to see if the directory we are in is "Loops", we then check if there is a directory called "Assets" inside. Assuming this passes, we then enter that directory and run the next script:

#!/usr/bin/env bash

set -e

# Run Composer Install if vendor doesnt exist.
if [[ -d "vendor" ]]; then
  composer update
else
  composer install
fi

# Move up to the root directories and then get wordpress
cd ../../../

# Determine if the trunk is checked out and if we have a wp-test-config.php
# If not we need to create both or one or the other then run the tests.
#
# We run phpunit twice because the second time is when it actually runs.
if [[ -d "trunk" ]]; then
  cd "trunk/"
  if [[ -f "wp-tests-config.php" ]]; then
    cd ../Freya/Loader/Assets
    phpunit > /dev/null
    phpunit
  else
    cd ../Freya/Loader/Assets
    cp wp-tests-config.php ../../trunk/
    phpunit > /dev/null
    phpunit
  fi
else
  sudo svn co http://develop.svn.wordpress.org/trunk/
  cd Freya/Loader/Assets
  cp wp-tests-config.php ../../trunk/
  phpunit > /dev/null
  phpunit
fi

The issue is once this script finishes, the rest of the first script I posted NEVER continues. Its like it just dies here. What I need is for the "parent" script to finish, it should back out of Loader/Assets and continue on to the next check. But it doesn't. The script just exists.

Why?

Note: In the second script we can assume it goes into the first nested if statement, where it checks, in the trunk directory for a wp-tests-config.php We can assume this because of the following output, which was captured from running the first script:

$ bin/run-tests 
Loading composer repositories with package information
Installing dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.

Configuration read from /vagrant/Freya/Exceptions/phpunit.xml

.

Time: 451 ms, Memory: 13.25Mb

OK (1 test, 1 assertion)
Loading composer repositories with package information
Installing dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.

Configuration read from /vagrant/Freya/Factory/phpunit.xml

...........

Time: 711 ms, Memory: 13.25Mb

OK (11 tests, 11 assertions)
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing freya/freya-templates (dev-master a33ecdb)
    Cloning a33ecdb231b06dc8a7eef363edb177b8c134d55b

Writing lock file
Generating autoload files
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.

Configuration read from /vagrant/Freya/Form/phpunit.xml

...............................................................

Time: 647 ms, Memory: 15.25Mb

OK (63 tests, 63 assertions)
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.

Configuration read from /vagrant/Freya/Loader/Assets/phpunit.xml

...........

Time: 1.76 seconds, Memory: 38.00Mb

OK (11 tests, 11 assertions)
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 4.6.4 by Sebastian Bergmann and contributors.

Configuration read from /vagrant/Freya/Loader/Assets/phpunit.xml

...........

Time: 1.79 seconds, Memory: 38.00Mb

We can see two issues here:

  • The /vagrant/Freya/Loader/Assets/ are run twice.
  • It never continues on in the first script.
2
  • 2
    Try to post a minimal example, as most people will not be willing to read through all of this so you will be unlikely to get an answer. Commented Apr 29, 2015 at 7:51
  • JID++. And you may find that in the process of compacting your script into a minimal example, you find the problem yourself! Commented Apr 29, 2015 at 11:41

1 Answer 1

2

It is difficult to debug this remotely without everything set up as on your machine. For more helpful answers, you could try to isolate the problem first by successively disabling parts of your code. I can see that this may be difficult in your scenario.

Here are just a few thoughts for debugging:

  • Both scripts run with set -e, i.e. they will exit on the first command (including the second script) terminating with a non-zero exit code . Try adding set -x as well to see the last commands executed.
  • You have a lot of relative cd calls. Try to pwd just before the last command identified in the previous step - you may just be in the wrong directory.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.