1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

MH - dicts and lists change-tracking is fixed (#2951)

* dicts and lists change-tracking is fixed

* added changelog fragment

* fixed sanity check
This commit is contained in:
Alexei Znamensky 2021-07-08 17:20:01 +12:00 committed by GitHub
parent c0740ca398
commit 56acd4356f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- module_helper module utils - fixed change-tracking for dictionaries and lists (https://github.com/ansible-collections/community.general/pull/2951).

View file

@ -6,6 +6,8 @@
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
import copy
class VarMeta(object): class VarMeta(object):
NOTHING = object() NOTHING = object()
@ -30,11 +32,11 @@ class VarMeta(object):
if fact is not None: if fact is not None:
self.fact = fact self.fact = fact
if initial_value is not self.NOTHING: if initial_value is not self.NOTHING:
self.initial_value = initial_value self.initial_value = copy.deepcopy(initial_value)
def set_value(self, value): def set_value(self, value):
if not self.init: if not self.init:
self.initial_value = value self.initial_value = copy.deepcopy(value)
self.init = True self.init = True
self.value = value self.value = value
return self return self

View file

@ -151,17 +151,45 @@ def test_vardict():
assert vd.meta('a').diff is False assert vd.meta('a').diff is False
assert vd.meta('a').change is False assert vd.meta('a').change is False
vd['b'] = 456 vd['b'] = 456
assert vd.meta('b').output is True
assert vd.meta('b').diff is False
assert vd.meta('b').change is False
vd.set_meta('a', diff=True, change=True) vd.set_meta('a', diff=True, change=True)
vd.set_meta('b', diff=True, output=False) vd.set_meta('b', diff=True, output=False)
vd['c'] = 789 vd['c'] = 789
assert vd.has_changed('c') is False
vd['a'] = 'new_a' vd['a'] = 'new_a'
assert vd.has_changed('a') is True
vd['c'] = 'new_c' vd['c'] = 'new_c'
assert vd.has_changed('c') is False
vd['b'] = 'new_b'
assert vd.has_changed('b') is False
assert vd.a == 'new_a' assert vd.a == 'new_a'
assert vd.c == 'new_c' assert vd.c == 'new_c'
assert vd.output() == {'a': 'new_a', 'c': 'new_c'} assert vd.output() == {'a': 'new_a', 'c': 'new_c'}
assert vd.diff() == {'before': {'a': 123}, 'after': {'a': 'new_a'}}, "diff={0}".format(vd.diff()) assert vd.diff() == {'before': {'a': 123}, 'after': {'a': 'new_a'}}, "diff={0}".format(vd.diff())
def test_variable_meta_change():
vd = VarDict()
vd.set('a', 123, change=True)
vd.set('b', [4, 5, 6], change=True)
vd.set('c', {'m': 7, 'n': 8, 'o': 9}, change=True)
vd.set('d', {'a1': {'a11': 33, 'a12': 34}}, change=True)
vd.a = 1234
assert vd.has_changed('a') is True
vd.b.append(7)
assert vd.b == [4, 5, 6, 7]
assert vd.has_changed('b')
vd.c.update({'p': 10})
assert vd.c == {'m': 7, 'n': 8, 'o': 9, 'p': 10}
assert vd.has_changed('c')
vd.d['a1'].update({'a13': 35})
assert vd.d == {'a1': {'a11': 33, 'a12': 34, 'a13': 35}}
assert vd.has_changed('d')
class MockMH(object): class MockMH(object):
changed = None changed = None