Skip to content

Commit

Permalink
stdlib: use the loaded library in tests and update README (nushell#…
Browse files Browse the repository at this point in the history
…8811)

Should close nushell#8809.

# Description
this PR uses the automatically loaded library from the tests by
replacing `use std.nu ...` with `use std ...`.

the `README` has been updated by
- removing the very deprencated "concrete examples"
- fixing the `use std` and the "run the tests" sections

the `README` can be previewed
[here](https://github.com/amtoine/nushell/blob/refactor/stdlib/use-std-in-tests-and-update-readme/crates/nu-std/README.md)
:+1:

# User-Facing Changes
```
$nothing
```

# Tests + Formatting
- :green_circle: `toolkit test stdlib`

# After Submitting
```
$nothing
```
  • Loading branch information
amtoine committed Apr 8, 2023
1 parent 60e6ea5 commit d128c0e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 61 deletions.
46 changes: 4 additions & 42 deletions crates/nu-std/README.md
Expand Up @@ -14,42 +14,9 @@ in order to "import" the standard library to either the interactive [*REPL*][REP
`nushell` or inside some `.nu` script, you might want to use the
[`use`](https://nushell.sh/commands/docs/use.html) command!
```bash
use /path/to/standard_library/std.nu
use std
```

> ### :mag: a concrete example
> - my name is @amtoine and i use the `ghq` tool to manage `git` projects
> > **Note**
> > `ghq` stores any repository inside `$env.GHQ_ROOT` under `<host>/<owner>/<repo>/`
> - the path to my local fork of `nushell` is then defined as
> ```bash
> let-env NUSHELL_REPO = ($env.GHQ_ROOT | path join "github.com" "amtoine" "nushell")
> ```
> - and the full path to the standard library is defined as
> ```bash
> let-env STD_LIB = ($env.NUSHELL_REPO | path join "crates" "nu-utils" "standard_library")
> ```
> > see the content of `$env.STD_LIB` :yum:
> > ```bash
> > >_ ls $env.STD_LIB | get name | str replace $env.STD_LIB "" | str trim -l -c "/"
> > ╭───┬───────────╮
> > │ 0 │ README.md │
> > │ 1 │ std.nu │
> > │ 2 │ tests.nu │
> > ╰───┴───────────╯
> > ```
> - finally we can `use` the standard library and have access to the commands it exposes :thumbsup:
> ```bash
> >_ use std.nu
> >_ help std
> Module: std
>
> Exported commands:
> assert (std assert), assert eq (std assert eq), assert ne (std assert ne), match (std match)
>
> This module does not export environment.
> ```
## :pencil2: contribute to the standard library
- all the commands of the standard_library are located in [`std.nu`](std.nu)
- the tests are located in files that have a name starting with "test_", e.g. [`test_std.nu`](test_std.nu)
Expand All @@ -64,13 +31,8 @@ use /path/to/standard_library/std.nu
### :test_tube: run the tests
the following call should return no errors
```bash
NU_LOG_LEVEL=DEBUG nu /path/to/standard_library/tests.nu
NU_LOG_LEVEL=DEBUG cargo run -- crates/nu-std/tests.nu
```

> #### :mag: a concrete example
> with `STD_LIB` defined as in the example above
> ```bash
> NU_LOG_LEVEL=DEBUG nu ($env.STD_LIB | path join "tests.nu")
> ```
[REPL]: https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop
> **Warning**
> the `cargo run --` part of this command is important to ensure the version of `nushell` and the version of the library are the same.
2 changes: 1 addition & 1 deletion crates/nu-std/test_asserts.nu
@@ -1,4 +1,4 @@
use std.nu *
use std *

export def test_assert [] {
assert true
Expand Down
14 changes: 7 additions & 7 deletions crates/nu-std/test_dirs.nu
@@ -1,5 +1,5 @@
use std.nu "assert length"
use std.nu "assert equal"
use std "assert length"
use std "assert equal"

def clean [path: path] {
cd $path
Expand All @@ -17,11 +17,11 @@ export def test_dirs_command [] {
mkdir $base_path $path_a $path_b

cd $base_path
use std.nu "dirs next"
use std.nu "dirs prev"
use std.nu "dirs add"
use std.nu "dirs drop"
use std.nu "dirs show"
use std "dirs next"
use std "dirs prev"
use std "dirs add"
use std "dirs drop"
use std "dirs show"

assert length $env.DIRS_LIST 1 "list is just pwd after initialization"
assert equal $base_path $env.DIRS_LIST.0 "list is just pwd after initialization"
Expand Down
6 changes: 3 additions & 3 deletions crates/nu-std/test_logger.nu
@@ -1,9 +1,9 @@
use std.nu *
use std *

def run [system_level, message_level] {
cd $env.FILE_PWD
do {
nu -c $'use std.nu; NU_LOG_LEVEL=($system_level) std log ($message_level) "test message"'
nu -c $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) "test message"'
} | complete | get -i stderr
}
def "assert no message" [system_level, message_level] {
Expand Down Expand Up @@ -43,4 +43,4 @@ export def example [] {
log warning "Warning message"
log error "Error message"
log critical "Critical message"
}
}
4 changes: 2 additions & 2 deletions crates/nu-std/test_std.nu
@@ -1,7 +1,7 @@
use std.nu
use std

export def test_path_add [] {
use std.nu "assert equal"
use std "assert equal"

with-env [PATH []] {
assert equal $env.PATH []
Expand Down
10 changes: 5 additions & 5 deletions crates/nu-std/test_xml.nu
@@ -1,7 +1,7 @@
use std.nu "xaccess"
use std.nu "xupdate"
use std.nu "xinsert"
use std.nu "assert equal"
use std "xaccess"
use std "xupdate"
use std "xinsert"
use std "assert equal"

export def test_xml_xaccess [] {
let sample_xml = ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d></a>' | from xml)
Expand All @@ -27,4 +27,4 @@ export def test_xml_xinsert [] {
assert equal ($sample_xml | xinsert [a] {tag: b attributes:{} content: []}) ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d><b></b></a>' | from xml)
assert equal ($sample_xml | xinsert [a d *] {tag: null attributes: null content: 'n'} | to xml) '<a><b><c a="b"></c></b><c></c><d><e>zn</e><e>xn</e></d></a>'
assert equal ($sample_xml | xinsert [a *] {tag: null attributes: null content: 'n'}) ('<a><b><c a="b"></c>n</b><c>n</c><d><e>z</e><e>x</e>n</d></a>' | from xml)
}
}
2 changes: 1 addition & 1 deletion crates/nu-std/tests.nu
@@ -1,4 +1,4 @@
use std.nu *
use std *

# show a test record in a pretty way
#
Expand Down

0 comments on commit d128c0e

Please sign in to comment.