mirror of
https://github.com/storytold/figcraft.git
synced 2026-10-25 02:47:02 -06:00
Merge pull request #608 from Silvertree2010/fix/h1871-file-close-with-a-negative-index
Reject negative or fractional index in file.close
This commit is contained in:
@@ -1280,7 +1280,7 @@ fn append_pages(doc: &mut Document, other: &Document) -> Result<Vec<NodeId>> {
|
||||
}
|
||||
|
||||
fn close(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let i = num(p, "index").map(|x| x as usize).unwrap_or(s.active);
|
||||
let i = if get(p, "index").is_none() { s.active } else { req_index("file.close", p, "index")? };
|
||||
let Some(ds) = s.docs.get(i) else { return Err(bad("file.close", "no such document")) };
|
||||
// A branch tab's edits are in its main file already (which then has unsaved changes).
|
||||
if ds.branch_of.is_none() && ds.is_dirty() && !boolean(p, "force").unwrap_or(false) {
|
||||
|
||||
@@ -49,3 +49,16 @@ fn an_intact_file_opens_without_a_message() {
|
||||
assert!(s.message.is_none());
|
||||
let _ = std::fs::remove_dir_all(path.parent().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_close_rejects_a_bad_index_instead_of_closing_another_document() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let docs = s.docs.len();
|
||||
for index in [json!(-1), json!(1.5), json!(99)] {
|
||||
let r = s.execute("file.close", &json!({"index": index, "force": true}));
|
||||
assert!(r.is_err(), "index {index} must be rejected, got {r:?}");
|
||||
assert_eq!(s.docs.len(), docs, "index {index} closed a document");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user