forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

relative_sources.rs107 lines, 4.2 KiBHistoryRawDownload
1//! Source paths in a saved project: a file inside the project's directory tree
2//! is written relative to the project, however its path was spelled, and so a
3//! folder of media and its project can be copied anywhere and still open with
4//! nothing offline.
5
6use std::path::{Path, PathBuf};
7
8use engine::PlaybackSession;
9use engine::scratch::Scratch;
10
11fn asset(name: &str) -> PathBuf {
12 Path::new(env!("CARGO_MANIFEST_DIR"))
13 .join("../../assets")
14 .join(name)
15}
16
17/// A project at `dir/edit.edith` whose two sources both sit in `dir/media/`,
18/// one of them placed on the timeline after the other.
19fn project_with_media_in_a_subfolder(dir: &Path) -> PathBuf {
20 std::fs::create_dir_all(dir.join("media")).expect("the media folder");
21 std::fs::copy(asset("test_av.mp4"), dir.join("media/a.mp4")).expect("copy a");
22 std::fs::copy(asset("test_av2.mp4"), dir.join("media/b.mp4")).expect("copy b");
23 engine::ao::set_silent_output();
24 let mut session = PlaybackSession::open(dir.join("media/a.mp4")).expect("open a");
25 let second = dir.join("media/b.mp4");
26 session.import(&second).expect("import b");
27 let end = session.timeline_duration();
28 assert!(
29 session
30 .place_stream_at(end, &second, 0, None)
31 .expect("place b")
32 );
33 let project = dir.join("edit.edith");
34 session.save_project(&project).expect("save");
35 project
36}
37
38#[test]
39fn media_in_a_subfolder_is_saved_relative_and_survives_a_copy() {
40 let from = Scratch::dir("ve_relative_from");
41 let project = project_with_media_in_a_subfolder(&from);
42 let text = std::fs::read_to_string(&project).unwrap();
43 assert!(
44 text.contains("media/a.mp4") && text.contains("media/b.mp4"),
45 "the sources are written relative to the project:\n{text}"
46 );
47 assert!(
48 !text.contains(from.to_str().unwrap()),
49 "no absolute path into the original folder:\n{text}"
50 );
51
52 // Copy the whole folder somewhere else, then remove the original: nothing
53 // absolute can still resolve.
54 let to = Scratch::dir("ve_relative_to");
55 std::fs::create_dir_all(to.join("media")).unwrap();
56 for name in ["media/a.mp4", "media/b.mp4", "edit.edith"] {
57 std::fs::copy(from.join(name), to.join(name)).expect("copy");
58 }
59 std::fs::remove_dir_all(&from).expect("remove the original");
60
61 let moved = PlaybackSession::open_project(&to.join("edit.edith")).expect("open the copy");
62 assert!(
63 moved.offline_sources().is_empty(),
64 "offline after a copy: {:?}",
65 moved.offline_sources()
66 );
67 assert!(moved.sources().iter().all(|s| s.path.starts_with(&to)));
68}
69
70#[test]
71fn a_project_opened_through_a_symlink_saves_relative_sources() {
72 let real = Scratch::dir("ve_relative_real");
73 let project = project_with_media_in_a_subfolder(&real);
74 let link = real.with_extension("link");
75 std::os::unix::fs::symlink(&real, &link).expect("symlink");
76
77 // Opened through the link, the sources are reached through it too; saving
78 // must still write them relative to the project.
79 let via = PlaybackSession::open_project(&link.join("edit.edith")).expect("open via link");
80 let out = link.join("again.edith");
81 via.save_project(&out).expect("save via link");
82 let text = std::fs::read_to_string(&out).unwrap();
83 assert!(
84 text.contains("source 0 media/a.mp4") && text.contains("media/b.mp4"),
85 "relative through a link:\n{text}"
86 );
87 assert!(!text.contains(real.to_str().unwrap()), "absolute:\n{text}");
88 std::fs::remove_file(&link).ok();
89 drop(project);
90}
91
92#[test]
93fn a_file_outside_the_project_is_still_written_absolute() {
94 let dir = Scratch::dir("ve_relative_outside");
95 std::fs::create_dir_all(dir.join("proj")).unwrap();
96 std::fs::copy(asset("test_av.mp4"), dir.join("elsewhere.mp4")).unwrap();
97 engine::ao::set_silent_output();
98 let session = PlaybackSession::open(dir.join("elsewhere.mp4")).expect("open");
99 let project = dir.join("proj/edit.edith");
100 session.save_project(&project).expect("save");
101 let text = std::fs::read_to_string(&project).unwrap();
102 let absolute = dir.join("elsewhere.mp4");
103 assert!(
104 text.contains(absolute.to_str().unwrap()),
105 "outside the tree stays absolute:\n{text}"
106 );
107}