diff --git a/CHANGELOG.md b/CHANGELOG.md index d3d9b672..7660a722 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- **`avocado connect clean` keeps comments after the connect-config + extension.** Removing `avocado-ext-connect-config` deleted the blank lines + and comments that followed it, such as a comment above the next extension + or a section header after the last one. They now stay with what comes next. + ## [1.2.0] - 2026-11-19 ### Added diff --git a/src/utils/config_edit.rs b/src/utils/config_edit.rs index 05500ab1..fe045f3d 100644 --- a/src/utils/config_edit.rs +++ b/src/utils/config_edit.rs @@ -989,11 +989,13 @@ fn remove_extension_definition(content: &str, ext_name: &str) -> Result .ok_or_else(|| anyhow::anyhow!("Extension '{ext_name}' not found in avocado.yaml"))?; let ext_def_indent = leading_spaces(lines[ext_line]); - // Find end of this extension's block (all lines indented deeper) + // Find end of this extension's block: its last line indented deeper. + // Blank lines and comments after that line belong to whatever comes + // next (a comment above the next extension, a section header), so they + // only join the block when a deeper child line follows them. let mut block_end = ext_line + 1; for (i, line) in lines.iter().enumerate().skip(ext_line + 1) { if line.trim().is_empty() || line.trim().starts_with('#') { - block_end = i + 1; continue; } let indent = leading_spaces(line); @@ -2087,6 +2089,98 @@ extensions: assert!(result.contains("- avocado-ext-tunnels")); } + #[test] + fn test_remove_connect_config_extension_keeps_comment_above_next_extension() { + let config = r#"extensions: + app: + packages: + hello: '*' + avocado-ext-connect-config: + types: + - confext + version: "0.1.0" + overlay: overlay + + # Sensor support + sensors: + packages: + i2c-tools: '*' + +## +## Runtimes +## +runtimes: + dev: + extensions: + - app + - avocado-ext-connect-config + - sensors +"#; + let expected = r#"extensions: + app: + packages: + hello: '*' + + # Sensor support + sensors: + packages: + i2c-tools: '*' + +## +## Runtimes +## +runtimes: + dev: + extensions: + - app + - sensors +"#; + let (result, changed) = remove_connect_config_extension_in_yaml(config, "dev").unwrap(); + assert!(changed); + assert_eq!(result, expected); + let _: serde_yaml::Value = serde_yaml::from_str(&result).unwrap(); + } + + #[test] + fn test_remove_connect_config_extension_keeps_header_after_last_extension() { + let config = r#"extensions: + sensors: + packages: + i2c-tools: '*' + avocado-ext-connect-config: + types: + - confext + version: "0.1.0" + overlay: overlay + +## +## Runtimes +## +runtimes: + dev: + extensions: + - sensors + - avocado-ext-connect-config +"#; + let expected = r#"extensions: + sensors: + packages: + i2c-tools: '*' + +## +## Runtimes +## +runtimes: + dev: + extensions: + - sensors +"#; + let (result, changed) = remove_connect_config_extension_in_yaml(config, "dev").unwrap(); + assert!(changed); + assert_eq!(result, expected); + let _: serde_yaml::Value = serde_yaml::from_str(&result).unwrap(); + } + #[test] fn test_remove_connect_config_extension_not_present() { let config = r#"runtimes: