Browse Source

uninstall allow prefix (fix #121) (#191)

* If no version is matching => we print out a message (like we used to)
* If only one version is matching (because the user provided a specific version `6.17.1` or because the prefix match only one installed version) => the version get uninstalled
* If multiple installed versions are matched: we print out the matching installed versions and ask the user to re-run the command with the correct version to uninstall.

Note: it's not possible to uninstall using `latest` or `latest-v9.x`, ... But that's already the case so nothing has changed here.
remotes/origin/add-simple-redirecting-site
Corentin Leruth 5 years ago committed by GitHub
parent
commit
2799222b42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      executable/Uninstall.re
  2. 1
      test/TestFnm.re
  3. 54
      test/TestUninstall.re

40
executable/Uninstall.re

@ -2,14 +2,20 @@ open Fnm; @@ -2,14 +2,20 @@ open Fnm;
open Lwt.Infix;
let run = (~version) => {
let version = Versions.format(version);
let%lwt installedVersions = Versions.getInstalledVersions();
let%lwt installedVersion =
Versions.getInstalledVersions()
>|= List.find_opt(x => Versions.Local.(x.name == version));
let formattedVersionName = Versions.format(version);
let matchingLocalVersions =
installedVersions
|> Versions.(
List.filter(v =>
isVersionFitsPrefix(formattedVersionName, Local.(v.name))
|| v.name == formattedVersionName
)
);
switch (installedVersion) {
| None =>
switch (matchingLocalVersions) {
| [] =>
Logger.error(
<Pastel>
"The version "
@ -18,7 +24,7 @@ let run = (~version) => { @@ -18,7 +24,7 @@ let run = (~version) => {
</Pastel>,
);
Lwt.return_error(1);
| Some(installedVersion) =>
| [installedVersion] =>
Logger.debug(
<Pastel>
"Uninstalling node "
@ -38,5 +44,25 @@ let run = (~version) => { @@ -38,5 +44,25 @@ let run = (~version) => {
</Pastel>,
);
Lwt.return_ok();
| _ =>
Logger.info(
<Pastel>
"There are multiple versions matching your criterias:"
</Pastel>,
);
matchingLocalVersions
|> List.iter(matchingVersion =>
Logger.info(
<Pastel color=Pastel.Cyan>
Versions.Local.(matchingVersion.name)
</Pastel>,
)
);
Logger.info(
<Pastel color=Pastel.Red>
"\nPlease run the command again with the correct version."
</Pastel>,
);
Lwt.return_error(1);
};
};

1
test/TestFnm.re

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
include SmokeTest;
include TestSemver;
include TestFs;
include TestUninstall;
include TestListRemote;
TestFramework.cli();

54
test/TestUninstall.re

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
open TestFramework;
let installVersion = version => run([|"install", version|]);
let uninstallVersion = version => run([|"uninstall", version|]);
let isVersionInstalled = version =>
run([|"ls"|])
|> String.split_on_char('\n')
|> List.exists(v => v == "* v" ++ version);
describe("Uninstall", ({test}) => {
test("Should be possible to uninstall a specific version", ({expect, _}) => {
let version = "6.0.0";
let _ = installVersion(version);
let response = uninstallVersion(version);
expect.string(response).toMatch(
".*v" ++ version ++ ".* has correctly been removed.*",
);
expect.bool(isVersionInstalled(version)).toBeFalse();
});
test(
"Should print out a message if multiple versions match the prefix",
({expect, _}) => {
let v1 = "6.10.0";
let v2 = "6.11.0";
let _ = installVersion(v1);
let _ = installVersion(v2);
let response =
uninstallVersion("6")
|> String.split_on_char('\n')
|> String.concat(" ");
expect.string(response).toMatch(
".*multiple versions.*" ++ v1 ++ ".*" ++ v2 ++ ".*",
);
expect.bool(isVersionInstalled(v1)).toBeTrue();
expect.bool(isVersionInstalled(v2)).toBeTrue();
clearTmpDir();
});
test(
"Should be able to uninstall with a prefix if only one version match",
({expect, _}) => {
let v1 = "6.10.0";
let v2 = "6.11.0";
let _ = installVersion(v1);
let _ = installVersion(v2);
let response = uninstallVersion("6.10");
expect.string(response).toMatch(
".*v" ++ v1 ++ ".* has correctly been removed.*",
);
expect.bool(isVersionInstalled(v1)).toBeFalse();
expect.bool(isVersionInstalled(v2)).toBeTrue();
clearTmpDir();
});
});
Loading…
Cancel
Save