Skip to content

Commit 0f3ae7b

Browse files
[#126] Added bats test for ahoy operations (#140)
Co-authored-by: Alex Skrypnyk <alex@drevops.com>
1 parent d0709ff commit 0f3ae7b

File tree

5 files changed

+153
-7
lines changed

5 files changed

+153
-7
lines changed

.devtools/provision.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ echo
4545
# Extension name, taken from .info file.
4646
extension="$(basename -s .info.yml -- ./*.info.yml)"
4747
[ "${extension}" == "*" ] && fail "ERROR: No .info.yml file found." && exit 1
48+
extension_type="module"
49+
if cat "${extension}.info.yml" | grep -Fq "type: theme"; then
50+
extension_type="theme"
51+
fi
4852

4953
# Database file path.
5054
db_file="/tmp/site_${extension}.sqlite"
@@ -56,7 +60,11 @@ pass "Drupal installed."
5660
drush status
5761

5862
info "Enabling extension ${extension}."
59-
drush pm:enable "${extension}" -y
63+
if [ "${extension_type}" = "theme" ]; then
64+
drush theme:enable "${extension}" -y
65+
else
66+
drush pm:enable "${extension}" -y
67+
fi
6068

6169
info "Clearing caches."
6270
drush cr

.github/workflows/scaffold-test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ jobs:
3434
- name: Setup kcov
3535
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
3636

37+
- name: Install Ahoy
38+
run: |
39+
os=$(uname -s | tr '[:upper:]' '[:lower:]') && architecture=$(case $(uname -m) in x86_64 | amd64) echo "amd64" ;; aarch64 | arm64 | armv8) echo "arm64" ;; *) echo "amd64" ;; esac)
40+
sudo wget -q https://github.com/ahoy-cli/ahoy/releases/download/v2.1.1/ahoy-bin-"${os}-${architecture}" -O /usr/local/bin/ahoy
41+
sudo chown "$USER" /usr/local/bin/ahoy && chmod +x /usr/local/bin/ahoy
42+
3743
- name: Install dependencies
3844
run: npm ci
3945
working-directory: .scaffold/tests

.scaffold/tests/bats/_assert_init.bash

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ assert_workflow() {
120120

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

123+
# Very basic workflow.
123124
./.devtools/assemble.sh
125+
./.devtools/start.sh
126+
./.devtools/provision.sh
124127

125-
# Lint.
126128
pushd "build" >/dev/null || exit 1
127-
129+
# Lint.
128130
vendor/bin/phpcs
129131
vendor/bin/phpstan
130132
vendor/bin/rector --clear-cache --dry-run
131133
vendor/bin/phpmd . text phpmd.xml
132134
vendor/bin/twig-cs-fixer
133-
135+
# Test.
136+
vendor/bin/phpunit
134137
popd >/dev/null || exit 1
135138

136-
# Change mode to make bats have enough permission to clean tmp test directory.
137-
chmod -R 777 "build/web/sites/default"
138-
139139
popd >/dev/null || exit 1
140140
}

.scaffold/tests/bats/_helper.bash

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ setup() {
5656
}
5757

5858
teardown() {
59+
# Some tests start php in background, we need kill it for bats able to finish the test.
60+
# This make break tests in parallel.
61+
killall -9 php >/dev/null 2>&1 || true
62+
sleep 1
63+
# Give bats enought permission to clean the build dir.
64+
if [ -d "build" ]; then
65+
chmod -Rf 777 build >/dev/null || true
66+
fi
67+
5968
# Move back to the original directory.
6069
popd >/dev/null || cd "${CUR_DIR}" || exit 1
6170
}

.scaffold/tests/bats/ahoy.bats

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bats
2+
3+
load _helper
4+
5+
export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1
6+
7+
@test "ahoy assemble" {
8+
run ahoy assemble
9+
assert_success
10+
11+
assert_output_contains "ASSEMBLE COMPLETE"
12+
assert_dir_exists "${BUILD_DIR}/build/vendor"
13+
assert_file_exists "${BUILD_DIR}/build/composer.json"
14+
assert_file_exists "${BUILD_DIR}/build/composer.lock"
15+
}
16+
17+
@test "ahoy start" {
18+
run ahoy start
19+
assert_failure
20+
21+
ahoy assemble
22+
run ahoy start
23+
assert_success
24+
25+
assert_output_contains "ENVIRONMENT READY"
26+
}
27+
28+
@test "ahoy stop" {
29+
run ahoy stop
30+
assert_success
31+
assert_output_contains "ENVIRONMENT STOPPED"
32+
33+
ahoy assemble
34+
ahoy start
35+
36+
run ahoy stop
37+
assert_success
38+
39+
assert_output_contains "ENVIRONMENT STOPPED"
40+
}
41+
42+
@test "ahoy lint, lint-fix" {
43+
ahoy assemble
44+
assert_success
45+
46+
ahoy lint
47+
assert_success
48+
49+
# shellcheck disable=SC2016
50+
echo '$a=123;echo $a;' >>your_extension.module
51+
run ahoy lint
52+
assert_failure
53+
54+
run ahoy lint-fix
55+
run ahoy lint
56+
assert_success
57+
}
58+
59+
@test "ahoy build - basic workflow" {
60+
run ahoy build
61+
assert_success
62+
assert_output_contains "PROVISION COMPLETE"
63+
64+
run ahoy drush status
65+
assert_success
66+
assert_output_contains "Database : Connected"
67+
assert_output_contains "Drupal bootstrap : Successful"
68+
69+
run ahoy login
70+
assert_success
71+
assert_output_contains "user/reset/1/"
72+
73+
ahoy lint
74+
assert_success
75+
76+
ahoy test
77+
assert_success
78+
79+
ahoy test-unit
80+
assert_success
81+
82+
ahoy test-kernel
83+
assert_success
84+
85+
ahoy test-functional
86+
assert_success
87+
}
88+
89+
@test "ahoy test unit failure" {
90+
run ahoy assemble
91+
assert_success
92+
93+
run ahoy test-unit
94+
assert_success
95+
96+
sed -i -e "s/assertEquals/assertNotEquals/g" "${BUILD_DIR}/tests/src/Unit/YourExtensionServiceUnitTest.php"
97+
run ahoy test-unit
98+
assert_failure
99+
}
100+
101+
@test "ahoy test functional failure" {
102+
run ahoy build
103+
assert_success
104+
105+
run ahoy test-functional
106+
assert_success
107+
108+
sed -i -e "s/responseContains/responseNotContains/g" "${BUILD_DIR}/tests/src/Functional/YourExtensionFunctionalTest.php"
109+
run ahoy test-functional
110+
assert_failure
111+
}
112+
113+
@test "ahoy test kernel failure" {
114+
run ahoy build
115+
assert_success
116+
117+
run ahoy test-kernel
118+
assert_success
119+
120+
sed -i -e "s/assertEquals/assertNotEquals/g" "${BUILD_DIR}/tests/src/Kernel/YourExtensionServiceKernelTest.php"
121+
run ahoy test-kernel
122+
assert_failure
123+
}

0 commit comments

Comments
 (0)