Skip to content
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

Fix nvm_ls_current fast unit test #2322

Merged
merged 1 commit into from
Oct 19, 2020
Merged

Fix nvm_ls_current fast unit test #2322

merged 1 commit into from
Oct 19, 2020

Conversation

reasonablytall
Copy link
Contributor

Fixes one of the failing tests in #2181

nvm_ls_current returns 'system' when nvm_tree_contains_path is aliased to return zero, as if nvm is deactivated. This fix instead sets NVM_DIR to match TEST_DIR so that nvm_tree_contains_path passes and the mock executable is actually used. Let me know if this approach is reasonable!

Relevant branches in nvm_ls_current:

nvm/nvm.sh

Lines 915 to 925 in e01060f

elif nvm_tree_contains_path "${NVM_DIR}" "${NVM_LS_CURRENT_NODE_PATH}"; then
local VERSION
VERSION="$(node --version 2>/dev/null)"
if [ "${VERSION}" = "v0.6.21-pre" ]; then
nvm_echo 'v0.6.21'
else
nvm_echo "${VERSION}"
fi
else
nvm_echo 'system'
fi

@ljharb
Copy link
Member

ljharb commented Oct 14, 2020

It's not clear to me why this test is passing in CI but fails locally, and I'm hesitant to change the test without knowing why this change fixes it.

@reasonablytall
Copy link
Contributor Author

I think I mixed up return_zero being truthy/falsey in my initial explanation, and thought that that was the issue 😅

Digging deeper I believe I've figured out some things:

  1. Inline aliasing nvm_tree_contains_path in the tests doesn't do anything because aliases don't apply until the next line (reference).
  2. When running the tests via npm run test/fast, the which linked into the test directory is actually node_modules/.bin/which rather than the builtin.
    ln -s "$(command which which)" "$TEST_DIR/which"

    That's a #!/usr/bin/env node script so when command which node 2>/dev/null is run in nvm_ls_current, the mock node is used, and NVM_LS_CURRENT_NODE_PATH is set to VERSION FOO!, which doesn't seem quite right 😆

    nvm/nvm.sh

    Line 911 in e01060f

    if ! NVM_LS_CURRENT_NODE_PATH="$(command which node 2>/dev/null)"; then

Based on this, I'm working on a fix that links the correct which during the test. Let me know if I'm missing something!

@ljharb
Copy link
Member

ljharb commented Oct 15, 2020

Thanks, I really appreciate the digging, and this sounds like the right track.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, almost there :-)

@@ -19,20 +19,21 @@ fi

rm -rf "$TEST_DIR"
mkdir "$TEST_DIR"
ln -s "$(command which which)" "$TEST_DIR/which"
# Ensure that the system version of which is used, not node_modules/.bin/which
ln -s "$(PATH="/usr/bin" command which which)" "$TEST_DIR/which"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think /usr/bin will always be where it's located. i think the issue is that node_modules/.bin is in the PATH when npm run is used - so, can we remove that from the PATH rather than hardcoding one possible location of which?

ln -s "$(command which dirname)" "$TEST_DIR/dirname"
ln -s "$(command which printf)" "$TEST_DIR/printf"

[ "$(PATH="$TEST_DIR" nvm_ls_current)" = "none" ] || die 'when node not installed, nvm_ls_current did not return "none"'
[ "@$(PATH="$TEST_DIR" nvm_ls_current 2> /dev/stdout 1> /dev/null)@" = "@@" ] || die 'when node not installed, nvm_ls_current returned error output'

echo "#!/bin/bash" > "$TEST_DIR/node"
echo "echo 'VERSION FOO!'" > "$TEST_DIR/node"
echo "echo 'VERSION FOO!'" >> "$TEST_DIR/node"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(-‸ლ) oops

@reasonablytall
Copy link
Contributor Author

Thanks for the review! Made the change :)

@@ -19,20 +19,21 @@ fi

rm -rf "$TEST_DIR"
mkdir "$TEST_DIR"
ln -s "$(command which which)" "$TEST_DIR/which"
# Ensure that the system version of which is used, not node_modules/.bin/which
ln -s "$(PATH=$(echo $PATH | tr ":" "\n" | grep -v "node_modules/.bin" | tr "\n" ":") command which which)" "$TEST_DIR/which"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, too bad there's no ergonomic way to use nvm_strip_path here

@ljharb ljharb added the testing Stuff related to testing nvm itself. label Oct 19, 2020
@ljharb ljharb merged commit e77ed07 into nvm-sh:master Oct 19, 2020
@ljharb ljharb added the hacktoberfest-accepted If you're interested in a free shirt, this PR counts towards it. label Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted If you're interested in a free shirt, this PR counts towards it. testing Stuff related to testing nvm itself.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants