| 1 | mod root { |
| 2 | use rust_apt::config::Config; |
| 3 | use rust_apt::new_cache; |
| 4 | use rust_apt::progress::{AcquireProgress, DynAcquireProgress, InstallProgress}; |
| 5 | use rust_apt::raw::{AcqTextStatus, ItemDesc, ItemState, PkgAcquire}; |
| 6 | use rust_apt::util::*; |
| 7 | |
| 8 | #[test] |
| 9 | fn lock() { |
| 10 | apt_lock().unwrap(); |
| 11 | apt_lock().unwrap(); |
| 12 | assert!(apt_is_locked()); |
| 13 | |
| 14 | apt_unlock(); |
| 15 | assert!(apt_is_locked()); |
| 16 | |
| 17 | apt_unlock(); |
| 18 | assert!(!apt_is_locked()); |
| 19 | } |
| 20 | |
| 21 | #[test] |
| 22 | fn update() { |
| 23 | struct Progress {} |
| 24 | |
| 25 | impl DynAcquireProgress for Progress { |
| 26 | fn pulse_interval(&self) -> usize { 0 } |
| 27 | |
| 28 | fn hit(&mut self, item: &ItemDesc) { |
| 29 | println!("\rHit:{} {}", item.owner().id(), item.description()); |
| 30 | } |
| 31 | |
| 32 | fn fetch(&mut self, item: &ItemDesc) { |
| 33 | let mut string = format!("\rGet:{} {}", item.owner().id(), item.description()); |
| 34 | |
| 35 | let file_size = item.owner().file_size(); |
| 36 | if file_size != 0 { |
| 37 | string.push_str(&format!(" [{}]", unit_str(file_size, NumSys::Decimal))); |
| 38 | } |
| 39 | |
| 40 | println!("{string}"); |
| 41 | } |
| 42 | |
| 43 | fn done(&mut self, _item: &ItemDesc) {} |
| 44 | |
| 45 | fn start(&mut self) {} |
| 46 | |
| 47 | fn stop(&mut self, owner: &AcqTextStatus) { |
| 48 | if owner.fetched_bytes() != 0 { |
| 49 | println!( |
| 50 | "Fetched {} in {} ({}/s)", |
| 51 | unit_str(owner.fetched_bytes(), NumSys::Decimal), |
| 52 | time_str(owner.elapsed_time()), |
| 53 | unit_str(owner.current_cps(), NumSys::Decimal) |
| 54 | ); |
| 55 | } else { |
| 56 | println!("Nothing to fetch."); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn fail(&mut self, item: &ItemDesc) { |
| 61 | let mut show_error = true; |
| 62 | let error_text = item.owner().error_text(); |
| 63 | let desc = format!("{} {}", item.owner().id(), item.description()); |
| 64 | |
| 65 | match item.owner().status() { |
| 66 | ItemState::StatIdle | ItemState::StatDone => { |
| 67 | println!("\rIgn: {desc}"); |
| 68 | if error_text.is_empty() |
| 69 | || Config::new().bool("Acquire::Progress::Ignore::ShowErrorText", false) |
| 70 | { |
| 71 | show_error = false; |
| 72 | } |
| 73 | }, |
| 74 | _ => { |
| 75 | println!("\rErr: {desc}"); |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | if show_error { |
| 80 | println!("\r{error_text}"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn pulse(&mut self, _status: &AcqTextStatus, _owner: &PkgAcquire) {} |
| 85 | } |
| 86 | |
| 87 | let cache = new_cache!().unwrap(); |
| 88 | |
| 89 | // Test the default implementation for it |
| 90 | let mut progress = AcquireProgress::apt(); |
| 91 | cache.update(&mut progress).unwrap(); |
| 92 | |
| 93 | let cache = new_cache!().unwrap(); |
| 94 | |
| 95 | // Test a new impl for AcquireProgress |
| 96 | let mut progress = AcquireProgress::new(Progress {}); |
| 97 | cache.update(&mut progress).unwrap(); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn install_and_remove() { |
| 102 | let cache = new_cache!().unwrap(); |
| 103 | |
| 104 | let pkg = cache.get("neofetch").unwrap(); |
| 105 | |
| 106 | pkg.protect(); |
| 107 | pkg.mark_install(true, true); |
| 108 | cache.resolve(false).unwrap(); |
| 109 | dbg!(pkg.marked_install()); |
| 110 | |
| 111 | let mut progress = AcquireProgress::apt(); |
| 112 | let mut inst_progress = InstallProgress::apt(); |
| 113 | |
| 114 | cache.commit(&mut progress, &mut inst_progress).unwrap(); |
| 115 | // After commit a new cache must be created for more operations |
| 116 | let cache = new_cache!().unwrap(); |
| 117 | |
| 118 | // I need to pick a better package. This removes my neofetch every time! |
| 119 | let pkg = cache.get("neofetch").unwrap(); |
| 120 | |
| 121 | pkg.mark_delete(true); |
| 122 | |
| 123 | cache.commit(&mut progress, &mut inst_progress).unwrap(); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn install_with_debs() { |
| 128 | let debs = [ |
| 129 | "tests/files/cache/dep-pkg1_0.0.1.deb", |
| 130 | "tests/files/cache/dep-pkg2_0.0.1.deb", |
| 131 | ]; |
| 132 | let cache = new_cache!(&debs).unwrap(); |
| 133 | |
| 134 | let pkg1 = cache.get("dep-pkg1").unwrap(); |
| 135 | let pkg2 = cache.get("dep-pkg2").unwrap(); |
| 136 | |
| 137 | pkg1.mark_install(true, true); |
| 138 | pkg2.mark_install(true, true); |
| 139 | cache.resolve(false).unwrap(); |
| 140 | |
| 141 | let mut progress = AcquireProgress::apt(); |
| 142 | let mut inst_progress = InstallProgress::apt(); |
| 143 | cache.commit(&mut progress, &mut inst_progress).unwrap(); |
| 144 | |
| 145 | // You have to get a new cache after using commit. |
| 146 | let cache = new_cache!(&debs).unwrap(); |
| 147 | |
| 148 | // New packages will be required as well. |
| 149 | let pkg1 = cache.get("dep-pkg1").unwrap(); |
| 150 | let pkg2 = cache.get("dep-pkg2").unwrap(); |
| 151 | |
| 152 | // Leave no trace |
| 153 | pkg1.mark_delete(true); |
| 154 | pkg2.mark_delete(true); |
| 155 | |
| 156 | cache.commit(&mut progress, &mut inst_progress).unwrap(); |
| 157 | } |
| 158 | } |