nspkg / tests / coracle / local_write_compatibility_test.v
111 lines · 106 sloc · 4.81 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module coracle
2
3import nspkg.engine
4import nspkg.tests.archivefixtures
5import os
6
7fn local_write_fixture(base string, description string) ! {
8 entry := os.join_path(base, 'local', 'app-1-1')
9 os.mkdir_all(entry)!
10 os.write_file(os.join_path(base, 'local', 'ALPM_DB_VERSION'), '9\n')!
11 os.write_file(os.join_path(entry, 'desc'), description)!
12 os.write_file(os.join_path(entry, 'files'), 'files stay untouched\n')!
13}
14
15fn local_reference_install(old &Handle, path string) ! {
16 pkg := old.pkg_load(path, 1, 0)!
17 old.trans_init(.db_only | .no_deps | .no_hooks | .no_scriptlet)!
18 defer { old.trans_release() or { eprintln(err) } }
19 old.add_pkg(pkg)!
20 prepared := old.trans_prepare()!
21 prepared.free()
22 result := old.trans_commit()!
23 result.free()
24}
25
26fn test_native_install_reason_writes_match_libalpm() ! {
27 dir := os.join_path(os.vtmp_dir(), 'alpm-local-reason-parity-${os.getpid()}')
28 os.mkdir_all(dir)!
29 defer { os.rmdir_all(dir) or { eprintln(err) } }
30 for fields in 0 .. 32 {
31 mut text := '%NAME%\napp\n\n%VERSION%\n1-1\n\n%REASON%\n0\n\n'
32 for index, key in ['BASE', 'DESC', 'URL', 'ARCH', 'PACKAGER'] {
33 if fields & (1 << index) != 0 {
34 text += '%${key}%\n${if fields % 3 == 0 { '' } else { 'value with spaces ' }}\n\n'
35 }
36 }
37 text += '%BUILDDATE%\n-1\n\n%INSTALLDATE%\n1700000000\n\n%SIZE%\n42\n\n%GROUPS%\nz\na\nz\n\n%LICENSE%\nMIT\nGPL\n\n%VALIDATION%\npgp\nsha256\nnone\n\n%DEPENDS%\nbase>=2:1\n\n%OPTDEPENDS%\nviewer: helps\n\n%MAKEDEPENDS%\ncompiler\n\n%CHECKDEPENDS%\nrunner\n\n%PROVIDES%\nvirtual=1\n\n%CONFLICTS%\nold<2\n\n%REPLACES%\nold\n\n%XDATA%\npkgtype=pkg\ncustom=a=b\n\n'
38 old_base := os.join_path(dir, '${fields}-old')
39 base := os.join_path(dir, '${fields}-native')
40 local_write_fixture(old_base, text)!
41 local_write_fixture(base, text)!
42 old := initialize('/', old_base)!
43 defer { old.release() or { eprintln(err) } }
44 mut native := engine.initialize('/', base)!
45 defer { native.release() or { eprintln(err) } }
46 old_db := old.get_localdb()!
47 mut db := native.local_database()!
48 pkg := old_db.get_pkg('app') or { return error('missing C package') }
49 mut actual := db.get_pkg('app') or { return error('missing native package') }
50 for reason in [0, 1, 2] {
51 pkg.set_reason(unsafe { PackageReason(reason) })!
52 actual.set_reason(unsafe { engine.InstallReason(reason) })!
53 assert u32(actual.reason) == u32(pkg.get_reason())
54 assert os.read_file(os.join_path(base, 'local', 'app-1-1', 'desc'))! == os.read_file(os.join_path(old_base, 'local', 'app-1-1', 'desc'))!, '${fields}/${reason}'
55 assert os.read_file(os.join_path(base, 'local', 'app-1-1', 'files'))! == 'files stay untouched\n'
56 }
57 }
58}
59
60fn test_native_local_desc_and_files_match_c_database_only_installs() ! {
61 dir := os.join_path(os.vtmp_dir(), 'alpm-local-record-parity-${os.getpid()}')
62 root := os.join_path(dir, 'root')
63 archives := os.join_path(dir, 'archives')
64 dbpath := os.join_path(dir, 'db')
65 for path in [root, archives, dbpath] { os.mkdir_all(path)! }
66 defer { os.rmdir_all(dir) or { eprintln(err) } }
67 old := initialize(root, dbpath)!
68 defer { old.release() or { eprintln(err) } }
69 for index in 0 .. 8 {
70 name := 'fixture${index}'
71 mut info := 'pkgname = ${name}\npkgver = 1:2-3\nxdata = pkgtype=pkg\nxdata = custom=a=b\n'
72 if index != 0 {
73 for key in ['pkgbase', 'pkgdesc', 'url', 'arch', 'packager'] {
74 info += '${key} = ${if index == 1 {
75 ''
76 } else if key == 'arch' {
77 'any'
78 } else {
79 'text'
80 }}\n'
81 }
82 info += 'builddate = 1700000000\nsize = 42\ngroup = z\ngroup = a\nlicense = MIT\nlicense = GPL\ndepend = base>=2\noptdepend = tool: helps\nmakedepend = compiler\ncheckdepend = runner\nreplaces = old\nconflict = old<2\nprovides = virtual=1\n'
83 }
84 if index % 2 == 0 { info += 'backup = etc/app.conf\n' }
85 path := os.join_path(archives, '${name}.pkg.tar')
86 mut entries := [archivefixtures.Entry{ name: '.PKGINFO', text: info }]
87 if index != 3 {
88 entries << [archivefixtures.Entry{ name: 'etc/', directory: true },
89 archivefixtures.Entry{ name: 'etc/app.conf', text: 'config' },
90 archivefixtures.Entry{ name: 'usr/', directory: true },
91 archivefixtures.Entry{ name: 'usr/app', text: 'app' }]
92 }
93 archivefixtures.write(path, entries, index % 2 == 0)!
94 local_reference_install(old, path)!
95 assert !os.exists(os.join_path(root, 'etc', 'app.conf'))
96 }
97 mut process := os.new_process('v')
98 process.set_args([os.join_path(@VMODROOT, 'engine', 'local_storage_test.v')])
99 mut environment := os.environ()
100 environment['ALPM_LOCAL_WRITE_REFERENCE'] = os.join_path(dbpath, 'local')
101 environment['ALPM_LOCAL_WRITE_ARCHIVES'] = archives
102 process.set_environment(environment)
103 process.set_redirect_stdio()
104 process.run()
105 defer { process.close() }
106 output := process.stdout_slurp()
107 diagnostic := process.stderr_slurp()
108 process.wait()
109 assert process.code == 0, '${diagnostic}\n${output}'
110 assert output.contains('native local writer matched 8 C records')
111}
112