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

Add nupm test #19

Merged
merged 38 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
47bffb5
Add main `nupm` command; Start `dirs` module
kubouch Sep 5, 2023
df65a3a
Add some comments; Remove tmp path (unused for now)
kubouch Sep 5, 2023
607d5a3
Use $env.NUPM_HOME directly in `nupm install`
kubouch Sep 5, 2023
0a52295
Remove currently unused dirs creation
kubouch Sep 5, 2023
2a9d939
Fix sentence
kubouch Sep 5, 2023
27889f9
Adjust comment
kubouch Sep 5, 2023
32ecbab
Unspan errors; Simplify env check
kubouch Sep 8, 2023
6be3671
Fix empty env var error
kubouch Sep 8, 2023
beeef3c
Remove print
kubouch Sep 8, 2023
a5ab5ff
Add NUPM_HOME prompt to `nupm install`
kubouch Sep 8, 2023
bd3ce39
Make args more generic
kubouch Sep 8, 2023
5c0167b
Don't require description and license
kubouch Sep 8, 2023
9f6f3e7
Fix module install; Refactor
kubouch Sep 8, 2023
b14e091
Pass package.nuon filename to build.nu; Format
kubouch Sep 12, 2023
26809f8
Add missing default
kubouch Sep 12, 2023
ddfed56
Build custom package in temporary directory
kubouch Sep 12, 2023
b4ffee5
Format command to reduce super-long lines
kubouch Sep 12, 2023
11c5989
Merge branch 'main' into nupm-install
kubouch Sep 12, 2023
2145928
Remove merge leftover line
kubouch Sep 12, 2023
49c4cf5
Add some type annotations
kubouch Sep 18, 2023
983a645
Add newline to error
kubouch Sep 18, 2023
0ae5405
Use implicit return from tmp buid dir
kubouch Sep 18, 2023
f4a5e42
Simplify flag propagation
kubouch Sep 26, 2023
fe71822
Add type annotation
kubouch Sep 26, 2023
deaa426
Start `nupm test` command
kubouch Sep 18, 2023
00e2d35
Allow running `nupm test` from any directory
kubouch Sep 19, 2023
085fc35
Fix nupm install error message
kubouch Sep 19, 2023
cf37eea
Add basic tests for nupm install
kubouch Sep 19, 2023
a05f6dc
Change test prefix to "test-"; Improve test glob
kubouch Sep 22, 2023
ec51c81
Abstract away setting $env.NUPM_HOME in tests
kubouch Sep 22, 2023
b70f59c
Allow showing test stdout; Fix nupm install scripts
kubouch Sep 22, 2023
ae38099
Add output type to nupm test
kubouch Sep 22, 2023
1b6c1b9
Allow filtering tests
kubouch Sep 22, 2023
4843f51
Add test packages
kubouch Sep 22, 2023
ba8c489
Treat tests directory as a module
kubouch Sep 22, 2023
46ab2ad
Merge branch 'main' into nupm-test
kubouch Oct 2, 2023
a963169
Add newlines
kubouch Oct 2, 2023
3325de8
Remove unnecessary file
kubouch Oct 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 12 additions & 29 deletions nupm/install.nu
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
use std log

use utils/dirs.nu [ nupm-home-prompt script-dir module-dir tmp-dir ]

def throw-error [
error: string
text?: string
--span: record<start: int, end: int>
] {
let error = $"(ansi red_bold)($error)(ansi reset)"

if $span == null {
error make --unspanned { msg: $error }
}

error make {
msg: $error
label: {
text: ($text | default "this caused an internal error")
start: $span.start
end: $span.end
}
}
}
use utils/log.nu throw-error

