From 60ffd58b1eba7b086d598959cc43791330b0bbcf Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Wed, 12 Oct 2022 10:49:12 +0200 Subject: [PATCH] fix(converter): escape quotes (#1790) --- .../src/old_config_converter/old_layout.rs | 22 +++++++++++++------ .../unit/convert_layout_tests.rs | 16 ++++++++++++++ .../old_yaml_layout_with_quoted_args.yaml | 6 +++++ ...nvert_layout_with_command_quoted_args.snap | 17 ++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 zellij-client/src/old_config_converter/unit/fixtures/old_yaml_layout_with_quoted_args.yaml create mode 100644 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 diff --git a/zellij-client/src/old_config_converter/old_layout.rs b/zellij-client/src/old_config_converter/old_layout.rs index cd1aa66f..8cadd795 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::>() .join(" ") )); @@ -315,7 +322,8 @@ pub fn layout_yaml_to_layout_kdl(raw_yaml_layout: &str) -> Result 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 00000000..fc595f79 --- /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 00000000..eb38da80 --- /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" + } + } + } +}