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

[#126] Added bats test for ahoy operations #140

Merged
merged 26 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
10 changes: 9 additions & 1 deletion .devtools/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/scaffold-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions .scaffold/tests/bats/_assert_init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ assert_workflow() {

pushd "${dir}" >/dev/null || exit 1

# Very basic workflow.
tannguyen04 marked this conversation as resolved.
Show resolved Hide resolved
./.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
}
9 changes: 9 additions & 0 deletions .scaffold/tests/bats/_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
123 changes: 123 additions & 0 deletions .scaffold/tests/bats/ahoy.bats
Original file line number Diff line number Diff line change
@@ -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
}
Loading