module main import nspkg import os import testdb fn test_orphan_pkgs() ! { tmp := os.join_path(os.vtmp_dir(), 'vpac_test_orphans_${os.getpid()}') os.mkdir_all(os.join_path(tmp, 'root'))! defer { os.rmdir_all(tmp) or {} } testdb.write_local(os.join_path(tmp, 'db'), [ // kept: explicit, and everything it reaches testdb.Pkg{ name: 'app' reason: 0 depends: ['lib1>=1.0', 'libvirtual'] optdepends: ['opt: extra things'] }, testdb.Pkg{ name: 'lib1' reason: 1 depends: ['lib2'] }, testdb.Pkg{ name: 'lib2' reason: 1 }, // reached through what it provides testdb.Pkg{ name: 'impl' reason: 1 provides: ['libvirtual'] }, // a second provider of the same name is required as much as the // first, as pacman's compute_requiredby has it; one whose provision // is too old for a versioned dependency is not testdb.Pkg{ name: 'impl2' reason: 1 provides: ['libvirtual'] }, testdb.Pkg{ name: 'wants-new' reason: 0 depends: ['virt>=2'] }, testdb.Pkg{ name: 'virt-new' reason: 1 provides: ['virt=2'] }, testdb.Pkg{ name: 'virt-old' reason: 1 provides: ['virt=1'] }, // kept as an optional dependency of app, with what it needs testdb.Pkg{ name: 'opt' reason: 1 depends: ['optlib'] }, testdb.Pkg{ name: 'optlib' reason: 1 }, // orphans: one nothing requires, and a pair that only requires // itself, which pacman's -Qdt misses testdb.Pkg{ name: 'old' reason: 1 }, testdb.Pkg{ name: 'cyc-a' reason: 1 depends: ['cyc-b'] }, testdb.Pkg{ name: 'cyc-b' reason: 1 depends: ['cyc-a'] }, ])! handle := nspkg.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')! defer { handle.release() or {} } localdb := handle.get_localdb()! mut pkgcache := localdb.get_pkgcache() or { return error('the fake local db has no packages') } assert pkgcache.pkgs().len == 13, 'libalpm read the fake db' mut names := orphan_pkgs(mut pkgcache, false).map(it.get_name()) names.sort() assert names == ['cyc-a', 'cyc-b', 'old', 'virt-old'] // -tt: an optional dependency keeps nothing, so what only an optdepends // line names is listed too, with what hangs off it mut all := orphan_pkgs(mut pkgcache, true).map(it.get_name()) all.sort() assert all == ['cyc-a', 'cyc-b', 'old', 'opt', 'optlib', 'virt-old'] }