Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
plist: add more test for variables handling
Baptiste Daroussin committed 2 years ago
commit 26101cc33610c7e0f72fa1a900933de56fb29767
parent 9906ba2
3 files changed +46 -2
modified libpkg/pkg_ports.c
@@ -1079,7 +1079,7 @@ plist_free(struct plist *p)
	free(p);
}

-
static char *
+
char *
expand_plist_variables(const char *in, kvlist_t *vars)
{
	xstring *buf;
@@ -1101,6 +1101,7 @@ expand_plist_variables(const char *in, kvlist_t *vars)
		if (in[0] == '\0')
			break;
		if (in[0] != '%') {
+
			fputc('%', buf->fp);
			fputc(in[0], buf->fp);
			in++;
			continue;
@@ -1116,7 +1117,6 @@ expand_plist_variables(const char *in, kvlist_t *vars)
		}
		if (in[0] != '%') {
			fprintf(buf->fp, "%%%%%.*s", (int)(in - cp), cp);
-
			in++;
			continue;
		}
		len = in - cp -1;
@@ -1127,6 +1127,7 @@ expand_plist_variables(const char *in, kvlist_t *vars)
				continue;
			fputs(i->item->value, buf->fp);
			found = true;
+
			in++;
			break;
		}
		if (found)
modified libpkg/private/pkg.h
@@ -872,5 +872,6 @@ bool stringlist_contains(stringlist_t *l, const char *name);

int pkg_parse_manifest_ucl(struct pkg *pkg, ucl_object_t *o);
int pkg_get_reposdirfd(void);
+
char * expand_plist_variables(const char *in, kvlist_t *vars);

#endif
modified tests/lib/plist.c
@@ -29,6 +29,7 @@
#include <private/pkg.h>

ATF_TC_WITHOUT_HEAD(parse_mode);
+
ATF_TC_WITHOUT_HEAD(expand_plist_variables);

ATF_TC_BODY(parse_mode, tc)
{
@@ -271,12 +272,53 @@ ATF_TC_BODY(parse_plist, tc)
	plist_free(plist);
}

+
ATF_TC_BODY(expand_plist_variables, tc)
+
{
+
	char *plop;
+
	kvlist_t kv = tll_init();
+

+
	plop = expand_plist_variables("%%this%% is a line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "%%this%% is a line");
+
	free(plop);
+

+
	struct pkg_kv *keyval = pkg_kv_new("this", "@comment ");
+
	tll_push_back(kv, keyval);
+

+
	plop = expand_plist_variables("%%this%% is a line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "@comment  is a line");
+
	free(plop);
+

+
	plop = expand_plist_variables("%%thos%% is a line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "%%thos%% is a line");
+
	free(plop);
+

+
	plop = expand_plist_variables("%F is a line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "%F is a line");
+
	free(plop);
+

+
	struct pkg_kv *kv2 = pkg_kv_new("new", "var");
+
	tll_push_back(kv, kv2);
+

+
	plop = expand_plist_variables("%%this%% %F is a %%new%% line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "@comment  %F is a var line");
+
	free(plop);
+

+
	plop = expand_plist_variables("%%this%% %F is %% a %%new%% line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "@comment  %F is %% a var line");
+
	free(plop);
+

+
	plop = expand_plist_variables("%%this%% %F is %%kof a %%new%% line", &kv);
+
	ATF_REQUIRE_STREQ(plop, "@comment  %F is %%kof a var line");
+
	free(plop);
+
}
+

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, parse_mode);
	ATF_TP_ADD_TC(tp, parse_plist);
	ATF_TP_ADD_TC(tp, parse_keyword_attributes);
	ATF_TP_ADD_TC(tp, parse_keyword);
+
	ATF_TP_ADD_TC(tp, expand_plist_variables);

	return (atf_no_error());
}