Skip to content

Commit

Permalink
travis: extract functionality to scripts
Browse files Browse the repository at this point in the history
It's very advantageous to be able to run the Travis scripts locally. So
we extract them into separate scripts. They are explained in the README.

Closes #398.
  • Loading branch information
petertseng committed Feb 1, 2017
1 parent 0a37a4d commit cb04038
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 67 deletions.
60 changes: 3 additions & 57 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,64 +39,10 @@ script:
# Explicit set exercises' resolver only if it's not the current one.
if [ "${CURRENT}" != "YES" ]; then
SET_RESOLVER="--resolver ${RESOLVER}"
export SET_RESOLVER="--resolver ${RESOLVER}"
fi
test_exercise () {
stack test ${SET_RESOLVER} `# Select the correct resolver. `\
--install-ghc `# Download GHC if not in cache.`\
--no-terminal `# Terminal detection is broken.`\
--pedantic `# Enable -Wall and -Werror. `
}
for exercise in ${TRAVIS_BUILD_DIR}/exercises/*/ ; do
exercisename=$(basename "$exercise")
pushd ${exercise}
examplename="stub"
buildfolder="${TRAVIS_BUILD_DIR}/build/${exercisename}/${examplename}"
mkdir -p "${buildfolder}"
cp -rL stack.yaml package.yaml src test "${buildfolder}"
pushd $buildfolder
examplecache="${HOME}/.foldercache/${exercisename}/${examplename}/.stack-work"
mkdir -p "$examplecache"
ln -f -s "$examplecache"
if [ -f "${exercise}/.meta/DONT-TEST-STUB" ]; then
echo "only building stub"
stack build ${SET_RESOLVER} --install-ghc --no-terminal
else
echo "testing stub"
stack test ${SET_RESOLVER} --install-ghc --no-terminal --no-run-tests
fi
popd
if ! stat -t examples/*/ > /dev/null 2>&1; then
echo "No examples for ${exercise}!"
exit 1
else
for example in examples/*/ ; do
examplename=$(basename "$example")
buildfolder="${TRAVIS_BUILD_DIR}/build/${exercisename}/${examplename}"
mkdir -p "${buildfolder}"
cp -rL stack.yaml test ${example}/* "${buildfolder}"
pushd $buildfolder
examplecache="${HOME}/.foldercache/${exercisename}/${examplename}/.stack-work"
mkdir -p "$examplecache"
ln -f -s "$examplecache"
echo "testing ${example}"
test_exercise
popd
done
fi
popd
bin/test-stub $exercise
bin/test-all-examples $exercise
done
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,12 @@ a few seconds.

First you need to provide an [example solution](#example-solution).

Running `stack test --pedantic` compiles and runs the tests with
`-Wall -Werror`, but unfortunately it doesn't recompile unchanged
source code already compiled with warnings.
We provide three scripts in the `bin` directory of this repository to run the tests.
These are the same scripts as those used by Travis CI.

To be really sure that everything compiles correctly without
warnings, your must first run `stack clean`.

```bash
stack clean
stack test --pedantic
```
* `test-example path/to/example/dir` runs the tests on a single example.
* `test-all-examples path/to/exercise/dir` runs the tests on all examples for an exercise.
* `test-stub path/to/exercise/dir` checks that the stub for the given exercise compiles.

### Running HLint
All code in this repository should be as idiomatic as possible, so we
Expand Down
20 changes: 20 additions & 0 deletions bin/test-all-examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

set -e

if [ $# -eq 0 ]; then
echo "usage: $0 /path/to/exercise"
exit 1
fi

exercisedir=$1

if ! stat -t ${exercisedir}/examples/*/ > /dev/null 2>&1; then
echo "No examples for ${exercisedir}!"
exit 1
else
mydir=$(dirname $0)
for example in ${exercisedir}/examples/*/ ; do
$mydir/test-example $example
done
fi
35 changes: 35 additions & 0 deletions bin/test-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

if [ $# -eq 0 ]; then
echo "usage: $0 /path/to/exercise/example"
exit 1
fi

exampledir=$(cd "$1" && pwd)

examplename=$(basename "$exampledir")
exercisedir=$(cd "$exampledir/../.." && pwd)
exercisename=$(basename "$exercisedir")
xhaskell=$(cd "$(dirname "$0")/.." && pwd)

buildfolder="${TRAVIS_BUILD_DIR:-$xhaskell}/build/${exercisename}/${examplename}"
mkdir -p "${buildfolder}"
cp -rL ${exercisedir}/stack.yaml ${exercisedir}/test ${exampledir}/* "${buildfolder}"

cd $buildfolder

if [ -n "$TRAVIS" ]; then
cachedir="$HOME"
else
cachedir="$xhaskell"
fi
examplecache="${cachedir}/.foldercache/${exercisename}/${examplename}/.stack-work"
mkdir -p "$examplecache"
ln -f -s "$examplecache"

echo "testing ${exampledir}"
# SET_RESOLVER passed by .travis.yml - sets --resolver if not current.
stack test ${SET_RESOLVER} `# Select the correct resolver. `\
--install-ghc `# Download GHC if not in cache.`\
--no-terminal `# Terminal detection is broken.`\
--pedantic `# Enable -Wall and -Werror. `
35 changes: 35 additions & 0 deletions bin/test-stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh

if [ $# -eq 0 ]; then
echo "usage: $0 /path/to/exercise"
exit 1
fi

exercisedir=$(cd "$1" && pwd)
exercisename=$(basename "$exercisedir")
examplename="stub"
xhaskell=$(cd "$(dirname "$0")/.." && pwd)

buildfolder="${TRAVIS_BUILD_DIR:-$xhaskell}/build/${exercisename}/${examplename}"
mkdir -p "${buildfolder}"
cp -rL ${exercisedir}/stack.yaml ${exercisedir}/package.yaml ${exercisedir}/src ${exercisedir}/test "${buildfolder}"

cd $buildfolder

if [ -n "$TRAVIS" ]; then
cachedir="$HOME"
else
cachedir="$xhaskell"
fi
examplecache="${cachedir}/.foldercache/${exercisename}/${examplename}/.stack-work"
mkdir -p "$examplecache"
ln -f -s "$examplecache"

# SET_RESOLVER passed by .travis.yml - sets --resolver if not current.
if [ -f "${exercisedir}/.meta/DONT-TEST-STUB" ]; then
echo "only building stub"
stack build ${SET_RESOLVER} --install-ghc --no-terminal
else
echo "testing stub"
stack test ${SET_RESOLVER} --install-ghc --no-terminal --no-run-tests
fi

0 comments on commit cb04038

Please sign in to comment.