-# When our "bundle install"s need to install new gems to satisfy
-# dependencies, we want them to go where "gem install --user-install"
-# would put them. This could be ~/.gem/..., or something rvm has set
-# up. We don't want to install them to our GEMHOME tmpdir, though:
-# that would mean re-downloading all dependencies each time we run
-# tests with clean tmpdirs. The first dir in `gem env gempath` seems
-# to give us this dir. (Note: this is a no-op if rvm is in use.)
-user_gempath="$(gem env gempath)"
-export GEM_HOME="${user_gempath%%:*}"
-PATH="$(gem env gemdir)/bin:$PATH"
-
-# Wherever "HOME=$GEMHOME gem install --user-install" installs stuff
-# to, we want its bin to be in our PATH. (Normally, this gempath is
-# inside our nice clean $GEMHOME, which means we can install the
-# current version there and expect integration tests to find it before
-# finding any other versions that happen to be installed
-# somewhere. But if rvm is enforcing its own idea where gempath should
-# go, we'll just assume rvm has been set up correctly rather than
-# fight it.)
-tmpdir_gempath="$(HOME="$GEMHOME" gem env gempath)"
-PATH="${tmpdir_gempath%%:*}/bin:$PATH"
+setup_ruby_environment() {
+ if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
+ source "$HOME/.rvm/scripts/rvm"
+ using_rvm=true
+ elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
+ source "/usr/local/rvm/scripts/rvm"
+ using_rvm=true
+ else
+ using_rvm=false
+ fi
+
+ if [[ "$using_rvm" == true ]]; then
+ # If rvm is in use, we can't just put separate "dependencies"
+ # and "gems-under-test" paths to GEM_PATH: passenger resets
+ # the environment to the "current gemset", which would lose
+ # our GEM_PATH and prevent our test suites from running ruby
+ # programs (for example, the Workbench test suite could not
+ # boot an API server or run arv). Instead, we have to make an
+ # rvm gemset and use it for everything.
+
+ [[ `type rvm | head -n1` == "rvm is a function" ]] \
+ || fatal 'rvm check'
+
+ # Put rvm's favorite path back in first place (overriding
+ # virtualenv, which just put itself there). Ignore rvm's
+ # complaint about not being in first place already.
+ rvm use @default 2>/dev/null
+
+ # Create (if needed) and switch to an @arvados-tests
+ # gemset. (Leave the choice of ruby to the caller.)
+ rvm use @arvados-tests --create \
+ || fatal 'rvm gemset setup'
+
+ rvm env
+ else
+ # When our "bundle install"s need to install new gems to
+ # satisfy dependencies, we want them to go where "gem install
+ # --user-install" would put them. (However, if the caller has
+ # already set GEM_HOME, we assume that's where dependencies
+ # should be installed, and we should leave it alone.)
+
+ if [ -z "$GEM_HOME" ]; then
+ user_gempath="$(gem env gempath)"
+ export GEM_HOME="${user_gempath%%:*}"
+ fi
+ PATH="$(gem env gemdir)/bin:$PATH"