File: assert_equal.py

package info (click to toggle)
python-mitogen 0.3.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,240 kB
  • sloc: python: 19,899; sh: 91; perl: 19; ansic: 18; makefile: 13
file content (70 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# Print data structure diff on assertion failure.
#
# assert_equal:  left=some.result right={1:2}
#

__metaclass__ = type

import inspect
import unittest2

import ansible.template

from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.module_utils.six import string_types


TEMPLATE_KWARGS = {}

_argspec = inspect.getargspec(ansible.template.Templar.template)
if 'bare_deprecated' in _argspec.args:
    TEMPLATE_KWARGS['bare_deprecated'] = False


class TestCase(unittest2.TestCase):
    def runTest(self):
        pass


def text_diff(a, b):
    tc = TestCase()
    tc.maxDiff = None
    try:
        tc.assertEqual(a, b)
        return None
    except AssertionError as e:
        return str(e)


class ActionModule(ActionBase):
    ''' Fail with custom message '''

    TRANSFERS_FILES = False
    _VALID_ARGS = frozenset(('left', 'right'))

    def template(self, obj):
        return self._templar.template(
            obj,
            convert_bare=True,
            **TEMPLATE_KWARGS
        )

    def run(self, tmp=None, task_vars=None):
        result = super(ActionModule, self).run(tmp, task_vars or {})
        left = self.template(self._task.args['left'])
        right = self.template(self._task.args['right'])

        diff = text_diff(left, right)
        if diff is None:
            return {
                'changed': False
            }

        return {
            'changed': False,
            'failed': True,
            'msg': diff,
            '_ansible_verbose_always': True,
        }