Skip to content
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

Merged
merged 6 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zellij-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ miette = { version = "3.3.0", features = ["fancy"] }
regex = "1.5.5"
tempfile = "3.2.0"
kdl = { version = "4.5.0", features = ["span"] }
shellexpand = "3.0.0"

#[cfg(not(target_family = "wasm"))]
[target.'cfg(not(target_family = "wasm"))'.dependencies]
Expand Down
57 changes: 57 additions & 0 deletions zellij-utils/src/input/unit/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2071,3 +2071,60 @@ fn run_plugin_location_parsing() {
};
assert_eq!(layout, expected_layout);
}

#[test]
fn env_var_expansion() {
let raw_layout = r#"
layout {
// cwd tests + composition
cwd "$TEST_GLOBAL_CWD"
pane cwd="relative" // -> /abs/path/relative
pane cwd="/another/abs" // -> /another/abs
pane cwd="$TEST_LOCAL_CWD" // -> /another/abs
pane cwd="$TEST_RELATIVE" // -> /abs/path/relative
pane command="ls" cwd="$TEST_ABSOLUTE" // -> /somewhere
pane edit="file.rs" cwd="$TEST_ABSOLUTE" // -> /somewhere/file.rs
pane edit="file.rs" cwd="~/backup" // -> /home/aram/backup/file.rs

// other paths
pane command="~/backup/executable" // -> /home/aram/backup/executable
pane edit="~/backup/foo.txt" // -> /home/aram/backup/foo.txt
}
"#;
let env_vars = [
("TEST_GLOBAL_CWD", "/abs/path"),
("TEST_LOCAL_CWD", "/another/abs"),
("TEST_RELATIVE", "relative"),
("TEST_ABSOLUTE", "/somewhere"),
("HOME", "/home/aram"),
];
let mut old_vars = Vec::new();
// set environment variables for test, keeping track of existing values.
for (key, value) in env_vars {
old_vars.push((key, std::env::var(key).ok()));
std::env::set_var(key, value);
}
let layout = Layout::from_kdl(raw_layout, "layout_file_name".into(), None, None);
// restore environment.
for (key, opt) in old_vars {
match opt {
Some(value) => std::env::set_var(key, &value),
None => std::env::remove_var(key),
}
}
let layout = layout.unwrap();
assert_snapshot!(format!("{layout:#?}"));
}

#[test]
fn env_var_missing() {
std::env::remove_var("SOME_UNIQUE_VALUE");
let kdl_layout = r#"
layout {
cwd "$SOME_UNIQUE_VALUE"
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");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
---
source: zellij-utils/src/input/./unit/layout_test.rs
assertion_line: 2116
expression: "format!(\"{layout:#?}\")"
---
Layout {
tabs: [],
focused_tab_index: None,
template: Some(
(
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Cwd(
"/abs/path/relative",
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Cwd(
"/another/abs",
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Cwd(
"/another/abs",
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Cwd(
"/abs/path/relative",
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "ls",
args: [],
cwd: Some(
"/somewhere",
),
hold_on_close: true,
hold_on_start: false,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
EditFile(
"/somewhere/file.rs",
None,
Some(
"/somewhere",
),
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
EditFile(
"/home/aram/backup/file.rs",
None,
Some(
"/home/aram/backup",
),
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "/home/aram/backup/executable",
args: [],
cwd: Some(
"/abs/path",
),
hold_on_close: true,
hold_on_start: false,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
TiledPaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
EditFile(
"/home/aram/backup/foo.txt",
None,
Some(
"/abs/path",
),
),
),
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
],
split_size: None,
run: None,
borderless: false,
focus: None,
external_children_index: None,
children_are_stacked: false,
is_expanded_in_stack: false,
exclude_from_sync: None,
},
[],
),
),
swap_layouts: [],
swap_tiled_layouts: [],
swap_floating_layouts: [],
}
Loading