Skip to content

Commit

Permalink
Add more installation tests; Better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kubouch committed Jan 28, 2024
1 parent 7d28528 commit 368294d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
8 changes: 7 additions & 1 deletion nupm/install.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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'
Expand Down
52 changes: 44 additions & 8 deletions tests/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def "assert installed" [path_tokens: list<string>] {
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
Expand Down Expand Up @@ -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)
}
}

Expand Down
12 changes: 1 addition & 11 deletions tests/packages/registry.nuon
Original file line number Diff line number Diff line change
@@ -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]
]
Expand Down
5 changes: 5 additions & 0 deletions tests/packages/spam_script_old/nupm.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
name: spam_script,
type: script,
version: "0.1.0",
}
4 changes: 4 additions & 0 deletions tests/packages/spam_script_old/spam_script.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env nu
def main [] {
"Hello world v0.1.0!"
}

0 comments on commit 368294d

Please sign in to comment.