Skip to content

Commit de22c83

Browse files
committed
57: fix DeleteColumnByNameG case
- two columns matching the glob should be removed - but the DELETED marker matches the glob pattern.
1 parent 3ad619b commit de22c83

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

src/csv/preprocessing.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ fn delete_column_name_glob(table: &mut Table, name: &str) -> Result<(), csv::Err
210210
})?;
211211

212212
if let Some(c) = table.columns.iter_mut().find(|col| {
213-
col.header.as_deref().unwrap_or_default() == name
214-
|| pattern.matches(col.header.as_deref().unwrap_or_default())
213+
let header = col.header.as_deref().unwrap_or_default();
214+
header != "DELETED" && (header == name || pattern.matches(header))
215215
}) {
216216
c.delete_contents();
217217
}
@@ -793,6 +793,45 @@ mod tests {
793793
assert!(!table.columns[3].rows.iter().all(|v| *v == Value::deleted()));
794794
}
795795

796+
#[test]
797+
fn test_delete_column_by_name_glob_twice_does_not_rematch_deleted() {
798+
// Scenario: columns ["Data A", "Data B", "Other"], call DeleteColumnByNameG("D*") twice.
799+
// Step 1 should delete "Data A", step 2 should delete "Data B".
800+
// Bug: "D*" also matches "DELETED", so step 2 re-matches the already-deleted column.
801+
let content = "Data A;Data B;Other\n1;2;3";
802+
let mut table = table_from_string(content);
803+
extract_headers(&mut table).unwrap();
804+
805+
// First call: should delete "Data A" (first match left-to-right)
806+
delete_column_name_glob(&mut table, "D*").unwrap();
807+
assert_eq!(
808+
table.columns[0].header.as_deref().unwrap(),
809+
"DELETED",
810+
"First call should delete 'Data A'"
811+
);
812+
assert_eq!(
813+
table.columns[1].header.as_deref().unwrap(),
814+
"Data B",
815+
"'Data B' should survive the first call"
816+
);
817+
818+
// Second call: should delete "Data B" (next match), NOT re-match "DELETED"
819+
delete_column_name_glob(&mut table, "D*").unwrap();
820+
assert_eq!(
821+
table.columns[1].header.as_deref().unwrap(),
822+
"DELETED",
823+
"Second call should delete 'Data B', not re-match the already-deleted column"
824+
);
825+
826+
// "Other" should be untouched
827+
assert_eq!(
828+
table.columns[2].header.as_deref().unwrap(),
829+
"Other",
830+
"'Other' should not be affected"
831+
);
832+
assert!(!table.columns[2].rows.iter().all(|v| *v == Value::deleted()));
833+
}
834+
796835
#[test]
797836
fn test_delete_column_by_name_glob_pattern_match() {
798837
let content = "Center x [mm];Center y [mm];Center z [mm];Other column\n1;2;3;4";

0 commit comments

Comments
 (0)