Skip to content

Commit ef7fc2f

Browse files
asolopovasljharb
authored andcommitted
[Fix] nvm_get_arch: proper value for alpine linux
1 parent b64e547 commit ef7fc2f

File tree

3 files changed

+139
-3
lines changed

3 files changed

+139
-3
lines changed

.github/workflows/windows-npm.yml

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ jobs:
126126
distribution: ${{ matrix.wsl-distrib }}
127127
additional-packages: bash git curl ca-certificates wget
128128
- name: Retrieve nvm on WSL
129-
env:
130-
C: /usr/bin/gcc-8
131-
CXX: /usr/bin/g++-8
132129
run: |
133130
if [ -z "${{ matrix.method }}" ]; then
134131
curl -fsSLo- "https://raw.githubusercontent.com/${NVM_INSTALL_GITHUB_REPO}/${NVM_INSTALL_VERSION}/install.sh" | bash
@@ -139,6 +136,47 @@ jobs:
139136
nvm install ${{ matrix.npm-node-version }}
140137
node -v
141138
139+
wsl_matrix_unofficial:
140+
name: 'WSL nvm install'
141+
defaults:
142+
run:
143+
shell: wsl-bash {0}
144+
runs-on: windows-latest
145+
env:
146+
WSLENV: NVM_INSTALL_GITHUB_REPO:NVM_INSTALL_VERSION:/p
147+
NVM_NODEJS_ORG_MIRROR: https://unofficial-builds.nodejs.org/download/release
148+
strategy:
149+
fail-fast: false
150+
matrix:
151+
wsl-distrib:
152+
- Alpine
153+
npm-node-version:
154+
- '--lts'
155+
- '21'
156+
- '18'
157+
- '16'
158+
- '14'
159+
- '12'
160+
- '11'
161+
- '10'
162+
method:
163+
- ''
164+
- 'script'
165+
steps:
166+
- uses: Vampire/setup-wsl@v2
167+
with:
168+
distribution: ${{ matrix.wsl-distrib }}
169+
additional-packages: bash git curl ca-certificates wget
170+
- name: Retrieve nvm on WSL
171+
run: |
172+
if [ -z "${{ matrix.method }}" ]; then
173+
curl -fsSLo- "https://raw.githubusercontent.com/${NVM_INSTALL_GITHUB_REPO}/${NVM_INSTALL_VERSION}/install.sh" | bash
174+
else
175+
curl -fsSLo- "https://raw.githubusercontent.com/${NVM_INSTALL_GITHUB_REPO}/${NVM_INSTALL_VERSION}/install.sh" | METHOD="${{matrix.method}}" bash
176+
fi
177+
. "$HOME/.nvm/nvm.sh"
178+
NVM_NODEJS_ORG_MIRROR=${{ env.NVM_NODEJS_ORG_MIRROR }} nvm install ${{ matrix.npm-node-version }}
179+
142180
nvm_windows:
143181
name: 'tests, on windows'
144182
permissions:

