| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | import os |
| 5 | import testdb |
| 6 | |
| 7 | fn test_orphan_pkgs() ! { |
| 8 | tmp := os.join_path(os.vtmp_dir(), 'vpac_test_orphans_${os.getpid()}') |
| 9 | os.mkdir_all(os.join_path(tmp, 'root'))! |
| 10 | defer { os.rmdir_all(tmp) or {} } |
| 11 | testdb.write_local(os.join_path(tmp, 'db'), [ |
| 12 | // kept: explicit, and everything it reaches |
| 13 | testdb.Pkg{ |
| 14 | name: 'app' |
| 15 | reason: 0 |
| 16 | depends: ['lib1>=1.0', 'libvirtual'] |
| 17 | optdepends: ['opt: extra things'] |
| 18 | }, |
| 19 | testdb.Pkg{ |
| 20 | name: 'lib1' |
| 21 | reason: 1 |
| 22 | depends: ['lib2'] |
| 23 | }, |
| 24 | testdb.Pkg{ |
| 25 | name: 'lib2' |
| 26 | reason: 1 |
| 27 | }, |
| 28 | // reached through what it provides |
| 29 | testdb.Pkg{ |
| 30 | name: 'impl' |
| 31 | reason: 1 |
| 32 | provides: ['libvirtual'] |
| 33 | }, |
| 34 | // a second provider of the same name is required as much as the |
| 35 | // first, as pacman's compute_requiredby has it; one whose provision |
| 36 | // is too old for a versioned dependency is not |
| 37 | testdb.Pkg{ |
| 38 | name: 'impl2' |
| 39 | reason: 1 |
| 40 | provides: ['libvirtual'] |
| 41 | }, |
| 42 | testdb.Pkg{ |
| 43 | name: 'wants-new' |
| 44 | reason: 0 |
| 45 | depends: ['virt>=2'] |
| 46 | }, |
| 47 | testdb.Pkg{ |
| 48 | name: 'virt-new' |
| 49 | reason: 1 |
| 50 | provides: ['virt=2'] |
| 51 | }, |
| 52 | testdb.Pkg{ |
| 53 | name: 'virt-old' |
| 54 | reason: 1 |
| 55 | provides: ['virt=1'] |
| 56 | }, |
| 57 | // kept as an optional dependency of app, with what it needs |
| 58 | testdb.Pkg{ |
| 59 | name: 'opt' |
| 60 | reason: 1 |
| 61 | depends: ['optlib'] |
| 62 | }, |
| 63 | testdb.Pkg{ |
| 64 | name: 'optlib' |
| 65 | reason: 1 |
| 66 | }, |
| 67 | // orphans: one nothing requires, and a pair that only requires |
| 68 | // itself, which pacman's -Qdt misses |
| 69 | testdb.Pkg{ |
| 70 | name: 'old' |
| 71 | reason: 1 |
| 72 | }, |
| 73 | testdb.Pkg{ |
| 74 | name: 'cyc-a' |
| 75 | reason: 1 |
| 76 | depends: ['cyc-b'] |
| 77 | }, |
| 78 | testdb.Pkg{ |
| 79 | name: 'cyc-b' |
| 80 | reason: 1 |
| 81 | depends: ['cyc-a'] |
| 82 | }, |
| 83 | ])! |
| 84 | |
| 85 | handle := nspkg.initialize(os.join_path(tmp, 'root') + '/', os.join_path(tmp, 'db') + '/')! |
| 86 | defer { handle.release() or {} } |
| 87 | localdb := handle.get_localdb()! |
| 88 | mut pkgcache := localdb.get_pkgcache() or { return error('the fake local db has no packages') } |
| 89 | assert pkgcache.pkgs().len == 13, 'libalpm read the fake db' |
| 90 | |
| 91 | mut names := orphan_pkgs(mut pkgcache, false).map(it.get_name()) |
| 92 | names.sort() |
| 93 | assert names == ['cyc-a', 'cyc-b', 'old', 'virt-old'] |
| 94 | |
| 95 | // -tt: an optional dependency keeps nothing, so what only an optdepends |
| 96 | // line names is listed too, with what hangs off it |
| 97 | mut all := orphan_pkgs(mut pkgcache, true).map(it.get_name()) |
| 98 | all.sort() |
| 99 | assert all == ['cyc-a', 'cyc-b', 'old', 'opt', 'optlib', 'virt-old'] |
| 100 | } |
| 101 | |