Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
query: for multiline print en empty line if no information to display
Baptiste Daroussin committed 1 month ago
commit 5e8bc8c6c516d15db2a18bb857c8a25f082dbd52
parent 7d88028
2 files changed +47 -1
modified src/query.c
@@ -381,18 +381,21 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	struct pkg_kvlist_iterator	*kit;

	output = xstring_new();
+
	bool printed = false;

	switch (multiline) {
	case 'd':
		while (pkg_deps(pkg, &dep) == EPKG_OK) {
			format_str(pkg, output, qstr, dep);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		break;
	case 'r':
		while (pkg_rdeps(pkg, &dep) == EPKG_OK) {
			format_str(pkg, output, qstr, dep);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		break;
	case 'C':
@@ -416,6 +419,7 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		while ((str = pkg_stringlist_next(slit))) {
			format_str(pkg, output, qstr, str);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		free(slit);
		free(sl);
@@ -424,12 +428,14 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		while (pkg_options(pkg, &option) == EPKG_OK) {
			format_str(pkg, output, qstr, option);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		break;
	case 'F':
		while (pkg_files(pkg, &file) == EPKG_OK) {
			format_str(pkg, output, qstr, file);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		break;
	case 'D':
@@ -437,6 +443,7 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		while (pkg_dirs(pkg, &dir) == EPKG_OK) {
			format_str(pkg, output, qstr, dir);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		break;
	case 'A':
@@ -445,6 +452,7 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		while ((kv = pkg_kvlist_next(kit))) {
			format_str(pkg, output, qstr, kv);
			printf("%s\n", output->buf);
+
			printed = true;
		}
		free(kit);
		free(kl);
@@ -452,8 +460,13 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
	default:
		format_str(pkg, output, qstr, dep);
		printf("%s\n", output->buf);
+
		printed = true;
		break;
	}
+
	if (!printed) {
+
		format_str(pkg, output, qstr, "");
+
		printf("%s\n", output->buf);
+
	}
	xstring_free(output);
}

modified tests/frontend/query.sh
@@ -3,7 +3,8 @@
. $(atf_get_srcdir)/test_environment.sh

tests_init \
-
	query
+
	query \
+
	query_empty_multiline

query_body() {
	touch plop
@@ -286,3 +287,35 @@ EOF
		-s exit:0 \
		pkg query -F ./plop-1.pkg '%c'
}
+

+
query_empty_multiline_body() {
+
	# Packages without licenses should not be skipped by %L queries
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg nolic nolic 1
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg withlicense withlicense 1
+
	cat >> withlicense.ucl << EOF
+
licenses: ["BSD2CLAUSE"]
+
EOF
+

+
	atf_check -o ignore pkg register -M nolic.ucl
+
	atf_check -o ignore pkg register -M withlicense.ucl
+

+
	# %n without multiline: both packages listed
+
	atf_check \
+
		-o inline:"nolic\nwithlicense\n" \
+
		-s exit:0 \
+
		pkg query "%n"
+

+
	# %n %L: both packages must appear, nolic with empty license
+
	atf_check \
+
		-o inline:"nolic \nwithlicense BSD2CLAUSE\n" \
+
		-s exit:0 \
+
		pkg query "%n %L"
+

+
	# Same for categories: nolic has no categories in test_subr
+
	# but test_subr adds categories: [test], so both have one.
+
	# Test with shlibs instead (no package has shlibs)
+
	atf_check \
+
		-o inline:"nolic \nwithlicense \n" \
+
		-s exit:0 \
+
		pkg query "%n %B"
+
}