team-ljyzipelo

공개
342줄11 KB
1#[test]
2fn extraction_completion_matches_actual_worker_selection_matrix() {
3 use std::collections::BTreeSet;
4
5 fn contents(root: &Path) -> BTreeSet<String> {
6 fn visit(root: &Path, current: &Path, found: &mut BTreeSet<String>) {
7 for entry in fs::read_dir(current).unwrap() {
8 let path = entry.unwrap().path();
9 found.insert(
10 path.strip_prefix(root)
11 .unwrap()
12 .to_str()
13 .unwrap()
14 .replace('\\', "/"),
15 );
16 if path.is_dir() {
17 visit(root, &path, found);
18 }
19 }
20 }
21 let mut found = BTreeSet::new();
22 visit(root, root, &mut found);
23 found
24 }
25
26 let scratch = Scratch::new();
27 let docs = make_directory(&scratch.path("docs"));
28 fs::create_dir(docs.join("empty")).unwrap();
29 fs::create_dir(docs.join("nested")).unwrap();
30 fs::write(docs.join("nested/one.txt"), b"nested content").unwrap();
31 let root_file = scratch.path("root.txt");
32 fs::write(&root_file, b"root content").unwrap();
33 let archive = scratch.path("selection.zip");
34 let (factory, observed) = observed_factory();
35 let mut screen = Screen::new(factory);
36 screen.app.add_sources(vec![docs, root_file]);
37 screen.render(vec![]);
38 screen.set_text("zip-name", "selection.zip");
39 screen.click("압축하기");
40 screen.settle();
41
42 let cases: [(&str, Option<&[&str]>, &[&str]); 6] = [
43 (
44 "all",
45 None,
46 &[
47 "docs",
48 "docs/empty",
49 "docs/nested",
50 "docs/nested/one.txt",
51 "root.txt",
52 ],
53 ),
54 (
55 "files",
56 Some(&["docs/nested/one.txt", "root.txt"]),
57 &["docs", "docs/nested", "docs/nested/one.txt", "root.txt"],
58 ),
59 (
60 "directory",
61 Some(&["docs"]),
62 &["docs", "docs/empty", "docs/nested", "docs/nested/one.txt"],
63 ),
64 (
65 "mixed",
66 Some(&["docs", "docs/nested/one.txt"]),
67 &["docs", "docs/empty", "docs/nested", "docs/nested/one.txt"],
68 ),
69 ("empty", Some(&["docs/empty"]), &["docs", "docs/empty"]),
70 (
71 "nested-directory",
72 Some(&["docs/nested"]),
73 &["docs", "docs/nested", "docs/nested/one.txt"],
74 ),
75 ];
76 for (name, selected, expected) in cases {
77 screen.open(&archive);
78 let ids = selected.map(|names| {
79 names
80 .iter()
81 .map(|name| {
82 screen
83 .app
84 .model
85 .entries
86 .iter()
87 .find(|entry| entry.name.trim_end_matches('/') == *name)
88 .unwrap_or_else(|| panic!("목록 항목 누락: {name}"))
89 .id
90 })
91 .collect::<BTreeSet<_>>()
92 });
93 if let Some(ids) = &ids {
94 screen.app.model.selected = ids.clone();
95 }
96 let output = make_directory(&scratch.path(name));
97 screen.extract(&output);
98 let terminal = screen.app.model.terminal.as_ref().unwrap();
99 assert_eq!(terminal.outcome, Outcome::Completed, "{name}");
100 assert!(screen.app.model.confirmed, "{name}");
101 assert!(terminal.error.is_none(), "{name}");
102 let expected = expected
103 .iter()
104 .map(|name| name.to_string())
105 .collect::<BTreeSet<_>>();
106 assert_eq!(contents(&output), expected, "{name}");
107 let reports = screen
108 .app
109 .model
110 .outputs
111 .iter()
112 .map(|(name, disposition)| {
113 assert_eq!(*disposition, Disposition::Created);
114 name.clone()
115 })
116 .collect::<BTreeSet<_>>();
117 assert_eq!(reports, expected, "{name}");
118 assert_eq!(terminal.created_count as usize, expected.len(), "{name}");
119 for (path, bytes) in [
120 ("docs/nested/one.txt", b"nested content".as_slice()),
121 ("root.txt", b"root content".as_slice()),
122 ] {
123 if expected.contains(path) {
124 assert_eq!(fs::read(output.join(path)).unwrap(), bytes, "{name}");
125 }
126 }
127 match observed.borrow().starts.last().unwrap() {
128 Start::Extract { selection, .. } => match (selection, &ids) {
129 (Selection::All, None) => {}
130 (Selection::EntryIds(actual), Some(ids)) => {
131 assert_eq!(
132 actual,
133 &zipelo_ui_memory_app::archive_browser::expand_selection(
134 &screen.app.model.entries,
135 ids
136 )
137 .into_iter()
138 .collect::<Vec<_>>(),
139 "{name}"
140 );
141 }
142 _ => panic!("요청 선택 범위 불일치: {name}"),
143 },
144 _ => panic!("추출 요청 누락: {name}"),
145 }
146 let shown = screen.render(vec![]);
147 assert!(
148 shown.iter().any(|(text, _)| text == "압축을 풀었습니다"),
149 "{name}: {shown:?}"
150 );
151 assert!(
152 !shown
153 .iter()
154 .any(|(text, _)| text.contains("저장이 끝났는지") || text.contains("일부 파일만")),
155 "{name}"
156 );
157 screen.click("확인");
158 }
159 assert_reaped(&observed);
160}
161
162#[cfg(unix)]
163#[test]
164fn auto_destination_is_private_under_desktop_umask() {
165 use std::{os::unix::fs::PermissionsExt, process::Command};
166
167 const CHILD: &str = "ZIPELO_PRIVATE_DESTINATION_CHILD";
168 if std::env::var_os(CHILD).is_none() {
169 let output = Command::new("sh")
170 .args([
171 "-c",
172 "umask 0002; exec \"$@\"",
173 "zipelo-private-destination",
174 ])
175 .arg(std::env::current_exe().unwrap())
176 .args([
177 "--exact",
178 "screen::auto_destination_is_private_under_desktop_umask",
179 "--nocapture",
180 ])
181 .env(CHILD, "1")
182 .output()
183 .unwrap();
184 assert!(
185 output.status.success(),
186 "umask 0002 추출 회귀 실패: {}{}",
187 String::from_utf8_lossy(&output.stdout),
188 String::from_utf8_lossy(&output.stderr)
189 );
190 return;
191 }
192
193 let mask = Command::new("sh").args(["-c", "umask"]).output().unwrap();
194 assert_eq!(String::from_utf8(mask.stdout).unwrap().trim(), "0002");
195 let scratch = Scratch::new();
196 let archive = scratch.path("sample.zip");
197 fs::copy(
198 Path::new(env!("CARGO_MANIFEST_DIR"))
199 .join("../../tests/rust-app-real-worker/fixtures/sample.zip"),
200 &archive,
201 )
202 .unwrap();
203 let output = scratch.path("sample");
204 assert!(!output.exists());
205 let (factory, observed) = observed_factory();
206 let mut screen = Screen::new(factory);
207 screen.open(&archive);
208 assert!(screen.app.begin_extract());
209 assert!(screen.app.model.busy());
210 screen.settle();
211 assert_eq!(
212 fs::metadata(&output).unwrap().permissions().mode() & 0o777,
213 0o700
214 );
215 assert_eq!(
216 fs::read(output.join("content.txt")).unwrap(),
217 b"Zipelo real worker flow\n"
218 );
219 assert_eq!(
220 screen.app.model.terminal.as_ref().unwrap().publication,
221 Publication::Complete
222 );
223 assert!(
224 screen
225 .render(vec![])
226 .iter()
227 .any(|(text, _)| text == "압축을 풀었습니다")
228 );
229 assert_reaped(&observed);
230}
231
232#[test]
233fn explorer_folder_click_and_escape_control_actual_extraction_scope() {
234 let scratch = Scratch::new();
235 let folder = make_directory(&scratch.path("docs"));
236 fs::write(folder.join("child.txt"), b"selected subtree").unwrap();
237 let outside = scratch.path("outside.txt");
238 fs::write(&outside, b"outside subtree").unwrap();
239 let archive = scratch.path("picked.zip");
240 let selected_output = make_directory(&scratch.path("selected-output"));
241 let all_output = make_directory(&scratch.path("all-output"));
242 let (factory, observed) = observed_factory();
243 let mut screen = Screen::new(factory);
244 screen.app.add_sources(vec![folder, outside]);
245 screen.render(vec![]);
246 screen.set_text("zip-name", "picked.zip");
247 screen.click("압축하기");
248 screen.settle();
249 screen.open(&archive);
250 screen.click("docs");
251 assert!(!screen.app.model.selected.is_empty());
252 screen.extract(&selected_output);
253 assert_eq!(
254 fs::read(selected_output.join("docs/child.txt")).unwrap(),
255 b"selected subtree"
256 );
257 assert!(!selected_output.join("outside.txt").exists());
258 assert!(matches!(
259 observed.borrow().starts.last(),
260 Some(Start::Extract {
261 selection: Selection::EntryIds(_),
262 ..
263 })
264 ));
265 screen.click("확인");
266 screen
267 .context
268 .memory_mut(|memory| memory.request_focus(egui::Id::unique("archive-list")));
269 screen.render(vec![]);
270 screen.escape();
271 assert!(screen.app.model.selected.is_empty());
272 screen.extract(&all_output);
273 assert_eq!(
274 fs::read(all_output.join("outside.txt")).unwrap(),
275 b"outside subtree"
276 );
277 assert_eq!(
278 fs::read(all_output.join("docs/child.txt")).unwrap(),
279 b"selected subtree"
280 );
281 assert!(matches!(
282 observed.borrow().starts.last(),
283 Some(Start::Extract {
284 selection: Selection::All,
285 ..
286 })
287 ));
288 assert_reaped(&observed);
289}
290
291// 작업자는 새로 만든 상위 폴더도 출력으로 알림. 하위 폴더 안 파일도 그 파일 자체를 열어야 함.
292#[test]
293fn double_click_open_extracts_nested_file_and_opens_that_file() {
294 let scratch = Scratch::new();
295 let folder = make_directory(&scratch.path("site"));
296 fs::write(folder.join("index.html"), b"<p>zipelo</p>").unwrap();
297 fs::write(folder.join("style.css"), b"p{}").unwrap();
298 let archive = scratch.path("site.zip");
299 let (factory, observed) = observed_factory();
300 let mut screen = Screen::new(factory);
301 let opened = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
302 let record = opened.clone();
303 screen.app.set_opener(Box::new(move |path| {
304 record.borrow_mut().push(path.to_path_buf());
305 Ok(())
306 }));
307 screen.app.add_sources(vec![folder]);
308 screen.render(vec![]);
309 screen.set_text("zip-name", "site.zip");
310 screen.click("압축하기");
311 screen.settle();
312 screen.open(&archive);
313 for name in ["site/index.html", "site/style.css"] {
314 let id = screen
315 .app
316 .model
317 .entries
318 .iter()
319 .find(|entry| entry.name == name)
320 .unwrap()
321 .id;
322 assert!(screen.app.open_entry(id));
323 screen.settle();
324 screen.render(vec![]);
325 }
326 let root = scratch.path(&zipelo_ui_memory_app::archive_present::open_root_name("site.zip"));
327 assert_eq!(
328 *opened.borrow(),
329 vec![
330 root.join("1/site/index.html"),
331 root.join("2/site/style.css")
332 ]
333 );
334 assert_eq!(
335 fs::read(root.join("1/site/index.html")).unwrap(),
336 b"<p>zipelo</p>"
337 );
338 assert!(!root.join("1/site/style.css").exists());
339 assert_reaped(&observed);
340 drop(screen);
341 assert!(!root.exists());
342}