| ... | ... | @@ -19,6 +19,60 @@ fn cached_sections_refuse_links_outside_the_notebook_and_dangling_links() { |
| 19 | 19 | assert!(notebook.section(path, || {}).is_err()); |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | #[test] |
| 23 | fn renaming_an_unlisted_section_creates_its_toc_entry_and_preserves_its_neighbor() { |
| 24 | let temporary = tempfile::tempdir().unwrap(); |
| 25 | let folder = temporary.path().join("notebook"); |
| 26 | let cache = temporary.path().join("cache"); |
| 27 | let page = PageCreation::new(None, Some("Garden plan"), "Fixture").unwrap(); |
| 28 | let mut notebook = Notebook::create(&folder, &cache, Notebook::NEW_COLOR, &page).unwrap(); |
| 29 | let original = notebook.catalog().sections[0].path.clone(); |
| 30 | let other = notebook.create_section("", "Other", &page).unwrap(); |
| 31 | let identity = notebook.section_path(&original).unwrap().file_id; |
| 32 | let neighbor = notebook.section_path(&other).unwrap().file_id; |
| 33 | drop(notebook); |
| 34 | fs::remove_file(folder.join(TOC)).unwrap(); |
| 35 | let mut notebook = Notebook::open(&folder, &cache).unwrap(); |
| 36 | assert!(notebook.catalog().toc.is_none()); |
| 37 | assert_eq!(notebook.catalog().sections.len(), 2); |
| 38 | assert_eq!( |
| 39 | notebook.rename(&original, "Delayed section").unwrap(), |
| 40 | "Delayed section.one" |
| 41 | ); |
| 42 | assert!(!folder.join(&original).exists()); |
| 43 | let check = |notebook: &Notebook| { |
| 44 | assert_eq!(notebook.catalog().sections.len(), 2); |
| 45 | assert!(notebook.section_path(&original).is_err()); |
| 46 | assert_eq!( |
| 47 | notebook |
| 48 | .section_path("Delayed section.one") |
| 49 | .unwrap() |
| 50 | .file_id, |
| 51 | identity |
| 52 | ); |
| 53 | assert_eq!(notebook.section_path(&other).unwrap().file_id, neighbor); |
| 54 | let toc = notebook.read_toc("").unwrap(); |
| 55 | assert!(lists(&toc, identity).unwrap()); |
| 56 | assert_eq!( |
| 57 | stored_pages(&notebook.read_section("Delayed section.one").unwrap()).unwrap()[0] |
| 58 | .page |
| 59 | .title, |
| 60 | "Garden plan" |
| 61 | ); |
| 62 | }; |
| 63 | check(&notebook); |
| 64 | drop(notebook); |
| 65 | check(&Notebook::open(&folder, temporary.path().join("cold-cache")).unwrap()); |
| 66 | if let Some(export) = std::env::var_os("SNOWBOUND_RENAME_EXPORT") { |
| 67 | let export = Path::new(&export); |
| 68 | fs::create_dir_all(export).unwrap(); |
| 69 | for entry in fs::read_dir(&folder).unwrap() { |
| 70 | let entry = entry.unwrap(); |
| 71 | fs::copy(entry.path(), export.join(entry.file_name())).unwrap(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 22 | 76 | /// A mounted notebook where another writer commits `revision` to the section file just |
| 23 | 77 | /// before Snowbound's rewritten file takes its place. |
| 24 | 78 | struct Racing { |