From 0f24575e6d2b757f8a90d390ef988cd4302b5dc7 Mon Sep 17 00:00:00 2001 From: Ashwini Mhatre Date: Thu, 11 Jan 2024 18:37:49 +0530 Subject: [PATCH] Add integration and unit tests --- plugins/plugin_utils/fact_diff.py | 15 +++---- .../targets/utils_fact_diff/tasks/filter.yaml | 45 +++++++++++-------- tests/unit/plugins/filter/test_fact_diff.py | 10 +++++ 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/plugins/plugin_utils/fact_diff.py b/plugins/plugin_utils/fact_diff.py index 1a65fc69..8e486807 100644 --- a/plugins/plugin_utils/fact_diff.py +++ b/plugins/plugin_utils/fact_diff.py @@ -123,14 +123,13 @@ def get_fact_diff(self, difflist): has_diff = False for line in difflines: has_diff = True - if not diff["common"]: - if line.startswith("+"): - line = stringc(line, C.COLOR_DIFF_ADD) - elif line.startswith("-"): - line = stringc(line, C.COLOR_DIFF_REMOVE) - elif line.startswith("@@"): - line = stringc(line, C.COLOR_DIFF_LINES) - ret.append(line) + if diff["common"]: + if line.startswith("+") or line.startswith("-"): + pass + else: + ret.append(line) + else: + ret.append(line) if has_diff: ret.append("\n") if "prepared" in diff: diff --git a/tests/integration/targets/utils_fact_diff/tasks/filter.yaml b/tests/integration/targets/utils_fact_diff/tasks/filter.yaml index 2ff2f1ad..bd6e02c8 100644 --- a/tests/integration/targets/utils_fact_diff/tasks/filter.yaml +++ b/tests/integration/targets/utils_fact_diff/tasks/filter.yaml @@ -20,28 +20,16 @@ msg: "The regex '+', is not valid" - name: Check argspec validation - ansible.builtin.set_fact: "{{ ansible.utils.fact_diff([1, 2, 3]) }}" + ansible.builtin.set_fact: + result: "{{ [1, 2, 3] | ansible.utils.fact_diff() }}" + register: error ignore_errors: true - register: result - name: Assert ansible.builtin.assert: - that: "{{ msg in result.msg }}" + that: "{{ msg in error.msg }}" vars: msg: "missing required arguments: after" - when: "result.msg | type_debug == 'list'" - -- name: Check argspec validation - ansible.builtin.set_fact: "{{ ansible.utils.fact_diff([1, 2, 3]) }}" - ignore_errors: true - register: result - -- name: Assert - ansible.builtin.assert: - that: "{{ msg in result.msg }}" - vars: - msg: "missing required arguments: before" - when: "result.msg | type_debug == 'list'" - name: Set fact ansible.builtin.set_fact: @@ -69,7 +57,7 @@ before: "{{ before | ansible.utils.to_paths }}" after: "{{ after | ansible.utils.to_paths }}" -- name: Show the difference in json format +- name: Show the difference in path format ansible.builtin.set_fact: result: "{{ before | ansible.utils.fact_diff(after) }}" @@ -78,6 +66,27 @@ before: "{{ before | to_nice_yaml }}" after: "{{ after | to_nice_yaml }}" -- name: Show the difference in json format +- name: Show the difference in yaml format ansible.builtin.set_fact: result: "{{ before | ansible.utils.fact_diff(after) }}" + +- name: Set fact + ansible.builtin.set_fact: + before: + a: + b: + c: + d: + - 0 + - 1 + after: + a: + b: + c: + d: + - 2 + - 3 + +- name: Show the common lines in json format + ansible.builtin.set_fact: + result: "{{ before | ansible.utils.fact_diff(after, common=true) }}" diff --git a/tests/unit/plugins/filter/test_fact_diff.py b/tests/unit/plugins/filter/test_fact_diff.py index f112d12e..c4bdff01 100644 --- a/tests/unit/plugins/filter/test_fact_diff.py +++ b/tests/unit/plugins/filter/test_fact_diff.py @@ -106,3 +106,13 @@ def test_argspec(self): "missing required arguments: after", str(error.exception), ) + + def test_diff_dict_common(self): + """Compare two dicts that with common option""" + self.maxDiff = None + before = {"a": {"b": {"c": {"d": [0, 1, 2, 3]}}}} + after = {"a": {"b": {"c": {"d": [0, 1, 2, 4]}}}} + result = _fact_diff("", before, after, common=True) + self.assertIn(" 0", result) + self.assertIn(" 1", result) + self.assertIn(" 2", result)