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

[New] nvm use/nvm install: add --save option #2869

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maartin0
Copy link

In this PR I've:

  • added a --save option as per nvm use should write a .nvmrc file by default #2849
  • implemented it for both nvm use and nvm install (which actually just carries over the option)
  • manually tested it in bash, sh and zsh
  • written one test, however I can't get the slow tests to run on my system so hopefully it runs okay 🤞 (if someone would be able to check that, that'd be great)

image
image

@ljharb ljharb changed the title Add --save option for 'npm use' and 'npm install' [New] Add --save option for nvm use and nvm install Aug 29, 2022
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.

Let's add a bunch more tests - here's some example input:

  • nvm use --save --lts
  • nvm use --save lts/argon
  • nvm use --save lts/*
  • nvm use --save node
  • nvm use --save iojs
  • nvm use --save foo (where foo is a custom user alias)
  • nvm use --save (where the version is read from a .nvmrc in a higher directory)

Let's also add some error tests for the cases where the .nvmrc file fails to be written (no permissions in the current directory, or the disk is full, for example)

nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
@maartin0 maartin0 marked this pull request as draft August 29, 2022 22:16
@maartin0
Copy link
Author

Thanks for all the advice, it's been really helpful!
With the tests, would you suggest making seperate files for each one or loosely grouping them?

@ljharb
Copy link
Member

ljharb commented Aug 29, 2022

If you can make them be "fast" tests, then one file is totally fine - mainly it'll depend on how much setup is required. Generally each test file should only have one batch of setup at the beginning, and one batch of cleanup at the end.

nvm.sh Outdated Show resolved Hide resolved
@maartin0 maartin0 marked this pull request as ready for review August 30, 2022 17:46
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
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.

The nvm install and nvm use tests should definitely be in separate files.

Also, could the tests all be moved to fast? they'd be able to use mocks for nvm ls-remote calls, and as long as the dynamically-looked-up expected versions were fake-installed already, then they wouldn't need to actually install anything.

nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
@maartin0 maartin0 marked this pull request as draft August 31, 2022 19:58
@maartin0
Copy link
Author

Also, could the tests all be moved to fast? they'd be able to use mocks for nvm ls-remote calls, and as long as the dynamically-looked-up expected versions were fake-installed already, then they wouldn't need to actually install anything.

I've moved all of the test to 'fast' and split them into different files, however I'm not sure how to do fake installs (have you got any existing functions to do that?)

@maartin0 maartin0 marked this pull request as ready for review August 31, 2022 20:23
@ljharb
Copy link
Member

ljharb commented Aug 31, 2022

however I'm not sure how to do fake installs (have you got any existing functions to do that?)

i do! grep for make_fake_node

@maartin0
Copy link
Author

however I'm not sure how to do fake installs (have you got any existing functions to do that?)

i do! grep for make_fake_node

Hmm, I tried that (from common.js) and it didn't seem to prevent downloading the latest version

@maartin0
Copy link
Author

If you'd like I can prepend that to all of the nvm install statements anyway

@ljharb
Copy link
Member

ljharb commented Aug 31, 2022

ah right, the other piece is that you have to mock out nvm_ls_remote - see https://github.com/nvm-sh/nvm/blob/master/test/fast/Unit%20tests/nvm%20ls-remote#L59-L66 for an example.

@maartin0
Copy link
Author

maartin0 commented Sep 4, 2022

I'm not quite sure how to mock nvm_ls_remote, so I've added 3 todo notes as to where it needs to be ran;
All of the tests run fine, just some of them need to download specific versions of node

@ljharb
Copy link
Member

ljharb commented Sep 4, 2022

I added the mocks; I think this might be good to go!

It'll have to wait until the broken travis tests on master are fixed; please keep the fork/branch undeleted and the PR open until then :-)

@ljharb ljharb changed the title [New] Add --save option for nvm use and nvm install [New] nvm use/nvm install: add --save option Oct 8, 2022
@ljharb ljharb force-pushed the master branch 3 times, most recently from 67ab6bb to a40bfed Compare October 8, 2022 21:26
@ljharb
Copy link
Member

ljharb commented Oct 8, 2022

I'll get the tests fixed and get this landed shortly.

@maartin0 maartin0 force-pushed the master branch 3 times, most recently from d9748eb to ae27ed5 Compare February 22, 2024 12:31
@ljharb
Copy link
Member

ljharb commented Feb 22, 2024

I intentionally use nonexistent node versions in the tests at some point; make_fake_node should be working fine already.

nvm.sh Outdated Show resolved Hide resolved
nvm.sh Outdated Show resolved Hide resolved
@maartin0
Copy link
Author

I've refactored it a bit by moving the block under nvm_write_nvmrc and then only running it on nvm use (I removed the local declaration in the nvm use block so NVM_WRITE_TO_NVMRC is passed through). Can you figure out why the tests aren't working? They run fine as independent shells scripts or by running urchin directly in the test/fast/Unit tests directory, but they fail as soon as you try running it further up and I can't get any error messages out of it- is something in one of the setup or setup_dir scripts interfering with it?

@maartin0 maartin0 force-pushed the master branch 2 times, most recently from 5799632 to 36844a9 Compare February 29, 2024 19:56
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.

This is looking pretty good assuming tests pass! (you can ignore "automatic rebase" and the failing WSL tests)

nvm.sh Show resolved Hide resolved
@maartin0
Copy link
Author

@ljharb The issue with the tests was that I was using set -e when sourcing nvm.sh which I only just realised you haven't done in any of the other tests in fast/Unit tests - is that ok? It seems a bit dodgy

@maartin0 maartin0 force-pushed the master branch 3 times, most recently from b226e57 to 2519281 Compare February 29, 2024 20:11
@maartin0 maartin0 marked this pull request as ready for review February 29, 2024 20:12
@maartin0
Copy link
Author

maartin0 commented Mar 1, 2024

Just pushed again with a 3 more things:

  • Added -w alias for --save and a test to go with it
  • Fixed a bug in nvm_auto where it tries to resolve the alias default which resolves to N/A which is a non-empty string but invalid version so when it calls nvm use or nvm install it'll exit 3 if set -e is set -- this has got the number of failing fast tests from 7 down to 2
  • Added set -e flags back to the tests I wrote since nvm.sh can be sourced successfully now

@ljharb
Copy link
Member

ljharb commented Mar 1, 2024

@ljharb The issue with the tests was that I was using set -e when sourcing nvm.sh which I only just realised you haven't done in any of the other tests in fast/Unit tests - is that ok? It seems a bit dodgy

yeah, that's fine - sometimes i have to set -e after sourcing nvm.sh.

setting -e isn't even really a good practice anymore, so i'm not too worried about it. good to set it in tests if possible tho :-)

@maartin0
Copy link
Author

Just rebased again if you think this is ready to merge?

@ljharb
Copy link
Member

ljharb commented Mar 16, 2024

Unfortunately the Travis tests are still failing - https://app.travis-ci.com/github/nvm-sh/nvm/jobs/619295015

Fixes nvm-sh#2849.

Co-authored-by: Martin <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
@maartin0
Copy link
Author

@ljharb There was only one issue which was surprisingly simple to fix, but annoying to figure out - I just needed to add a couple of extra bits to the cleanup functions in all the tests, since they were leaving behind installed versions which the other tests didn't expect, which in turn changed the output format of nvm ls-remote to literally just have one blue rather than black line:
nvm ls-remote test expected/actual output
And this is what I needed to change cleanup to (which I stole from nvm_install_no_progress_bar)

cleanup () {
  nvm cache clear
  nvm deactivate
  nvm unalias default
  rm -rf ${NVM_DIR}/v* .nvmrc
  if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
  unset -f nvm_ls_remote nvm_ls_remote_iojs
}

@maartin0 maartin0 marked this pull request as ready for review March 17, 2024 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants