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 line_ending() -> String {
27 #[cfg(windows)]
28 {
29 String::from("\r\n")
30 }
31
32 #[cfg(not(windows))]
33 {
34 String::from("\n")
35 }
36}
37
38pub fn files_exist_at(files: &[impl AsRef<Path>], path: impl AsRef<AbsolutePath>) -> bool {
39 let path = path.as_ref();
40 files.iter().all(|f| path.join(f.as_ref()).exists())
41}
42
43pub fn executable_path() -> AbsolutePathBuf {
44 let mut path = binaries();
45 path.push("nu");
46 path
47}
48
49pub fn installed_nu_path() -> AbsolutePathBuf {
50 let path = std::env::var_os(crate::NATIVE_PATH_ENV_VAR);
51 if let Ok(path) = which::which_in("nu", path, ".") {
52 AbsolutePathBuf::try_from(path).expect("installed nushell path is absolute")
53 } else {
54 executable_path()
55 }
56}
57
58pub fn root() -> AbsolutePathBuf {
59 let manifest_dir = if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
60 AbsolutePathBuf::try_from(manifest_dir).expect("CARGO_MANIFEST_DIR is not an absolute path")
61 } else {
62 AbsolutePathBuf::try_from(env!("CARGO_MANIFEST_DIR"))
63 .expect("CARGO_MANIFEST_DIR is not an absolute path")
64 };
65
66 let test_path = manifest_dir.join("Cargo.lock");
67 if test_path.exists() {
68 manifest_dir
69 } else {
70 manifest_dir
71 .parent()
72 .expect("Couldn't find the debug binaries directory")
73 .parent()
74 .expect("Couldn't find the debug binaries directory")
75 .into()
76 }
77}
78
79pub fn binaries() -> AbsolutePathBuf {
80 let build_target = std::env::var("CARGO_BUILD_TARGET").unwrap_or_default();
81
82 let profile = if let Ok(env_profile) = std::env::var("NUSHELL_CARGO_PROFILE") {
83 env_profile
84 } else if cfg!(debug_assertions) {
85 "debug".into()
86 } else {
87 "release".into()
88 };
89
90 std::env::var("CARGO_TARGET_DIR")
91 .or_else(|_| std::env::var("CARGO_BUILD_TARGET_DIR"))
92 .ok()
93 .and_then(|p| AbsolutePathBuf::try_from(p).ok())
94 .unwrap_or_else(|| root().join("target"))
95 .join(build_target)
96 .join(profile)
97}
98
99pub fn fixtures() -> AbsolutePathBuf {
100 let mut path = root();
101 path.push("tests");
102 path.push("fixtures");
103 path
104}
105
106pub fn assets() -> AbsolutePathBuf {
107 let mut path = root();
108 path.push("tests");
109 path.push("assets");
110 path
111}
112
113pub fn in_directory(path: impl AsRef<nu_path::Path>) -> AbsolutePathBuf {
114 root().join(path)
115}