Skip to content

Commit b115fe4

Browse files
authored
fix: add script to use node 18.20 for e2e tests (#937)
* add 18.20 node installation to build_windows * remove extra empty line * add 18.20 node installation to buildLinux * setup node version in _setupGen2E2ETestsLinux * update setupNode in _setupGen2E2ETests * remove redundant code * add refreshenv * refresh env in powerShell * refresh env var in linux * update refresh env var * change node setup location * try cmd //c refreshenv * manually update PATH * add more debug statements * use shell: true to properly execute the .cmd file on Windows * remove debug statements * refresh env var using export PATH=$PATH, remove manual update * remove comment * update the comment doc for getAmpxPath, scope down the shell option to windows only * setup node version for _buildLinux and _buildWindows * fix typo * not setup node version for _buildLinux and _buildWindows for now * set up node version for build linux and build windows again * typo
1 parent 9f37a8c commit b115fe4

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

packages/amplify-codegen-e2e-tests/src/gen2-codegen-tests-base/commands.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,30 @@ import { spawnSync } from 'child_process';
88
* @returns the local `npx` executable path.
99
*/
1010
const getNpxPath = (): string => (process.platform === 'win32' ? getScriptRunnerPath().replace('node.exe', 'npx.cmd') : 'npx');
11+
1112
/**
12-
* Retrieve the path to the `ampx` executable for interacting with the amplify gen2 cli.
13+
* Retrieve the path to the `ampx` executable for interacting with the Amplify Gen2 CLI.
1314
* @param cwd current working directory
1415
* @returns the local `ampx` executable path
16+
*
17+
* Note:
18+
* On Windows, batch files (like npx.cmd) must be executed through a shell.
19+
* Therefore, this function uses `shell: process.platform === 'win32'` to ensure that the command
20+
* is run via the shell on Windows. This change was required after upgrading to Node 18,
21+
* where a security update now causes an EINVAL error if a .cmd file is executed without the shell option.
22+
*
23+
* See: https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2#command-injection-via-args-parameter-of-child_processspawn-without-shell-option-enabled-on-windows-cve-2024-27980---high
24+
*
25+
* Warning:
26+
* Command arguments **must** be sanitized to avoid injection risks.
1527
*/
1628
const getAmpxPath = (cwd: string): string =>
17-
spawnSync(getNpxPath(), ['which', 'ampx'], { cwd, env: process.env, stdio: 'pipe' }).stdout.toString().trim();
29+
spawnSync(getNpxPath(), ['which', 'ampx'], {
30+
cwd,
31+
env: process.env,
32+
stdio: 'pipe',
33+
shell: process.platform === 'win32',
34+
}).stdout.toString().trim();
1835

1936
const codegenPackagesInGen2 = [
2037
'@aws-amplify/graphql-generator',

shared-scripts.sh

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
AMPLIFY_NODE_VERSION=18.20.4
4+
35
# set exit on error to true
46
set -e
57

@@ -131,13 +133,17 @@ function _setShell {
131133

132134
function _buildLinux {
133135
_setShell
136+
echo "Setup Node Version $AMPLIFY_NODE_VERSION for Linux"
137+
_setupNodeVersionLinux $AMPLIFY_NODE_VERSION
134138
echo "Linux Build"
135139
yarn run production-build
136140
storeCacheForLinuxBuildJob
137141
}
138142

139143
function _buildWindows {
140144
echo "Linux Build"
145+
echo "Setup Node Version $AMPLIFY_NODE_VERSION for Windows"
146+
_setupNodeVersionWindows $AMPLIFY_NODE_VERSION
141147
yarn run production-build
142148
storeCacheForWindowsBuildJob
143149
}
@@ -288,7 +294,6 @@ function _setupGen2E2ETestsWindows {
288294
_setShell
289295
}
290296

291-
292297
function _runE2ETestsLinux {
293298
echo "RUN E2E Tests Linux"
294299
retry runE2eTest
@@ -301,11 +306,15 @@ function _runE2ETestsWindows {
301306

302307
function _runGen2E2ETestsLinux {
303308
echo "RUN Gen2 E2E Tests Linux"
309+
echo "Setup Node Version"
310+
_setupNodeVersionLinux $AMPLIFY_NODE_VERSION
304311
retry runGen2E2eTest
305312
}
306313

307314
function _runGen2E2ETestsWindows {
308315
echo "RUN Gen2 E2E Tests Windows"
316+
echo "Setup Node Version"
317+
_setupNodeVersionWindows $AMPLIFY_NODE_VERSION
309318
retry runGen2E2eTest
310319
}
311320

@@ -529,4 +538,46 @@ function _emitRegionalizedCanaryMetric {
529538
--value $CODEBUILD_BUILD_SUCCEEDING \
530539
--dimensions branch=release,region=$CLI_REGION \
531540
--region us-west-2
532-
}
541+
}
542+
543+
function _setupNodeVersionLinux {
544+
local version=$1 # Version number passed as an argument
545+
546+
echo "Installing NVM and setting Node.js version to $version"
547+
548+
# Install NVM
549+
curl -o - https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
550+
551+
# Load NVM
552+
export NVM_DIR="$HOME/.nvm"
553+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
554+
555+
# Install and use the specified Node.js version
556+
nvm install "$version"
557+
nvm use "$version"
558+
559+
# Refresh environment variables
560+
source ~/.bashrc # Or source the appropriate shell profile file like ~/.bash_profile or ~/.zshrc
561+
562+
# Verify the Node.js version in use
563+
echo "Node.js version in use:"
564+
node -v
565+
}
566+
567+
function _setupNodeVersionWindows {
568+
local version=$1 # Version number passed as an argument
569+
570+
echo "Installing Node.js version $version on Windows"
571+
572+
# Install Node.js using Chocolatey
573+
echo "Installing Node.js version $version using Chocolatey"
574+
choco install -fy nodejs-lts --version=$version
575+
576+
# Refresh environment variables
577+
echo "Refreshing environment variables"
578+
export PATH=$PATH
579+
580+
# Verify the Node.js version in use
581+
nodeVersion=$(node -v)
582+
echo "Node version: $nodeVersion"
583+
}

0 commit comments

Comments
 (0)