Skip to content

Commit 78380b6

Browse files
hanoiityler36weitzman
authored
feat: add a more robust set of tests (#85)
* wip: initial tests refactor * feat: d10 and d11 tests * tests: d10 remove corepack and use d11 yarn install approach * refactor: tests rename * refactor: moves common code into _common.bash * feat: require-dev on d11 test as well * feat: test drupal version with drush * feat: add bats_require_minimum_version 1.5.0 * feat: single full.bats test and GHA matrix strategy * fix: using eval for running bats tests with env vars * refactor: move common code back to its own file * fix: avoid setting DRUPAL_CORE on default tests * fix: allow to run tests without TEST_DRUPAL_CORE It will run the default core * feat: test for presence of web/core and vendor * fix: allow drupal core version to work without a specified test core * refactor: fix typos and composer.json cleanup * docs: README tweaks about testing * feat: using bats-assert * fix: use `brew --prefix` for BATS_LIB_PATH * feat: using submodules for bats * docs: small tweak * docs: proper submodule git commands * docs: simplified tests commands * docs: apply suggestions from code review Co-authored-by: tyler36 <tyler36@users.noreply.github.com> --------- Co-authored-by: tyler36 <tyler36@users.noreply.github.com> Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>
1 parent 4e0a730 commit 78380b6

File tree

12 files changed

+207
-40
lines changed

12 files changed

+207
-40
lines changed

.github/workflows/tests.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ jobs:
2828
strategy:
2929
matrix:
3030
ddev_version: [stable, HEAD]
31+
drupal_version: ["default", "10", "11"]
3132
fail-fast: false
3233

3334
runs-on: ubuntu-latest
3435

3536
steps:
37+
- uses: actions/checkout@v4
38+
with:
39+
submodules: true
40+
3641
- uses: ddev/github-action-add-on-test@v2
3742
with:
43+
disable_checkout_action: true
3844
ddev_version: ${{ matrix.ddev_version }}
3945
token: ${{ secrets.GITHUB_TOKEN }}
4046
debug_enabled: ${{ github.event.inputs.debug_enabled }}
4147
addon_repository: ${{ env.GITHUB_REPOSITORY }}
4248
addon_ref: ${{ env.GITHUB_REF }}
49+
test_command: eval TEST_DRUPAL_CORE=${{ matrix.drupal_version }} ./tests/bats/bin/bats tests

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[submodule "tests/bats"]
2+
path = tests/bats
3+
url = https://github.com/bats-core/bats-core.git
4+
[submodule "tests/helpers/bats-assert"]
5+
path = tests/helpers/bats-assert
6+
url = https://github.com/bats-core/bats-assert.git
7+
[submodule "tests/helpers/bats-support"]
8+
path = tests/helpers/bats-support
9+
url = https://github.com/bats-core/bats-support.git

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,39 @@ ddev phpcbf -q
109109

110110
3. Mark the file as executable: `chmod +x pre-commit`.
111111

112+
## Add-on tests
113+
114+
Tests are done with Bats. It is a testing framework that uses Bash.
115+
116+
To run tests locally you need to first install bats' git submodules with:
117+
118+
```sh
119+
git submodule update --init
120+
```
121+
122+
Then you can run within the root of this project:
123+
124+
```sh
125+
./tests/bats/bin/bats ./tests
126+
```
127+
128+
Tests will be run using the default drupal core of the contrib. To test against a different Drupal core version, update the `TEST_DRUPAL_CORE` environment
129+
variable.
130+
131+
i.e. `TEST_DRUPAL_CORE=11 ./tests/bats/bin/bats ./tests`.
132+
133+
Tests are triggered automatically on every push to the
134+
repository, and periodically each night. The automated tests are agains all of
135+
the supported Drupal core versions.
136+
137+
Please make sure to attend to test failures when they happen. Others will be
138+
depending on you.
139+
140+
Also, consider adding tests to test for bugs or new features on your PR.
141+
142+
To learn more about Bats see the [documentation][bats-docs].
143+
144+
[bats-docs]: https://bats-core.readthedocs.io/en/stable/
112145

113146
## Troubleshooting
114147

tests/_common.bash

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
_common_setup() {
4+
bats_require_minimum_version 1.5.0
5+
export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
6+
export PROJNAME=test-drupal-contrib
7+
export TESTDIR=~/tmp/${PROJNAME}
8+
mkdir -p $TESTDIR
9+
export DDEV_NON_INTERACTIVE=true
10+
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1 || true
11+
cp -R ${DIR}/tests/testdata/test_drupal_contrib/* ${TESTDIR}
12+
cd ${TESTDIR}
13+
ddev config --project-name=${PROJNAME} --project-type=drupal --docroot=web
14+
if [ -n "$TEST_DRUPAL_CORE" ] && [ "$TEST_DRUPAL_CORE" != "default" ]; then
15+
echo -e "web_environment:\n - DRUPAL_CORE=^${TEST_DRUPAL_CORE}" > .ddev/config.~overrides.yaml
16+
fi
17+
ddev get ${DIR}
18+
}
19+
20+
_common_teardown() {
21+
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1
22+
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
23+
}
24+
25+
_common_test_poser() {
26+
ddev poser
27+
ddev mutagen sync
28+
ls -la web/core
29+
ls -la vendor/
30+
ls -la
31+
}

tests/bats

Submodule bats added at 89a7fae
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
setup_file() {
2+
if [ -n "$TEST_DRUPAL_CORE" ] && [ "$TEST_DRUPAL_CORE" != "default" ]; then
3+
skip "TEST_DRUPAL_CORE=$TEST_DRUPAL_CORE not handled by this test suite" >&2
4+
fi
5+
load '_common.bash'
6+
_common_setup
7+
ddev start
8+
}
9+
10+
teardown_file() {
11+
load '_common.bash'
12+
_common_teardown
13+
}
14+
15+
@test "ddev poser without composer.json" {
16+
load '_common.bash'
17+
rm -f composer.json
18+
_common_test_poser
19+
}

tests/full.bats

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
load helpers/bats-support/load.bash
2+
load helpers/bats-assert/load.bash
3+
4+
setup_file() {
5+
if [ -n "$TEST_DRUPAL_CORE" ] && [ "$TEST_DRUPAL_CORE" != "10" ] && [ "$TEST_DRUPAL_CORE" != "11" ]; then
6+
skip "TEST_DRUPAL_CORE=$TEST_DRUPAL_CORE not handled by this test suite" >&2
7+
fi
8+
load '_common.bash'
9+
_common_setup
10+
ddev config --php-version=8.2
11+
if [ "$TEST_DRUPAL_CORE" = "11" ]; then
12+
ddev config --php-version=8.3 --corepack-enable
13+
fi
14+
ddev start
15+
}
16+
17+
teardown_file() {
18+
load '_common.bash'
19+
_common_teardown
20+
}
21+
22+
@test "ddev poser with composer.json" {
23+
load '_common.bash'
24+
_common_test_poser
25+
}
26+
27+
@test "ddev symlink-project" {
28+
ddev symlink-project
29+
ddev mutagen sync
30+
ls -la web/modules/custom/test_drupal_contrib/test_drupal_contrib.info.yml
31+
}
32+
33+
@test "php tools availability" {
34+
ddev phpcs --version
35+
ddev phpstan --version
36+
ddev phpunit --version
37+
}
38+
39+
@test "drupal core version" {
40+
run -0 ddev exec 'drush st --fields=drupal-version --format=string | cut -d. -f1'
41+
if [ -n "${TEST_DRUPAL_CORE}" ]; then
42+
assert_output "${TEST_DRUPAL_CORE}"
43+
else
44+
DDEV_DRUPAL_CORE=$(ddev exec 'echo "${DRUPAL_CORE/^/}"')
45+
assert_output "$DDEV_DRUPAL_CORE"
46+
fi
47+
}
48+
49+
@test "node tools availability" {
50+
ddev exec "cd web/core && yarn install"
51+
ddev exec touch web/core/.env
52+
ddev mutagen sync
53+
ddev stylelint --version
54+
ddev eslint --version
55+
}

tests/helpers/bats-assert

Submodule bats-assert added at e2d855b

tests/helpers/bats-support

Submodule bats-support added at 9bf10e8

tests/test.bats

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)