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

feat: add a more robust set of tests #85

Merged
merged 25 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
21a260f
wip: initial tests refactor
hanoii Oct 7, 2024
bb6fa5d
feat: d10 and d11 tests
hanoii Oct 7, 2024
607740e
tests: d10 remove corepack and use d11 yarn install approach
hanoii Oct 7, 2024
257965a
refactor: tests rename
hanoii Oct 7, 2024
ebe0621
refactor: moves common code into _common.bash
hanoii Oct 8, 2024
215eade
feat: require-dev on d11 test as well
hanoii Oct 8, 2024
8c03c6e
feat: test drupal version with drush
hanoii Oct 8, 2024
1a643fa
feat: add bats_require_minimum_version 1.5.0
hanoii Oct 8, 2024
10dfe96
feat: single full.bats test and GHA matrix strategy
hanoii Oct 8, 2024
0f7246d
fix: using eval for running bats tests with env vars
hanoii Oct 8, 2024
661a83c
refactor: move common code back to its own file
hanoii Oct 8, 2024
be1e001
fix: avoid setting DRUPAL_CORE on default tests
hanoii Oct 10, 2024
9db9b87
fix: allow to run tests without TEST_DRUPAL_CORE
hanoii Oct 11, 2024
0badf96
feat: test for presence of web/core and vendor
hanoii Oct 11, 2024
e6bba3a
fix: allow drupal core version to work without a specified test core
hanoii Oct 11, 2024
ded820f
refactor: fix typos and composer.json cleanup
hanoii Oct 11, 2024
f1e92c3
docs: README tweaks about testing
hanoii Oct 11, 2024
fb29154
feat: using bats-assert
hanoii Oct 11, 2024
b2ba34c
fix: use `brew --prefix` for BATS_LIB_PATH
hanoii Oct 11, 2024
ee13990
feat: using submodules for bats
hanoii Oct 11, 2024
b66a920
docs: small tweak
hanoii Oct 11, 2024
1e40a4f
docs: proper submodule git commands
hanoii Oct 11, 2024
9cb313b
docs: simplified tests commands
hanoii Oct 11, 2024
ae2ffbe
docs: apply suggestions from code review
hanoii Oct 16, 2024
2a0927f
Merge branch 'main' into tests-rehaul
weitzman Oct 21, 2024
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
7 changes: 7 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ jobs:
strategy:
matrix:
ddev_version: [stable, HEAD]
drupal_version: ["default", "10", "11"]
fail-fast: false

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
submodules: true

- uses: ddev/github-action-add-on-test@v2
with:
disable_checkout_action: true
ddev_version: ${{ matrix.ddev_version }}
token: ${{ secrets.GITHUB_TOKEN }}
debug_enabled: ${{ github.event.inputs.debug_enabled }}
addon_repository: ${{ env.GITHUB_REPOSITORY }}
addon_ref: ${{ env.GITHUB_REF }}
test_command: eval TEST_DRUPAL_CORE=${{ matrix.drupal_version }} ./tests/bats/bin/bats tests
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "tests/bats"]
path = tests/bats
url = https://github.com/bats-core/bats-core.git
[submodule "tests/helpers/bats-assert"]
path = tests/helpers/bats-assert
url = https://github.com/bats-core/bats-assert.git
[submodule "tests/helpers/bats-support"]
path = tests/helpers/bats-support
url = https://github.com/bats-core/bats-support.git
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,39 @@ ddev phpcbf -q

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

## Add-on tests

Tests are done with Bats. It is a testing framework that uses Bash.

To run tests locally you need to first install bats' git submodules with:

```sh
git submodule update --init
```

Then you can run within the root of this project:

```sh
./tests/bats/bin/bats ./tests
```

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
variable.

i.e. `TEST_DRUPAL_CORE=11 ./tests/bats/bin/bats ./tests`.

Tests are triggered automatically on every push to the
repository, and periodically each night. The automated tests are agains all of
the supported Drupal core versions.

Please make sure to attend to test failures when they happen. Others will be
depending on you.

Also, consider adding tests to test for bugs or new features on your PR.

To learn more about Bats see the [documentation][bats-docs].

[bats-docs]: https://bats-core.readthedocs.io/en/stable/

## Troubleshooting

Expand Down
31 changes: 31 additions & 0 deletions tests/_common.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