def open-package-file [dir: path] {
let package_file = $dir | path join "package.nuon"
Expand Down Expand Up @@ -64,15 +44,18 @@ def install-scripts [
]: list<path> -> nothing {
each {|script|
let src_path = $pkg_dir | path join $script
let tgt_path = $scripts_dir | path join $script

if ($src_path | path type) != file {
throw-error $"Script ($src_path) does not exist"
}

if ($tgt_path | path type) == file and (not $force) {
throw-error ($"Script ($src_path) is already installed as"
+ $" ($tgt_path). Use `--force` to override the package.")
if (($scripts_dir
| path join ($script | path basename)
| path type) == file
and (not $force)
kubouch marked this conversation as resolved.
Show resolved Hide resolved
) {
throw-error ($"Script ($src_path) is already installed in"
+ $" ($scripts_dir). Use `--force` to override the package.")
}

log debug $"installing script `($src_path)` to `($scripts_dir)`"
Expand Down Expand Up @@ -120,15 +103,15 @@ def install-path [
log debug $"installing scripts for package ($package.name)"

$package.scripts
| install-scripts $pkg_dir (script-dir --ensure) --force $force
| install-scripts $pkg_dir (script-dir --ensure) --force $force
}
},
"script" => {
log debug $"installing scripts for package ($package.name)"

$package.scripts?
| default [ ($pkg_dir | path join $"($package.name).nu") ]
| install-scripts $pkg_dir (script-dir --ensure) --force $force
[ ($pkg_dir | path join $"($package.name).nu") ]
| append ($package.scripts? | default [])
kubouch marked this conversation as resolved.
Show resolved Hide resolved
| install-scripts $pkg_dir (script-dir --ensure) --force $force
},
"custom" => {
let build_file = $pkg_dir | path join "build.nu"
Expand Down
80 changes: 80 additions & 0 deletions nupm/test.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use utils/dirs.nu [ tmp-dir find-root ]
use utils/log.nu throw-error

# Experimental test runner
export def main [
filter?: string = '' # Run only tests containing this substring
--dir: path # Directory where to run tests (default: $env.PWD)
--show-stdout # Show standard output of each test
]: nothing -> nothing {
let dir = ($dir | default $env.PWD | path expand -s)
let pkg_root = find-root $dir

if $pkg_root == null {
throw-error ($'Could not find "package.nuon" in ($dir)'
+ ' or any parent directory.')
}

print $'Testing package ($pkg_root)'
cd $pkg_root

let tests = nu [
--no-config-file
--commands
'use tests/
kubouch marked this conversation as resolved.
Show resolved Hide resolved

scope commands
| where ($it.name | str starts-with tests)
kubouch marked this conversation as resolved.
Show resolved Hide resolved
| get name
| to nuon'
]
| from nuon

let out = $tests
| where ($filter in $it)
| par-each {|test|
let res = do {
nu [
--no-config-file
--commands
$'use tests/; ($test)'
]
}
| complete

if $res.exit_code == 0 {
print $'($test) ... (ansi gb)SUCCESS(ansi reset)'
} else {
print $'($test) ... (ansi rb)FAILURE(ansi reset)'
}

if $show_stdout {
print 'stdout:'
print $res.stdout
}

{
name: $test
stdout: $res.stdout
stderr: $res.stderr
exit_code: $res.exit_code
}
}

let successes = $out | where exit_code == 0
let failures = $out | where exit_code != 0

$failures | each {|fail|
print ($'(char nl)Test "($fail.name)" failed with exit code'
+ $' ($fail.exit_code):(char nl)'
+ ($fail.stderr | str trim))
}

if ($failures | length) != 0 {
print ''
}

print ($'Ran ($out | length) tests.'
+ $' ($successes | length) succeeded,'
+ $' ($failures | length) failed.')
}
21 changes: 21 additions & 0 deletions nupm/utils/dirs.nu
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,24 @@ export def tmp-dir [subdir: string, --ensure]: nothing -> path {

$d
}

# Try to find the package root directory by looking for package.nuon in parent
# directories.
export def find-root [dir: path]: [ nothing -> path, nothing -> nothing] {
kubouch marked this conversation as resolved.
Show resolved Hide resolved
let root_candidate = 1..($dir | path split | length)
| reduce -f $dir {|_, acc|
if ($acc | path join package.nuon | path exists) {
$acc
} else {
$acc | path dirname
}
}

# We need to do the last check in case the reduce loop ran to the end
# without finding package.nuon
if ($root_candidate | path join package.nuon | path type) == 'file' {
$root_candidate
} else {
null
}
}
21 changes: 21 additions & 0 deletions nupm/utils/log.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

export def throw-error [
error: string
text?: string
--span: record<start: int, end: int>
] {
let error = $"(ansi red_bold)($error)(ansi reset)"

if $span == null {
error make --unspanned { msg: $error }
}

error make {
msg: $error
label: {
text: ($text | default "this caused an internal error")
start: $span.start
end: $span.end
}
}
}
7 changes: 7 additions & 0 deletions package.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
name: nupm
type: module
version: "0.1.0"
description: "Nushell package manager"
license: "LICENSE"
}
49 changes: 49 additions & 0 deletions tests/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std assert

use ../nupm/utils/dirs.nu tmp-dir
use ../nupm


def with-nupm-home [closure: closure]: nothing -> nothing {
let dir = tmp-dir test --ensure
with-env { NUPM_HOME: $dir } $closure
rm -r $dir
}

export def install-script [] {
with-nupm-home {
cd tests/packages/spam_script

nupm install --path .
assert ([$env.NUPM_HOME scripts spam_script.nu]
| path join
| path exists)
assert ([$env.NUPM_HOME scripts spam_bar.nu]
| path join
| path exists)
}
}

export def install-module [] {
with-nupm-home {
cd tests/packages/spam_module

nupm install --path .
assert ([$env.NUPM_HOME scripts script.nu] | path join | path exists)
assert ([$env.NUPM_HOME modules spam_module] | path join | path exists)
assert ([$env.NUPM_HOME modules spam_module mod.nu]
| path join
| path exists)
}
}

export def install-custom [] {
with-nupm-home {
cd tests/packages/spam_custom

nupm install --path .
assert ([$env.NUPM_HOME plugins nu_plugin_test]
| path join
| path exists)
}
}
6 changes: 6 additions & 0 deletions tests/packages/spam_custom/build.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main [package_file: path] {
let package = open $package_file
print $"Installing ($package.name) to ($env.NUPM_HOME) inside ($env.PWD)"
mkdir ($env.NUPM_HOME | path join plugins)
touch ($env.NUPM_HOME | path join plugins nu_plugin_test)
}
5 changes: 5 additions & 0 deletions tests/packages/spam_custom/package.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
name: spam_custom,
type: custom,
version: "0.1.0"
}
6 changes: 6 additions & 0 deletions tests/packages/spam_module/package.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
name: spam_module,
type: module,
version: "0.1.0"
scripts: script.nu
}
1 change: 1 addition & 0 deletions tests/packages/spam_module/script.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print 'scriptscript'
3 changes: 3 additions & 0 deletions tests/packages/spam_module/spam_module/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export def main [] {
"Hello world!"
}
6 changes: 6 additions & 0 deletions tests/packages/spam_script/package.nuon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
name: spam_script,
type: script,
version: "0.1.0",
scripts: ['spam_bar.nu']
}
3 changes: 3 additions & 0 deletions tests/packages/spam_script/spam_bar.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export def main [] {
'Hello bar!'
}
4 changes: 4 additions & 0 deletions tests/packages/spam_script/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!"
}