| 1 | module main |
| 2 | |
| 3 | import json2 |
| 4 | import os |
| 5 | |
| 6 | fn test_parse_cache_file() { |
| 7 | cases := { |
| 8 | 'zsh-5.9.2-1-x86_64.pkg.tar.zst': 'zsh 5.9.2-1' |
| 9 | '/var/cache/pacman/pkg/zsh-5.9.2-1-x86_64.pkg.tar.zst': 'zsh 5.9.2-1' |
| 10 | 'gcc-libs-14.2.1+r1-1-x86_64.pkg.tar.zst': 'gcc-libs 14.2.1+r1-1' |
| 11 | 'vim-2:9.1.0-1-x86_64.pkg.tar.zst': 'vim 2:9.1.0-1' |
| 12 | 'python-3.12.7-1-x86_64.pkg.tar.xz': 'python 3.12.7-1' |
| 13 | // dashes in the cache dir are not part of the name |
| 14 | '/tmp/a-dir-with-dashes/foo-1.0-1-any.pkg.tar.zst': 'foo 1.0-1' |
| 15 | } |
| 16 | for file, want in cases { |
| 17 | cf := parse_cache_file(file) or { |
| 18 | assert false, '${file} did not parse' |
| 19 | continue |
| 20 | } |
| 21 | assert '${cf.name} ${cf.version}' == want, file |
| 22 | assert cf.file == file |
| 23 | } |
| 24 | for bad in ['broken.pkg.tar.zst', 'a-1-x.pkg.tar.zst', '-1-1-any.pkg.tar.zst'] { |
| 25 | if cf := parse_cache_file(bad) { |
| 26 | assert false, '${bad} parsed as ${cf.name} ${cf.version}' |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | fn test_installed_file() { |
| 32 | prefixes := ['zsh-5.9.2-1-', 'gcc-libs-14.2.1-1-'] |
| 33 | // the cache is listed as full paths: the file name is what counts |
| 34 | assert installed_file('/var/cache/pacman/pkg/zsh-5.9.2-1-x86_64.pkg.tar.zst', prefixes) |
| 35 | assert installed_file('zsh-5.9.2-1-x86_64.pkg.tar.zst', prefixes) |
| 36 | assert installed_file('/c/gcc-libs-14.2.1-1-x86_64.pkg.tar.zst', prefixes) |
| 37 | assert !installed_file('/var/cache/pacman/pkg/zsh-5.9.1-1-x86_64.pkg.tar.zst', prefixes) |
| 38 | assert !installed_file('/c/zsh-completions-0.35-1-any.pkg.tar.zst', prefixes) |
| 39 | // a dir that looks like the prefix does not make its files installed |
| 40 | assert !installed_file('/c/zsh-5.9.2-1-x/foo-1.0-1-any.pkg.tar.zst', prefixes) |
| 41 | assert !installed_file('/c/zsh-5.9.2-1-x86_64.pkg.tar.zst', []) |
| 42 | // an uncompressed archive is one too |
| 43 | assert installed_file('/c/zsh-5.9.2-1-x86_64.pkg.tar', prefixes) |
| 44 | assert is_pkg_archive('zsh-5.9.2-1-x86_64.pkg.tar') && is_pkg_archive('a-1-1-any.pkg.tar.zst') |
| 45 | assert !is_pkg_archive('a-1-1-any.pkg.tar.zst.sig') && !is_pkg_archive('core.db') |
| 46 | // a package whose name begins with another's name-version- is its own: |
| 47 | // zsh-5.9.2-1-extras 2-1 is not zsh 5.9.2-1 |
| 48 | assert !installed_file('/c/zsh-5.9.2-1-extras-2-1-any.pkg.tar.zst', prefixes) |
| 49 | } |
| 50 | |
| 51 | // an unreadable cache dir does not stop the cleaning of the ones that read: |
| 52 | // the files of the rest come through, spelled out whole |
| 53 | fn test_cached_pkg_files_skips_a_lost_cache_dir() ! { |
| 54 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_cache_${os.getpid()}') |
| 55 | real := os.join_path(tmp, 'pkg') + '/' |
| 56 | os.mkdir_all(real)! |
| 57 | defer { |
| 58 | os.rmdir_all(tmp) or {} |
| 59 | } |
| 60 | os.write_file(os.join_path(real, 'zsh-5.9.2-1-x86_64.pkg.tar.zst'), '.')! |
| 61 | os.write_file(os.join_path(real, 'zsh-5.9.2-1-x86_64.pkg.tar.zst.sig'), '.')! |
| 62 | |
| 63 | mut old := cfg_cachedirs.clone() |
| 64 | defer { cfg_cachedirs = old.clone() } |
| 65 | cfg_cachedirs = [real, '/nonexistent-vpac-test/'] |
| 66 | files := cached_pkg_files()! |
| 67 | assert files == ['${real}zsh-5.9.2-1-x86_64.pkg.tar.zst'], files.str() |
| 68 | // the spelled-out path is a real one |
| 69 | assert os.exists(files[0]) |
| 70 | } |
| 71 | |
| 72 | // kept_names is the set paccache_keep keeps, as sorted file names |
| 73 | fn kept_names(kept map[string]bool) []string { |
| 74 | mut names := map[string]bool{} |
| 75 | for f, on in kept { |
| 76 | if !on { |
| 77 | continue |
| 78 | } |
| 79 | names[os.base(f).all_before('.pkg.tar.')] = true |
| 80 | } |
| 81 | mut out := names.keys() |
| 82 | out.sort() |
| 83 | return out |
| 84 | } |
| 85 | |
| 86 | fn test_paccache_keep() { |
| 87 | files := [ |
| 88 | '/c/foo-1.9-1-any.pkg.tar.zst', |
| 89 | '/c/foo-1.10-1-any.pkg.tar.zst', |
| 90 | '/c/zsh-5.9.1-1-x86_64.pkg.tar.zst', |
| 91 | '/c/zsh-5.9.2-1-x86_64.pkg.tar.zst', |
| 92 | '/c/zsh-6.0-1-x86_64.pkg.tar.zst', |
| 93 | ] |
| 94 | installed := ['zsh-5.9.2-1-'] |
| 95 | // newest by pacman's vercmp: 1.10 is newer than 1.9. the installed |
| 96 | // version is always kept, and it counts among the newest |
| 97 | assert kept_names(paccache_keep(1, files, installed)) == ['foo-1.10-1-any', 'zsh-5.9.2-1-x86_64', |
| 98 | 'zsh-6.0-1-x86_64'] |
| 99 | assert kept_names(paccache_keep(2, files, installed)) == ['foo-1.10-1-any', 'foo-1.9-1-any', |
| 100 | 'zsh-5.9.2-1-x86_64', 'zsh-6.0-1-x86_64'] |
| 101 | assert kept_names(paccache_keep(3, files, installed)).len == 5 |
| 102 | assert kept_names(paccache_keep(1, files, [])) == ['foo-1.10-1-any', 'zsh-6.0-1-x86_64'] |
| 103 | } |
| 104 | |
| 105 | fn test_clean_event_build_root() ! { |
| 106 | saved := cfg_cachedirs.clone() |
| 107 | defer { cfg_cachedirs = saved.clone() } |
| 108 | cfg_cachedirs = ['/cache/'] |
| 109 | // no invoker known: the builds were not looked at, and the event says so |
| 110 | // by leaving the root out, rather than an empty one beside no builds |
| 111 | unknown := clean_event(['/cache/a.pkg.tar.zst'], 10, BuildClean{}, []string{}, false) |
| 112 | assert unknown.build_root == none |
| 113 | text := json2.encode(unknown) |
| 114 | assert !text.contains('build_root'), text |
| 115 | assert text.contains('"files":["/cache/a.pkg.tar.zst"]') |
| 116 | // an invoker known, with no build found: the root goes out, the list is |
| 117 | // empty, and that does mean none |
| 118 | looked := clean_event([], 0, BuildClean{ |
| 119 | user: 'someone' |
| 120 | root: '/home/someone/.cache/vpac/pkgbuild' |
| 121 | }, ['/db/sync/gone.db'], true) |
| 122 | assert looked.build_root? == '/home/someone/.cache/vpac/pkgbuild' |
| 123 | doc := json2.decode[json2.Any](json2.encode(looked))!.as_map() |
| 124 | assert doc['build_root']!.str() == '/home/someone/.cache/vpac/pkgbuild' |
| 125 | assert doc['builds']!.as_array().len == 0 |
| 126 | assert doc['dbs']!.as_array().map(it.str()) == ['/db/sync/gone.db'] |
| 127 | assert doc['preview']!.bool() |
| 128 | assert doc['type']!.str() == 'clean' |
| 129 | } |
| 130 | |
| 131 | fn test_tree_size_says_when_it_is_a_floor() ! { |
| 132 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_tree_size_${os.getpid()}') |
| 133 | deep := os.join_path(tmp, 'a', 'closed') |
| 134 | os.mkdir_all(deep)! |
| 135 | os.mkdir_all(os.join_path(tmp, 'b', 'empty'))! |
| 136 | defer { |
| 137 | os.chmod(deep, 0o755) or {} |
| 138 | os.rmdir_all(tmp) or {} |
| 139 | } |
| 140 | os.write_file(os.join_path(tmp, 'top'), 'x'.repeat(10))! |
| 141 | os.write_file(os.join_path(tmp, 'a', 'file'), 'x'.repeat(100))! |
| 142 | os.write_file(os.join_path(deep, 'hidden'), 'x'.repeat(1000))! |
| 143 | os.write_file(os.join_path(tmp, 'b', 'file'), 'x'.repeat(5))! |
| 144 | // a link is not followed, whatever it points at, and is no gap |
| 145 | os.symlink(os.join_path(tmp, 'a'), os.join_path(tmp, 'b', 'link'))! |
| 146 | os.symlink('/nonexistent-vpac-test', os.join_path(tmp, 'b', 'dangling'))! |
| 147 | // everything readable: the whole size, said to be whole |
| 148 | size, whole := tree_size(tmp) |
| 149 | assert size == 1115 && whole |
| 150 | // a plain file, an empty dir and a link are each whole |
| 151 | fsize, fwhole := tree_size(os.join_path(tmp, 'top')) |
| 152 | assert fsize == 10 && fwhole |
| 153 | esize, ewhole := tree_size(os.join_path(tmp, 'b', 'empty')) |
| 154 | assert esize == 0 && ewhole |
| 155 | lsize, lwhole := tree_size(os.join_path(tmp, 'b', 'link')) |
| 156 | assert lsize == 0 && lwhole |
| 157 | // what is not there is nothing, and not a gap in something |
| 158 | gsize, gwhole := tree_size(os.join_path(tmp, 'gone')) |
| 159 | assert gsize == 0 && gwhole |
| 160 | if os.geteuid() == 0 { |
| 161 | // root reads a closed directory all the same |
| 162 | return |
| 163 | } |
| 164 | // a directory that cannot be listed, two levels down: the rest is |
| 165 | // still counted, and the sum is said to be a floor all the way up |
| 166 | os.chmod(deep, 0o000)! |
| 167 | psize, pwhole := tree_size(tmp) |
| 168 | assert psize == 115 && !pwhole |
| 169 | asize, awhole := tree_size(os.join_path(tmp, 'a')) |
| 170 | assert asize == 100 && !awhole |
| 171 | // its sibling is not tainted by it |
| 172 | bsize, bwhole := tree_size(os.join_path(tmp, 'b')) |
| 173 | assert bsize == 5 && bwhole |
| 174 | // the closed directory itself is nothing, and not whole |
| 175 | csize, cwhole := tree_size(deep) |
| 176 | assert csize == 0 && !cwhole |
| 177 | // the builds' sum carries the mark, and a person is told 'at least' |
| 178 | saved := pkgbuild_dir |
| 179 | defer { pkgbuild_dir = saved } |
| 180 | pkgbuild_dir = tmp |
| 181 | bc := build_dirs_to_clean('nobody', true, [], [])! |
| 182 | assert bc.dirs.len == 2 && bc.size == 105 && bc.partial |
| 183 | assert size_text(bc.size, bc.partial) == 'at least 105B' || size_text(bc.size, bc.partial).starts_with('at least 105') |
| 184 | assert !size_text(105, false).starts_with('at least') |
| 185 | ev := json2.decode[json2.Any](json2.encode(clean_event([], 0, bc, [], false)))!.as_map() |
| 186 | assert ev['build_size_partial']!.bool() && ev['build_size']!.i64() == 105 |
| 187 | // a build tree that is all readable leaves the mark off, and present |
| 188 | os.chmod(deep, 0o755)! |
| 189 | ok := build_dirs_to_clean('nobody', true, [], [])! |
| 190 | assert ok.size == 1105 && !ok.partial |
| 191 | whole_ev := json2.decode[json2.Any](json2.encode(clean_event([], 0, ok, [], false)))!.as_map() |
| 192 | assert !whole_ev['build_size_partial']!.bool() |
| 193 | } |
| 194 | |