diff --git a/crates/nu-cli/src/commands/history/history_.rs b/crates/nu-cli/src/commands/history/history_.rs index 376ea19d7c..b45b30147c 100644 --- a/crates/nu-cli/src/commands/history/history_.rs +++ b/crates/nu-cli/src/commands/history/history_.rs @@ -131,10 +131,7 @@ impl Command for History { } } } else { - Err(ShellError::FileNotFound { - file: "history file".into(), - span: head, - }) + Err(ShellError::ConfigDirNotFound { span: Some(head) }) } } diff --git a/crates/nu-command/src/env/config/config_reset.rs b/crates/nu-command/src/env/config/config_reset.rs index 42ddd2c759..95afe4c7dd 100644 --- a/crates/nu-command/src/env/config/config_reset.rs +++ b/crates/nu-command/src/env/config/config_reset.rs @@ -52,13 +52,7 @@ impl Command for ConfigReset { let mut config_path = match nu_path::config_dir() { Some(path) => path, None => { - return Err(ShellError::GenericError { - error: "Could not find config path".into(), - msg: "Could not find config path".into(), - span: None, - help: None, - inner: vec![], - }); + return Err(ShellError::ConfigDirNotFound { span: None }); } }; config_path.push("nushell"); diff --git a/crates/nu-protocol/src/eval_const.rs b/crates/nu-protocol/src/eval_const.rs index b128ec29e6..314c3c9465 100644 --- a/crates/nu-protocol/src/eval_const.rs +++ b/crates/nu-protocol/src/eval_const.rs @@ -26,19 +26,23 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result { + path.push("nushell"); + Ok(path) + } + None => Err(Value::error( + ShellError::ConfigDirNotFound { span: Some(span) }, + span, + )), + }; + record.push( "default-config-dir", - if let Some(mut path) = nu_path::config_dir() { - path.push("nushell"); - Value::string(path.to_string_lossy(), span) - } else { - Value::error( - ShellError::IOError { - msg: "Could not get config directory".into(), - }, - span, - ) - }, + config_path.as_ref().map_or_else( + |e| e.clone(), + |path| Value::string(path.to_string_lossy(), span), + ), ); record.push( @@ -46,16 +50,13 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result Result { - path.push("history.sqlite3"); + config_path.clone().map_or_else( + |e| e, + |mut path| { + match engine_state.config.history.file_format { + HistoryFileFormat::Sqlite => { + path.push("history.sqlite3"); + } + HistoryFileFormat::PlainText => { + path.push("history.txt"); + } } - HistoryFileFormat::PlainText => { - path.push("history.txt"); - } - } - let canon_hist_path = canonicalize_path(engine_state, &path); - Value::string(canon_hist_path.to_string_lossy(), span) - } else { - Value::error( - ShellError::IOError { - msg: "Could not find history path".into(), - }, - span, - ) - }, + let canon_hist_path = canonicalize_path(engine_state, &path); + Value::string(canon_hist_path.to_string_lossy(), span) + }, + ), ); record.push( "loginshell-path", - if let Some(mut path) = nu_path::config_dir() { - path.push("nushell"); - path.push("login.nu"); - let canon_login_path = canonicalize_path(engine_state, &path); - Value::string(canon_login_path.to_string_lossy(), span) - } else { - Value::error( - ShellError::IOError { - msg: "Could not find login shell path".into(), - }, - span, - ) - }, + config_path.clone().map_or_else( + |e| e, + |mut path| { + path.push("login.nu"); + let canon_login_path = canonicalize_path(engine_state, &path); + Value::string(canon_login_path.to_string_lossy(), span) + }, + ), ); #[cfg(feature = "plugin")] @@ -127,17 +115,14 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result, + }, } // TODO: Implement as From trait diff --git a/src/tests/test_config_path.rs b/src/tests/test_config_path.rs index ec17d27d8c..8bcc2f0e8e 100644 --- a/src/tests/test_config_path.rs +++ b/src/tests/test_config_path.rs @@ -28,32 +28,45 @@ fn test_default_config_path() { let _ = fs::create_dir_all(&config_dir_nushell); } let cwd = std::env::current_dir().expect("Could not get current working directory"); + + let config_dir_nushell = + std::fs::canonicalize(&config_dir_nushell).expect("canonicalize config dir failed"); + let actual = nu!(cwd: &cwd, "$nu.default-config-dir"); + assert_eq!(actual.out, adjust_canonicalization(&config_dir_nushell)); + let config_path = config_dir_nushell.join("config.nu"); - - // Create an empty file for canonicalization if it doesn't already exist - if !config_path.exists() { - let _ = std::fs::File::create(&config_path); - } - // We use canonicalize here in case the config or env is symlinked since $nu.config-path is returning the canonicalized path in #8653 - let canon_config_path = adjust_canonicalization( - std::fs::canonicalize(config_path).expect("canonicalize config-path failed"), - ); - + let canon_config_path = + adjust_canonicalization(std::fs::canonicalize(&config_path).unwrap_or(config_path)); let actual = nu!(cwd: &cwd, "$nu.config-path"); assert_eq!(actual.out, canon_config_path); + let env_path = config_dir_nushell.join("env.nu"); - - // Create an empty file for canonicalization if it doesn't already exist - if !env_path.exists() { - let _ = std::fs::File::create(&env_path); - } - - let canon_env_path = adjust_canonicalization( - std::fs::canonicalize(env_path).expect("canonicalize of env-path failed"), - ); + let canon_env_path = + adjust_canonicalization(std::fs::canonicalize(&env_path).unwrap_or(env_path)); let actual = nu!(cwd: &cwd, "$nu.env-path"); assert_eq!(actual.out, canon_env_path); + + let history_path = config_dir_nushell.join("history.txt"); + let canon_history_path = + adjust_canonicalization(std::fs::canonicalize(&history_path).unwrap_or(history_path)); + let actual = nu!(cwd: &cwd, "$nu.history-path"); + assert_eq!(actual.out, canon_history_path); + + let login_path = config_dir_nushell.join("login.nu"); + let canon_login_path = + adjust_canonicalization(std::fs::canonicalize(&login_path).unwrap_or(login_path)); + let actual = nu!(cwd: &cwd, "$nu.loginshell-path"); + assert_eq!(actual.out, canon_login_path); + + #[cfg(feature = "plugin")] + { + let plugin_path = config_dir_nushell.join("plugin.nu"); + let canon_plugin_path = + adjust_canonicalization(std::fs::canonicalize(&plugin_path).unwrap_or(plugin_path)); + let actual = nu!(cwd: &cwd, "$nu.plugin-path"); + assert_eq!(actual.out, canon_plugin_path); + } } #[test]