summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/t/t-pkg-format.c
blob: a6d33feb6f8490a416e4e9e7daca5b7f2bcf82df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * libdpkg - Debian packaging suite library routines
 * t-pkg-format.c - test pkg-format implementation
 *
 * Copyright © 2022 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <dpkg/test.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg.h>
#include <dpkg/pkg-format.h>

static void
prep_pkg(struct pkgset *set, struct pkginfo *pkg)
{
	pkg_blank(pkg);

	pkgset_blank(set);
	pkgset_link_pkg(set, pkg);

	set->name = "test-bin";
	pkg->installed.description = "short synopsis -- some package\n"
	        " This is the extended description for this package-\n"
	        " .\n"
	        " Potentially expanding multiple lines.\n";
	pkg->installed.arch = dpkg_arch_get(DPKG_ARCH_ALL);
	pkg->installed.version = DPKG_VERSION_OBJECT(0, "4.5", "2");
}

static void
prep_virtpkg(struct pkgset *set, struct pkginfo *pkg)
{
	pkg_blank(pkg);

	pkgset_blank(set);
	pkgset_link_pkg(set, pkg);

	set->name = "test-virt";
}

static void
test_field(struct pkginfo *pkg, const char *fmt, const char *exp)
{
	struct pkg_format_node *head;
	struct varbuf vb = VARBUF_INIT;

	head = pkg_format_parse(fmt, NULL);
	test_pass(head);
	pkg_format_print(&vb, head, pkg, &pkg->installed);
	test_str(vb.buf, ==, exp);
	pkg_format_free(head);
}

static void
test_pkg_format_real_fields(void)
{
	struct pkgset pkgset;
	struct pkginfo pkg;

	prep_pkg(&pkgset, &pkg);

	test_field(&pkg, "${Package}_${Version}_${Architecture}",
	                 "test-bin_4.5-2_all");
}

static void
test_pkg_format_virtual_fields(void)
{
	struct pkgset pkgset;
	struct pkginfo pkg;

	prep_pkg(&pkgset, &pkg);

	test_field(&pkg, "${source:Package}_${source:Version}",
	                 "test-bin_4.5-2");

	pkg.installed.source = "test-src";
	test_field(&pkg, "${source:Package}_${source:Version}",
	                 "test-src_4.5-2");
	test_field(&pkg, "${source:Upstream-Version}",
	                 "4.5");

	pkg.installed.source = "test-src (1:3.4-6)";
	test_field(&pkg, "${source:Package}_${source:Version}",
	                 "test-src_1:3.4-6");
	test_field(&pkg, "${source:Upstream-Version}",
	                 "3.4");

	test_field(&pkg, "${binary:Synopsis}",
	                 "short synopsis -- some package");

	test_field(&pkg, "${binary:Summary}",
	                 "short synopsis -- some package");

	prep_virtpkg(&pkgset, &pkg);
	test_field(&pkg, "${source:Package}_${source:Version}",
	                 "test-virt_");
	test_field(&pkg, "${source:Upstream-Version}",
	                 "");
}

static void
test_pkg_format_virtual_fields_db_fsys(void)
{
	struct pkg_format_node *head;

	head = pkg_format_parse("prefix ${unknown-variable} suffix", NULL);
	test_pass(head);
	test_fail(pkg_format_needs_db_fsys(head));
	pkg_format_free(head);

	head = pkg_format_parse("prefix ${db-fsys:Files} suffix", NULL);
	test_pass(head);
	test_pass(pkg_format_needs_db_fsys(head));
	pkg_format_free(head);
}

TEST_ENTRY(test)
{
	test_plan(24);

	test_pkg_format_real_fields();
	test_pkg_format_virtual_fields();
	test_pkg_format_virtual_fields_db_fsys();
}