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 ESC[4;38;5;81mTestGobjectQuery(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
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 env["G_DEBUG"] = "fatal-warnings"
63 print("Environment:", env)
64
65 # We want to ensure consistent line endings...
66 info = subprocess.run(
67 argv,
68 timeout=self.timeout_seconds,
69 stdout=subprocess.PIPE,
70 stderr=subprocess.PIPE,
71 env=env,
72 text=True,
73 encoding="utf-8",
74 )
75 info.check_returncode()
76 out = info.stdout.strip()
77 err = info.stderr.strip()
78
79 result = Result(info, out, err)
80
81 print("Output:", result.out)
82 return result
83
84 def test_help(self):
85 """Test the --help argument."""
86 result = self.runGobjectQuery("--help")
87 self.assertIn("usage: gobject-query", result.out)
88
89 def test_version(self):
90 """Test the --version argument."""
91 result = self.runGobjectQuery("--version")
92 self.assertIn("2.", result.out)
93
94 def test_froots(self):
95 """Test running froots with no other arguments."""
96 result = self.runGobjectQuery("froots")
97
98 self.assertEqual("", result.err)
99 self.assertIn("├gboolean", result.out)
100 self.assertIn("├GObject", result.out)
101
102 def test_tree(self):
103 """Test running tree with no other arguments."""
104 result = self.runGobjectQuery("tree")
105
106 self.assertEqual("", result.err)
107 self.assertIn("GObject", result.out)
108
109
110 if __name__ == "__main__":
111 unittest.main(testRunner=taptestrunner.TAPTestRunner())