Merge branch 'gobject-query' into 'main'

gobject-query: Minor cleanups and add unit tests

See merge request GNOME/glib!2739
This commit is contained in:
Philip Withnall 2022-06-09 17:33:13 +00:00
commit 103a9d5ae1
3 changed files with 147 additions and 36 deletions

View File

@ -32,21 +32,12 @@ static FILE *f_out = NULL;
static GType root = 0;
static gboolean recursion = TRUE;
#if 0
# define O_SPACE "\\as"
# define O_ESPACE " "
# define O_BRANCH "\\aE"
# define O_VLINE "\\al"
# define O_LLEAF "\\aL"
# define O_KEY_FILL "_"
#else
# define O_SPACE " "
# define O_ESPACE ""
# define O_BRANCH "+"
# define O_VLINE "|"
# define O_LLEAF "`"
# define O_KEY_FILL "_"
#endif
#define O_SPACE " "
#define O_ESPACE ""
#define O_BRANCH "├"
#define O_VLINE "│"
#define O_LLEAF "└"
#define O_KEY_FILL "_"
static void
show_nodes (GType type,
@ -61,10 +52,6 @@ show_nodes (GType type,
children = g_type_children (type, NULL);
if (type != root)
for (i = 0; i < spacing; i++)
g_fprintf (f_out, "%s%s\n", indent, O_VLINE);
g_fprintf (f_out, "%s%s%s%s",
indent,
sibling ? O_BRANCH : (type != root ? O_LLEAF : O_SPACE),
@ -96,18 +83,18 @@ show_nodes (GType type,
}
static gint
help (gchar *arg)
help (const gchar *arg)
{
g_fprintf (stderr, "usage: gobject-query <qualifier> [-r <type>] [-{i|b} \"\"] [-s #] [-{h|x|y}]\n");
g_fprintf (stderr, " -r specify root type\n");
g_fprintf (stderr, " -n don't descend type tree\n");
g_fprintf (stderr, " -h guess what ;)\n");
g_fprintf (stderr, " -b specify indent string\n");
g_fprintf (stderr, " -i specify incremental indent string\n");
g_fprintf (stderr, " -s specify line spacing\n");
g_fprintf (stderr, "qualifiers:\n");
g_fprintf (stderr, " froots iterate over fundamental roots\n");
g_fprintf (stderr, " tree print type tree\n");
g_fprintf (stdout, "usage: gobject-query <qualifier> [-r <type>] [-{i|b} \"\"] [-s #] [-{h|x|y}]\n");
g_fprintf (stdout, " -r specify root type\n");
g_fprintf (stdout, " -n don't descend type tree\n");
g_fprintf (stdout, " -h show help\n");
g_fprintf (stdout, " -b specify indent string\n");
g_fprintf (stdout, " -i specify incremental indent string\n");
g_fprintf (stdout, " -s specify line spacing\n");
g_fprintf (stdout, "qualifiers:\n");
g_fprintf (stdout, " froots iterate over fundamental roots\n");
g_fprintf (stdout, " tree print type tree\n");
return arg != NULL;
}
@ -183,11 +170,13 @@ main (gint argc,
{
gen_tree = 1;
}
else if (strcmp ("-h", argv[i]) == 0)
{
return help (NULL);
}
else if (strcmp ("--help", argv[i]) == 0)
else if (strcmp ("--version", argv[i]) == 0)
{
g_print (PACKAGE_VERSION "\n");
return 0;
}
else if (strcmp ("-h", argv[i]) == 0 ||
strcmp ("--help", argv[i]) == 0)
{
return help (NULL);
}
@ -213,9 +202,13 @@ main (gint argc,
for (i = 0; i <= G_TYPE_FUNDAMENTAL_MAX; i += G_TYPE_MAKE_FUNDAMENTAL (1))
{
const gchar *name = g_type_name (i);
GType sibling = i + G_TYPE_MAKE_FUNDAMENTAL (1);
if (sibling > G_TYPE_FUNDAMENTAL_MAX || g_type_name (sibling) == NULL)
sibling = 0;
if (name)
show_nodes (i, 0, iindent);
show_nodes (i, sibling, iindent);
}
}

View File

@ -0,0 +1,117 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright © 2022 Endless OS Foundation, LLC
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
"""Integration tests for gobject-query utility."""
import collections
import os
import shutil
import subprocess
import sys
from textwrap import dedent
import unittest
import taptestrunner
Result = collections.namedtuple("Result", ("info", "out", "err"))
class TestGobjectQuery(unittest.TestCase):
"""Integration test for running gobject-query.
This can be run when installed or uninstalled. When uninstalled, it
requires G_TEST_BUILDDIR and G_TEST_SRCDIR to be set.
The idea with this test harness is to test the gobject-query utility, its
handling of command line arguments, and its exit statuses.
"""
def setUp(self):
self.timeout_seconds = 10 # seconds per test
if "G_TEST_BUILDDIR" in os.environ:
self.__gobject_query = os.path.join(
os.environ["G_TEST_BUILDDIR"], "..", "gobject-query"
)
else:
self.__gobject_query = shutil.which("gobject-query")
print("gobject-query:", self.__gobject_query)
def runGobjectQuery(self, *args):
argv = [self.__gobject_query]
# shebang lines are not supported on native
# Windows consoles
if os.name == "nt":
argv.insert(0, sys.executable)
argv.extend(args)
print("Running:", argv)
env = os.environ.copy()
env["LC_ALL"] = "C.UTF-8"
print("Environment:", env)
# We want to ensure consistent line endings...
info = subprocess.run(
argv,
timeout=self.timeout_seconds,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
universal_newlines=True,
)
info.check_returncode()
out = info.stdout.strip()
err = info.stderr.strip()
result = Result(info, out, err)
print("Output:", result.out)
return result
def test_help(self):
"""Test the --help argument."""
result = self.runGobjectQuery("--help")
self.assertIn("usage: gobject-query", result.out)
def test_version(self):
"""Test the --version argument."""
result = self.runGobjectQuery("--version")
self.assertIn("2.", result.out)
def test_froots(self):
"""Test running froots with no other arguments."""
result = self.runGobjectQuery("froots")
self.assertEqual("", result.err)
self.assertIn("├gboolean", result.out)
self.assertIn("├GObject", result.out)
def test_tree(self):
"""Test running tree with no other arguments."""
result = self.runGobjectQuery("tree")
self.assertEqual("", result.err)
self.assertIn("GObject", result.out)
if __name__ == "__main__":
unittest.main(testRunner=taptestrunner.TAPTestRunner())

View File

@ -104,6 +104,7 @@ endif
python_tests = [
'genmarshal.py',
'gobject-query.py',
'mkenums.py',
]