Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bin/test to detect and trigger unit tests #982

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Remove vendored `jq` binary (#1112).
- Remove redundant Mercurial install step (#1111).
- Remove support for the Cedar-14 stack (#1110).
- Add `bin/test` to detect and trigger unit tests if desired

## v184 (2020-10-21)

Expand Down
45 changes: 45 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
# bin/test <build-dir> <env-dir>
# Inspired by test script in heroku/heroku-buildpack-php
set -eo pipefail

# Setup Path variables, for later use in the Buildpack.
BIN_DIR=$(cd "$(dirname "$0")"; pwd)
export PATH=/app/.heroku/python/bin:/app/.heroku/vendor/bin:$PATH

function unit_test_framework_is()
{
local framework="$1"
cat requirements.txt 2> /dev/null | grep --quiet -- "\s*${framework}\b" || \
cat Pipfile 2> /dev/null | grep --quiet -- "\s*${framework}\b"
}

# unlike a regular app.json "test" script, this one isn't run with the app dir as the CWD
# we `cd` with a default value, so that the script can also be called from inside the app dir (`cd ""` does not change cwd)
cd "${1-}"

# Import the utils script, which contains helper functions used throughout the buildpack.
# shellcheck source=bin/utils
source "$BIN_DIR/utils"

puts-step "Detecting test framework; first match will be used."

if [[ -n "${UNIT_TEST_COMMAND}" ]]; then
puts-step "Running test: ${UNIT_TEST_COMMAND}"
${UNIT_TEST_COMMAND} | indent
elif unit_test_framework_is "pytest"; then
puts-step "Running test: pytest"
pytest | indent
elif unit_test_framework_is "nose2"; then
puts-step "Running test: nose2"
nose2 | indent
elif unit_test_framework_is "nose"; then
puts-step "Running test: nosetests"
nosetests | indent
elif [[ -f manage.py ]]; then
puts-step "Running test: ./manage.py test"
./manage.py test | indent
else
puts-step "Running test: python -m unittest discover"
python -m unittest discover | indent
fi
1 change: 1 addition & 0 deletions test/fixtures/nose/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nose
5 changes: 5 additions & 0 deletions test/fixtures/nose/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_numbers_3_4():
assert (3 * 4) == 12

def test_strings_a_3():
assert 'a' * 3 == 'aaa'
1 change: 1 addition & 0 deletions test/fixtures/nose2/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nose2
5 changes: 5 additions & 0 deletions test/fixtures/nose2/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_numbers_3_4():
assert (3 * 4) == 12

def test_strings_a_3():
assert 'a' * 3 == 'aaa'
2 changes: 2 additions & 0 deletions test/fixtures/pipenv-pytest/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[packages]
"pytest" = "*"
1 change: 1 addition & 0 deletions test/fixtures/pytest/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
5 changes: 5 additions & 0 deletions test/fixtures/pytest/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_numbers_3_4():
assert (3 * 4) == 12

def test_strings_a_3():
assert 'a' * 3 == 'aaa'
1 change: 1 addition & 0 deletions test/fixtures/requirements-nose/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nose==1.3.7
5 changes: 5 additions & 0 deletions test/fixtures/unittest/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_numbers_3_4():
assert (3 * 4) == 12

def test_strings_a_3():
assert 'a' * 3 == 'aaa'
50 changes: 50 additions & 0 deletions test/run-features
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,56 @@ testHooks() {
assertCapturedSuccess
}

testRequirementsTestFramework() {
detect_test "requirements-nose"
assertCaptured "Running test: nosetests"
}

testRequirementsNoTestFramework() {
detect_test "requirements-standard"
assertCaptured "Running test: python -m unittest discover"
}

testPipenvTestFramework() {
detect_test "pipenv-pytest"
assertCaptured "Running test: pytest"
}

testPipenvNoTestFramework() {
detect_test "pipenv"
assertCaptured "Running test: python -m unittest discover"
}

testDjangoTest() {
compile_and_test "collectstatic"
assertCaptured "Running test: ./manage.py test"
assertCapturedSuccess
}

testPytest() {
compile_and_test "pytest"
assertCaptured "Running test: pytest"
assertCapturedSuccess
}

testUnittest() {
compile_and_test "unittest"
assertCaptured "Running test: python -m unittest discover"
assertCapturedSuccess
}

testNose() {
compile_and_test "nose"
assertCaptured "Running test: nosetests"
assertCapturedSuccess
}

testNose2() {
compile_and_test "nose2"
assertCaptured "Running test: nose2"
assertCapturedSuccess
}

pushd $(dirname 0) >/dev/null
popd >/dev/null

Expand Down
19 changes: 19 additions & 0 deletions test/utils
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ release() {
capture run_in_clean_env ${bp_dir}/bin/release ${bp_dir}/test/fixtures/$1
}

compile_and_test() {
default_process_types_cleanup
bp_dir=$(mktmpdir)
compile_dir=$(mktmpdir)
cp -a $(pwd)/* ${bp_dir}
cp -a ${bp_dir}/test/fixtures/$1/. ${compile_dir}
capture ${bp_dir}/bin/compile ${compile_dir} ${2:-$(mktmpdir)} $3
capture ${bp_dir}/bin/test ${compile_dir} $3
}

detect_test() {
default_process_types_cleanup
bp_dir=$(mktmpdir)
compile_dir=$(mktmpdir)
cp -a $(pwd)/* ${bp_dir}
cp -a ${bp_dir}/test/fixtures/$1/. ${compile_dir}
capture ${bp_dir}/bin/test ${compile_dir} $3
}

assertFile() {
assertEquals "$1" "$(cat ${compile_dir}/$2)"
}