gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gobject / tests / gobject-query.py
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright © 2022 Endless OS Foundation, LLC
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 # MA  02110-1301  USA
20
21 """Integration tests for gobject-query utility."""
22
23 import collections
24 import os
25 import shutil
26 import subprocess
27 import unittest
28
29 import taptestrunner
30
31
32 Result = collections.namedtuple("Result", ("info", "out", "err"))
33
34
35 class TestGobjectQuery(unittest.TestCase):
36     """Integration test for running gobject-query.
37
38     This can be run when installed or uninstalled. When uninstalled, it
39     requires G_TEST_BUILDDIR and G_TEST_SRCDIR to be set.
40
41     The idea with this test harness is to test the gobject-query utility, its
42     handling of command line arguments, and its exit statuses.
43     """
44
45     def setUp(self):
46         self.timeout_seconds = 10  # seconds per test
47         if "G_TEST_BUILDDIR" in os.environ:
48             self.__gobject_query = os.path.join(
49                 os.environ["G_TEST_BUILDDIR"], "..", "gobject-query"
50             )
51         else:
52             self.__gobject_query = shutil.which("gobject-query")
53         print("gobject-query:", self.__gobject_query)
54
55     def runGobjectQuery(self, *args):
56         argv = [self.__gobject_query]
57         argv.extend(args)
58         print("Running:", argv)
59
60         env = os.environ.copy()
61         env["LC_ALL"] = "C.UTF-8"
62         print("Environment:", env)
63
64         # We want to ensure consistent line endings...
65         info = subprocess.run(
66             argv,
67             timeout=self.timeout_seconds,
68             stdout=subprocess.PIPE,
69             stderr=subprocess.PIPE,
70             env=env,
71             text=True,
72             encoding="utf-8",
73         )
74         info.check_returncode()
75         out = info.stdout.strip()
76         err = info.stderr.strip()
77
78         result = Result(info, out, err)
79
80         print("Output:", result.out)
81         return result
82
83     def test_help(self):
84         """Test the --help argument."""
85         result = self.runGobjectQuery("--help")
86         self.assertIn("usage: gobject-query", result.out)
87
88     def test_version(self):
89         """Test the --version argument."""
90         result = self.runGobjectQuery("--version")
91         self.assertIn("2.", result.out)
92
93     def test_froots(self):
94         """Test running froots with no other arguments."""
95         result = self.runGobjectQuery("froots")
96
97         self.assertEqual("", result.err)
98         self.assertIn("├gboolean", result.out)
99         self.assertIn("├GObject", result.out)
100
101     def test_tree(self):
102         """Test running tree with no other arguments."""
103         result = self.runGobjectQuery("tree")
104
105         self.assertEqual("", result.err)
106         self.assertIn("GObject", result.out)
107
108
109 if __name__ == "__main__":
110     unittest.main(testRunner=taptestrunner.TAPTestRunner())