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.
This commit is contained in:
Bahex 2024-12-17 19:01:23 +03:00 committed by GitHub
parent d94b344342
commit c266e6adaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 0 deletions

View file

@ -4,6 +4,7 @@ mod exists;
mod expand;
mod join;
mod parse;
mod self_;
mod split;
mod type_;

View file

@ -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"));
}