diff --git a/crates/nu-command/tests/commands/path/mod.rs b/crates/nu-command/tests/commands/path/mod.rs index d14cbde181..ada11426e8 100644 --- a/crates/nu-command/tests/commands/path/mod.rs +++ b/crates/nu-command/tests/commands/path/mod.rs @@ -4,6 +4,7 @@ mod exists; mod expand; mod join; mod parse; +mod self_; mod split; mod type_; diff --git a/crates/nu-command/tests/commands/path/self_.rs b/crates/nu-command/tests/commands/path/self_.rs new file mode 100644 index 0000000000..b0c47195c8 --- /dev/null +++ b/crates/nu-command/tests/commands/path/self_.rs @@ -0,0 +1,64 @@ +use std::path::Path; + +use itertools::Itertools; +use nu_test_support::{fs::Stub, nu, playground::Playground}; + +#[test] +fn self_path_const() { + Playground::setup("path_self_const", |dirs, sandbox| { + sandbox + .within("scripts") + .with_files(&[Stub::FileWithContentToBeTrimmed( + "foo.nu", + r#" + export const paths = { + self: (path self), + dir: (path self .), + sibling: (path self sibling), + parent_dir: (path self ..), + cousin: (path self ../cousin), + } + "#, + )]); + + let actual = nu!(cwd: dirs.test(), r#"use scripts/foo.nu; $foo.paths | values | str join (char nul)"#); + let (self_, dir, sibling, parent_dir, cousin) = actual + .out + .split("\0") + .collect_tuple() + .expect("should have 5 NUL separated paths"); + + let mut pathbuf = dirs.test().to_path_buf(); + + pathbuf.push("scripts"); + assert_eq!(pathbuf, Path::new(dir)); + + pathbuf.push("foo.nu"); + assert_eq!(pathbuf, Path::new(self_)); + + pathbuf.pop(); + pathbuf.push("sibling"); + assert_eq!(pathbuf, Path::new(sibling)); + + pathbuf.pop(); + pathbuf.pop(); + assert_eq!(pathbuf, Path::new(parent_dir)); + + pathbuf.push("cousin"); + assert_eq!(pathbuf, Path::new(cousin)); + }) +} + +#[test] +fn self_path_runtime() { + let actual = nu!("path self"); + assert!(!actual.status.success()); + assert!(actual.err.contains("can only run during parse-time")); +} + +#[test] +fn self_path_repl() { + let actual = nu!("const foo = path self; $foo"); + assert!(!actual.status.success()); + assert!(actual.err.contains("nu::shell::file_not_found")); +}