Revision ae9f5b95ebf7b36577861434e2099b638540d730 authored by Anish Tondwalkar on 20 October 2019, 06:45:47 UTC, committed by Anish Tondwalkar on 20 October 2019, 06:45:47 UTC
1 parent eba2590
Raw File
travis
#!/bin/bash

set -eu
set -o pipefail

## Helper Functions

function loud {
  echo "$ $@"
  $@
}

# Source: https://github.com/travis-ci/travis-build/blob/fc4ae8a2ffa1f2b3a2f62533bbc4f8a9be19a8ae/lib/travis/build/script/templates/header.sh#L104-L123
RED="\033[31;1m"
GREEN="\033[32;1m"
RESET="\033[0m"
function travis_retry {
  local result=0
  local count=1
  while [ $count -le 3 ]; do
    [ $result -ne 0 ] && {
      echo -e "\n${RED}The command \"$@\" failed. Retrying, $count of 3.${RESET}\n" >&2
    }
    set +e
    "$@"
    result=$?
    set -e
    [ $result -eq 0 ] && break
    count=$(($count + 1))
    sleep 1
  done

  [ $count -eq 4 ] && {
    echo "\n${RED}The command \"$@\" failed 3 times.${RESET}\n" >&2
  }

  return $result
}

function prevent_timeout {
  local cmd="$@"

  $cmd &
  local cmd_pid=$!

  poke_stdout &
  local poke_pid=$!

  wait $cmd_pid
  exit_code=$?

  kill $poke_pid
  (wait $poke_pid 2>/dev/null) || true

  return $exit_code
}

function poke_stdout {
  # Print an invisible character every minute
  while true; do
    echo -ne "\xE2\x80\x8B"
    sleep 60
  done
}

function pastebin {
  curl -s -F 'clbin=<-' https://clbin.com
}

## Testing Stages

function clean_cache {
  local smt="$1"

  loud ghc-pkg unregister liquid-fixpoint --force || true
  loud rm "$HOME/.cabal/bin/$smt" || true
}

function install_smt {
  local smt="$1"

  mkdir -p "$HOME/.cabal/bin"
  loud curl "http://goto.ucsd.edu/~gridaphobe/$smt" -o "$HOME/.cabal/bin/$smt"
  loud chmod a+x "$HOME/.cabal/bin/$smt"
}

function install_cabal_deps {
  if ! _install_cabal_deps; then
    echo " ==> Cabal install failed. Clearing dependency cache and retrying."
    loud rm -rf "$HOME/.cabal"
    loud rm -rf "$HOME/.ghc"
    _install_cabal_deps
  fi
}

function _install_cabal_deps {
  loud travis_retry cabal update || return 1
  loud travis_retry cabal install --only-dependencies --enable-tests || return 1
}

function do_build {
  loud cabal configure -fbuild-external --enable-tests -v2
  loud prevent_timeout cabal build -j2
  loud cabal haddock
}

function do_test {
  loud prevent_timeout ./dist/build/test/test
}

function dump_fail_logs {
  find . -type f -wholename '*/.liquid/*' -name '*.log' -print0 | while IFS= read -r -d $'\0' file; do
    echo "${file}:"
    echo "    $(pastebin < "${file}")"
  done
}

function test_source_pkg {
  loud cabal sdist

  local src_tgz="dist/$(cabal info . | awk '{print $2 ".tar.gz";exit}')"

  if [ -f "$src_tgz" ]; then
    loud prevent_timeout cabal install -j4 "$src_tgz"
  else
    echo "expected '$src_tgz' not found"
    return 1
  fi
}

## Run Test Stage

stage="$1"
shift

$stage "$@"

back to top