Revert "Support other variables than PATH in pathvar (#3791)" (#3827)

This reverts commit f9f39c0a1c.
This commit is contained in:
Darren Schroeder 2021-07-23 09:03:28 -05:00 committed by GitHub
parent 5d7677dd07
commit 0f9e55dac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 44 additions and 504 deletions

View File

@ -1,10 +1,9 @@
use super::get_var;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
use nu_test_support::NATIVE_PATH_ENV_SEPARATOR;
use nu_test_support::{NATIVE_PATH_ENV_SEPARATOR, NATIVE_PATH_ENV_VAR};
use std::path::PathBuf;
pub struct SubCommand;
@ -15,14 +14,7 @@ impl WholeStreamCommand for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("pathvar add")
.required("path", SyntaxShape::FilePath, "path to add")
.named(
"var",
SyntaxShape::String,
"Use a different variable than PATH",
Some('v'),
)
Signature::build("pathvar add").required("path", SyntaxShape::FilePath, "path to add")
}
fn usage(&self) -> &str {
@ -45,21 +37,17 @@ impl WholeStreamCommand for SubCommand {
pub fn add(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = &args.context;
let var = get_var(&args)?;
let path_to_add: Tagged<PathBuf> = args.req(0)?;
let path = path_to_add.item.into_os_string().into_string();
if let Ok(mut path) = path {
path.push(NATIVE_PATH_ENV_SEPARATOR);
if let Some(old_pathvar) = ctx.scope.get_env(&var) {
if let Some(old_pathvar) = ctx.scope.get_env(NATIVE_PATH_ENV_VAR) {
path.push_str(&old_pathvar);
ctx.scope.add_env_var(&var.item, path);
ctx.scope.add_env_var(NATIVE_PATH_ENV_VAR, path);
Ok(OutputStream::empty())
} else {
Err(ShellError::unexpected(&format!(
"Variable {} not set",
&var.item
)))
Err(ShellError::unexpected("PATH not set"))
}
} else {
Err(ShellError::labeled_error(

View File

@ -1,10 +1,9 @@
use super::get_var;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
use nu_test_support::NATIVE_PATH_ENV_SEPARATOR;
use nu_test_support::{NATIVE_PATH_ENV_SEPARATOR, NATIVE_PATH_ENV_VAR};
use std::path::PathBuf;
pub struct SubCommand;
@ -15,14 +14,7 @@ impl WholeStreamCommand for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("pathvar append")
.required("path", SyntaxShape::FilePath, "path to append")
.named(
"var",
SyntaxShape::String,
"Use a different variable than PATH",
Some('v'),
)
Signature::build("pathvar append").required("path", SyntaxShape::FilePath, "path to append")
}
fn usage(&self) -> &str {
@ -44,22 +36,17 @@ impl WholeStreamCommand for SubCommand {
pub fn add(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = &args.context;
let var = get_var(&args)?;
let path_to_append_arg: Tagged<PathBuf> = args.req(0)?;
let path_to_append = path_to_append_arg.item.into_os_string().into_string();
if let Ok(path) = path_to_append {
if let Some(mut pathvar) = ctx.scope.get_env(&var) {
if let Some(mut pathvar) = ctx.scope.get_env(NATIVE_PATH_ENV_VAR) {
pathvar.push(NATIVE_PATH_ENV_SEPARATOR);
pathvar.push_str(&path);
ctx.scope.add_env_var(&var.item, pathvar);
ctx.scope.add_env_var(NATIVE_PATH_ENV_VAR, pathvar);
Ok(OutputStream::empty())
} else {
Err(ShellError::unexpected(&format!(
"Variable {} not set",
&var.item
)))
Err(ShellError::unexpected("PATH not set"))
}
} else {
Err(ShellError::labeled_error(

View File

@ -1,9 +1,8 @@
use super::get_var;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, Value};
use nu_test_support::NATIVE_PATH_ENV_SEPARATOR;
use nu_protocol::{Signature, Value};
use nu_test_support::{NATIVE_PATH_ENV_SEPARATOR, NATIVE_PATH_ENV_VAR};
pub struct Command;
@ -13,17 +12,11 @@ impl WholeStreamCommand for Command {
}
fn signature(&self) -> Signature {
Signature::build("pathvar").named(
"var",
SyntaxShape::String,
"Use a different variable than PATH",
Some('v'),
)
Signature::build("pathvar")
}
fn usage(&self) -> &str {
r#"Manipulate the PATH variable (pathvar) or a different variable following the
same rules."#
"Manipulate the PATH variable (or pathvar)."
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
@ -37,11 +30,6 @@ same rules."#
example: "pathvar",
result: None,
},
Example {
description: "Display the current session's LD_LIBRARY_PATH",
example: "pathvar -v LD_LIBRARY_PATH",
result: None,
},
Example {
description: "Add /usr/bin to the pathvar",
example: "pathvar add /usr/bin",
@ -57,9 +45,7 @@ same rules."#
}
pub fn get_pathvar(args: CommandArgs) -> Result<OutputStream, ShellError> {
let var = get_var(&args)?;
if let Some(pathvar) = args.context.scope.get_env(&var) {
if let Some(pathvar) = args.context.scope.get_env(NATIVE_PATH_ENV_VAR) {
let pathvar: Vec<Value> = pathvar
.split(NATIVE_PATH_ENV_SEPARATOR)
.map(Value::from)
@ -67,9 +53,6 @@ pub fn get_pathvar(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::from(pathvar))
} else {
Err(ShellError::unexpected(&format!(
"Variable {} not set",
&var.item
)))
Err(ShellError::unexpected("PATH not set"))
}
}

View File

@ -11,15 +11,3 @@ pub use command::Command as Pathvar;
pub use remove::SubCommand as PathvarRemove;
pub use reset::SubCommand as PathvarReset;
pub use save::SubCommand as PathvarSave;
use nu_engine::CommandArgs;
use nu_errors::ShellError;
use nu_source::{Tagged, TaggedItem};
use nu_test_support::NATIVE_PATH_ENV_VAR;
fn get_var(args: &CommandArgs) -> Result<Tagged<String>, ShellError> {
Ok(args
.get_flag("var")?
.unwrap_or_else(|| String::from(NATIVE_PATH_ENV_VAR))
.tagged_unknown())
}

View File

@ -1,10 +1,9 @@
use super::get_var;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
use nu_test_support::NATIVE_PATH_ENV_SEPARATOR;
use nu_test_support::{NATIVE_PATH_ENV_SEPARATOR, NATIVE_PATH_ENV_VAR};
pub struct SubCommand;
@ -14,18 +13,11 @@ impl WholeStreamCommand for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("pathvar remove")
.required(
"index",
SyntaxShape::Int,
"index of the path to remove (starting at 0)",
)
.named(
"var",
SyntaxShape::String,
"Use a different variable than PATH",
Some('v'),
)
Signature::build("pathvar remove").required(
"index",
SyntaxShape::Int,
"index of the path to remove (starting at 0)",
)
}
fn usage(&self) -> &str {
@ -47,12 +39,10 @@ impl WholeStreamCommand for SubCommand {
pub fn remove(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = &args.context;
let var = get_var(&args)?;
let index_to_remove_arg: Tagged<u64> = args.req(0)?;
let index_to_remove = index_to_remove_arg.item as usize;
if let Some(old_pathvar) = ctx.scope.get_env(&var) {
if let Some(old_pathvar) = ctx.scope.get_env(NATIVE_PATH_ENV_VAR) {
let mut paths: Vec<&str> = old_pathvar.split(NATIVE_PATH_ENV_SEPARATOR).collect();
if index_to_remove >= paths.len() {
@ -65,15 +55,12 @@ pub fn remove(args: CommandArgs) -> Result<OutputStream, ShellError> {
paths.remove(index_to_remove);
ctx.scope.add_env_var(
&var.item,
NATIVE_PATH_ENV_VAR,
paths.join(&NATIVE_PATH_ENV_SEPARATOR.to_string()),
);
Ok(OutputStream::empty())
} else {
Err(ShellError::unexpected(&format!(
"Variable {} not set",
&var.item
)))
Err(ShellError::unexpected("PATH not set"))
}
}

View File

@ -1,9 +1,8 @@
use super::get_var;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
use nu_test_support::NATIVE_PATH_ENV_SEPARATOR;
use nu_protocol::{Signature, UntaggedValue};
use nu_test_support::{NATIVE_PATH_ENV_SEPARATOR, NATIVE_PATH_ENV_VAR};
pub struct SubCommand;
@ -13,12 +12,7 @@ impl WholeStreamCommand for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("pathvar reset").named(
"var",
SyntaxShape::String,
"Use a different variable than PATH",
Some('v'),
)
Signature::build("pathvar reset")
}
fn usage(&self) -> &str {
@ -29,29 +23,24 @@ impl WholeStreamCommand for SubCommand {
reset(args)
}
}
pub fn reset(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = &args.context;
let var = get_var(&args)?;
let var_lower = var.clone().map(|s| s.to_lowercase());
if let Some(global_cfg) = &mut ctx.configs().lock().global_config {
let default_pathvar = global_cfg.vars.get(&var_lower.item);
let default_pathvar = global_cfg.vars.get("path");
if let Some(pathvar) = default_pathvar {
if let UntaggedValue::Table(paths) = &pathvar.value {
let pathvar_str = paths
.iter()
.map(|x| x.as_string().expect("Error converting path to string"))
.join(&NATIVE_PATH_ENV_SEPARATOR.to_string());
ctx.scope.add_env_var(&var.item, pathvar_str);
ctx.scope.add_env_var(NATIVE_PATH_ENV_VAR, pathvar_str);
}
} else {
return Err(ShellError::untagged_runtime_error(&format!(
"Default {} is not set in config file.",
&var_lower.item
)));
return Err(ShellError::untagged_runtime_error(
"Default path is not set in config file.",
));
}
Ok(OutputStream::empty())
} else {

View File

@ -1,9 +1,8 @@
use super::get_var;
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_test_support::NATIVE_PATH_ENV_SEPARATOR;
use nu_protocol::{Signature, UntaggedValue, Value};
use nu_test_support::{NATIVE_PATH_ENV_SEPARATOR, NATIVE_PATH_ENV_VAR};
pub struct SubCommand;
@ -13,12 +12,7 @@ impl WholeStreamCommand for SubCommand {
}
fn signature(&self) -> Signature {
Signature::build("pathvar save").named(
"var",
SyntaxShape::String,
"Use a different variable than PATH",
Some('v'),
)
Signature::build("pathvar save")
}
fn usage(&self) -> &str {
@ -33,11 +27,8 @@ pub fn save(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = &args.context;
let var = get_var(&args)?;
let var_lower = var.clone().map(|s| s.to_lowercase());
if let Some(global_cfg) = &mut ctx.configs().lock().global_config {
if let Some(pathvar) = ctx.scope.get_env(&var) {
if let Some(pathvar) = ctx.scope.get_env(NATIVE_PATH_ENV_VAR) {
let paths: Vec<Value> = pathvar
.split(NATIVE_PATH_ENV_SEPARATOR)
.map(Value::from)
@ -49,16 +40,13 @@ pub fn save(args: CommandArgs) -> Result<OutputStream, ShellError> {
Tag::from(Span::from(&span_range)),
);
global_cfg.vars.insert(var_lower.item, row);
global_cfg.vars.insert("path".to_string(), row);
global_cfg.write()?;
ctx.reload_config(global_cfg)?;
Ok(OutputStream::empty())
} else {
Err(ShellError::unexpected(&format!(
"Variable {} not set",
&var.item
)))
Err(ShellError::unexpected("PATH not set"))
}
} else {
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())

View File

@ -37,7 +37,6 @@ mod move_;
mod open;
mod parse;
mod path;
mod pathvar;
mod prepend;
mod random;
mod range;

View File

@ -1,372 +0,0 @@
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::fs::{AbsolutePath, DisplayPath};
use nu_test_support::playground::{says, Playground};
use nu_test_support::{nu, NATIVE_PATH_ENV_SEPARATOR};
use std::path::PathBuf;
use hamcrest2::assert_that;
use hamcrest2::prelude::*;
/// Helper function that joins string literals with ':' or ';', based on host OS
fn join_env_sep(pieces: &[&str]) -> String {
let sep_string = String::from(NATIVE_PATH_ENV_SEPARATOR);
pieces.join(&sep_string)
}
// Helpers
#[cfg(windows)]
#[test]
fn joins_env_on_windows() {
let pieces = ["sausage", "bacon", "spam"];
let actual = join_env_sep(&pieces);
assert_eq!(&actual, "sausage;bacon;spam");
}
#[cfg(not(windows))]
#[test]
fn joins_env_on_non_windows() {
let pieces = ["sausage", "bacon", "spam"];
let actual = join_env_sep(&pieces);
assert_eq!(&actual, "sausage:bacon:spam");
}
// pathvar
#[test]
fn pathvar_correctly_reads_path_from_config_and_env() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
path = ["/Users/andresrobalino/.volta/bin", "/Users/mosqueteros/bin"]
"#,
)])
.with_config(&file)
.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&PathBuf::from("/Users/mosquito/proboscis").display_path(),
);
let expected =
"/Users/andresrobalino/.volta/bin-/Users/mosqueteros/bin-/Users/mosquito/proboscis";
let actual = sandbox.pipeline(r#" pathvar | str collect '-' "#);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_correctly_reads_env_var_from_config_and_env() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env(
"BREAKFAST",
&join_env_sep(&["egg", "sausage", "bacon", "spam"]),
);
let expected = "egg-sausage-bacon-spam";
let actual = sandbox.pipeline(r#" pathvar -v BREAKFAST | str collect '-' "#);
assert_that!(actual, says().stdout(&expected));
})
}
// pathvar add
#[test]
fn pathvar_adds_to_path() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&PathBuf::from("/Users/mosquito/proboscis").display_path(),
);
let expected = "spam-/Users/mosquito/proboscis";
let actual = sandbox.pipeline(r#" pathvar add spam; $nu.path | str collect '-' "#);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_adds_to_env_var() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env("BREAKFAST", &join_env_sep(&["egg", "sausage", "bacon"]));
let expected = join_env_sep(&["spam", "egg", "sausage", "bacon"]);
let actual = sandbox.pipeline(r#" pathvar add -v BREAKFAST spam; $nu.env.BREAKFAST "#);
assert_that!(actual, says().stdout(&expected));
})
}
// pathvar append
#[test]
fn pathvar_appends_to_path() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&PathBuf::from("/Users/mosquito/proboscis").display_path(),
);
let expected = "/Users/mosquito/proboscis-spam";
let actual = sandbox.pipeline(r#" pathvar append spam; $nu.path | str collect '-' "#);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_appends_to_env_var() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env("BREAKFAST", &join_env_sep(&["egg", "sausage", "bacon"]));
let expected = join_env_sep(&["egg", "sausage", "bacon", "spam"]);
let actual = sandbox.pipeline(r#" pathvar append -v BREAKFAST spam; $nu.env.BREAKFAST "#);
assert_that!(actual, says().stdout(&expected));
})
}
// pathvar remove
#[test]
fn pathvar_removes_from_path() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&join_env_sep(&["/Users/mosquito/proboscis", "spam"]),
);
let expected = "/Users/mosquito/proboscis";
let actual = sandbox.pipeline(r#" pathvar remove 1; $nu.path"#);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_removes_from_env_var() {
Playground::setup("hi_there", |_, sandbox| {
sandbox.with_env(
"BREAKFAST",
&join_env_sep(&["egg", "sausage", "bacon", "spam"]),
);
let expected = join_env_sep(&["egg", "sausage", "bacon"]);
let actual = sandbox.pipeline(r#" pathvar remove -v BREAKFAST 3; $nu.env.BREAKFAST "#);
assert_that!(actual, says().stdout(&expected));
})
}
// pathvar reset
#[test]
fn pathvar_resets_path_from_config() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
path = ["/Users/andresrobalino/.volta/bin", "/Users/mosqueteros/bin"]
"#,
)])
.with_config(&file)
.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&PathBuf::from("/Users/mosquito/proboscis").display_path(),
);
let expected = "/Users/andresrobalino/.volta/bin-/Users/mosqueteros/bin";
let actual = sandbox.pipeline(
r#"
pathvar reset
pathvar | str collect '-'
"#,
);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_resets_env_var_from_config() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
breakfast = ["egg", "sausage", "bacon"]
"#,
)])
.with_config(&file)
.with_env(
"BREAKFAST",
&join_env_sep(&["egg", "sausage", "bacon", "spam"]),
);
let expected = "egg-sausage-bacon";
let actual = sandbox.pipeline(
r#"
pathvar reset -v BREAKFAST
pathvar -v BREAKFAST | str collect '-'
"#,
);
assert_that!(actual, says().stdout(&expected));
})
}
// pathvar save
#[test]
fn pathvar_saves_path_to_config() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
path = ["/Users/andresrobalino/.volta/bin", "/Users/mosqueteros/bin"]
"#,
)])
.with_config(&file)
.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&PathBuf::from("/Users/mosquito/proboscis").display_path(),
);
let expected =
"/Users/andresrobalino/.volta/bin-/Users/mosqueteros/bin-/Users/mosquito/proboscis";
let actual = sandbox.pipeline(
r#"
pathvar save
$nu.path | str collect '-'
"#,
);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_saves_env_var_to_config() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
breakfast = ["egg", "sausage", "bacon"]
"#,
)])
.with_config(&file)
.with_env("BREAKFAST", "spam");
let expected = "spam";
let actual = sandbox.pipeline(
r#"
pathvar save -v BREAKFAST
(config).breakfast | str collect '-'
"#,
);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_saves_new_path_to_config() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
"#,
)])
.with_config(&file)
.with_env(
nu_test_support::NATIVE_PATH_ENV_VAR,
&PathBuf::from("/Users/mosquito/proboscis").display_path(),
);
let expected = "/Users/mosquito/proboscis";
let actual = sandbox.pipeline(
r#"
pathvar save
$nu.path | str collect '-'
"#,
);
assert_that!(actual, says().stdout(&expected));
})
}
#[test]
fn pathvar_saves_new_env_var_to_config() {
Playground::setup("hi_there", |dirs, sandbox| {
let file = AbsolutePath::new(dirs.test().join("config.toml"));
sandbox
.with_files(vec![FileWithContent(
"config.toml",
r#"
skip_welcome_message = true
"#,
)])
.with_config(&file)
.with_env("BREAKFAST", "spam");
let expected = "spam";
let actual = sandbox.pipeline(
r#"
pathvar save -v BREAKFAST
(config).breakfast | str collect '-'
"#,
);
assert_that!(actual, says().stdout(&expected));
})
}
// test some errors
#[test]
fn pathvar_error_non_existent_env_var() {
Playground::setup("hi_there", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
"pathvar -v EGGS_BACON_SPAM_SAUSAGE_SPAM_AND_SPAM_WITH_EXTRA_SPAM"
);
assert!(actual.err.contains("Error"));
assert!(actual.err.contains("not set"));
})
}

View File

@ -9,12 +9,15 @@ pub struct Outcome {
pub err: String,
}
#[cfg(windows)]
pub const NATIVE_PATH_ENV_VAR: &str = "Path";
#[cfg(not(windows))]
pub const NATIVE_PATH_ENV_VAR: &str = "PATH";
#[cfg(windows)]
pub const NATIVE_PATH_ENV_SEPARATOR: char = ';';
#[cfg(not(windows))]
pub const NATIVE_PATH_ENV_SEPARATOR: char = ':';
#[cfg(not(windows))]
pub const NATIVE_PATH_ENV_SEPARATOR: char = ';';
impl Outcome {
pub fn new(out: String, err: String) -> Outcome {