From e803f3de0eb143aeb5c6d40c74dd22989df421c8 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Thu, 21 Sep 2023 12:21:49 -0400 Subject: [PATCH] Add simple tests for plugins --- .config/dictionary.txt | 1 + examples/plugins/modules/example.py | 24 ++++++++++++++++++++++++ test/test_file_utils.py | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 examples/plugins/modules/example.py diff --git a/.config/dictionary.txt b/.config/dictionary.txt index fb30b67ac94..d8c091b226a 100644 --- a/.config/dictionary.txt +++ b/.config/dictionary.txt @@ -16,6 +16,7 @@ MYTAG PYTHONBREAKPOINT PYTHONIOENCODING PYTHONPYCACHEPREFIX +Qalthos REQPASS RULEDIRS RUNLEVEL diff --git a/examples/plugins/modules/example.py b/examples/plugins/modules/example.py new file mode 100644 index 00000000000..f853b184957 --- /dev/null +++ b/examples/plugins/modules/example.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright: (c) 2023 Kate Case (@Qalthos) +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +"""An example module for testing purposes""" + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = r""" +--- +module: example +short_description: Does nothing +description: +- This module exists solely as an example +""" + +EXAMPLES = r""" +- name: "playbook" + tasks: + - name: Hello + debug: + msg: 'world' +""" diff --git a/test/test_file_utils.py b/test/test_file_utils.py index 81faa3253a0..c8f23d96197 100644 --- a/test/test_file_utils.py +++ b/test/test_file_utils.py @@ -258,6 +258,11 @@ def test_discover_lintables_umlaut(monkeypatch: MonkeyPatch) -> None: "playbook", id="43", ), # content should determine it as a play + pytest.param( + "examples/plugins/modules/example.py", + "plugin", + id="44", + ), ), ) def test_kinds(path: str, kind: FileType) -> None: @@ -536,3 +541,18 @@ def test_bug_2513( results = Runner(filename, rules=default_rules_collection).run() assert len(results) == 1 assert results[0].rule.id == "name" + + +def test_examples_content() -> None: + """Test that a module loads the correct content. + + Test that when CWD is outside ~, and argument is like ~/playbook.yml + we will still be able to process the files. + See: https://github.com/ansible/ansible-lint/issues/2513 + """ + filename = Path("examples/plugins/modules/example.py") + lintable = Lintable(filename) + # Lintable is now correctly purporting to be a YAML file + assert lintable.base_kind == "text/yaml" + # Lintable content should be contents of EXAMPLES + assert lintable.content == "---" + BASIC_PLAYBOOK