gio-tool-info: Fix critical warning when --attributes are specified

When `--attributes` is specified and doesn’t include `standard::name` in
its list, `gio` would print a critical warning from the (mandatory) call
to `g_file_info_get_name()`.

Fix that by making the call to `g_file_info_get_name()` optional.

Add a unit test too.

Signed-off-by: Philip Withnall <philip@tecnocode.co.uk>

Fixes: #3158
This commit is contained in:
Philip Withnall 2023-11-01 17:01:50 +00:00 committed by Philip Withnall
parent 3e0a99a059
commit 9028c9bdf3
2 changed files with 10 additions and 1 deletions

View File

@ -177,7 +177,8 @@ show_info (GFile *file, GFileInfo *info)
g_free (flatten);
}
name = g_file_info_get_name (info);
name = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_NAME) ?
g_file_info_get_name (info) : NULL;
if (name)
{
escaped = escape_string (name);

View File

@ -119,6 +119,14 @@ class TestGioTool(unittest.TestCase):
with self.assertRaises(subprocess.CalledProcessError):
self.runGio()
def test_info_non_default_attributes(self):
"""Test running `gio info --attributes` with a non-default list."""
with tempfile.NamedTemporaryFile(dir=self.tmpdir.name) as tmpfile:
result = self.runGio(
"info", "--attributes=standard::content-type", tmpfile.name
)
self.assertIn("standard::content-type: application/x-zerosize", result.out)
if __name__ == "__main__":
unittest.main(testRunner=taptestrunner.TAPTestRunner())