Skip to content

Commit 029b020

Browse files
committed
feat(tests): rewrite
Refactor code to make it easier to test a full deployment. - leverage shUnit2's `setUp` and `tearDown` functions to setup a few things - also introduce a `test::utils::setupFixture` function that copies a fixture into the container's HOME directory. This follows advice from shUnit2's developer: kward/shunit2#46 - add a more precise namespace to `test::` functions - introduce new tests (`test::legacy::php8x`)
1 parent 4f432a5 commit 029b020

File tree

4 files changed

+195
-124
lines changed

4 files changed

+195
-124
lines changed

test/helpers

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,82 @@
22

33
### Helpers functions
44

5-
test::helpers::get_php_version() {
6-
local old_home
5+
test::helpers::test_deploy() {
6+
# Deploy a PHP app in a Scalingo runtime and test that everything is as
7+
# expected. The different fixtures allow us to test different setup and
8+
# conditions.
79

8-
old_home="${HOME}"
10+
local fixture_name="${1}"
11+
local detect_expectation="${2}"
12+
local version="${3}"
913

10-
HOME="/app"
11-
export HOME
14+
# Setup the fixture:
15+
test::utils::setupFixture "${fixture_name}"
1216

13-
pushd "${HOME}" > /dev/null
17+
# Test that bin/detect works as expected:
18+
test::utils::detect
19+
test::utils::assertCapturedSuccess
20+
test::utils::assertCapturedEquals "${detect_expectation}"
1421

15-
cp --archive --recursive "${BUILD_DIR}"/* "${HOME}/"
22+
# Test that bin/compile works as expected:
23+
test::utils::compile
24+
# We can't use assertCapturedSuccess here:
25+
# With an empty composer.json file, composer will use stderr to warn us
26+
# that nothing had to be installed, causing the test to fail for no
27+
# reason -_-
28+
test::utils::assertSuccess
1629

17-
test::utils::capture ./vendor/php/bin/php --version
30+
# Switch environment:
31+
test::helpers::enter_prod
32+
33+
# Test that PHP has the awaited version:
34+
test::helpers::get_php_version
35+
test::utils::assertCapturedStartswith "PHP ${version}"
1836

19-
popd > /dev/null
37+
# Test that all default PHP modules are available:
38+
test::helpers::list_php_modules
39+
for module in "${default_modules[@]}"; do
40+
test::utils::assertFileContains "${module}" "${STD_OUT}"
41+
done
42+
}
2043

21-
rm -Rf "${HOME}"/*
44+
test::helpers::get_php_version() {
45+
# Captures the output of `php --version` so we can check that the version
46+
# installed is the expected one.
2247

23-
HOME="${old_home}"
24-
export HOME
48+
test::utils::capture ./vendor/php/bin/php --version
2549
}
2650

2751
test::helpers::list_php_modules() {
28-
local old_home
52+
# Captures the output of `php --modules` so we can check that all default
53+
# modules are indeed available.
54+
55+
test::utils::capture ./vendor/php/bin/php --modules
56+
}
2957

30-
old_home="${HOME}"
58+
test::helpers::enter_prod() {
59+
# Helper to switch to a production-like environment:
60+
# - $HOME is set to /app
61+
# - Working directory is set to $HOME
62+
# - The result of the `compile` script is copied to $HOME
63+
#
64+
# This environment is automatically reverted back, thanks to the `tearDown`
65+
# function override in `test/run`.
66+
# It would have been nice to put that in a `setUp` override. Unfortunately,
67+
# we can't because the switch of environment should only happen **during**
68+
# the test (after several steps have been reached), not before.
69+
#
70+
# /!\ This function is meant to be called after a successful call to
71+
# to `test::utils::compile` to further test the result of the
72+
# bin/compile call. It makes no sense to call it in another context.
73+
74+
PREV_HOME="${HOME}"
75+
export PREV_HOME
3176

3277
HOME="/app"
3378
export HOME
3479

3580
pushd "${HOME}" > /dev/null
3681

3782
cp --archive --recursive "${BUILD_DIR}"/* "${HOME}/"
38-
39-
test::utils::capture ./vendor/php/bin/php --modules
40-
41-
popd > /dev/null
42-
43-
rm -Rf "${HOME}"/*
44-
45-
HOME="${old_home}"
46-
export HOME
47-
}
48-
49-
50-
test::helpers::test_compile() {
51-
local fixture="${1}"
52-
local php_version="${2}"
53-
54-
# PHP 8.0 is only available on stack `scalingo-20`:
55-
[[ "${STACK}" != "scalingo-20" && "${php_version}" == "8.0." ]] \
56-
&& echo "[skipping] PHP 8.0 is not available on scalingo-22" \
57-
&& startSkipping
58-
59-
# Test that buildpack compiles:
60-
test::utils::compile "${fixture}"
61-
# We can't use assertCapturedSuccess here:
62-
# Even with an empty composer.json file, composer will use stderr to warn
63-
# us that nothing had to be installed, causing the test to fail for no
64-
# reason -_-
65-
test::utils::assertSuccess
66-
67-
# Test that we have the appropriate PHP version:
68-
test::helpers::get_php_version
69-
test::utils::assertCapturedStartswith "PHP ${php_version}"
70-
71-
# Test that all default PHP modules are present:
72-
test::helpers::list_php_modules
73-
74-
for module in "${default_modules[@]}"; do
75-
test::utils::assertFileContains "${module}" "${STD_OUT}"
76-
done
7783
}

test/run

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,35 @@ oneTimeTearDown() {
1616
}
1717

1818
setUp() {
19-
export OUTPUT_DIR="$( mktemp --directory "${SHUNIT_TMPDIR}/output.XXXX" )"
19+
OUTPUT_DIR="$( mktemp --directory "${SHUNIT_TMPDIR}/output.XXXX" )"
2020

2121
STD_OUT="${OUTPUT_DIR}/stdout"
2222
STD_ERR="${OUTPUT_DIR}/stderr"
2323

24-
export BUILD_DIR="${OUTPUT_DIR}/build"
25-
export CACHE_DIR="${OUTPUT_DIR}/cache"
26-
export HOME="${BUILD_DIR}"
24+
BUILD_DIR="${OUTPUT_DIR}/build"
25+
CACHE_DIR="${OUTPUT_DIR}/cache"
26+
HOME="${BUILD_DIR}"
27+
BUILDPACK_DIR="$( mktemp --directory "/tmp/test-XXXXXX" )"
2728

2829
mkdir -p "${OUTPUT_DIR}" "${BUILD_DIR}" "${CACHE_DIR}"
30+
export OUTPUT_DIR STD_OUT STD_ERR BUILD_DIR CACHE_DIR HOME BUILDPACK_DIR
31+
32+
# Copy the buildpack code into BUILDPACK_DIR:
33+
cp --archive "$( pwd )"/* "${BUILDPACK_DIR}/"
2934
}
3035

3136
tearDown() {
37+
# Empty OUTPUT_DIR and BUILDPACK_DIR
3238
rm -rf "${OUTPUT_DIR}"
39+
rm -rf "${BUILDPACK_DIR}"
40+
41+
unset BUILDPACK_DIR BUILD_DIR CACHE_DIR PHP_VERSION
42+
43+
# We may have changed working dir, let's switch back to the initial one:
44+
popd > /dev/null 2>&1
45+
46+
# Make sure /app is empty:
47+
rm -rf "/app"/*
3348
}
3449

3550
# Load shUnit2, which also run the tests:

test/tests

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
source "$( pwd )/test/utils"
44
source "$( pwd )/test/helpers"
55

6+
# List of modules that we want to ship with every PHP setup.
7+
# For now, users can't specify a version for these modules, we just ship
8+
# what we have. Hence no specific versions constraints here.
69
readonly default_modules=(
710
"apcu" "bcmath" "bz2" "Core" "ctype" "curl" "date" "dom" "exif" "fileinfo"
811
"filter" "gd" "hash" "iconv" "intl" "json" "libxml" "mbstring" "mongodb"
@@ -12,50 +15,112 @@ readonly default_modules=(
1215
"tokenizer" "xml" "xmlreader" "xmlwriter" "xsl" "Zend OPcache" "zip" "zlib"
1316
)
1417

15-
test::detect_legacy_default() {
16-
# Test the buildpack detect script with a single simple .php file
17-
test::utils::detect "legacy_default"
18-
test::utils::assertCapturedSuccess
19-
test::utils::assertCapturedEquals "PHP (classic)"
18+
19+
test::legacy::defaults() {
20+
# Test a deployment of a legacy app (not using Composer)
21+
# With default settings
22+
23+
test::helpers::test_deploy "legacy_default" "PHP (classic)" "8.1."
2024
}
2125

22-
test::detect_composer_default() {
23-
test::utils::detect "composer_default"
24-
test::utils::assertCapturedSuccess
25-
test::utils::assertCapturedEquals "PHP (composer.json)"
26+
test::legacy::php80() {
27+
# Test a deployment of a legacy app (not using Composer)
28+
# Specifying we want PHP 8.0.x via environment
29+
30+
PHP_VERSION="8.0"
31+
export PHP_VERSION
32+
33+
# PHP 8.0 is only available on stack `scalingo-20`:
34+
if [[ "${STACK}" != "scalingo-20" ]]; then
35+
echo "[skipping] PHP 8.0 is not available on scalingo-22"
36+
startSkipping
37+
fi
38+
39+
test::helpers::test_deploy "legacy_default" "PHP (classic)" "8.0."
2640
}
2741

28-
test::compile_legacy_default() {
29-
test::helpers::test_compile "legacy_default" "8.1."
42+
test::legacy::php81() {
43+
# Test a deployment of a legacy app (not using Composer)
44+
# Specifying we want PHP 8.1.x via environment
45+
46+
PHP_VERSION="8.1"
47+
export PHP_VERSION
48+
49+
test::helpers::test_deploy "legacy_default" "PHP (classic)" "8.1."
3050
}
3151

32-
test::compile_composer_default() {
33-
test::helpers::test_compile "composer_default" "8.1."
52+
test::legacy::php82() {
53+
# Test a deployment of a legacy app (not using Composer)
54+
# Specifying we want PHP 8.2.x via environment
55+
56+
PHP_VERSION="8.2"
57+
export PHP_VERSION
58+
59+
test::helpers::test_deploy "legacy_default" "PHP (classic)" "8.2."
3460
}
3561

36-
test::compile_composer_php80() {
37-
test::helpers::test_compile "composer_php80" "8.0."
62+
test::legacy::php83() {
63+
# Test a deployment of a legacy app (not using Composer)
64+
# Specifying we want PHP 8.3.x via environment
65+
66+
PHP_VERSION="8.3"
67+
export PHP_VERSION
68+
69+
test::helpers::test_deploy "legacy_default" "PHP (classic)" "8.3."
3870
}
3971

40-
test::compile_composer_php81() {
41-
test::helpers::test_compile "composer_php81" "8.1."
72+
test::composer::defaults() {
73+
# Test a deployment of a PHP app using Composer
74+
# With default settings
75+
76+
test::helpers::test_deploy "composer_default" "PHP (composer.json)" "8.1."
4277
}
4378

44-
test::compile_composer_php82() {
45-
test::helpers::test_compile "composer_php82" "8.2."
79+
test::composer::php80() {
80+
# Test a deployment of a PHP app using Composer
81+
# Specifying we want PHP 8.0.x in composer.json
82+
83+
# PHP 8.0 is only available on stack `scalingo-20`:
84+
if [[ "${STACK}" != "scalingo-20" ]]; then
85+
echo "[skipping] PHP 8.0 is not available on scalingo-22"
86+
startSkipping
87+
fi
88+
89+
test::helpers::test_deploy "composer_php80" "PHP (composer.json)" "8.0."
90+
}
91+
92+
test::composer::php81() {
93+
# Test a deployment of a PHP app using Composer
94+
# Specifying we want PHP 8.1.x in composer.json
95+
96+
test::helpers::test_deploy "composer_php81" "PHP (composer.json)" "8.1."
4697
}
4798

48-
test::compile_composer_php83() {
49-
test::helpers::test_compile "composer_php83" "8.3."
99+
test::composer::php82() {
100+
# Test a deployment of a PHP app using Composer
101+
# Specifying we want PHP 8.2.x in composer.json
102+
103+
test::helpers::test_deploy "composer_php82" "PHP (composer.json)" "8.2."
50104
}
51105

106+
test::composer::php83() {
107+
# Test a deployment of a PHP app using Composer
108+
# Specifying we want PHP 8.3.x in composer.json
109+
110+
test::helpers::test_deploy "composer_php83" "PHP (composer.json)" "8.3."
111+
}
112+
113+
52114
# Add these functions to the test suite:
53115

54-
suite_addTest test::detect_legacy_default
55-
suite_addTest test::compile_legacy_default
56-
suite_addTest test::detect_composer_default
57-
suite_addTest test::compile_composer_default
58-
suite_addTest test::compile_composer_php80
59-
suite_addTest test::compile_composer_php81
60-
suite_addTest test::compile_composer_php82
61-
suite_addTest test::compile_composer_php83
116+
suite_addTest test::legacy::defaults
117+
suite_addTest test::legacy::php80
118+
suite_addTest test::legacy::php81
119+
suite_addTest test::legacy::php82
120+
suite_addTest test::legacy::php83
121+
122+
suite_addTest test::composer::defaults
123+
suite_addTest test::composer::php80
124+
suite_addTest test::composer::php81
125+
suite_addTest test::composer::php82
126+
suite_addTest test::composer::php83

0 commit comments

Comments
 (0)