module main import json2 import os fn test_parse_cache_file() { cases := { 'zsh-5.9.2-1-x86_64.pkg.tar.zst': 'zsh 5.9.2-1' '/var/cache/pacman/pkg/zsh-5.9.2-1-x86_64.pkg.tar.zst': 'zsh 5.9.2-1' 'gcc-libs-14.2.1+r1-1-x86_64.pkg.tar.zst': 'gcc-libs 14.2.1+r1-1' 'vim-2:9.1.0-1-x86_64.pkg.tar.zst': 'vim 2:9.1.0-1' 'python-3.12.7-1-x86_64.pkg.tar.xz': 'python 3.12.7-1' // dashes in the cache dir are not part of the name '/tmp/a-dir-with-dashes/foo-1.0-1-any.pkg.tar.zst': 'foo 1.0-1' } for file, want in cases { cf := parse_cache_file(file) or { assert false, '${file} did not parse' continue } assert '${cf.name} ${cf.version}' == want, file assert cf.file == file } for bad in ['broken.pkg.tar.zst', 'a-1-x.pkg.tar.zst', '-1-1-any.pkg.tar.zst'] { if cf := parse_cache_file(bad) { assert false, '${bad} parsed as ${cf.name} ${cf.version}' } } } fn test_installed_file() { prefixes := ['zsh-5.9.2-1-', 'gcc-libs-14.2.1-1-'] // the cache is listed as full paths: the file name is what counts assert installed_file('/var/cache/pacman/pkg/zsh-5.9.2-1-x86_64.pkg.tar.zst', prefixes) assert installed_file('zsh-5.9.2-1-x86_64.pkg.tar.zst', prefixes) assert installed_file('/c/gcc-libs-14.2.1-1-x86_64.pkg.tar.zst', prefixes) assert !installed_file('/var/cache/pacman/pkg/zsh-5.9.1-1-x86_64.pkg.tar.zst', prefixes) assert !installed_file('/c/zsh-completions-0.35-1-any.pkg.tar.zst', prefixes) // a dir that looks like the prefix does not make its files installed assert !installed_file('/c/zsh-5.9.2-1-x/foo-1.0-1-any.pkg.tar.zst', prefixes) assert !installed_file('/c/zsh-5.9.2-1-x86_64.pkg.tar.zst', []) // an uncompressed archive is one too assert installed_file('/c/zsh-5.9.2-1-x86_64.pkg.tar', prefixes) assert is_pkg_archive('zsh-5.9.2-1-x86_64.pkg.tar') && is_pkg_archive('a-1-1-any.pkg.tar.zst') assert !is_pkg_archive('a-1-1-any.pkg.tar.zst.sig') && !is_pkg_archive('core.db') // a package whose name begins with another's name-version- is its own: // zsh-5.9.2-1-extras 2-1 is not zsh 5.9.2-1 assert !installed_file('/c/zsh-5.9.2-1-extras-2-1-any.pkg.tar.zst', prefixes) } // an unreadable cache dir does not stop the cleaning of the ones that read: // the files of the rest come through, spelled out whole fn test_cached_pkg_files_skips_a_lost_cache_dir() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_cache_${os.getpid()}') real := os.join_path(tmp, 'pkg') + '/' os.mkdir_all(real)! defer { os.rmdir_all(tmp) or {} } os.write_file(os.join_path(real, 'zsh-5.9.2-1-x86_64.pkg.tar.zst'), '.')! os.write_file(os.join_path(real, 'zsh-5.9.2-1-x86_64.pkg.tar.zst.sig'), '.')! mut old := cfg_cachedirs.clone() defer { cfg_cachedirs = old.clone() } cfg_cachedirs = [real, '/nonexistent-vpac-test/'] files := cached_pkg_files()! assert files == ['${real}zsh-5.9.2-1-x86_64.pkg.tar.zst'], files.str() // the spelled-out path is a real one assert os.exists(files[0]) } // kept_names is the set paccache_keep keeps, as sorted file names fn kept_names(kept map[string]bool) []string { mut names := map[string]bool{} for f, on in kept { if !on { continue } names[os.base(f).all_before('.pkg.tar.')] = true } mut out := names.keys() out.sort() return out } fn test_paccache_keep() { files := [ '/c/foo-1.9-1-any.pkg.tar.zst', '/c/foo-1.10-1-any.pkg.tar.zst', '/c/zsh-5.9.1-1-x86_64.pkg.tar.zst', '/c/zsh-5.9.2-1-x86_64.pkg.tar.zst', '/c/zsh-6.0-1-x86_64.pkg.tar.zst', ] installed := ['zsh-5.9.2-1-'] // newest by pacman's vercmp: 1.10 is newer than 1.9. the installed // version is always kept, and it counts among the newest assert kept_names(paccache_keep(1, files, installed)) == ['foo-1.10-1-any', 'zsh-5.9.2-1-x86_64', 'zsh-6.0-1-x86_64'] assert kept_names(paccache_keep(2, files, installed)) == ['foo-1.10-1-any', 'foo-1.9-1-any', 'zsh-5.9.2-1-x86_64', 'zsh-6.0-1-x86_64'] assert kept_names(paccache_keep(3, files, installed)).len == 5 assert kept_names(paccache_keep(1, files, [])) == ['foo-1.10-1-any', 'zsh-6.0-1-x86_64'] } fn test_clean_event_build_root() ! { saved := cfg_cachedirs.clone() defer { cfg_cachedirs = saved.clone() } cfg_cachedirs = ['/cache/'] // no invoker known: the builds were not looked at, and the event says so // by leaving the root out, rather than an empty one beside no builds unknown := clean_event(['/cache/a.pkg.tar.zst'], 10, BuildClean{}, []string{}, false) assert unknown.build_root == none text := json2.encode(unknown) assert !text.contains('build_root'), text assert text.contains('"files":["/cache/a.pkg.tar.zst"]') // an invoker known, with no build found: the root goes out, the list is // empty, and that does mean none looked := clean_event([], 0, BuildClean{ user: 'someone' root: '/home/someone/.cache/vpac/pkgbuild' }, ['/db/sync/gone.db'], true) assert looked.build_root? == '/home/someone/.cache/vpac/pkgbuild' doc := json2.decode[json2.Any](json2.encode(looked))!.as_map() assert doc['build_root']!.str() == '/home/someone/.cache/vpac/pkgbuild' assert doc['builds']!.as_array().len == 0 assert doc['dbs']!.as_array().map(it.str()) == ['/db/sync/gone.db'] assert doc['preview']!.bool() assert doc['type']!.str() == 'clean' } fn test_tree_size_says_when_it_is_a_floor() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_tree_size_${os.getpid()}') deep := os.join_path(tmp, 'a', 'closed') os.mkdir_all(deep)! os.mkdir_all(os.join_path(tmp, 'b', 'empty'))! defer { os.chmod(deep, 0o755) or {} os.rmdir_all(tmp) or {} } os.write_file(os.join_path(tmp, 'top'), 'x'.repeat(10))! os.write_file(os.join_path(tmp, 'a', 'file'), 'x'.repeat(100))! os.write_file(os.join_path(deep, 'hidden'), 'x'.repeat(1000))! os.write_file(os.join_path(tmp, 'b', 'file'), 'x'.repeat(5))! // a link is not followed, whatever it points at, and is no gap os.symlink(os.join_path(tmp, 'a'), os.join_path(tmp, 'b', 'link'))! os.symlink('/nonexistent-vpac-test', os.join_path(tmp, 'b', 'dangling'))! // everything readable: the whole size, said to be whole size, whole := tree_size(tmp) assert size == 1115 && whole // a plain file, an empty dir and a link are each whole fsize, fwhole := tree_size(os.join_path(tmp, 'top')) assert fsize == 10 && fwhole esize, ewhole := tree_size(os.join_path(tmp, 'b', 'empty')) assert esize == 0 && ewhole lsize, lwhole := tree_size(os.join_path(tmp, 'b', 'link')) assert lsize == 0 && lwhole // what is not there is nothing, and not a gap in something gsize, gwhole := tree_size(os.join_path(tmp, 'gone')) assert gsize == 0 && gwhole if os.geteuid() == 0 { // root reads a closed directory all the same return } // a directory that cannot be listed, two levels down: the rest is // still counted, and the sum is said to be a floor all the way up os.chmod(deep, 0o000)! psize, pwhole := tree_size(tmp) assert psize == 115 && !pwhole asize, awhole := tree_size(os.join_path(tmp, 'a')) assert asize == 100 && !awhole // its sibling is not tainted by it bsize, bwhole := tree_size(os.join_path(tmp, 'b')) assert bsize == 5 && bwhole // the closed directory itself is nothing, and not whole csize, cwhole := tree_size(deep) assert csize == 0 && !cwhole // the builds' sum carries the mark, and a person is told 'at least' saved := pkgbuild_dir defer { pkgbuild_dir = saved } pkgbuild_dir = tmp bc := build_dirs_to_clean('nobody', true, [], [])! assert bc.dirs.len == 2 && bc.size == 105 && bc.partial assert size_text(bc.size, bc.partial) == 'at least 105B' || size_text(bc.size, bc.partial).starts_with('at least 105') assert !size_text(105, false).starts_with('at least') ev := json2.decode[json2.Any](json2.encode(clean_event([], 0, bc, [], false)))!.as_map() assert ev['build_size_partial']!.bool() && ev['build_size']!.i64() == 105 // a build tree that is all readable leaves the mark off, and present os.chmod(deep, 0o755)! ok := build_dirs_to_clean('nobody', true, [], [])! assert ok.size == 1105 && !ok.partial whole_ev := json2.decode[json2.Any](json2.encode(clean_event([], 0, ok, [], false)))!.as_map() assert !whole_ev['build_size_partial']!.bool() }