Skip to content

Commit cff59dd

Browse files
committed
TESTS: Refactor assertion pattern in test helper functions
Move `run` command and `assert_success` assertions from test cases into helper functions for cleaner test code. Changes: - Wrapper functions (check_required_folders, check_required_files, check_required_symlinks, check_addons, check_services) now include `run` and `assert_success` internally - Leaf functions (check_assistant_is_installed, check_project_homepage_is_browsable, check_drupal_admin_access) refactored to use `run` for commands and `fail` for assertion failures - Test case now calls helper functions directly without `run` and `assert_success`, making the test more readable and concise
1 parent e1c70e3 commit cff59dd

File tree

1 file changed

+34
-45
lines changed

1 file changed

+34
-45
lines changed

tests/test.bats

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -137,61 +137,63 @@ check_required_items() {
137137
check_addons() {
138138
local INSTALLED_ADDONS
139139
INSTALLED_ADDONS=$(ddev add-on list --installed)
140-
check_items_from_list "add-on" "${DIR}/tests/required_addons.txt" check_addon_item "$INSTALLED_ADDONS"
140+
run check_items_from_list "add-on" "${DIR}/tests/required_addons.txt" check_addon_item "$INSTALLED_ADDONS"
141+
assert_success
141142
}
142143

143144
# Check a list of required folders.
144145
check_required_folders() {
145-
check_required_items "folder" "${DIR}/tests/required_folders.txt" "-d"
146+
run check_required_items "folder" "${DIR}/tests/required_folders.txt" "-d"
147+
assert_success
146148
}
147149

148150
# Check a list of required files.
149151
check_required_files() {
150-
check_required_items "file" "${DIR}/tests/required_files.txt" "-f"
152+
run check_required_items "file" "${DIR}/tests/required_files.txt" "-f"
153+
assert_success
151154
}
152155

153156
# Check a list of required symlinks.
154157
check_required_symlinks() {
155-
check_required_items "symlinks" "${DIR}/tests/required_symlinks.txt" "-L"
158+
run check_required_items "symlinks" "${DIR}/tests/required_symlinks.txt" "-L"
159+
assert_success
156160
}
157161

158162
# Check a list of services that should be running.
159163
check_services() {
160164
local RUNNING_SERVICES
161165
RUNNING_SERVICES=$(ddev describe)
162-
check_items_from_list "service" "${DIR}/tests/required_services.txt" check_service_item "$RUNNING_SERVICES"
166+
run check_items_from_list "service" "${DIR}/tests/required_services.txt" check_service_item "$RUNNING_SERVICES"
167+
assert_success
163168
}
164169

165170
# Check Aljibe Assistant is installed alongside Aljibe.
166171
check_assistant_is_installed() {
167-
local failed=0
168-
169172
echo -n "Checking if Aljibe Assistant is installed..."
170-
if ddev add-on list --installed | grep -q "aljibe-assistant"; then
173+
run ddev add-on list --installed
174+
assert_success
175+
176+
if echo "$output" | grep -q "aljibe-assistant"; then
171177
echo " Ok."
172178
else
173179
echo " Failed."
174-
failed=1
180+
fail "Aljibe Assistant is not installed"
175181
fi
176-
return "$failed"
177182
}
178183

179184
# Checks the homepage returns a 200 status code.
180185
check_project_homepage_is_browsable() {
181-
local failed=0
182-
183186
echo -n "Checking if the project is browsable..."
184-
local status_code
185-
status_code=$(curl -w "%{http_code}" -o NULL -s "https://${PROJNAME}.ddev.site")
187+
run curl -w "%{http_code}" -o /dev/null -s "https://${PROJNAME}.ddev.site"
188+
assert_success
186189

190+
local status_code="$output"
187191
if [ "$status_code" == "200" ]; then
188192
echo " Ok."
189193
else
190194
echo " Failed."
191-
failed=1
195+
fail "Project homepage returned status code $status_code instead of 200"
192196
fi
193-
194-
return "$failed"
195197
}
196198

197199
# Checks if the Drupal admin user is accessible via one-time login link.
@@ -200,29 +202,27 @@ check_project_homepage_is_browsable() {
200202
# the ULI URL is not valid the user is redirected to the login page with a 200
201203
# status code.
202204
check_drupal_admin_access() {
203-
local failed=0
204-
205205
echo -n "Checking if the Drupal admin is accessible..."
206206

207-
local login_url
208-
login_url=$(ddev drush uli 2>&1)
207+
run ddev drush uli
208+
assert_success
209+
local login_url="$output"
209210

210-
local response
211-
response=$(curl -sLb /tmp/cookies_$$ "$login_url" 2>&1)
211+
run curl -sLb /tmp/cookies_$$ "$login_url"
212+
assert_success
213+
local response="$output"
212214

213215
# For some reason, the message saying that you have used a one-time login link
214216
# is not always present, so we check for the password change prompt instead.
215217
if echo "$response" | grep -q "To change the current user password, enter the new password in both fields."; then
216218
echo " Ok."
217219
else
218220
echo " Failed."
219-
failed=1
221+
fail "Drupal admin login check failed - expected password change prompt not found"
220222
fi
221223

222224
# Cleanup
223225
rm -f /tmp/cookies_$$
224-
225-
return "$failed"
226226
}
227227

228228
@test "install from directory" {
@@ -241,31 +241,20 @@ check_drupal_admin_access() {
241241
assert_success
242242

243243
# Checks on files and folders required after installation.
244-
run check_required_folders
245-
assert_success
246-
247-
run check_required_files
248-
assert_success
249-
250-
run check_required_symlinks
251-
assert_success
244+
check_required_folders
245+
check_required_files
246+
check_required_symlinks
252247

253248
# Make sure Assistant is installed.
254-
run check_assistant_is_installed
255-
assert_success
249+
check_assistant_is_installed
256250

257251
# Check the required addons have installed correctly.
258-
run check_addons
259-
assert_success
252+
check_addons
260253

261254
# Check if the expected services are running.
262-
run check_services
263-
assert_success
255+
check_services
264256

265257
# Check the project's homepage is accessible.
266-
run check_project_homepage_is_browsable
267-
assert_success
268-
269-
run check_drupal_admin_access
270-
assert_success
258+
check_project_homepage_is_browsable
259+
check_drupal_admin_access
271260
}

0 commit comments

Comments
 (0)