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

Merged
merged 1 commit into from
Jul 26, 2024
Merged

Conversation

maartin0
Copy link
Contributor

@maartin0 maartin0 commented Aug 29, 2022

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

Fixes #2849

Fixes nvm-sh#2849.

Co-authored-by: Martin <maartin00000@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
@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
Contributor 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
Contributor 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
Contributor 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
Contributor 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
Contributor 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
@maartin0
Copy link
Contributor 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
Contributor 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
Contributor 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

@ljharb ljharb marked this pull request as draft March 16, 2024 15:46
@maartin0 maartin0 force-pushed the master branch 2 times, most recently from 28ebb07 to 4375ca3 Compare March 17, 2024 22:01
@maartin0
Copy link
Contributor 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
@ljharb
Copy link
Member

ljharb commented Jun 10, 2024

@maartin0 sorry for the delay here; can you please rebase this and resolve the conflicts?

@maartin0
Copy link
Contributor Author

@ljharb All done and no worries, although Travis seems to have another issue?

$ git clone --depth=50 https://github.com/nvm-sh/nvm.git nvm-sh/nvm
$ git submodule update --init --recursive --quiet
Warning: Permanently added the ECDSA host key for IP address '140.82.112.4' to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

@ljharb
Copy link
Member

ljharb commented Jun 10, 2024

yep, known and don’t worry about that one :-)

@ljharb ljharb marked this pull request as ready for review June 10, 2024 15:04
@ljharb ljharb force-pushed the master branch 2 times, most recently from c6cfc3a to c20db2a Compare June 10, 2024 18:13
@ljharb ljharb merged commit f439acd into nvm-sh:master Jul 26, 2024
110 checks passed
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.

nvm use should write a .nvmrc file by default
2 participants