@webship.co/workspace-express 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +190 -2
- package/ai/agents/express-builder.md +60 -0
- package/ai/agents/express-tester.md +55 -0
- package/ai/commands/express-test.md +29 -0
- package/ai/prompts/express-build-and-test.md +20 -0
- package/ai/prompts/express-scenarios-for-a-project.md +18 -0
- package/ai/skills/express-scenarios/SKILL.md +75 -0
- package/ai/skills/express-scenarios/examples/accounts.feature +24 -0
- package/ai/skills/express-scenarios/examples/answers.feature +26 -0
- package/ai/skills/express-scenarios/examples/session.feature +10 -0
- package/ai/skills/express-scenarios/examples/static.feature +11 -0
- package/commands/cmd-express-project.sh +34 -0
- package/demo/app.js +143 -0
- package/demo/public/app.css +6 -0
- package/dist/src/index.d.ts +29 -0
- package/dist/src/index.js +53 -0
- package/dist/src/index.js.map +1 -0
- package/features/express-runtimes.feature +51 -0
- package/features/express.feature +35 -0
- package/package.json +103 -4
- package/scripts/args/arg-express.sh +42 -0
- package/scripts/docker-compose.express.yaml +33 -0
- package/scripts/functions/fun-express.sh +371 -0
- package/testing/site.feature +22 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+
# Express for the Webship Workspace: make an Express app (the step pack's own demo app at a line of Express, or an app
|
|
4
|
+
# from its repository), serve it with Node.js in a container of its own, and wire the Express step pack into the
|
|
5
|
+
# testing setup of that app.
|
|
6
|
+
#
|
|
7
|
+
# Placed by `plugins.sh --install express` in core/scripts/plugins/express/functions/, where bootstrap sources it after
|
|
8
|
+
# the core's own functions. Everything runs in containers, never on the host: Node.js is the app's language, and the
|
|
9
|
+
# app runs in Node 24's official image (by its digest), its packages installed in the app's folder (app/node_modules)
|
|
10
|
+
# at the first start and kept after, in production mode, node as the container's main process under an init, so a stop
|
|
11
|
+
# reaches it (the demo closes its server on SIGTERM).
|
|
12
|
+
#
|
|
13
|
+
# The runtimes:
|
|
14
|
+
# - DDEV (the default): a generic project; the app is a service of the project (.ddev/docker-compose.express.yaml),
|
|
15
|
+
# and the router sends the project's address to it.
|
|
16
|
+
# - Docker Compose, Podman and Kubernetes (--runtime docker, podman, kubernetes): the core's runtime layer
|
|
17
|
+
# (webship/workspace#841): Node's image as the stack (rt_stack image), the app's server as the web container's own
|
|
18
|
+
# (rt_serve).
|
|
19
|
+
# Lando and Docksal are not proven for it yet: asked for one, the builder says so and changes nothing.
|
|
20
|
+
#
|
|
21
|
+
# The project's folder: app/ (the app) and .webship-express/ (0700): the app's start command, the line, and for the
|
|
22
|
+
# demo its session secret (0600, made here, never in the code).
|
|
23
|
+
#
|
|
24
|
+
# Express has no database of its own: the plugin declares no database hook, and the row's Backup keeps the files. The
|
|
25
|
+
# demo keeps its accounts in memory while it runs: a restart forgets them, and the step pack registers them again.
|
|
26
|
+
#
|
|
27
|
+
# A project of this builder is recognised by its mark (.ddev/config.express.yaml on DDEV, `platform: express` in
|
|
28
|
+
# compose.yaml elsewhere), and these names are the ones the core's tools look for:
|
|
29
|
+
# set_express_users the default accounts (cmd-tools-add-users.sh <project> express)
|
|
30
|
+
# add_express_users register them through the demo's own form (the same tool, and --add-users)
|
|
31
|
+
# cancel_express_users the demo has no way to stop an account: says so
|
|
32
|
+
# testing_configure_express the step pack in the testing stack (cmd-tools-testing.sh <project>)
|
|
33
|
+
|
|
34
|
+
readonly EXPRESS_STEPS_PACKAGE='@webship-js/express-steps';
|
|
35
|
+
readonly EXPRESS_STEPS_VERSION='^2.0.0';
|
|
36
|
+
|
|
37
|
+
# The lines of Express the builder is proven with (the demo's exact packages, as the step pack pins them).
|
|
38
|
+
readonly EXPRESS_LINES='5.3.0 4.22.3';
|
|
39
|
+
readonly EXPRESS_DEFAULT_LINE='5.3.0';
|
|
40
|
+
readonly EXPRESS_SESSION_VERSION='1.19.0';
|
|
41
|
+
|
|
42
|
+
# Node 24's official image, pinned by digest (docker.io/library/node:24-bookworm-slim, Node 24.23.0).
|
|
43
|
+
readonly EXPRESS_NODE_IMAGE='docker.io/library/node@sha256:f3690d9da7fcdea20facb2f14f08cbd0f6e442164d8b4bae44f11e850597b48f';
|
|
44
|
+
|
|
45
|
+
# The port the app listens on, the app's folder, the builder's own folder.
|
|
46
|
+
readonly EXPRESS_PORT='3000';
|
|
47
|
+
readonly EXPRESS_APP='app';
|
|
48
|
+
readonly EXPRESS_OWN='.webship-express';
|
|
49
|
+
readonly EXPRESS_DEMO_START='node app.js';
|
|
50
|
+
readonly EXPRESS_RUNTIMES='ddev docker podman kubernetes';
|
|
51
|
+
|
|
52
|
+
# The default accounts, the ones a scenario signs in as: registered through the demo's own form.
|
|
53
|
+
function set_express_users() {
|
|
54
|
+
users=(tester other);
|
|
55
|
+
user_tester_name='webship-tester' ; user_tester_mail='tester@example.com' ; user_tester_password='dD.123123ddd' ;
|
|
56
|
+
user_tester_fullname='' ; user_tester_role='user' ;
|
|
57
|
+
user_other_name='webship-other' ; user_other_mail='other@example.com' ; user_other_password='dD.123123ddd' ;
|
|
58
|
+
user_other_fullname='' ; user_other_role='user' ;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function express_runtime() {
|
|
62
|
+
if declare -f runtime_of > /dev/null ; then runtime_of ; else echo 'ddev' ; fi
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function express_url() {
|
|
66
|
+
if declare -f rt_url > /dev/null ; then rt_url ; else echo "https://${PROJECT_NAME}.ddev.site" ; fi
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function express_is_project() {
|
|
70
|
+
local project_path="$1";
|
|
71
|
+
[ -f "${project_path}/.ddev/config.express.yaml" ] && return 0 ;
|
|
72
|
+
declare -f rt_is_platform > /dev/null && rt_is_platform "${project_path}" express ;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function express_app_service() {
|
|
76
|
+
if [ "$(express_runtime)" = 'ddev' ] ; then echo 'express' ; else echo 'web' ; fi
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
# The command the app's container runs, from the project's root in it (/var/www/html): into app/, its packages when it
|
|
80
|
+
# has none (npm ci with a lock file, npm install without), then the app's start command, as the main process.
|
|
81
|
+
function express_serve_line() {
|
|
82
|
+
echo "cd /var/www/html/${EXPRESS_APP} && export HOME=/tmp npm_config_cache=/tmp/.npm npm_config_update_notifier=false NODE_ENV=production PORT=${EXPRESS_PORT} SESSION_SECRET_FILE=/var/www/html/${EXPRESS_OWN}/session-secret && { [ -d node_modules ] || { if [ -f package-lock.json ] ; then npm ci --omit=dev --no-audit --no-fund ; else npm install --omit=dev --no-audit --no-fund ; fi ; } ; } && exec \$(cat ../${EXPRESS_OWN}/start)" ;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
# The demo app in app/: the step pack's own (placed with the plugin), its package.json pinned to the line.
|
|
86
|
+
function express_get_demo() {
|
|
87
|
+
local project_path="$1" line="$2" from="${WORKSPACE_SCRIPTS}/plugins/express" app ;
|
|
88
|
+
app="${project_path}/${EXPRESS_APP}" ;
|
|
89
|
+
for f in app.js app.css ; do
|
|
90
|
+
[ -f "${from}/${f}" ] || { echo "${f} is not in ${from}: install the plugin (plugins.sh --install express)." ; return 1 ; } ;
|
|
91
|
+
done
|
|
92
|
+
mkdir -p "${app}/public" && cp "${from}/app.js" "${app}/app.js" && cp "${from}/app.css" "${app}/public/app.css" || return 1 ;
|
|
93
|
+
printf '{\n "name": "webship-express-demo",\n "private": true,\n "license": "MIT",\n "dependencies": {\n "express": "%s",\n "express-session": "%s"\n }\n}\n' \
|
|
94
|
+
"${line}" "${EXPRESS_SESSION_VERSION}" > "${app}/package.json" ;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
# The app from its repository, its folder in app/.
|
|
98
|
+
function express_get_source() {
|
|
99
|
+
local project_path="$1" source="$2" ref="$3" app_path="$4" tmp app="${project_path}/${EXPRESS_APP}" ;
|
|
100
|
+
echo "Getting the app: ${source}${ref:+ at ${ref}}${app_path:+, its folder ${app_path}/}" ;
|
|
101
|
+
tmp="$(mktemp -d)" ;
|
|
102
|
+
git clone --quiet --depth 1 ${ref:+--branch "${ref}"} "${source}" "${tmp}/source" < /dev/null \
|
|
103
|
+
|| { rm -rf "${tmp}" ; echo "The app could not be cloned from ${source}${ref:+ at ${ref}}." ; return 1 ; } ;
|
|
104
|
+
mkdir -p "${app}" && cp -a "${tmp}/source/${app_path:+${app_path}/}." "${app}/" ;
|
|
105
|
+
rm -rf "${tmp}" "${app:?}/.git" ;
|
|
106
|
+
[ -f "${app}/package.json" ] || { echo "The app has no package.json in ${app_path:-its root}: name its folder with --path." ; return 1 ; } ;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
# The builder's own files: the start command, the line, and the demo's session secret (made once, kept on later builds).
|
|
110
|
+
function express_write_own() {
|
|
111
|
+
local project_path="$1" line="$2" start="$3" own ;
|
|
112
|
+
own="${project_path}/${EXPRESS_OWN}" ;
|
|
113
|
+
mkdir -p -m 700 "${own}" || return 1 ;
|
|
114
|
+
( umask 077
|
|
115
|
+
[ -s "${own}/session-secret" ] || python3 -c 'import secrets; print(secrets.token_hex(32))' > "${own}/session-secret" ;
|
|
116
|
+
printf '%s\n' "${start}" > "${own}/start" ;
|
|
117
|
+
printf '%s\n' "${line}" > "${own}/line" ;
|
|
118
|
+
) ;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function express_write_ddev_config() {
|
|
122
|
+
local project_path="$1" made="$2" demo="${3:-no}" from="${WORKSPACE_SCRIPTS}/plugins/express" ;
|
|
123
|
+
if [ ! -f "${from}/docker-compose.express.yaml" ] ; then
|
|
124
|
+
echo "docker-compose.express.yaml is not in ${from}: install the plugin (plugins.sh --install express)." ;
|
|
125
|
+
return 1 ;
|
|
126
|
+
fi
|
|
127
|
+
mkdir -p "${project_path}/.ddev" || return 1 ;
|
|
128
|
+
cp "${from}/docker-compose.express.yaml" "${project_path}/.ddev/docker-compose.express.yaml" || return 1 ;
|
|
129
|
+
printf 'EXPRESS_NODE_IMAGE=%s\n' "${EXPRESS_NODE_IMAGE}" > "${project_path}/.ddev/.env" || return 1 ;
|
|
130
|
+
printf '%s\n' "# An Express app in this project: its service (docker-compose.express.yaml), Node's image in .env, the app in" \
|
|
131
|
+
"# ../${EXPRESS_APP}/, the builder's files in ../${EXPRESS_OWN}/. Written by the Express builder of the workspace; build again" \
|
|
132
|
+
"# rather than editing by hand." "# app: ${made}" > "${project_path}/.ddev/config.express.yaml" ;
|
|
133
|
+
# The demo keeps its accounts in memory: every start begins with none, so every start says how to make them again.
|
|
134
|
+
if [ "${demo}" = yes ] ; then
|
|
135
|
+
printf '%s\n' 'hooks:' ' post-start:' \
|
|
136
|
+
" - exec-host: 'echo \"The demo app starts with no accounts (they are kept in memory): run cmd-tools-add-users.sh $(basename "${project_path}") express, or build with --add-users, to register them again.\"'" \
|
|
137
|
+
>> "${project_path}/.ddev/config.express.yaml" ;
|
|
138
|
+
fi
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function express_runtime_config() {
|
|
142
|
+
rt_claim express || return 1 ;
|
|
143
|
+
rt_stack image "${EXPRESS_NODE_IMAGE}" || return 1 ;
|
|
144
|
+
rt_serve "${EXPRESS_PORT}" "$(express_serve_line)" ;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
# Wait until the app answers its front page (from inside its container), up to 4 minutes for a first start.
|
|
148
|
+
function express_wait() {
|
|
149
|
+
local attempt ;
|
|
150
|
+
for attempt in $(seq 1 120) ; do
|
|
151
|
+
if ddev exec -s "$(express_app_service)" -d / "node -e \"fetch('http://127.0.0.1:${EXPRESS_PORT}/').then((r) => process.exit(r.status < 500 ? 0 : 1), () => process.exit(1))\"" < /dev/null > /dev/null 2>&1 ; then return 0 ; fi
|
|
152
|
+
sleep 2 ;
|
|
153
|
+
done
|
|
154
|
+
echo "The app does not answer at its front page." ;
|
|
155
|
+
return 1 ;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function express_build() {
|
|
159
|
+
trim_project_name ;
|
|
160
|
+
local base_url runtime line="${LINE:-${EXPRESS_DEFAULT_LINE}}" ;
|
|
161
|
+
runtime="$(express_runtime)" ;
|
|
162
|
+
case " ${EXPRESS_LINES} " in
|
|
163
|
+
*" ${line} "*) ;;
|
|
164
|
+
*) echo "Express ${line} is not a line this builder is proven with: ${EXPRESS_LINES}. Nothing was changed." ; exit 1 ;;
|
|
165
|
+
esac
|
|
166
|
+
if [ "${RUNTIME:-_none_}" != '_none_' ] && [ "${RUNTIME}" != "${runtime}" ] ; then
|
|
167
|
+
echo "--runtime ${RUNTIME} needs a Webship Workspace with the container runtimes (webship/workspace#694): update the workspace, or build on DDEV." ;
|
|
168
|
+
exit 1 ;
|
|
169
|
+
fi
|
|
170
|
+
case " ${EXPRESS_RUNTIMES} " in
|
|
171
|
+
*" ${runtime} "*) ;;
|
|
172
|
+
*) echo "An Express app builds on DDEV, Docker Compose, Podman and Kubernetes for now (--runtime ddev, docker, podman or kubernetes), not on ${runtime}: that runtime is not proven for it yet. Nothing was changed." ; exit 1 ;;
|
|
173
|
+
esac
|
|
174
|
+
local source="${SOURCE:-}" ref="${REF:-}" app_path="${APP_PATH:-}" start="${START:-}" ;
|
|
175
|
+
if [ -n "${app_path}" ] && [ -z "${source}" ] ; then
|
|
176
|
+
echo "--path names a folder of the repository given with --source: give --source too. Nothing was changed." ;
|
|
177
|
+
exit 1 ;
|
|
178
|
+
fi
|
|
179
|
+
if [ -n "${source}" ] && [ -z "${start}" ] ; then
|
|
180
|
+
echo "An app of your own needs its start command named with --start (for example: --start 'node server.js'). Nothing was changed." ;
|
|
181
|
+
exit 1 ;
|
|
182
|
+
fi
|
|
183
|
+
if [[ "${app_path}" == /* ]] || [[ "/${app_path}/" == */../* ]] || [[ "${app_path}${source}${ref}${start}" == *"'"* ]] || [[ "${start}" =~ [\;\&\|\`\$\<\>] ]] ; then
|
|
184
|
+
echo "--source, --ref, --path and --start take a repository, a branch or tag, a folder inside it, and a plain command. Nothing was changed." ;
|
|
185
|
+
exit 1 ;
|
|
186
|
+
fi
|
|
187
|
+
if [ ${#PROJECT_NAME} -gt 40 ] || ! [[ "${PROJECT_NAME}" =~ ^[a-z0-9][a-z0-9-]*$ ]] ; then
|
|
188
|
+
echo "The project name ${PROJECT_NAME} must be small letters, digits and dashes (its address and its containers take it). Nothing was changed." ;
|
|
189
|
+
exit 1 ;
|
|
190
|
+
fi
|
|
191
|
+
local demo=no made ;
|
|
192
|
+
[ -z "${source}" ] && { demo=yes ; start="${EXPRESS_DEMO_START}" ; } ;
|
|
193
|
+
if [ "${demo}" = yes ] ; then made="the step pack's demo app on Express ${line}" ; else made="${source}${ref:+ (${ref})}${app_path:+, ${app_path}/}" ; fi
|
|
194
|
+
local project_path="${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}" ;
|
|
195
|
+
|
|
196
|
+
cd ${WORKSPACE_ROOT}/${doc_name} || exit 1 ;
|
|
197
|
+
drop_database ;
|
|
198
|
+
rm -rf "${PROJECT_NAME:?}" ;
|
|
199
|
+
mkdir -p "${PROJECT_NAME}" ;
|
|
200
|
+
cd "${project_path}" || exit 1 ;
|
|
201
|
+
|
|
202
|
+
if [ "${demo}" = yes ] ; then express_get_demo "${project_path}" "${line}" || exit 1 ;
|
|
203
|
+
else express_get_source "${project_path}" "${source}" "${ref}" "${app_path}" || exit 1 ; fi
|
|
204
|
+
ddev config --project-type=generic --webserver-type=generic --docroot="" --project-name=${PROJECT_NAME} \
|
|
205
|
+
--omit-containers=db --auto < /dev/null || exit 1 ;
|
|
206
|
+
base_url="$(express_url)" ;
|
|
207
|
+
express_write_own "${project_path}" "${line}" "${start}" || exit 1 ;
|
|
208
|
+
if [ "${runtime}" == 'ddev' ] ; then
|
|
209
|
+
express_write_ddev_config "${project_path}" "${made}" "${demo}" || exit 1 ;
|
|
210
|
+
else
|
|
211
|
+
express_runtime_config || { echo "The app's stack could not be written through the runtime layer." ; exit 1 ; } ;
|
|
212
|
+
fi
|
|
213
|
+
|
|
214
|
+
if [ "${INSTALL}" == 'yes' ] || [ "${ADD_USERS}" == 'yes' ] ; then
|
|
215
|
+
echo "Starting the project: ${made}${runtime:+ on ${runtime}}, its packages installed at the first start." ;
|
|
216
|
+
ddev_up start -y < /dev/null || exit 1 ;
|
|
217
|
+
express_wait || exit 1 ;
|
|
218
|
+
if [ "${ADD_USERS}" == 'yes' ] ; then
|
|
219
|
+
set_express_users ;
|
|
220
|
+
add_express_users || exit 1 ;
|
|
221
|
+
fi
|
|
222
|
+
echo "${doc_name} ${PROJECT_NAME} is served: ${made}, by Node.js." ;
|
|
223
|
+
echo "Go to ${base_url}" ;
|
|
224
|
+
else
|
|
225
|
+
echo "${doc_name} ${PROJECT_NAME} is ready: ${made}." ;
|
|
226
|
+
if [ "${runtime}" = 'ddev' ] ; then echo "Serve it with: cd ${PROJECT_NAME} && ddev start (or build with --install)" ;
|
|
227
|
+
else echo "Serve it with Start in its row of the dashboard (or build with --install)" ; fi
|
|
228
|
+
fi
|
|
229
|
+
[ "${LAUNCH}" == 'yes' ] && ddev launch ;
|
|
230
|
+
cd ${WORKSPACE_ROOT}/${doc_name};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function express_enter_project() {
|
|
234
|
+
local project_path="${WORKSPACE_ROOT}/${doc_name}/${PROJECT_NAME}" ;
|
|
235
|
+
if ! express_is_project "${project_path}" ; then
|
|
236
|
+
echo "${doc_name}/${PROJECT_NAME} is not a project of the Express builder." ;
|
|
237
|
+
return 1 ;
|
|
238
|
+
fi
|
|
239
|
+
cd "${project_path}" || return 1 ;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
# Register the default accounts through the demo's own register form, from inside the app's container (the name and
|
|
243
|
+
# password on its input, never on a command line). The demo keeps them in memory while it runs: a restart forgets them.
|
|
244
|
+
function add_express_users() {
|
|
245
|
+
express_enter_project || return 1 ;
|
|
246
|
+
local user user_name user_password answer status=0 ;
|
|
247
|
+
if [ "$(cat "${EXPRESS_OWN}/start" 2> /dev/null)" != "${EXPRESS_DEMO_START}" ] || [ ! -f "${EXPRESS_APP}/app.js" ] || ! grep -q 'webship-express-demo' "${EXPRESS_APP}/package.json" 2> /dev/null ; then
|
|
248
|
+
echo " The app is not the step pack's demo app: register its accounts its own way." ;
|
|
249
|
+
cd ${WORKSPACE_ROOT}/${doc_name} ; return 0 ;
|
|
250
|
+
fi
|
|
251
|
+
for user in "${users[@]}" ; do
|
|
252
|
+
user_name="user_${user}_name"; user_password="user_${user}_password";
|
|
253
|
+
echo " ---------------------------------------------------------------- ";
|
|
254
|
+
echo " User name: ${!user_name}";
|
|
255
|
+
echo " User password: ${!user_password}";
|
|
256
|
+
echo " ================================================================= ";
|
|
257
|
+
answer="$(printf '%s\n%s\n' "${!user_name}" "${!user_password}" | ddev exec -s "$(express_app_service)" -d / "node -e \"
|
|
258
|
+
let input = ''; process.stdin.on('data', (d) => { input += d; }).on('end', async () => {
|
|
259
|
+
const [username, password] = input.split('\\n');
|
|
260
|
+
const r = await fetch('http://127.0.0.1:${EXPRESS_PORT}/register', { method: 'POST', redirect: 'manual', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ username, password }) });
|
|
261
|
+
console.log(r.headers.get('location') === '/login' ? 'made' : 'there already');
|
|
262
|
+
});\"" 2> /dev/null | tail -1)" ;
|
|
263
|
+
case "${answer}" in
|
|
264
|
+
made|'there already') echo " ${answer}." ;;
|
|
265
|
+
*) echo " not made." ; status=1 ;;
|
|
266
|
+
esac
|
|
267
|
+
done
|
|
268
|
+
echo " The demo keeps its accounts in memory while it runs: a restart forgets them, and the step pack registers them again." ;
|
|
269
|
+
cd ${WORKSPACE_ROOT}/${doc_name};
|
|
270
|
+
return ${status} ;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function cancel_express_users() {
|
|
274
|
+
echo " The demo app has no way to stop an account from signing in (a restart forgets them all): nothing was changed." ;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
# ---------------------------------------------------------------------------------------------------------------------
|
|
278
|
+
# The step pack in the testing stack
|
|
279
|
+
# ---------------------------------------------------------------------------------------------------------------------
|
|
280
|
+
|
|
281
|
+
# The step pack in the testing stack of an app of this builder: the pack in package.json, its step definitions in
|
|
282
|
+
# cucumber.js after webship-js's own, the app's address where the scaffold has its placeholder, the accounts the
|
|
283
|
+
# scenarios name (worldParameters.users: tester and other, their passwords from the environment, the workspace's by
|
|
284
|
+
# default), and for the demo app its session cookie's name, the 404 and 500 its first feature asks for (by their
|
|
285
|
+
# address) and a first feature. Every part is skipped when it is already done.
|
|
286
|
+
function testing_configure_express() {
|
|
287
|
+
local project_path="$1";
|
|
288
|
+
express_is_project "${project_path}" || return 0 ;
|
|
289
|
+
if [ ! -f "${project_path}/cucumber.js" ] || [ ! -f "${project_path}/package.json" ] ; then
|
|
290
|
+
echo " Express: cucumber.js is not here yet — run the testing setup again once webship-js is scaffolded." ;
|
|
291
|
+
return 1 ;
|
|
292
|
+
fi
|
|
293
|
+
local demo=no address ;
|
|
294
|
+
[ "$(cat "${project_path}/${EXPRESS_OWN}/start" 2> /dev/null)" = "${EXPRESS_DEMO_START}" ] && grep -q 'webship-express-demo' "${project_path}/${EXPRESS_APP}/package.json" 2> /dev/null && demo=yes ;
|
|
295
|
+
address="$(cd "${project_path}" && PROJECT_NAME="${PROJECT_NAME:-$(basename "${project_path}")}" express_url)" ;
|
|
296
|
+
echo "Adding the Express step pack (${EXPRESS_STEPS_PACKAGE} ${EXPRESS_STEPS_VERSION}).";
|
|
297
|
+
python3 - "${project_path}" "${EXPRESS_STEPS_PACKAGE}" "${EXPRESS_STEPS_VERSION}" "${demo}" "${address}" <<'PYEOF' || return 1
|
|
298
|
+
import json, re, sys
|
|
299
|
+
|
|
300
|
+
project, pack, version, demo, address = sys.argv[1:6]
|
|
301
|
+
|
|
302
|
+
pkg_file = project + '/package.json'
|
|
303
|
+
pkg = json.load(open(pkg_file))
|
|
304
|
+
if pack not in pkg.get('dependencies', {}) and pack not in pkg.get('devDependencies', {}):
|
|
305
|
+
pkg.setdefault('devDependencies', {})[pack] = version
|
|
306
|
+
open(pkg_file, 'w').write(json.dumps(pkg, indent=2) + '\n')
|
|
307
|
+
print(' package.json: %s %s' % (pack, version))
|
|
308
|
+
|
|
309
|
+
cucumber_file = project + '/cucumber.js'
|
|
310
|
+
src = open(cucumber_file).read()
|
|
311
|
+
before = src
|
|
312
|
+
|
|
313
|
+
if ('node_modules/%s/' % pack) not in src:
|
|
314
|
+
entry = "'node_modules/%s/tests/step-definitions/*.js'," % pack
|
|
315
|
+
core = re.search(r"^([ \t]*)'node_modules/webship-js/tests/step-definitions/[^']*',.*$", src, re.M)
|
|
316
|
+
opening = re.search(r"^([ \t]*)require:[ \t]*\[[ \t]*$", src, re.M)
|
|
317
|
+
if core:
|
|
318
|
+
src = src[:core.end()] + '\n' + core.group(1) + entry + src[core.end():]
|
|
319
|
+
elif opening:
|
|
320
|
+
src = src[:opening.end()] + '\n' + opening.group(1) + ' ' + entry + src[opening.end():]
|
|
321
|
+
else:
|
|
322
|
+
sys.exit(" cucumber.js has no require list this can add to. Add this line to it:\n " + entry)
|
|
323
|
+
print(' cucumber.js: the step definitions of %s' % pack)
|
|
324
|
+
|
|
325
|
+
wp = re.search(r"^([ \t]*)worldParameters:[ \t]*\{[ \t]*$", src, re.M)
|
|
326
|
+
if wp and not re.search(r"^\s*users:\s*\{", src, re.M):
|
|
327
|
+
indent = wp.group(1) + ' '
|
|
328
|
+
users = ["users: {",
|
|
329
|
+
" tester: { username: process.env.EXPRESS_TESTER_USERNAME || 'webship-tester', password: process.env.EXPRESS_TESTER_PASSWORD || 'dD.123123ddd' },",
|
|
330
|
+
" other: { username: process.env.EXPRESS_OTHER_USERNAME || 'webship-other', password: process.env.EXPRESS_OTHER_PASSWORD || 'dD.123123ddd' },",
|
|
331
|
+
"},"]
|
|
332
|
+
src = src[:wp.end()] + '\n' + '\n'.join(indent + l for l in users) + src[wp.end():]
|
|
333
|
+
print(' cucumber.js: the accounts tester and other (EXPRESS_TESTER_PASSWORD, EXPRESS_OTHER_PASSWORD)')
|
|
334
|
+
wp = re.search(r"^([ \t]*)worldParameters:[ \t]*\{[ \t]*$", src, re.M)
|
|
335
|
+
|
|
336
|
+
if demo == 'yes' and wp and not re.search(r"^\s*express:\s*\{", src, re.M):
|
|
337
|
+
indent = wp.group(1) + ' '
|
|
338
|
+
src = src[:wp.end()] + '\n' + indent + "// The demo app's session cookie." + '\n' + indent + "express: { sessionCookie: 'webship.sid' }," + src[wp.end():]
|
|
339
|
+
print(" cucumber.js: the demo's session cookie (webship.sid)")
|
|
340
|
+
wp = re.search(r"^([ \t]*)worldParameters:[ \t]*\{[ \t]*$", src, re.M)
|
|
341
|
+
|
|
342
|
+
if demo == 'yes' and wp and 'webship-no-such-page' not in src:
|
|
343
|
+
pattern = json.dumps('^Failed to load resource: the server responded with a status of (?:404 \\([^)]*\\) @ [^ ]+/webship-no-such-page|500 \\([^)]*\\) @ [^ ]+/boom)$')
|
|
344
|
+
empty = re.search(r"(\bignore:\s*)(''|\"\"|null)", src)
|
|
345
|
+
block = re.search(r"^([ \t]*)javascript:[ \t]*\{[ \t]*$", src, re.M)
|
|
346
|
+
if empty:
|
|
347
|
+
src = src[:empty.start(2)] + pattern + src[empty.end(2):]
|
|
348
|
+
elif block:
|
|
349
|
+
src = src[:block.end()] + '\n' + block.group(1) + ' ignore: ' + pattern + ',' + src[block.end():]
|
|
350
|
+
else:
|
|
351
|
+
indent = wp.group(1) + ' '
|
|
352
|
+
src = src[:wp.end()] + '\n' + indent + "// The first feature loads a page the app does not have, and the demo's page that fails: their 404 and 500, by address." + '\n' + indent + 'javascript: { ignore: ' + pattern + ' },' + src[wp.end():]
|
|
353
|
+
print(' cucumber.js: the 404 and the 500 the first feature asks for, let through by their address')
|
|
354
|
+
|
|
355
|
+
placeholder = "process.env.LAUNCH_URL || 'http://localhost:8080'"
|
|
356
|
+
if address and placeholder in src:
|
|
357
|
+
src = src.replace(placeholder, "process.env.LAUNCH_URL || '%s'" % address)
|
|
358
|
+
print(' cucumber.js: the address of the app (%s)' % address)
|
|
359
|
+
|
|
360
|
+
if src != before:
|
|
361
|
+
open(cucumber_file, 'w').write(src)
|
|
362
|
+
PYEOF
|
|
363
|
+
local feature="${WORKSPACE_SCRIPTS}/plugins/express/site.feature" ;
|
|
364
|
+
if [ -f "${feature}" ] && [ ! -f "${project_path}/tests/features/site.feature" ] ; then
|
|
365
|
+
if [ "${demo}" = yes ] ; then
|
|
366
|
+
mkdir -p "${project_path}/tests/features" && cp "${feature}" "${project_path}/tests/features/site.feature" && echo " tests/features/site.feature: a first feature for the demo app" ;
|
|
367
|
+
else
|
|
368
|
+
echo " Express: the app is not the demo app, so no first feature is written: write one from what its pages show." ;
|
|
369
|
+
fi
|
|
370
|
+
fi
|
|
371
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# A first feature for the demo app of an Express project of the workspace, written by its plugin's testing setup: an
|
|
2
|
+
# account signed in and out, Express's own refusal pages with no stack trace, and the session cookie. The step pack
|
|
3
|
+
# (@webship-js/express-steps) has more: redirects, the body parsers' refusals, static files, headers.
|
|
4
|
+
Feature: The project's Express app
|
|
5
|
+
An account signs in and out; a page the app does not have, and one that fails, are refused without a stack trace.
|
|
6
|
+
|
|
7
|
+
Background:
|
|
8
|
+
Given the HTTP app has the account "tester"
|
|
9
|
+
|
|
10
|
+
Scenario: Signed in and out, the session cookie HttpOnly
|
|
11
|
+
Given I am signed in to the HTTP app as "tester"
|
|
12
|
+
Then the HTTP app should say "Signed in as webship-tester."
|
|
13
|
+
And the HTTP app's session cookie should be HttpOnly
|
|
14
|
+
When I sign out of the HTTP app
|
|
15
|
+
Then I should not be signed in to the HTTP app
|
|
16
|
+
|
|
17
|
+
Scenario: Refusals without a stack trace
|
|
18
|
+
When I go to "/webship-no-such-page"
|
|
19
|
+
Then the HTTP app should answer 404 "Cannot GET /webship-no-such-page"
|
|
20
|
+
When I go to "/boom"
|
|
21
|
+
Then the HTTP app should answer 500 "Internal Server Error"
|
|
22
|
+
And the HTTP app should show no stack trace
|