-
-
Notifications
You must be signed in to change notification settings - Fork 651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(layout): Support environment variables in cwd (#2288) #2291
Changes from 3 commits
2a8ca06
1a15c27
ae476bc
f977bda
ec70ef9
cf2fdb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2031,3 +2031,157 @@ fn run_plugin_location_parsing() { | |
}; | ||
assert_eq!(layout, expected_layout); | ||
} | ||
|
||
#[track_caller] | ||
fn env_test_helper(layout_str: &str, env_vars: Vec<(&str, &str)>, expected_output: Vec<&str>) { | ||
for (key, value) in &env_vars { | ||
std::env::set_var(key, value); | ||
} | ||
let layout = Layout::from_kdl(layout_str, "layout_file_name".into(), None, None).unwrap(); | ||
let layout = format!("{layout:#?}",); | ||
for (key, value) in &env_vars { | ||
assert!( | ||
!layout.contains(&format!("${key}")) && layout.contains(value), | ||
"environment variable `{key}={value}` was not properly expanded", | ||
); | ||
} | ||
for s in expected_output { | ||
assert!(layout.contains(s), "expected string `{s}` was not found"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about asserting the cwd in the parsed structures (iirc they are a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the snapshot test I added handles this request indirectly 😃 |
||
} | ||
} | ||
|
||
#[test] | ||
fn env_valid_global_cwd() { | ||
env_test_helper( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great, but I personally find it a little hard to understand at a glance. Which I think is important when we're considering tests. How about we change it to something like (pseudocode, if you have a better idea then by all means!) LayoutTesterWithEnvironment::new()
.set_env_variables(/* ... */)
.parse_layout(stringified_layout)
.assert(/* .. */)
.assert(/* .. */) |
||
r#" | ||
layout { | ||
cwd "$Z_VALID_GLOBAL" | ||
pane cwd="relative" // -> /abs/path/relative | ||
pane cwd="/another/abs" // -> /another/abs | ||
} | ||
"#, | ||
vec![("Z_VALID_GLOBAL", "/abs/path")], | ||
vec!["/abs/path/relative", "/another/abs"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_valid_local_abs_cwd() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
cwd "/abs/path" | ||
pane cwd="relative" // -> /abs/path/relative | ||
pane cwd="$Z_VALID_LOCAL" // -> /another/abs | ||
} | ||
"#, | ||
vec![("Z_VALID_LOCAL", "/another/abs")], | ||
vec!["/abs/path/relative", "/another/abs"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_valid_local_relative_cwd() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
cwd "/abs/path" | ||
pane cwd="relative" // -> /abs/path/relative | ||
pane cwd="$Z_VALID_LOCAL_REL" // -> /abs/path/relative | ||
} | ||
"#, | ||
vec![("Z_VALID_LOCAL_REL", "relative")], | ||
vec!["/abs/path/relative"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_command_cwd() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
pane command="ls" cwd="$Z_COMMAND" // -> some/path | ||
} | ||
"#, | ||
vec![("Z_COMMAND", "some/path")], | ||
vec!["some/path"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_edit_cwd() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
pane edit="file.rs" cwd="$Z_EDIT" // -> some/path/file.rs | ||
} | ||
"#, | ||
vec![("Z_EDIT", "some/path")], | ||
vec!["some/path/file.rs"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_tilde_cwd() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
pane edit="file.rs" cwd="~/my/folder" // -> /home/aram/my/folder/file.rs | ||
} | ||
"#, | ||
vec![("HOME", "/home/aram")], | ||
vec!["/home/aram/my/folder/file.rs"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_command() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
pane command="~/backup/executable" // -> /home/aram/backup/executable | ||
} | ||
"#, | ||
vec![("HOME", "/home/aram")], | ||
vec!["/home/aram/backup/executable"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_edit() { | ||
env_test_helper( | ||
r#" | ||
layout { | ||
pane edit="~/backup/foo.txt" // -> /home/aram/backup/foo.txt | ||
} | ||
"#, | ||
vec![("HOME", "/home/aram")], | ||
vec!["/home/aram/backup/foo.txt"], | ||
); | ||
} | ||
|
||
#[test] | ||
fn env_invalid_global_cwd() { | ||
std::env::remove_var("Z_INVALID_GLOBAL"); | ||
let kdl_layout = r#" | ||
layout { | ||
cwd "$Z_INVALID_GLOBAL" | ||
pane cwd="relative" | ||
} | ||
"#; | ||
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None); | ||
assert!(layout.is_err(), "invalid env var lookup should fail"); | ||
} | ||
|
||
#[test] | ||
fn env_invalid_local_cwd() { | ||
std::env::remove_var("Z_INVALID_LOCAL"); | ||
let kdl_layout = r#" | ||
layout { | ||
cwd "/abs/path" | ||
pane cwd="$Z_INVALID_LOCAL" | ||
} | ||
"#; | ||
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None); | ||
assert!(layout.is_err(), "invalid env var lookup should fail"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will set the environment variable globally for all the tests, no? I'm a little wary of such things. Ideally we can find a solution that does this locally only for one test, and if not maybe we can unset these after the assert?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's true, and this is why I used weird names for the vars I set (besides
$HOME
).current version handles restoring the previous env after parsing.