diff --git a/nupm/install.nu b/nupm/install.nu index 42912eb..e20d7a4 100644 --- a/nupm/install.nu +++ b/nupm/install.nu @@ -8,6 +8,12 @@ use utils/registry.nu search-package use utils/version.nu filter-by-version def open-package-file [dir: path] { + if not ($dir | path exists) { + throw-error "package_dir_does_not_exist" ( + $"Package directory ($dir) does not exist" + ) + } + let package_file = $dir | path join "nupm.nuon" if not ($package_file | path exists) { @@ -219,7 +225,7 @@ def fetch-package [ --exact-match) if ($regs | is-empty) { - throw-error 'No registries found' + throw-error $'Package ($package) not found in any registry' } else if ($regs | length) > 1 { # TODO: Here could be interactive prompt throw-error 'Multiple registries contain the same package' diff --git a/tests/mod.nu b/tests/mod.nu index a342bae..a5c8046 100644 --- a/tests/mod.nu +++ b/tests/mod.nu @@ -31,6 +31,12 @@ def "assert installed" [path_tokens: list] { assert ($path_tokens | prepend $env.NUPM_HOME | path join | path exists) } +def check-file-content [content: string] { + let file_str = open ($env.NUPM_HOME | path join scripts spam_script.nu) + assert ($file_str | str contains $content) +} + + export def install-script [] { with-test-env { nupm install --path tests/packages/spam_script @@ -59,25 +65,55 @@ export def install-custom [] { } export def install-from-local-registry [] { - def check-file [] { - let contents = open ($env.NUPM_HOME | path join scripts spam_script.nu) - assert ($contents | str contains '0.2.0') - } - with-test-env { $env.NUPM_REGISTRIES = {} nupm install --registry $TEST_REGISTRY_PATH spam_script - check-file + check-file-content 0.2.0 } with-test-env { nupm install --registry test spam_script - check-file + check-file-content 0.2.0 } with-test-env { nupm install spam_script - check-file + check-file-content 0.2.0 + } +} + +export def install-with-version [] { + with-test-env { + nupm install spam_script -v 0.1.0 + check-file-content 0.1.0 + } +} + +export def install-multiple-registries-fail [] { + with-test-env { + $env.NUPM_REGISTRIES.test2 = $TEST_REGISTRY_PATH + + let out = try { + nupm install spam_script + "wrong value that shouldn't match the assert below" + } catch {|err| + $err.msg + } + + assert ("Multiple registries contain the same package" in $out) + } +} + +export def install-package-not-found [] { + with-test-env { + let out = try { + nupm install invalid-package + "wrong value that shouldn't match the assert below" + } catch {|err| + $err.msg + } + + assert ("Package invalid-package not found in any registry" in $out) } } diff --git a/tests/packages/registry.nuon b/tests/packages/registry.nuon index 1e08a89..5eed899 100644 --- a/tests/packages/registry.nuon +++ b/tests/packages/registry.nuon @@ -1,19 +1,9 @@ # Testing registry for testing packages { - git: [ - [name version url revision path]; - [ - spam_script - 0.1.0 - https://github.com/nushell/nupm.git - e46e2a93adacf1b8daec83882dc0aa9437a0a7b4 - tests/packages/spam_script - ] - ] - local: [ [name version path]; [spam_script 0.2.0 spam_script] + [spam_script 0.1.0 spam_script_old] [spam_custom 0.1.0 spam_custom] [spam_module 0.1.0 spam_module] ] diff --git a/tests/packages/spam_script_old/nupm.nuon b/tests/packages/spam_script_old/nupm.nuon new file mode 100644 index 0000000..1612782 --- /dev/null +++ b/tests/packages/spam_script_old/nupm.nuon @@ -0,0 +1,5 @@ +{ + name: spam_script, + type: script, + version: "0.1.0", +} diff --git a/tests/packages/spam_script_old/spam_script.nu b/tests/packages/spam_script_old/spam_script.nu new file mode 100755 index 0000000..3f37512 --- /dev/null +++ b/tests/packages/spam_script_old/spam_script.nu @@ -0,0 +1,4 @@ +#!/usr/bin/env nu +def main [] { + "Hello world v0.1.0!" +}