dev-build/meson-format-array: add some doctests

Signed-off-by: Mike Gilbert <floppym@gentoo.org>
This commit is contained in:
Mike Gilbert
2024-07-18 11:57:51 -04:00
parent c785341749
commit 8d06d85786
2 changed files with 29 additions and 5 deletions

View File

@@ -6,10 +6,22 @@ import itertools
import shlex
import sys
def quote(s):
""" Surround a value with quotes, escape embedded quotes.
>>> quote("foo'bar")
"'foo\\\\'bar'"
"""
return "'" + s.replace("\\", "\\\\").replace("'", "\\'") + "'"
def main(args):
def format_array(args):
""" Format shell-compatible expressions as a meson array.
>>> format_array(['-O2 -pipe -DFOO="bar baz"'])
"['-O2', '-pipe', '-DFOO=bar baz']"
"""
# Split each argument according to shell rules
args = (shlex.split(x) for x in args)
@@ -20,7 +32,12 @@ def main(args):
args = (quote(x) for x in args)
# Format the result
print("[" + ", ".join(args) + "]")
return "[" + ", ".join(args) + "]"
def main(args):
print(format_array(args))
if __name__ == "__main__":
main(sys.argv[1:])

View File

@@ -4,7 +4,7 @@
EAPI=8
PYTHON_COMPAT=( python3_{10..13} )
inherit python-r1
inherit edo python-r1
DESCRIPTION="Format shell expressions into a meson array"
HOMEPAGE="https://wiki.gentoo.org/wiki/No_homepage"
@@ -17,6 +17,13 @@ REQUIRED_USE="${PYTHON_REQUIRED_USE}"
RDEPEND="${PYTHON_DEPS}"
S="${WORKDIR}"
src_install() {
python_foreach_impl python_doscript "${FILESDIR}"/meson-format-array
src_test() {
run_doctest() {
edo ${EPYTHON} -B -m doctest "${FILESDIR}/meson-format-array.py"
}
python_foreach_impl run_doctest
}
src_install() {
python_foreach_impl python_newscript "${FILESDIR}"/meson-format-array.py meson-format-array
}