From b3d52fc272b6599b0a86353520998645111d8a13 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:08:57 +0100 Subject: [PATCH] Import Data Sets: greyed until the active document has a variable (#13920) File > Import > Variable Data Sets was enabled on any open document, so importing into one without variables made data sets with no values. Gate it on the active document defining at least one variable, as the export counterparts already gate on data sets. --- crates/engine/src/cmd/variables_cmds.rs | 13 +++ .../variables_cmds/import_enabled_tests.rs | 87 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 crates/engine/src/cmd/variables_cmds/import_enabled_tests.rs diff --git a/crates/engine/src/cmd/variables_cmds.rs b/crates/engine/src/cmd/variables_cmds.rs index c56ee0f6c04..88882446784 100644 --- a/crates/engine/src/cmd/variables_cmds.rs +++ b/crates/engine/src/cmd/variables_cmds.rs @@ -27,6 +27,16 @@ fn has_data_sets(s: &Session) -> std::result::Result<(), String> { Ok(()) } +/// File > Import > Variable Data Sets needs variables to fill: without any, every header column +/// is an unknown name and the import makes empty data sets (#13920). +fn has_variables(s: &Session) -> std::result::Result<(), String> { + let st = s.active().ok_or_else(|| "no document".to_string())?; + if st.doc.variables.defs.is_empty() { + return Err("no variables in document".into()); + } + Ok(()) +} + fn bad(cmd: &str, msg: impl Into) -> EngineError { EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } } @@ -530,6 +540,7 @@ pub fn specs() -> Vec { "Variable Data Sets…", &["File", "Import"], "{path, delimiter?} → {imported}: CSV header = variable names, each row a data set (first column may be the data-set name)", + has_variables, |s, p| import_data_sets(s, p) ), spec!( @@ -553,5 +564,7 @@ pub fn specs() -> Vec { ] } +#[cfg(test)] +mod import_enabled_tests; #[cfg(test)] mod tests; diff --git a/crates/engine/src/cmd/variables_cmds/import_enabled_tests.rs b/crates/engine/src/cmd/variables_cmds/import_enabled_tests.rs new file mode 100644 index 00000000000..823a226a72b --- /dev/null +++ b/crates/engine/src/cmd/variables_cmds/import_enabled_tests.rs @@ -0,0 +1,87 @@ +//! #13920: File > Import > Variable Data Sets is greyed until the active document has a variable. + +use super::*; + +const IMPORT: &str = "file.import.variableDataSets"; + +fn new_doc(s: &mut Session) { + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); +} + +fn define_variable(s: &mut Session) { + let layer = s.active().unwrap().doc.layers[0].id; + s.execute("image.variables.define", json!({"defs": [{"name": "show", "layer": layer.0, "type": "visibility"}]})).unwrap(); +} + +fn refusal(s: &mut Session) -> String { + let path = std::env::temp_dir().join(format!("pc-vars-{}-import-enabled.csv", std::process::id())); + match s.execute(IMPORT, json!({"path": path.to_string_lossy()})) { + Err(EngineError::Disabled(id, why)) => { + assert_eq!(id, IMPORT); + why + } + other => panic!("expected a refusal, got {other:?}"), + } +} + +/// The other File > Import entries, with whether each is enabled now. +fn other_imports(s: &Session) -> Vec<(&'static str, bool)> { + crate::cmd::command_specs().iter().filter(|c| c.menu == ["File", "Import"] && c.id != IMPORT).map(|c| (c.id, s.is_enabled(c.id))).collect() +} + +#[test] +fn import_is_disabled_without_a_document() { + let mut s = Session::new(); + assert!(!s.is_enabled(IMPORT)); + assert_eq!(refusal(&mut s), "no document"); +} + +#[test] +fn import_is_disabled_without_variables_and_leaves_the_document_alone() { + let mut s = Session::new(); + new_doc(&mut s); + assert!(!s.is_enabled(IMPORT)); + assert_eq!(refusal(&mut s), "no variables in document"); + assert!(s.active().unwrap().doc.variables.data_sets.is_empty()); +} + +#[test] +fn import_is_enabled_with_a_variable_and_no_data_sets() { + let mut s = Session::new(); + new_doc(&mut s); + define_variable(&mut s); + assert!(s.active().unwrap().doc.variables.data_sets.is_empty()); + assert!(s.is_enabled(IMPORT)); + + let path = std::env::temp_dir().join(format!("pc-vars-{}-import-enabled-ok.csv", std::process::id())); + std::fs::write(&path, "show\ntrue\nfalse\n").unwrap(); + let r = s.execute(IMPORT, json!({"path": path.to_string_lossy()})).unwrap(); + let _ = std::fs::remove_file(&path); + assert_eq!(r["imported"], 2); +} + +#[test] +fn import_follows_the_active_document() { + let mut s = Session::new(); + new_doc(&mut s); + define_variable(&mut s); + new_doc(&mut s); + // The new, active document has no variables; the one behind it does. + assert!(!s.is_enabled(IMPORT)); + assert_eq!(refusal(&mut s), "no variables in document"); + + assert!(s.set_active(0)); + assert!(s.is_enabled(IMPORT)); + assert!(s.set_active(1)); + assert!(!s.is_enabled(IMPORT)); +} + +#[test] +fn other_import_entries_do_not_depend_on_variables() { + let mut s = Session::new(); + new_doc(&mut s); + let without = other_imports(&s); + assert!(!without.is_empty(), "File > Import has other entries"); + define_variable(&mut s); + assert_eq!(other_imports(&s), without); +}