Export top-level commands from std; don't import some modules by default

This commit is contained in:
NotTheDr01ds 2024-09-23 19:23:44 -04:00
parent 2998cc8299
commit 0d8d959e8f
34 changed files with 114 additions and 236 deletions

View File

@ -18,23 +18,20 @@ pub fn load_standard_library(
let mut std_files = vec![
("mod.nu", include_str!("../std/mod.nu")),
("core", include_str!("../std/core/mod.nu")),
("assert", include_str!("../std/assert/mod.nu")),
("bench", include_str!("../std/bench/mod.nu")),
("dirs", include_str!("../std/dirs/mod.nu")),
(
"deprecated_dirs",
include_str!("../std/deprecated_dirs/mod.nu"),
),
("dt", include_str!("../std/dt/mod.nu")),
("formats", include_str!("../std/formats/mod.nu")),
("help", include_str!("../std/help/mod.nu")),
("input", include_str!("../std/input/mod.nu")),
("iter", include_str!("../std/iter/mod.nu")),
("log", include_str!("../std/log/mod.nu")),
("math", include_str!("../std/math/mod.nu")),
("util", include_str!("../std/util/mod.nu")),
("xml", include_str!("../std/xml/mod.nu")),
("core", include_str!("../std/core.nu")),
("assert", include_str!("../std/assert.nu")),
("bench", include_str!("../std/bench.nu")),
("dirs", include_str!("../std/dirs.nu")),
("deprecated_dirs", include_str!("../std/deprecated_dirs.nu")),
("dt", include_str!("../std/dt.nu")),
("formats", include_str!("../std/formats.nu")),
("help", include_str!("../std/help.nu")),
("input", include_str!("../std/input.nu")),
("iter", include_str!("../std/iter.nu")),
("log", include_str!("../std/log.nu")),
("math", include_str!("../std/math.nu")),
("lib", include_str!("../std/lib.nu")),
("xml", include_str!("../std/xml.nu")),
];
let mut working_set = StateWorkingSet::new(engine_state);

View File

@ -1,161 +1,18 @@
# std.nu, `used` to load all standard library components
export module core
export module bench
export module assert
export module dirs
export module dt
export module formats
export module help
#export module assert
#export module bench
#export module dirs
#export module dt
#export module formats
#export module help
export module input
export module iter
export module log
export module math
export module util
export module xml
export-env {
use dirs []
use log []
}
#export module log
#export module math
#export module xml
def deprecation_warning [
cmd_name: string
] {
print -e $"
(ansi red)Warning:(ansi reset) '($cmd_name)' is being moved from the 'std' library to
'std/util'. To remove this warning, import it using:
use std/util ($cmd_name)
"
}
# Add the given paths to the PATH.
#
# # Example
# - adding some dummy paths to an empty PATH
# ```nushell
# >_ with-env { PATH: [] } {
# std path add "foo"
# std path add "bar" "baz"
# std path add "fooo" --append
#
# assert equal $env.PATH ["bar" "baz" "foo" "fooo"]
#
# print (std path add "returned" --ret)
# }
# ╭───┬──────────╮
# │ 0 │ returned │
# │ 1 │ bar │
# │ 2 │ baz │
# │ 3 │ foo │
# │ 4 │ fooo │
# ╰───┴──────────╯
# ```
# - adding paths based on the operating system
# ```nushell
# >_ std path add {linux: "foo", windows: "bar", darwin: "baz"}
# ```
export def --env "path add" [
--ret (-r) # return $env.PATH, useful in pipelines to avoid scoping.
--append (-a) # append to $env.PATH instead of prepending to.
...paths # the paths to add to $env.PATH.
] {
deprecation_warning "path add"
let span = (metadata $paths).span
let paths = $paths | flatten
if ($paths | is-empty) or ($paths | length) == 0 {
error make {msg: "Empty input", label: {
text: "Provide at least one string or a record",
span: $span
}}
}
let path_name = if "PATH" in $env { "PATH" } else { "Path" }
let paths = $paths | each {|p|
let p = match ($p | describe | str replace --regex '<.*' '') {
"string" => $p,
"record" => { $p | get --ignore-errors $nu.os-info.name },
}
$p | path expand --no-symlink
}
if null in $paths or ($paths | is-empty) {
error make {msg: "Empty input", label: {
text: $"Received a record, that does not contain a ($nu.os-info.name) key",
span: $span
}}
}
load-env {$path_name: (
$env
| get $path_name
| split row (char esep)
| if $append { append $paths } else { prepend $paths }
)}
if $ret {
$env | get $path_name
}
}
# the cute and friendly mascot of Nushell :)
export def ellie [] {
deprecation_warning ellie
let ellie = [
" __ ,",
" .--()°'.'",
"'|, . ,'",
" !_-(_\\",
]
$ellie | str join "\n" | $"(ansi green)($in)(ansi reset)"
}
# repeat anything a bunch of times, yielding a list of *n* times the input
#
# # Examples
# repeat a string
# > "foo" | std repeat 3 | str join
# "foofoofoo"
export def repeat [
n: int # the number of repetitions, must be positive
]: any -> list<any> {
deprecation_warning repeat
let item = $in
if $n < 0 {
let span = metadata $n | get span
error make {
msg: $"(ansi red_bold)invalid_argument(ansi reset)"
label: {
text: $"n should be a positive integer, found ($n)"
span: $span
}
}
}
if $n == 0 {
return []
}
1..$n | each { $item }
}
# return a null device file.
#
# # Examples
# run a command and ignore it's stderr output
# > cat xxx.txt e> (null-device)
export def null-device []: nothing -> path {
deprecation_warning "null-device"
if ($nu.os-info.name | str downcase) == "windows" {
'\\.\NUL'
} else {
"/dev/null"
}
}
# Make commands available in the top-level module
export use lib *
export use formats *
export use dt *

View File

@ -1,4 +1,4 @@
use std log
use std/log
def "nu-complete threads" [] {
seq 1 (sys cpu | length)
@ -287,8 +287,6 @@ export def run-tests [
--list, # list the selected tests without running them.
--threads: int@"nu-complete threads", # Amount of threads to use for parallel execution. Default: All threads are utilized
] {
use std log
let available_threads = (sys cpu | length)
# Can't use pattern matching here due to https://github.com/nushell/nushell/issues/9198

View File

@ -1,4 +1,5 @@
use std *
use std/assert
def run [
system_level,
@ -6,9 +7,9 @@ def run [
--short
] {
if $short {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --short "test message"'
^$nu.current-exe --no-config-file --commands $'use std; use std/log; NU_LOG_LEVEL=($system_level) log ($message_level) --short "test message"'
} else {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) "test message"'
^$nu.current-exe --no-config-file --commands $'use std; use std/log; NU_LOG_LEVEL=($system_level) log ($message_level) "test message"'
}
| complete | get --ignore-errors stderr
}

View File

@ -1,5 +1,4 @@
use std *
use std log *
use std/assert
use commons.nu *
def run-command [
@ -12,12 +11,12 @@ def run-command [
] {
if ($level_prefix | is-empty) {
if ($ansi | is-empty) {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "($message)" "($format)" ($log_level)'
^$nu.current-exe --no-config-file --commands $'use std/log; NU_LOG_LEVEL=($system_level) log custom "($message)" "($format)" ($log_level)'
} else {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "($message)" "($format)" ($log_level) --ansi "($ansi)"'
^$nu.current-exe --no-config-file --commands $'use std/log; NU_LOG_LEVEL=($system_level) log custom "($message)" "($format)" ($log_level) --ansi "($ansi)"'
}
} else {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log custom "($message)" "($format)" ($log_level) --level-prefix "($level_prefix)" --ansi "($ansi)"'
^$nu.current-exe --no-config-file --commands $'use std/log; NU_LOG_LEVEL=($system_level) log custom "($message)" "($format)" ($log_level) --level-prefix "($level_prefix)" --ansi "($ansi)"'
}
| complete | get --ignore-errors stderr
}
@ -31,6 +30,7 @@ def errors_during_deduction [] {
#[test]
def valid_calls [] {
use std/log *
assert equal (run-command "DEBUG" "msg" "%MSG%" 25 --level-prefix "abc" --ansi (ansi default) | str trim --right) "msg"
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"((log-prefix).INFO) msg"
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" --level-prefix "abc" 20 | str trim --right) "abc msg"
@ -39,6 +39,7 @@ def valid_calls [] {
#[test]
def log-level_handling [] {
use std/log *
assert equal (run-command "DEBUG" "msg" "%LEVEL% %MSG%" 20 | str trim --right) $"((log-prefix).INFO) msg"
assert equal (run-command "WARNING" "msg" "%LEVEL% %MSG%" 20 | str trim --right) ""
}

View File

@ -1,5 +1,6 @@
use std *
use std log *
use std/log *
use std/assert
use commons.nu *
def run-command [
@ -10,9 +11,9 @@ def run-command [
--short
] {
if $short {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --format "($format)" --short "($message)"'
^$nu.current-exe --no-config-file --commands $'use std; use std/log; NU_LOG_LEVEL=($system_level) log ($message_level) --format "($format)" --short "($message)"'
} else {
^$nu.current-exe --no-config-file --commands $'use std; NU_LOG_LEVEL=($system_level) std log ($message_level) --format "($format)" "($message)"'
^$nu.current-exe --no-config-file --commands $'use std; use std/log; NU_LOG_LEVEL=($system_level) log ($message_level) --format "($format)" "($message)"'
}
| complete | get --ignore-errors stderr
}

View File

@ -1,5 +1,6 @@
use std *
use std log *
use std/assert
use std/log *
#[test]
def env_log-ansi [] {

View File

@ -1,4 +1,5 @@
use std *
use std/assert
#[test]
def assert_basic [] {

View File

@ -1,7 +1,7 @@
use std assert
use std/assert
#[test]
def banner [] {
use std core
use std/core
assert ((core banner | lines | length) == 15)
}

View File

@ -1,5 +1,5 @@
use std assert
use std log
use std/assert
use std/log
# A couple of nuances to understand when testing module that exports environment:
# Each 'use' for that module in the test script will execute the def --env block.
@ -47,7 +47,7 @@ def dirs_command [] {
# must execute these uses for the UOT commands *after* the test and *not* just put them at top of test module.
# the def --env gets messed up
use std dirs
use std/dirs
# Stack: [BASE]
assert equal [$c.base_path] $env.DIRS_LIST "list is just pwd after initialization"
@ -95,7 +95,7 @@ def dirs_next [] {
cd $c.base_path
assert equal $env.PWD $c.base_path "test setup"
use std dirs
use std/dirs
cur_dir_check $c.base_path "use module test setup"
dirs add $c.path_a $c.path_b
@ -116,7 +116,7 @@ def dirs_cd [] {
# must set PWD *before* doing `use` that will run the def --env block in dirs module.
cd $c.base_path
use std dirs
use std/dirs
cur_dir_check $c.base_path "use module test setup"
@ -138,7 +138,7 @@ def dirs_cd [] {
def dirs_goto_bug10696 [] {
let $c = $in
cd $c.base_path
use std dirs
use std/dirs
dirs add $c.path_a
cd $c.path_b
@ -152,7 +152,7 @@ def dirs_goto_bug10696 [] {
def dirs_goto [] {
let $c = $in
cd $c.base_path
use std dirs
use std/dirs
# check that goto can move *from* any position in the ring *to* any other position (correctly)

View File

@ -1,5 +1,5 @@
use std assert
use std dt *
use std/assert
use std/dt *
#[test]
def equal_times [] {

View File

@ -1,4 +1,4 @@
use std assert
use std/assert
def test_data_multiline [] {
let lines = [
@ -19,7 +19,7 @@ def test_data_multiline [] {
#[test]
def from_ndjson_multiple_objects [] {
use std formats *
use std/formats *
let result = test_data_multiline | from ndjson
let expect = [{a:1},{a:2},{a:3},{a:4},{a:5},{a:6}]
assert equal $result $expect "could not convert from NDJSON"
@ -27,7 +27,7 @@ def from_ndjson_multiple_objects [] {
#[test]
def from_ndjson_single_object [] {
use std formats *
use std/formats *
let result = '{"a": 1}' | from ndjson
let expect = [{a:1}]
assert equal $result $expect "could not convert from NDJSON"
@ -35,13 +35,13 @@ def from_ndjson_single_object [] {
#[test]
def from_ndjson_invalid_object [] {
use std formats *
use std/formats *
assert error { '{"a":1' | from ndjson }
}
#[test]
def from_jsonl_multiple_objects [] {
use std formats *
use std/formats *
let result = test_data_multiline | from jsonl
let expect = [{a:1},{a:2},{a:3},{a:4},{a:5},{a:6}]
assert equal $result $expect "could not convert from JSONL"
@ -49,7 +49,7 @@ def from_jsonl_multiple_objects [] {
#[test]
def from_jsonl_single_object [] {
use std formats *
use std/formats *
let result = '{"a": 1}' | from jsonl
let expect = [{a:1}]
assert equal $result $expect "could not convert from JSONL"
@ -57,13 +57,13 @@ def from_jsonl_single_object [] {
#[test]
def from_jsonl_invalid_object [] {
use std formats *
use std/formats *
assert error { '{"a":1' | from jsonl }
}
#[test]
def to_ndjson_multiple_objects [] {
use std formats *
use std/formats *
let result = [{a:1},{a:2},{a:3},{a:4},{a:5},{a:6}] | to ndjson | str trim
let expect = test_data_multiline
assert equal $result $expect "could not convert to NDJSON"
@ -71,7 +71,7 @@ def to_ndjson_multiple_objects [] {
#[test]
def to_ndjson_single_object [] {
use std formats *
use std/formats *
let result = [{a:1}] | to ndjson | str trim
let expect = "{\"a\":1}"
assert equal $result $expect "could not convert to NDJSON"
@ -79,7 +79,7 @@ def to_ndjson_single_object [] {
#[test]
def to_jsonl_multiple_objects [] {
use std formats *
use std/formats *
let result = [{a:1},{a:2},{a:3},{a:4},{a:5},{a:6}] | to jsonl | str trim
let expect = test_data_multiline
assert equal $result $expect "could not convert to JSONL"
@ -87,7 +87,7 @@ def to_jsonl_multiple_objects [] {
#[test]
def to_jsonl_single_object [] {
use std formats *
use std/formats *
let result = [{a:1}] | to jsonl | str trim
let expect = "{\"a\":1}"
assert equal $result $expect "could not convert to JSONL"

View File

@ -1,5 +1,5 @@
use std assert
use std help
use std/assert
use std/help
#[test]
def show_help_on_commands [] {

View File

@ -1,4 +1,5 @@
use std *
use std/assert
#[test]
def iter_find [] {

View File

@ -1,8 +1,8 @@
use std util
use std/lib
#[test]
def path_add [] {
use std assert
use std/assert
let path_name = if "PATH" in $env { "PATH" } else { "Path" }
@ -11,19 +11,19 @@ def path_add [] {
assert equal (get_path) []
util path add "/foo/"
lib path add "/foo/"
assert equal (get_path) (["/foo/"] | path expand)
util path add "/bar/" "/baz/"
lib path add "/bar/" "/baz/"
assert equal (get_path) (["/bar/", "/baz/", "/foo/"] | path expand)
load-env {$path_name: []}
util path add "foo"
util path add "bar" "baz" --append
lib path add "foo"
lib path add "bar" "baz" --append
assert equal (get_path) (["foo", "bar", "baz"] | path expand)
assert equal (util path add "fooooo" --ret) (["fooooo", "foo", "bar", "baz"] | path expand)
assert equal (lib path add "fooooo" --ret) (["fooooo", "foo", "bar", "baz"] | path expand)
assert equal (get_path) (["fooooo", "foo", "bar", "baz"] | path expand)
load-env {$path_name: []}
@ -35,18 +35,18 @@ def path_add [] {
android: "quux",
}
util path add $target_paths
lib path add $target_paths
assert equal (get_path) ([($target_paths | get $nu.os-info.name)] | path expand)
load-env {$path_name: [$"(["/foo", "/bar"] | path expand | str join (char esep))"]}
util path add "~/foo"
lib path add "~/foo"
assert equal (get_path) (["~/foo", "/foo", "/bar"] | path expand)
}
}
#[test]
def path_add_expand [] {
use std assert
use std/assert
# random paths to avoid collision, especially if left dangling on failure
let real_dir = $nu.temp-path | path join $"real-dir-(random chars)"
@ -63,7 +63,7 @@ def path_add_expand [] {
with-env {$path_name: []} {
def get_path [] { $env | get $path_name }
util path add $link_dir
lib path add $link_dir
assert equal (get_path) ([$link_dir])
}
@ -72,12 +72,12 @@ def path_add_expand [] {
#[test]
def repeat_things [] {
use std assert
assert error { "foo" | util repeat -1 }
use std/assert
assert error { "foo" | lib repeat -1 }
for x in ["foo", [1 2], {a: 1}] {
assert equal ($x | util repeat 0) []
assert equal ($x | util repeat 1) [$x]
assert equal ($x | util repeat 2) [$x $x]
assert equal ($x | lib repeat 0) []
assert equal ($x | lib repeat 1) [$x]
assert equal ($x | lib repeat 2) [$x $x]
}
}

View File

@ -1,5 +1,5 @@
use std log
use std assert
use std/log
use std/assert
#[before-each]
def before-each [] {

View File

@ -0,0 +1,22 @@
use std/assert
#[test]
def std_pre_import [] {
# These commands shouldn't exist without an import
assert length (scope commands | where name == "path add") 0
assert length (scope commands | where name == "ellie") 0
assert length (scope commands | where name == "repeat") 0
assert length (scope commands | where name == "from jsonl") 0
assert length (scope commands | where name == "datetime-diff") 0
}
def std_post_import [] {
# After importing std, these commands should be
# available at the top level namespace
use std *
assert length (scope commands | where name == "path add") 1
assert length (scope commands | where name == "ellie") 1
assert length (scope commands | where name == "repeat") 1
assert length (scope commands | where name == "from jsonl") 1
assert length (scope commands | where name == "datetime-diff") 1
}

View File

@ -1,7 +1,5 @@
use std xml xaccess
use std xml xupdate
use std xml xinsert
use std assert
use std/xml *
use std/assert
#[before-each]
def before-each [] {

View File

@ -7,5 +7,5 @@ fn not_loaded() -> TestResult {
#[test]
fn use_command() -> TestResult {
run_test_std("use std assert; assert true; print 'it works'", "it works")
run_test_std("use std/assert; assert true; print 'it works'", "it works")
}

View File

@ -224,10 +224,10 @@ fn std_log_env_vars_are_not_overridden() {
("NU_LOG_DATE_FORMAT".to_string(), "%Y".to_string()),
],
r#"
use std
use std/log
print -e $env.NU_LOG_FORMAT
print -e $env.NU_LOG_DATE_FORMAT
std log error "err"
log error "err"
"#
);
assert_eq!(actual.err, "%MSG%\n%Y\nerr\n");