mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-12 08:51:11 +00:00
Merge pull request #2128 from Silvertree2010/fix/h12225-file-openrecent-silently-turns-a
Reject negative and fractional index in file.openRecent
This commit is contained in:
2 files changed
+20
-2
No files matched your search
@@ -30,8 +30,9 @@ pub(super) fn open_recent(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
allow(&path, Access::Read)?;
|
||||
Some(path)
|
||||
}
|
||||
(None, Some(i)) => {
|
||||
Some(s.prefs.recent_files.iter().filter(|r| listed(r)).nth(i.max(0.0) as usize).cloned().ok_or_else(|| bad(CMD, "no such recent file"))?)
|
||||
(None, Some(_)) => {
|
||||
let i = req_index(CMD, p, "index")?;
|
||||
Some(s.prefs.recent_files.iter().filter(|r| listed(r)).nth(i).cloned().ok_or_else(|| bad(CMD, "no such recent file"))?)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
@@ -84,3 +84,20 @@ fn outlined_layers_draw_as_outlines_and_the_rest_normally() {
|
||||
assert_eq!(outlined.pixel(20, 20).unwrap(), empty, "only its outline");
|
||||
assert_eq!(outlined.pixel(80, 20).unwrap(), plain.pixel(80, 20).unwrap(), "the other layer draws normally");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_recent_rejects_negative_and_fractional_index() {
|
||||
let dir = std::env::temp_dir().join(format!("figcraft-recent-bad-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let a = dir.join("a.figcraft").to_string_lossy().to_string();
|
||||
let b = dir.join("b.figcraft").to_string_lossy().to_string();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.save", &json!({"path": a})).unwrap();
|
||||
s.execute("file.saveAs", &json!({"path": b})).unwrap();
|
||||
let recent = s.prefs.recent_files.clone();
|
||||
for i in [json!(-1), json!(1.5), json!(-0.5)] {
|
||||
assert!(s.execute("file.openRecent", &json!({"index": i})).is_err(), "index {i} must be an error");
|
||||
}
|
||||
assert_eq!(s.prefs.recent_files, recent);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user