1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright © 2018, 2019 Endless Mobile, Inc.
5 # Copyright © 2023 Philip Withnall
6 #
7 # SPDX-License-Identifier: LGPL-2.1-or-later
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13 #
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 # MA 02110-1301 USA
23
24 """Integration tests for the gio utility."""
25
26 import collections
27 import os
28 import shutil
29 import subprocess
30 import sys
31 import tempfile
32 import unittest
33
34 import taptestrunner
35
36
37 Result = collections.namedtuple("Result", ("info", "out", "err"))
38
39
40 class ESC[4;38;5;81mTestGioTool(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
41 """Integration test for running the gio tool.
42
43 This can be run when installed or uninstalled. When uninstalled, it
44 requires G_TEST_BUILDDIR and G_TEST_SRCDIR to be set.
45
46 The idea with this test harness is to test the gio utility, its
47 handling of command line arguments, its exit statuses, and its actual
48 effects on the file system.
49 """
50
51 # Track the cwd, we want to back out to that to clean up our tempdir
52 cwd = ""
53
54 def setUp(self):
55 self.timeout_seconds = 6 # seconds per test
56 self.tmpdir = tempfile.TemporaryDirectory()
57 self.cwd = os.getcwd()
58 os.chdir(self.tmpdir.name)
59 print("tmpdir:", self.tmpdir.name)
60
61 ext = ""
62 if os.name == "nt":
63 ext = ".exe"
64
65 if "G_TEST_BUILDDIR" in os.environ:
66 self.__gio = os.path.join(
67 os.environ["G_TEST_BUILDDIR"],
68 "..",
69 "gio" + ext,
70 )
71 else:
72 self.__gio = shutil.which("gio" + ext)
73 print("gio:", self.__gio)
74
75 def tearDown(self):
76 os.chdir(self.cwd)
77 self.tmpdir.cleanup()
78
79 def runGio(self, *args):
80 argv = [self.__gio]
81 argv.extend(args)
82 print("Running:", argv)
83
84 env = os.environ.copy()
85 env["LC_ALL"] = "C.UTF-8"
86 env["G_DEBUG"] = "fatal-warnings"
87 print("Environment:", env)
88
89 # We want to ensure consistent line endings...
90 info = subprocess.run(
91 argv,
92 timeout=self.timeout_seconds,
93 stdout=subprocess.PIPE,
94 stderr=subprocess.PIPE,
95 env=env,
96 universal_newlines=True,
97 )
98 info.check_returncode()
99 out = info.stdout.strip()
100 err = info.stderr.strip()
101
102 result = Result(info, out, err)
103
104 print("Output:", result.out)
105 return result
106
107 def test_help(self):
108 """Test the --help argument and help subcommand."""
109 result = self.runGio("--help")
110 result2 = self.runGio("help")
111
112 self.assertEqual(result.out, result2.out)
113 self.assertEqual(result.err, result2.err)
114
115 self.assertIn("Usage:\n gio COMMAND", result.out)
116 self.assertIn("List the contents of locations", result.out)
117
118 def test_no_args(self):
119 """Test running with no arguments at all."""
120 with self.assertRaises(subprocess.CalledProcessError):
121 self.runGio()
122
123 def test_info_non_default_attributes(self):
124 """Test running `gio info --attributes` with a non-default list."""
125 with tempfile.NamedTemporaryFile(dir=self.tmpdir.name) as tmpfile:
126 result = self.runGio(
127 "info", "--attributes=standard::content-type", tmpfile.name
128 )
129 if sys.platform == "darwin":
130 self.assertIn("standard::content-type: public.text", result.out)
131 else:
132 self.assertIn(
133 "standard::content-type: application/x-zerosize", result.out
134 )
135
136
137 if __name__ == "__main__":
138 unittest.main(testRunner=taptestrunner.TAPTestRunner())