Fix/simplify cwd in benchmarks (#12812)

# Description
The benchmarks currently panic when trying to set the initial CWD. This
is because the code that sets the CWD also tries to get the CWD.
This commit is contained in:
Ian Manske 2024-05-09 02:16:57 +00:00 committed by GitHub
parent ba6f38510c
commit 948b299e65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,10 +9,7 @@ use nu_protocol::{
}; };
use nu_std::load_standard_library; use nu_std::load_standard_library;
use nu_utils::{get_default_config, get_default_env}; use nu_utils::{get_default_config, get_default_env};
use std::{ use std::rc::Rc;
path::{Path, PathBuf},
rc::Rc,
};
use std::hint::black_box; use std::hint::black_box;
@ -22,34 +19,16 @@ fn load_bench_commands() -> EngineState {
nu_command::add_shell_command_context(nu_cmd_lang::create_default_context()) nu_command::add_shell_command_context(nu_cmd_lang::create_default_context())
} }
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
let cwd = engine_state.cwd_as_string(None).unwrap();
if path.exists() {
match nu_path::canonicalize_with(path, cwd) {
Ok(canon_path) => canon_path,
Err(_) => path.to_owned(),
}
} else {
path.to_owned()
}
}
fn get_home_path(engine_state: &EngineState) -> PathBuf {
nu_path::home_dir()
.map(|path| canonicalize_path(engine_state, &path))
.unwrap_or_default()
}
fn setup_engine() -> EngineState { fn setup_engine() -> EngineState {
let mut engine_state = load_bench_commands(); let mut engine_state = load_bench_commands();
let home_path = get_home_path(&engine_state); let cwd = std::env::current_dir()
.unwrap()
.into_os_string()
.into_string()
.unwrap();
// parsing config.nu breaks without PWD set, so set a valid path // parsing config.nu breaks without PWD set, so set a valid path
engine_state.add_env_var( engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data()));
"PWD".into(),
Value::string(home_path.to_string_lossy(), Span::test_data()),
);
let nu_const = create_nu_constant(&engine_state, Span::unknown()) let nu_const = create_nu_constant(&engine_state, Span::unknown())
.expect("Failed to create nushell constant."); .expect("Failed to create nushell constant.");