_common_setup() {
bats_require_minimum_version 1.5.0
export DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )/.."
export PROJNAME=test-drupal-contrib
export TESTDIR=~/tmp/${PROJNAME}
mkdir -p $TESTDIR
export DDEV_NON_INTERACTIVE=true
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1 || true
cp -R ${DIR}/tests/testdata/test_drupal_contrib/* ${TESTDIR}
cd ${TESTDIR}
ddev config --project-name=${PROJNAME} --project-type=drupal --docroot=web
if [ -n "$TEST_DRUPAL_CORE" ] && [ "$TEST_DRUPAL_CORE" != "default" ]; then
echo -e "web_environment:\n - DRUPAL_CORE=^${TEST_DRUPAL_CORE}" > .ddev/config.~overrides.yaml
fi
ddev get ${DIR}
}

_common_teardown() {
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1
[ "${TESTDIR}" != "" ] && rm -rf ${TESTDIR}
}

_common_test_poser() {
ddev poser
ddev mutagen sync
ls -la web/core
ls -la vendor/
ls -la
}
1 change: 1 addition & 0 deletions tests/bats
Submodule bats added at 89a7fa
19 changes: 19 additions & 0 deletions tests/default-no-composer-json.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
setup_file() {
if [ -n "$TEST_DRUPAL_CORE" ] && [ "$TEST_DRUPAL_CORE" != "default" ]; then
skip "TEST_DRUPAL_CORE=$TEST_DRUPAL_CORE not handled by this test suite" >&2
fi
load '_common.bash'
_common_setup
ddev start
}

teardown_file() {
load '_common.bash'
_common_teardown
}

@test "ddev poser without composer.json" {
load '_common.bash'
rm -f composer.json
_common_test_poser
}
55 changes: 55 additions & 0 deletions tests/full.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
load helpers/bats-support/load.bash
load helpers/bats-assert/load.bash

setup_file() {
if [ -n "$TEST_DRUPAL_CORE" ] && [ "$TEST_DRUPAL_CORE" != "10" ] && [ "$TEST_DRUPAL_CORE" != "11" ]; then
skip "TEST_DRUPAL_CORE=$TEST_DRUPAL_CORE not handled by this test suite" >&2
fi
load '_common.bash'
_common_setup
ddev config --php-version=8.2
if [ "$TEST_DRUPAL_CORE" = "11" ]; then
ddev config --php-version=8.3 --corepack-enable
fi
hanoii marked this conversation as resolved.
Show resolved Hide resolved
ddev start
}

teardown_file() {
load '_common.bash'
_common_teardown
}

@test "ddev poser with composer.json" {
load '_common.bash'
_common_test_poser
}

@test "ddev symlink-project" {
ddev symlink-project
ddev mutagen sync
ls -la web/modules/custom/test_drupal_contrib/test_drupal_contrib.info.yml
}

@test "php tools availability" {
ddev phpcs --version
ddev phpstan --version
ddev phpunit --version
}

@test "drupal core version" {
run -0 ddev exec 'drush st --fields=drupal-version --format=string | cut -d. -f1'
if [ -n "${TEST_DRUPAL_CORE}" ]; then
assert_output "${TEST_DRUPAL_CORE}"
else
DDEV_DRUPAL_CORE=$(ddev exec 'echo "${DRUPAL_CORE/^/}"')
assert_output "$DDEV_DRUPAL_CORE"
fi
}

@test "node tools availability" {
ddev exec "cd web/core && yarn install"
ddev exec touch web/core/.env
ddev mutagen sync
ddev stylelint --version
ddev eslint --version
}
1 change: 1 addition & 0 deletions tests/helpers/bats-assert
Submodule bats-assert added at e2d855
1 change: 1 addition & 0 deletions tests/helpers/bats-support
Submodule bats-support added at 9bf10e
40 changes: 0 additions & 40 deletions tests/test.bats

This file was deleted.

47 changes: 47 additions & 0 deletions tests/testdata/test_drupal_contrib/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "drupal/ddev_drupal_contrib",
"type": "drupal-module",
"license": "GPL-2.0+",
"homepage": "https://github.com/ddev/ddev-drupal-contrib",
"minimum-stability": "dev",
"repositories": [
{
"type": "composer",
"url": "https://packages.drupal.org/8"
}
],
"require-dev": {
"drupal/devel": "^5.0",
"drush/drush": "^11 || ^12 || ^13"
},
"config": {
"allow-plugins": true
},
"extra": {
"drupal-scaffold": {
"locations": {
"web-root": "web"
}
},
"installer-paths": {
"web/core": [
"type:drupal-core"
],
"web/libraries/{$name}": [
"type:drupal-library"
],
"web/modules/contrib/{$name}": [
"type:drupal-module"
],
"web/profiles/contrib/{$name}": [
"type:drupal-profile"
],
"web/themes/contrib/{$name}": [
"type:drupal-theme"
],
"drush/contrib/{$name}": [
"type:drupal-drush"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: 'ddev-drupal-contrib dummy module for tests'
type: module
core_version_requirement: ^8.8 || ^9 || ^10 || ^11