Add xdg_mime module (#10007)

* Add version of xdg_mime module

* Fix xdg_mime_get since the command is different

* Add query parameter

* Fix order of parameters

* Add myself to BOTMETA

* Add unit tests

* Fix the way we deal when there is no handler set

* Improve documentation

* Remove unused import

* Fix documentation

* Strip xdg-mime from version string

* Fix information about version

* Add error message sample

* Add test to invalid handler

* Add support to multiple mime-types

* Change the output parameter from handlers to handler

* Change tests related to multiple mime-type support

* Small fixes

* Stop using constant to enable changed state

* Add before_handlers and after_handlers

* Change tests to use before and after structures

* Add a stronger message about using a non-installed handler

* Manage some edge cases

* Change error message to match the new value

* Add some fixes

* Change some tests

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Remove a blank line

* Remove single quote

* Add xdg-mime to the version in the mocks

* Remove after_handlers and make code simpler

* Update tests to work without after_handlers

* Remove diff_params and clean output_params

* Make mime_type plural since it supports multiple items

* Move the handler check to module init

* Use anchors in the test to make yaml simpler

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Add blank line to separe examples

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* Add a small homage to my late grandma

* Update plugins/modules/xdg_mime.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Fix pep8 problem with the homage

* Remove trailing whitespace

* Update plugins/modules/xdg_mime.py

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

---------

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Marcos Alano 2025-04-26 07:34:03 -03:00 committed by GitHub
parent 8fa357e74b
commit 373334d668
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 288 additions and 0 deletions

View file

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2025, Marcos Alano <marcoshalano@gmail.com>
# Based on gio_mime module. Copyright (c) 2022, Alexei Znamensky <russoz@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible_collections.community.general.plugins.modules import xdg_mime
from .uthelper import UTHelper, RunCommandMock
UTHelper.from_module(xdg_mime, __name__, mocks=[RunCommandMock])

View file

@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2025, Marcos Alano <marcoshalano@gmail.com>
# Based on gio_mime module. Copyright (c) 2022, Alexei Znamensky <russoz@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# TODO: add tests for setting multiple mime types at once
---
anchors:
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
input: &input
mime_types: x-scheme-handler/http
handler: google-chrome.desktop
get_version: &get_version
command: [/testbin/xdg-mime, --version]
environ: *env-def
rc: 0
out: "xdg-mime 1.2.1\n"
err: ''
query_mime_type: &query_mime_type
command: [/testbin/xdg-mime, query, default, x-scheme-handler/http]
environ: *env-def
rc: 0
out: ''
err: ''
set_handler: &set_handler
command: [/testbin/xdg-mime, default, google-chrome.desktop, x-scheme-handler/http]
environ: *env-def
rc: 0
out: ''
err: ''
test_cases:
- id: test_set_handler
input: *input
output:
current_handlers: ['']
changed: true
mocks:
run_command:
- *get_version
- *query_mime_type
- *set_handler
- id: test_set_handler_check
input: *input
output:
current_handlers: ['google-chrome.desktop']
changed: false
flags:
check: true
mocks:
run_command:
- *get_version
- <<: *query_mime_type
out: |
google-chrome.desktop
- id: test_set_handler_idempot
input: *input
output:
current_handlers: ['google-chrome.desktop']
changed: false
mocks:
run_command:
- *get_version
- <<: *query_mime_type
out: |
google-chrome.desktop
- id: test_set_handler_idempot_check
input: *input
output:
current_handlers: ['google-chrome.desktop']
changed: false
flags:
check: true
mocks:
run_command:
- *get_version
- <<: *query_mime_type
out: |
google-chrome.desktop
- id: test_set_invalid_handler
input:
<<: *input
handler: google-chrome.desktopX
output:
failed: true
msg: Handler must be a .desktop file
mocks:
run_command:
- *get_version