diff options
author | Aram Drevekenin <aram@poor.dev> | 2022-10-12 09:46:03 +0200 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2022-10-12 09:46:03 +0200 |
commit | 9b5c78a92fdb991832a2599dff987fd256d74fea (patch) | |
tree | 5864fe1ff98bf691f479fb39717fa0be6ae1a7b1 | |
parent | f6fa5213132a9ab5793bc9a1c9af1b9016b738f7 (diff) |
fix(converter): escape quotesescape-quotes-in-layout-conversion
4 files changed, 54 insertions, 7 deletions
diff --git a/zellij-client/src/old_config_converter/old_layout.rs b/zellij-client/src/old_config_converter/old_layout.rs index cd1aa66fb..8cadd7952 100644 --- a/zellij-client/src/old_config_converter/old_layout.rs +++ b/zellij-client/src/old_config_converter/old_layout.rs @@ -16,7 +16,8 @@ fn pane_line( ) -> String { let mut pane_line = format!("pane"); if let Some(pane_name) = pane_name { - pane_line.push_str(&format!(" name=\"{}\"", pane_name)); + // we use debug print here so that quotes and backslashes will be escaped + pane_line.push_str(&format!(" name={:?}", pane_name)); } if let Some(split_size) = split_size { pane_line.push_str(&format!(" size={}", split_size)); @@ -38,7 +39,8 @@ fn tab_line( ) -> String { let mut pane_line = format!("tab"); if let Some(pane_name) = pane_name { - pane_line.push_str(&format!(" name=\"{}\"", pane_name)); + // we use debug print here so that quotes and backslashes will be escaped + pane_line.push_str(&format!(" name={:?}", pane_name)); } if let Some(split_size) = split_size { pane_line.push_str(&format!(" size={}", split_size)); @@ -61,7 +63,8 @@ fn pane_line_with_children( ) -> String { let mut pane_line = format!("pane"); if let Some(pane_name) = pane_name { - pane_line.push_str(&format!(" name=\"{}\"", pane_name)); + // we use debug print here so that quotes and backslashes will be escaped + pane_line.push_str(&format!(" name={:?}", pane_name)); } if let Some(split_size) = split_size { pane_line.push_str(&format!(" size={}", split_size)); @@ -85,7 +88,8 @@ fn pane_command_line( ) -> String { let mut pane_line = format!("pane command={:?}", command); if let Some(pane_name) = pane_name { - pane_line.push_str(&format!(" name=\"{}\"", pane_name)); + // we use debug print here so that quotes and backslashes will be escaped + pane_line.push_str(&format!(" name={:?}", pane_name)); } if let Some(split_size) = split_size { pane_line.push_str(&format!(" size={}", split_size)); @@ -108,7 +112,8 @@ fn tab_line_with_children( ) -> String { let mut pane_line = format!("tab"); if let Some(pane_name) = pane_name { - pane_line.push_str(&format!(" name=\"{}\"", pane_name)); + // we use debug print here so that quotes and backslashes will be escaped + pane_line.push_str(&format!(" name={:?}", pane_name)); } if let Some(split_size) = split_size { pane_line.push_str(&format!(" size={}", split_size)); @@ -218,7 +223,9 @@ fn stringify_template( command_from_yaml .args .iter() - .map(|s| format!("\"{}\"", s)) + // we use debug print here so that quotes and backslashes will be + // escaped + .map(|s| format!("{:?}", s)) .collect::<Vec<String>>() .join(" ") )); @@ -315,7 +322,8 @@ pub fn layout_yaml_to_layout_kdl(raw_yaml_layout: &str) -> Result<String, String kdl_layout.push_str("\n}"); let layout_config = config_yaml_to_config_kdl(raw_yaml_layout, true)?; if let Some(session_name) = layout_from_yaml.session.name { - kdl_layout.push_str(&format!("\nsession_name \"{}\"", session_name)); + // we use debug print here so that quotes and backslashes will be escaped + kdl_layout.push_str(&format!("\nsession_name {:?}", session_name)); if let Some(attach_to_session) = layout_from_yaml.session.attach { kdl_layout.push_str(&format!("\nattach_to_session {}", attach_to_session)); } diff --git a/zellij-client/src/old_config_converter/unit/convert_layout_tests.rs b/zellij-client/src/old_config_converter/unit/convert_layout_tests.rs index 30e689bb1..1915da2ab 100644 --- a/zellij-client/src/old_config_converter/unit/convert_layout_tests.rs +++ b/zellij-client/src/old_config_converter/unit/convert_layout_tests.rs @@ -140,3 +140,19 @@ fn properly_convert_layout_example_4() -> Result<(), String> { assert_snapshot!(format!("{}", kdl_config)); Ok(()) } + +#[test] +fn properly_convert_layout_with_command_quoted_args() -> Result<(), String> { + let fixture = PathBuf::from(format!( + "{}/src/old_config_converter/unit/fixtures/old_yaml_layout_with_quoted_args.yaml", + env!("CARGO_MANIFEST_DIR") + )); + let mut handle = File::open(&fixture).map_err(|e| format!("{}", e))?; + let mut raw_config_file = String::new(); + handle + .read_to_string(&mut raw_config_file) + .map_err(|e| format!("{}", e))?; + let kdl_config = layout_yaml_to_layout_kdl(&raw_config_file)?; + assert_snapshot!(format!("{}", kdl_config)); + Ok(()) +} diff --git a/zellij-client/src/old_config_converter/unit/fixtures/old_yaml_layout_with_quoted_args.yaml b/zellij-client/src/old_config_converter/unit/fixtures/old_yaml_layout_with_quoted_args.yaml new file mode 100644 index 000000000..fc595f799 --- /dev/null +++ b/zellij-client/src/old_config_converter/unit/fixtures/old_yaml_layout_with_quoted_args.yaml @@ -0,0 +1,6 @@ +tabs: + - direction: Vertical + parts: + - direction: Vertical + run: + command: {cmd: bash, args: ["-c", "exec bash --init-file <(echo \"echo hi\") -i"]} diff --git a/zellij-client/src/old_config_converter/unit/snapshots/zellij_client__old_config_converter__old_layout__convert_layout_test__properly_convert_layout_with_command_quoted_args.snap b/zellij-client/src/old_config_converter/unit/snapshots/zellij_client__old_config_converter__old_layout__convert_layout_test__properly_convert_layout_with_command_quoted_args.snap new file mode 100644 index 000000000..eb38da80f --- /dev/null +++ b/zellij-client/src/old_config_converter/unit/snapshots/zellij_client__old_config_converter__old_layout__convert_layout_test__properly_convert_layout_with_command_quoted_args.snap @@ -0,0 +1,17 @@ +--- +source: zellij-client/src/old_config_converter/./unit/convert_layout_tests.rs +assertion_line: 156 +expression: "format!(\"{}\", kdl_config)" +--- +layout { + default_tab_template { + children + } + tab split_direction="Vertical" { + pane split_direction="Vertical" { + pane command="bash" { + args "-c" "exec bash --init-file <(echo \"echo hi\") -i" + } + } + } +} |