nvm.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,10 @@ nvm_get_arch() {
19651965
HOST_ARCH=armv7l
19661966
fi
19671967

1968+
if [ -f "/etc/alpine-release" ]; then
1969+
NVM_ARCH=x64-musl
1970+
fi
1971+
19681972
nvm_echo "${NVM_ARCH}"
19691973
}
19701974

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/sh
2+
3+
# Save the PATH as it was when the test started to restore it when it finishes
4+
ORIG_PATH="${PATH}"
5+
6+
cleanup() {
7+
# Restore the PATH as it was when the test started
8+
export PATH="${ORIG_PATH}"
9+
rm -rf "${TMP_DIR}"
10+
}
11+
12+
die() {
13+
cleanup
14+
echo "$@"
15+
exit 1
16+
}
17+
18+
. ../../../nvm.sh
19+
20+
# Sets the PATH for these tests to include the symlinks to the mocked binaries
21+
export PATH=".:${PATH}"
22+
23+
TMP_DIR=$(mktemp -d)
24+
CHROOT_WITH_ALPINE="$TMP_DIR/with_alpine"
25+
CHROOT_WITHOUT_ALPINE="$TMP_DIR/without_alpine"
26+
27+
setup_chroot() {
28+
chroot_dir=$1
29+
30+
# Directories
31+
mkdir -p "${chroot_dir}/etc"
32+
mkdir -p "${chroot_dir}/bin"
33+
mkdir -p "${chroot_dir}/usr/bin"
34+
mkdir -p "${chroot_dir}/lib64"
35+
mkdir -p "${chroot_dir}/dev"
36+
37+
# Files and binaries
38+
cp ../../../nvm.sh "${chroot_dir}/"
39+
cp /bin/sh /usr/bin/dirname "${chroot_dir}/bin/"
40+
[ "${chroot_dir}" = "${CHROOT_WITH_ALPINE}" ] && touch "${chroot_dir}/etc/alpine-release"
41+
42+
# Libraries
43+
for binary in /bin/sh /usr/bin/dirname; do
44+
for lib in $(ldd $binary | awk '{print $3}' | grep "^/"); do
45+
dir=$(dirname "${lib}")
46+
mkdir -p "${chroot_dir}${dir}"
47+
cp "${lib}" "${chroot_dir}${dir}/"
48+
done
49+
done
50+
51+
# Dynamic linker
52+
cp /lib64/ld-linux-x86-64.so.2 "${chroot_dir}/lib64/"
53+
54+
# /dev/null
55+
sudo mknod "${chroot_dir}/dev/null" c 1 3
56+
}
57+
58+
setup_chroot "${CHROOT_WITH_ALPINE}"
59+
setup_chroot "${CHROOT_WITHOUT_ALPINE}"
60+
61+
# Run tests in chroot environments
62+
ARCH_WITH_ALPINE=$(sudo chroot "${CHROOT_WITH_ALPINE}" /bin/sh -c ". ./nvm.sh && nvm_get_arch")
63+
[ "${ARCH_WITH_ALPINE}" = "x64-musl" ] || die "Expected x64-musl for alpine environment but got ${ARCH_WITH_ALPINE}"
64+
65+
ARCH_WITHOUT_ALPINE=$(sudo chroot "${CHROOT_WITHOUT_ALPINE}" /bin/sh -c ". ./nvm.sh && nvm_get_arch")
66+
[ "${ARCH_WITHOUT_ALPINE}" != "x64-musl" ] || die "Did not expect x64-musl for non-alpine environment"
67+
68+
# Run tests for nvm ls-remote
69+
test_default_ls_remote() {
70+
mock_response='N/A'
71+
result=$(NVM_NODEJS_ORG_MIRROR='http://nonexistent-url' nvm ls-remote 18)
72+
if [ "${result}" = "${mock_response}" ]; then
73+
die "Test failed: Expected '${mock_response}' for but got '${result}'"
74+
else
75+
echo "Test passed"
76+
fi
77+
}
78+
79+
test_unofficial_mirror_ls_remote() {
80+
mock_response='v18.18.0 (LTS: Hydrogen)'
81+
result=$(NVM_NODEJS_ORG_MIRROR='https://unofficial-builds.nodejs.org/download/release' nvm ls-remote 18.18.0 | sed -e 's/^[[:space:]]*//')
82+
result=$(echo "${result}" | sed 's/\x1b\[[0-9;]*m//g')
83+
84+
if [ "${result}" = "${mock_response}" ]; then
85+
echo "Test passed"
86+
else
87+
die "Test failed: Expected '${mock_response}' but got '${result}'"
88+
fi
89+
}
90+
91+
test_default_ls_remote
92+
test_unofficial_mirror_ls_remote
93+
94+
cleanup

0 commit comments

Comments
 (0)