2024-07-12 04:43:10 +02:00
|
|
|
use nu_path::{AbsolutePath, AbsolutePathBuf, Path};
|
2019-12-15 17:15:06 +01:00
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
pub enum Stub<'a> {
|
|
|
|
FileWithContent(&'a str, &'a str),
|
|
|
|
FileWithContentToBeTrimmed(&'a str, &'a str),
|
|
|
|
EmptyFile(&'a str),
|
2023-02-01 21:48:21 +01:00
|
|
|
FileWithPermission(&'a str, bool),
|
2019-12-15 17:15:06 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn file_contents(full_path: impl AsRef<AbsolutePath>) -> String {
|
2019-12-15 17:15:06 +01:00
|
|
|
let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file");
|
|
|
|
let mut contents = String::new();
|
|
|
|
file.read_to_string(&mut contents)
|
|
|
|
.expect("can not read file");
|
|
|
|
contents
|
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn file_contents_binary(full_path: impl AsRef<AbsolutePath>) -> Vec<u8> {
|
2019-12-15 17:15:06 +01:00
|
|
|
let mut file = std::fs::File::open(full_path.as_ref()).expect("can not open file");
|
|
|
|
let mut contents = Vec::new();
|
|
|
|
file.read_to_end(&mut contents).expect("can not read file");
|
|
|
|
contents
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn line_ending() -> String {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
String::from("\r\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
{
|
|
|
|
String::from("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 10:09:13 +02:00
|
|
|
pub fn files_exist_at(files: &[impl AsRef<Path>], path: impl AsRef<AbsolutePath>) -> bool {
|
2024-07-12 04:43:10 +02:00
|
|
|
let path = path.as_ref();
|
|
|
|
files.iter().all(|f| path.join(f.as_ref()).exists())
|
2019-12-15 17:15:06 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn executable_path() -> AbsolutePathBuf {
|
2020-01-12 22:44:22 +01:00
|
|
|
let mut path = binaries();
|
|
|
|
path.push("nu");
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn installed_nu_path() -> AbsolutePathBuf {
|
2023-02-13 13:42:08 +01:00
|
|
|
let path = std::env::var_os(crate::NATIVE_PATH_ENV_VAR);
|
2024-07-12 04:43:10 +02:00
|
|
|
if let Ok(path) = which::which_in("nu", path, ".") {
|
|
|
|
AbsolutePathBuf::try_from(path).expect("installed nushell path is absolute")
|
|
|
|
} else {
|
|
|
|
executable_path()
|
|
|
|
}
|
2023-02-13 13:42:08 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn root() -> AbsolutePathBuf {
|
2020-03-04 19:58:20 +01:00
|
|
|
let manifest_dir = if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
|
2024-07-12 04:43:10 +02:00
|
|
|
AbsolutePathBuf::try_from(manifest_dir).expect("CARGO_MANIFEST_DIR is not an absolute path")
|
2020-03-04 19:58:20 +01:00
|
|
|
} else {
|
2024-07-12 04:43:10 +02:00
|
|
|
AbsolutePathBuf::try_from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.expect("CARGO_MANIFEST_DIR is not an absolute path")
|
2020-03-04 19:58:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let test_path = manifest_dir.join("Cargo.lock");
|
|
|
|
if test_path.exists() {
|
|
|
|
manifest_dir
|
|
|
|
} else {
|
|
|
|
manifest_dir
|
|
|
|
.parent()
|
|
|
|
.expect("Couldn't find the debug binaries directory")
|
|
|
|
.parent()
|
|
|
|
.expect("Couldn't find the debug binaries directory")
|
2024-07-12 04:43:10 +02:00
|
|
|
.into()
|
2020-03-04 19:58:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn binaries() -> AbsolutePathBuf {
|
2023-10-09 16:31:15 +02:00
|
|
|
let build_target = std::env::var("CARGO_BUILD_TARGET").unwrap_or_default();
|
Fix unit tests on Android (#10224)
# Description
* The path to the binaries for tests is slightly incorrect. It is
missing the build target when it is set with the `CARGO_BUILD_TARGET`
environment variable. For example, when `CARGO_BUILD_TARGET` is set to
`aarch64-linux-android`, the path to the `nu` binary is:
`./target/aarch64-linux-android/debug/nu`
rather than
`./target/debug/nu`
This is common on Termux since the default target that rustc detects can
cause problems on some projects, such as [python's `cryptography`
package](https://github.com/pyca/cryptography/issues/7248).
This technically isn't a problem specific to Android, but is more likely
to happen on Android due to the latter.
* Additionally, the existing variable named `NUSHELL_CARGO_TARGET` is in
fact the profile, not the build target, so this was renamed to
`NUSHELL_CARGO_PROFILE`. This change is included because without the
rename, the build system would be using `CARGO_BUILD_TARGET` for the
build target and `NUSHELL_CARGO_TARGET` for the build profile, which is
confusing.
* `std path add` tests were missing `android` test
# User-Facing Changes
For those who would like to build nushell on Termux, the unit tests will
pass now.
2023-09-05 10:17:34 +02:00
|
|
|
|
|
|
|
let profile = if let Ok(env_profile) = std::env::var("NUSHELL_CARGO_PROFILE") {
|
|
|
|
env_profile
|
|
|
|
} else if cfg!(debug_assertions) {
|
|
|
|
"debug".into()
|
|
|
|
} else {
|
|
|
|
"release".into()
|
|
|
|
};
|
2021-03-15 04:59:04 +01:00
|
|
|
|
2020-03-27 22:13:59 +01:00
|
|
|
std::env::var("CARGO_TARGET_DIR")
|
2024-07-12 04:43:10 +02:00
|
|
|
.ok()
|
|
|
|
.and_then(|p| AbsolutePathBuf::try_from(p).ok())
|
|
|
|
.unwrap_or_else(|| root().join("target"))
|
Fix unit tests on Android (#10224)
# Description
* The path to the binaries for tests is slightly incorrect. It is
missing the build target when it is set with the `CARGO_BUILD_TARGET`
environment variable. For example, when `CARGO_BUILD_TARGET` is set to
`aarch64-linux-android`, the path to the `nu` binary is:
`./target/aarch64-linux-android/debug/nu`
rather than
`./target/debug/nu`
This is common on Termux since the default target that rustc detects can
cause problems on some projects, such as [python's `cryptography`
package](https://github.com/pyca/cryptography/issues/7248).
This technically isn't a problem specific to Android, but is more likely
to happen on Android due to the latter.
* Additionally, the existing variable named `NUSHELL_CARGO_TARGET` is in
fact the profile, not the build target, so this was renamed to
`NUSHELL_CARGO_PROFILE`. This change is included because without the
rename, the build system would be using `CARGO_BUILD_TARGET` for the
build target and `NUSHELL_CARGO_TARGET` for the build profile, which is
confusing.
* `std path add` tests were missing `android` test
# User-Facing Changes
For those who would like to build nushell on Termux, the unit tests will
pass now.
2023-09-05 10:17:34 +02:00
|
|
|
.join(build_target)
|
|
|
|
.join(profile)
|
2020-03-04 19:58:20 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn fixtures() -> AbsolutePathBuf {
|
|
|
|
let mut path = root();
|
|
|
|
path.push("tests");
|
|
|
|
path.push("fixtures");
|
|
|
|
path
|
2021-03-04 07:35:13 +01:00
|
|
|
}
|
|
|
|
|
2024-07-14 10:37:57 +02:00
|
|
|
pub fn assets() -> AbsolutePathBuf {
|
|
|
|
let mut path = root();
|
|
|
|
path.push("tests");
|
|
|
|
path.push("assets");
|
|
|
|
path
|
|
|
|
}
|
2020-03-04 19:58:20 +01:00
|
|
|
|
2024-07-12 04:43:10 +02:00
|
|
|
pub fn in_directory(path: impl AsRef<nu_path::Path>) -> AbsolutePathBuf {
|
|
|
|
root().join(path)
|
2019-12-15 17:15:06 +01:00
|
|
|
}
|