diff --git a/crates/ui-egui/src/dialogs/script_run.rs b/crates/ui-egui/src/dialogs/script_run.rs index 690feebd62..b7f3897931 100644 --- a/crates/ui-egui/src/dialogs/script_run.rs +++ b/crates/ui-egui/src/dialogs/script_run.rs @@ -15,7 +15,9 @@ //! and dialogs it needs, features only the reference app has, and its includes (at most //! [`SHOWN_INCLUDES`], then the count of the rest). It says when the script is too large to check, //! compiled or not a script, when the check failed, and when a list was cut short at its limit. -//! The listing is information: Run runs the script as before. The growth question +//! The listing is information: Run runs the script as before, after checking it again. When the +//! report has changed since the warning opened, nothing runs and the warning opens again with the +//! new report. The growth question //! (`scriptGrowth`) and the question quitting asks (`scriptClose`) have the `choice` field alone. //! //! A script's dialog (kind `scriptDialog`) has the fields `kind` (alert, confirm or prompt), @@ -522,8 +524,14 @@ pub(crate) fn chosen(app: &mut VectorcraftApp, params: Value) -> Result Result { + let path = params.get("path").and_then(Value::as_str); + let name = params.get("name").and_then(Value::as_str).or(path).map_or_else(|| "script".to_string(), vectorcraft_engine::cmd::fileio::file_name); // The script's name is the message: it is what the person reads first. let prompt = StandardPrompt::custom(name) .title(tl!("Run Script")) @@ -537,6 +545,10 @@ pub(crate) fn chosen(app: &mut VectorcraftApp, params: Value) -> Result() + && let Some(RunWarning { params, reaches: listed }) = answer.take_tag::() { - let stored = if answer.skip { app.run("prefs.set", json!({"key": "showExternalScriptWarning", "value": false})).map(drop) } else { Ok(()) }; - if let Err(e) = stored.and_then(|()| app.run("script.run", params).map(drop)) { + // A script whose report changed while the warning was open is listed again before it runs. + let now = reaches(app, ¶ms); + let ran = if now == listed { + let stored = + if answer.skip { app.run("prefs.set", json!({"key": "showExternalScriptWarning", "value": false})).map(drop) } else { Ok(()) }; + stored.and_then(|()| app.run("script.run", params).map(drop)) + } else { + app.status(CHANGED_SINCE_WARNING); + warn(app, params, now).map(drop) + }; + if let Err(e) = ran { app.status(e); } } diff --git a/crates/ui-egui/src/tests/script_run.rs b/crates/ui-egui/src/tests/script_run.rs index 0f26951362..11d9a3ed0c 100644 --- a/crates/ui-egui/src/tests/script_run.rs +++ b/crates/ui-egui/src/tests/script_run.rs @@ -541,6 +541,28 @@ fn the_warning_lists_what_a_scripts_included_files_reach() { let _ = std::fs::remove_dir_all(&dir); } +/// A script file that changes what it reaches while its warning is open does not run on Run: the +/// warning opens again with the new report, and the next Run runs it. +#[test] +fn run_lists_a_script_again_when_it_changed_after_its_warning_opened() { + let mut app = app(); + let dir = folder("changed"); + let path = dir.join("changed.jsx"); + std::fs::write(&path, DRAW).unwrap(); + menus::run_ui_command(&mut app, "ui.runScript", &json!({"path": path.to_string_lossy()})).unwrap().unwrap(); + assert_eq!(fields(&app, KIND)["reaches"]["outside"], json!([])); + std::fs::write(&path, format!("{DRAW}\nvar s = new Socket();\n")).unwrap(); + assert!(confirm(&mut app, KIND)); + assert!(!app.session.script_running()); + assert!(is_open::(&app)); + assert_eq!(app.ui.status, script_run::CHANGED_SINCE_WARNING); + assert_eq!(fields(&app, KIND)["reaches"]["outside"][0]["id"], "network"); + assert!(confirm(&mut app, KIND)); + assert!(app.session.script_running()); + finish(&mut app); + let _ = std::fs::remove_dir_all(&dir); +} + /// A script with more includes than the warning lists gives the count of the rest. #[test] fn the_warning_counts_the_includes_it_does_not_list() { diff --git a/docs/mcp.md b/docs/mcp.md index 8ef2380bca..5cd7f9210f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2643,8 +2643,9 @@ files it opens in other apps, messages to other apps, the network, native code, app's preferences and code the checker can't read), the files and dialogs it needs, features only the reference app has, and the files its `#include` lines name (the first 20, then the count of the rest; VectorCraft's own limit). It says when the script is too large to check, compiled or not a script, when the check failed, and when a list was cut -short at its limit. The listing does not stop the run. A -script from the Scripts folder runs without the warning. +short at its limit. The listing does not stop the run. Run checks the script again, and when the report has changed +since the warning opened, nothing runs and the warning opens again with the new report. A script from the Scripts +folder runs without the warning. Pass exactly one of `path` and `source`. A `path` must end in `.js` or `.jsx`, in any case. A `.jsxbin` file or text that starts with `@JSXBIN@` is compiled and ends with an error, as does text that is not UTF-8. The language is ES5.1, except that identifiers take the characters Unicode's `ID_Start` and