-
Notifications
You must be signed in to change notification settings - Fork 1
Normalize Windows PWD before nvm install #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5ea78ee
c70bc9b
336d987
76ed5a8
de272a5
b5c64ab
9481e8c
d7bb7c4
e00aea8
f5e00f4
101a69b
4dc8ec5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Allow for the creation of an .nvmrc file in the CI machine. | ||
| # This allows testing the plugin fallback to .nvmrc without a confusing instance in the repo root. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| if [[ -z "${NVM_BUILDKITE_PLUGIN_TEST_NVMRC_VERSION:-}" ]]; then | ||
| exit 0 | ||
| fi | ||
|
|
||
| printf '%s\n' "$NVM_BUILDKITE_PLUGIN_TEST_NVMRC_VERSION" > .nvmrc | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Use `make test` for shell helper tests and `make validate` for plugin schema validation. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @AGENTS.md |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| test: | ||
| bash tests/nvm-plugin-helpers-test.sh | ||
| bash tests/pre-command-smoke-test.sh | ||
|
|
||
| validate: | ||
| docker run -it --rm -v "$(shell pwd):/plugin:ro" buildkite/plugin-linter --id automattic/nvm | ||
|
Comment on lines
+1
to
6
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Sourced helper only, so no set options. Up to callers to set strict mode before loading it. | ||
| # | ||
| # nvm walks PWD with slash trimming, Windows paths can make that loop forever. | ||
| # These helpers allow preventing that behavior. | ||
|
|
||
| nvm_plugin_should_normalize_windows_pwd() { | ||
| local shell_name="$1" | ||
| local current_pwd="$2" | ||
|
|
||
| case "$shell_name" in | ||
| CYGWIN* | MINGW* | MSYS*) ;; | ||
| *) return 1 ;; | ||
| esac | ||
|
|
||
| case "$current_pwd" in | ||
| *\\* | ?:*) return 0 ;; | ||
| *) return 1 ;; | ||
| esac | ||
| } | ||
|
|
||
| nvm_plugin_windows_pwd_to_posix() { | ||
| local current_pwd="$1" | ||
| local drive | ||
| local drive_lower | ||
| local rest | ||
|
|
||
| case "$current_pwd" in | ||
| [A-Za-z]:*) | ||
| drive="${current_pwd:0:1}" | ||
| drive_lower="$(printf '%s' "$drive" | tr '[:upper:]' '[:lower:]')" | ||
| rest="${current_pwd:2}" | ||
| rest="${rest//\\//}" | ||
|
|
||
| printf '/%s%s\n' "$drive_lower" "$rest" | ||
| return 0 | ||
| ;; | ||
| \\\\*) | ||
| printf '%s\n' "${current_pwd//\\//}" | ||
| return 0 | ||
| ;; | ||
| *) | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } | ||
|
|
||
| nvm_plugin_normalize_windows_pwd_for_nvm() { | ||
| local shell_name | ||
| shell_name="$(uname -s 2>/dev/null || true)" | ||
|
|
||
| if ! nvm_plugin_should_normalize_windows_pwd "$shell_name" "${PWD:-}"; then | ||
| return 0 | ||
| fi | ||
|
|
||
| local normalized_pwd | ||
| if ! normalized_pwd="$(nvm_plugin_windows_pwd_to_posix "$PWD")"; then | ||
| echo "Cannot normalize Windows PWD for nvm from ${PWD}" | ||
| return 0 | ||
| fi | ||
|
|
||
| if [[ -z "$normalized_pwd" || "$normalized_pwd" == "$PWD" ]]; then | ||
| echo "Cannot normalize Windows PWD for nvm from ${PWD}" | ||
| return 0 | ||
| fi | ||
|
Comment on lines
+57
to
+66
|
||
|
|
||
| if [[ ! -d "$normalized_pwd" ]]; then | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we detect a Windows PWD that needs normalization but the normalized path does not exist, the helper logs and returns success, then we continue into |
||
| echo "Cannot normalize Windows PWD for nvm because ${normalized_pwd} is not a directory" | ||
| return 0 | ||
| fi | ||
|
|
||
| echo "Normalizing Windows PWD for nvm from ${PWD} to ${normalized_pwd}" | ||
| cd "$normalized_pwd" || { | ||
| echo "Failed to normalize Windows PWD for nvm to ${normalized_pwd}" | ||
| return 0 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| # shellcheck source=hooks/lib/nvm-buildkite-plugin.bash | ||
| source "$repo_root/hooks/lib/nvm-buildkite-plugin.bash" | ||
|
|
||
| fail() { | ||
| echo "FAIL: $*" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| nvm_plugin_should_normalize_windows_pwd "MSYS_NT-10.0" 'C:\buildkite-agent\repo' || fail "MSYS backslash PWD should be normalized" | ||
| nvm_plugin_should_normalize_windows_pwd "CYGWIN_NT-10.0" 'C:/buildkite-agent/repo' || fail "Cygwin drive PWD should be normalized" | ||
|
|
||
| if nvm_plugin_should_normalize_windows_pwd "MSYS_NT-10.0" "/c/buildkite-agent/repo"; then | ||
| fail "MSYS POSIX PWD should not be normalized" | ||
| fi | ||
|
|
||
| if nvm_plugin_should_normalize_windows_pwd "linux-gnu" 'C:\buildkite-agent\repo'; then | ||
| fail "non-Windows shell PWD should not be normalized" | ||
| fi | ||
|
|
||
| [[ "$(nvm_plugin_windows_pwd_to_posix 'C:\buildkite-agent\repo')" == "/c/buildkite-agent/repo" ]] || fail "backslash drive path should convert to POSIX path" | ||
| [[ "$(nvm_plugin_windows_pwd_to_posix 'D:/buildkite-agent/repo')" == "/d/buildkite-agent/repo" ]] || fail "slash drive path should convert to POSIX path" | ||
| [[ "$(nvm_plugin_windows_pwd_to_posix '\\server\share\repo')" == "//server/share/repo" ]] || fail "UNC path should convert to POSIX path" | ||
|
|
||
| if nvm_plugin_windows_pwd_to_posix "/c/buildkite-agent/repo" >/dev/null; then | ||
| fail "POSIX path should not be converted" | ||
| fi | ||
|
|
||
| echo "All helper tests passed." |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" | ||
| tmpdir="$(mktemp -d)" | ||
| trap 'rm -rf "$tmpdir"' EXIT | ||
|
|
||
| mkdir -p "$tmpdir/bin" "$tmpdir/work" | ||
|
|
||
| cat > "$tmpdir/bin/curl" <<'CURL' | ||
| #!/usr/bin/env bash | ||
| cat <<'INSTALLER' | ||
| #!/usr/bin/env bash | ||
| mkdir -p "$NVM_DIR" | ||
| cat > "$NVM_DIR/nvm.sh" <<'NVM' | ||
| nvm() { | ||
| case "$1" in | ||
| install) | ||
| echo "fake nvm install ${*:2}" | ||
| ;; | ||
| use) | ||
| echo "fake nvm use ${*:2}" | ||
| ;; | ||
| *) | ||
| echo "unsupported nvm command: $*" >&2 | ||
| return 1 | ||
| ;; | ||
| esac | ||
| } | ||
| NVM | ||
| INSTALLER | ||
| CURL | ||
| chmod +x "$tmpdir/bin/curl" | ||
|
|
||
| cat > "$tmpdir/bin/node" <<'NODE' | ||
| #!/usr/bin/env bash | ||
| if [[ "${1:-}" == "--version" ]]; then | ||
| echo "v20.19.5" | ||
| elif [[ "${1:-}" == "-" ]]; then | ||
| cat >/dev/null | ||
| echo "Hello Node.js" | ||
| else | ||
| echo "unsupported node command: $*" >&2 | ||
| exit 1 | ||
| fi | ||
| NODE | ||
| chmod +x "$tmpdir/bin/node" | ||
|
|
||
| ( | ||
| export NVM_BUILDKITE_PLUGIN_TEST_NVMRC_VERSION=20.19.5 | ||
|
|
||
| cd "$tmpdir/work" | ||
| bash "$repo_root/.buildkite/hooks/pre-command" | ||
| ) | ||
|
|
||
| ( | ||
| export PATH="$tmpdir/bin:$PATH" | ||
| export BUILDKITE_JOB_ID="pre-command-smoke" | ||
| export TMPDIR="$tmpdir/tmp" | ||
| mkdir -p "$TMPDIR" | ||
|
|
||
| cd "$tmpdir/work" | ||
| bash "$repo_root/hooks/pre-command" | ||
| ) > "$tmpdir/pre-command.out" | ||
|
|
||
| grep -F "fake nvm install --no-progress" "$tmpdir/pre-command.out" >/dev/null | ||
| grep -F "fake nvm use" "$tmpdir/pre-command.out" >/dev/null | ||
| grep -F "Node.js and nvm successfully set up." "$tmpdir/pre-command.out" >/dev/null | ||
|
|
||
| echo "pre-command smoke test passed." |
Uh oh!
There was an error while loading. Please reload this page.