Skip to content

Commit

Permalink
Add integration and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwini Mhatre committed Jan 11, 2024
1 parent 0c017e1 commit 0f24575
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
15 changes: 7 additions & 8 deletions plugins/plugin_utils/fact_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 27 additions & 18 deletions tests/integration/targets/utils_fact_diff/tasks/filter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) }}"

Expand All @@ -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) }}"
10 changes: 10 additions & 0 deletions tests/unit/plugins/filter/test_fact_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0f24575

Please sign in to comment.