Skip to main content

nu_test_support/
fs.rs

1use nu_path::{AbsolutePath, AbsolutePathBuf, Path};
2use std::io::Read;
3
4pub enum Stub<'a> {
5    FileWithContent(&'a str, &'a str),
6    FileWithContentToBeTrimmed(&'a str, &'a str),
7    EmptyFile(&'a str),
8    FileWithPermission(&'a str, bool),
9}
10
11pub fn file_contents(full_path: impl AsRef<AbsolutePath>) -> String {
12    let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file");
13    let mut contents = String::new();
14    file.read_to_string(&mut contents)
15        .expect("can not read file");
16    contents
17}
18
19pub fn file_contents_binary(full_path: impl AsRef<AbsolutePath>) -> Vec<u8> {
20    let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file");
21    let mut contents = Vec::new();
22    file.read_to_end(&mut contents).expect("can not read file");
23    contents
24}
25
26pub fn files_exist_at(files: &[impl AsRef<Path>], path: impl AsRef<AbsolutePath>) -> bool {
27    let path = path.as_ref();
28    files.iter().all(|f| path.join(f.as_ref()).exists())
29}
30
31pub fn executable_path() -> AbsolutePathBuf {
32    let mut path = binaries();
33    path.push("nu");
34    path
35}
36
37pub fn installed_nu_path() -> AbsolutePathBuf {
38    let path = std::env::var_os(nu_utils::consts::NATIVE_PATH_ENV_VAR);
39    if let Ok(path) = which::which_in("nu", path, ".") {
40        AbsolutePathBuf::try_from(path).expect("installed nushell path is absolute")
41    } else {
42        executable_path()
43    }
44}
45
46pub fn root() -> AbsolutePathBuf {
47    let manifest_dir = if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
48        AbsolutePathBuf::try_from(manifest_dir).expect("CARGO_MANIFEST_DIR is not an absolute path")
49    } else {
50        AbsolutePathBuf::try_from(env!("CARGO_MANIFEST_DIR"))
51            .expect("CARGO_MANIFEST_DIR is not an absolute path")
52    };
53
54    let test_path = manifest_dir.join("Cargo.lock");
55    if test_path.exists() {
56        manifest_dir
57    } else {
58        manifest_dir
59            .parent()
60            .expect("Couldn't find the debug binaries directory")
61            .parent()
62            .expect("Couldn't find the debug binaries directory")
63            .into()
64    }
65}
66
67pub fn binaries() -> AbsolutePathBuf {
68    let build_target = std::env::var("CARGO_BUILD_TARGET").unwrap_or_default();
69
70    let profile = if let Ok(env_profile) = std::env::var("NUSHELL_CARGO_PROFILE") {
71        env_profile
72    } else if cfg!(debug_assertions) {
73        "debug".into()
74    } else {
75        "release".into()
76    };
77
78    std::env::var("CARGO_TARGET_DIR")
79        .or_else(|_| std::env::var("CARGO_BUILD_TARGET_DIR"))
80        .ok()
81        .and_then(|p| AbsolutePathBuf::try_from(p).ok())
82        .unwrap_or_else(|| root().join("target"))
83        .join(build_target)
84        .join(profile)
85}
86
87pub fn fixtures() -> AbsolutePathBuf {
88    let mut path = root();
89    path.push("tests");
90    path.push("fixtures");
91    path
92}
93
94pub fn assets() -> AbsolutePathBuf {
95    let mut path = root();
96    path.push("tests");
97    path.push("assets");
98    path
99}
100
101pub fn in_directory(path: impl AsRef<nu_path::Path>) -> AbsolutePathBuf {
102    root().join(path)
103}