From c266e6adaf63bbd72f16a8ba6df80e6cb0d6e3d1 Mon Sep 17 00:00:00 2001 From: Bahex Date: Tue, 17 Dec 2024 19:01:23 +0300 Subject: [PATCH] test(path self): Add tests (#14607) # Description Add tests for `path self`. I wasn't very familiar with the code base, especially the testing utilities, when I first implemented `path self`. It's been on my mind to add tests for it since then. --- crates/nu-command/tests/commands/path/mod.rs | 1 + .../nu-command/tests/commands/path/self_.rs | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 crates/nu-command/tests/commands/path/self_.rs 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")); +}