From 0f3ae7b023db6495997370e0f7efa091934ce188 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Wed, 8 May 2024 08:07:07 +0700 Subject: [PATCH] [#126] Added bats test for ahoy operations (#140) Co-authored-by: Alex Skrypnyk --- .devtools/provision.sh | 10 +- .github/workflows/scaffold-test.yml | 6 ++ .scaffold/tests/bats/_assert_init.bash | 12 +-- .scaffold/tests/bats/_helper.bash | 9 ++ .scaffold/tests/bats/ahoy.bats | 123 +++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 .scaffold/tests/bats/ahoy.bats diff --git a/.devtools/provision.sh b/.devtools/provision.sh index 1a97ef0..cef343c 100755 --- a/.devtools/provision.sh +++ b/.devtools/provision.sh @@ -45,6 +45,10 @@ echo # Extension name, taken from .info file. extension="$(basename -s .info.yml -- ./*.info.yml)" [ "${extension}" == "*" ] && fail "ERROR: No .info.yml file found." && exit 1 +extension_type="module" +if cat "${extension}.info.yml" | grep -Fq "type: theme"; then + extension_type="theme" +fi # Database file path. db_file="/tmp/site_${extension}.sqlite" @@ -56,7 +60,11 @@ pass "Drupal installed." drush status info "Enabling extension ${extension}." -drush pm:enable "${extension}" -y +if [ "${extension_type}" = "theme" ]; then + drush theme:enable "${extension}" -y +else + drush pm:enable "${extension}" -y +fi info "Clearing caches." drush cr diff --git a/.github/workflows/scaffold-test.yml b/.github/workflows/scaffold-test.yml index 3c32c85..032a20b 100644 --- a/.github/workflows/scaffold-test.yml +++ b/.github/workflows/scaffold-test.yml @@ -34,6 +34,12 @@ jobs: - name: Setup kcov run: wget https://github.com/SimonKagstrom/kcov/releases/download/v42/kcov-amd64.tar.gz && tar -xf kcov-amd64.tar.gz && sudo mv ./usr/local/bin/kcov /usr/local/bin/kcov && kcov --version + - name: Install Ahoy + run: | + os=$(uname -s | tr '[:upper:]' '[:lower:]') && architecture=$(case $(uname -m) in x86_64 | amd64) echo "amd64" ;; aarch64 | arm64 | armv8) echo "arm64" ;; *) echo "amd64" ;; esac) + sudo wget -q https://github.com/ahoy-cli/ahoy/releases/download/v2.1.1/ahoy-bin-"${os}-${architecture}" -O /usr/local/bin/ahoy + sudo chown "$USER" /usr/local/bin/ahoy && chmod +x /usr/local/bin/ahoy + - name: Install dependencies run: npm ci working-directory: .scaffold/tests diff --git a/.scaffold/tests/bats/_assert_init.bash b/.scaffold/tests/bats/_assert_init.bash index a752515..339d61e 100644 --- a/.scaffold/tests/bats/_assert_init.bash +++ b/.scaffold/tests/bats/_assert_init.bash @@ -120,21 +120,21 @@ assert_workflow() { pushd "${dir}" >/dev/null || exit 1 + # Very basic workflow. ./.devtools/assemble.sh + ./.devtools/start.sh + ./.devtools/provision.sh - # Lint. pushd "build" >/dev/null || exit 1 - + # Lint. vendor/bin/phpcs vendor/bin/phpstan vendor/bin/rector --clear-cache --dry-run vendor/bin/phpmd . text phpmd.xml vendor/bin/twig-cs-fixer - + # Test. + vendor/bin/phpunit popd >/dev/null || exit 1 - # Change mode to make bats have enough permission to clean tmp test directory. - chmod -R 777 "build/web/sites/default" - popd >/dev/null || exit 1 } diff --git a/.scaffold/tests/bats/_helper.bash b/.scaffold/tests/bats/_helper.bash index 38d42c0..9687b4a 100644 --- a/.scaffold/tests/bats/_helper.bash +++ b/.scaffold/tests/bats/_helper.bash @@ -56,6 +56,15 @@ setup() { } teardown() { + # Some tests start php in background, we need kill it for bats able to finish the test. + # This make break tests in parallel. + killall -9 php >/dev/null 2>&1 || true + sleep 1 + # Give bats enought permission to clean the build dir. + if [ -d "build" ]; then + chmod -Rf 777 build >/dev/null || true + fi + # Move back to the original directory. popd >/dev/null || cd "${CUR_DIR}" || exit 1 } diff --git a/.scaffold/tests/bats/ahoy.bats b/.scaffold/tests/bats/ahoy.bats new file mode 100644 index 0000000..c38dfbe --- /dev/null +++ b/.scaffold/tests/bats/ahoy.bats @@ -0,0 +1,123 @@ +#!/usr/bin/env bats + +load _helper + +export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1 + +@test "ahoy assemble" { + run ahoy assemble + assert_success + + assert_output_contains "ASSEMBLE COMPLETE" + assert_dir_exists "${BUILD_DIR}/build/vendor" + assert_file_exists "${BUILD_DIR}/build/composer.json" + assert_file_exists "${BUILD_DIR}/build/composer.lock" +} + +@test "ahoy start" { + run ahoy start + assert_failure + + ahoy assemble + run ahoy start + assert_success + + assert_output_contains "ENVIRONMENT READY" +} + +@test "ahoy stop" { + run ahoy stop + assert_success + assert_output_contains "ENVIRONMENT STOPPED" + + ahoy assemble + ahoy start + + run ahoy stop + assert_success + + assert_output_contains "ENVIRONMENT STOPPED" +} + +@test "ahoy lint, lint-fix" { + ahoy assemble + assert_success + + ahoy lint + assert_success + + # shellcheck disable=SC2016 + echo '$a=123;echo $a;' >>your_extension.module + run ahoy lint + assert_failure + + run ahoy lint-fix + run ahoy lint + assert_success +} + +@test "ahoy build - basic workflow" { + run ahoy build + assert_success + assert_output_contains "PROVISION COMPLETE" + + run ahoy drush status + assert_success + assert_output_contains "Database : Connected" + assert_output_contains "Drupal bootstrap : Successful" + + run ahoy login + assert_success + assert_output_contains "user/reset/1/" + + ahoy lint + assert_success + + ahoy test + assert_success + + ahoy test-unit + assert_success + + ahoy test-kernel + assert_success + + ahoy test-functional + assert_success +} + +@test "ahoy test unit failure" { + run ahoy assemble + assert_success + + run ahoy test-unit + assert_success + + sed -i -e "s/assertEquals/assertNotEquals/g" "${BUILD_DIR}/tests/src/Unit/YourExtensionServiceUnitTest.php" + run ahoy test-unit + assert_failure +} + +@test "ahoy test functional failure" { + run ahoy build + assert_success + + run ahoy test-functional + assert_success + + sed -i -e "s/responseContains/responseNotContains/g" "${BUILD_DIR}/tests/src/Functional/YourExtensionFunctionalTest.php" + run ahoy test-functional + assert_failure +} + +@test "ahoy test kernel failure" { + run ahoy build + assert_success + + run ahoy test-kernel + assert_success + + sed -i -e "s/assertEquals/assertNotEquals/g" "${BUILD_DIR}/tests/src/Kernel/YourExtensionServiceKernelTest.php" + run ahoy test-kernel + assert_failure +}