13306: Changes to arvados-cwl-runner code after running futurize --stage2
[arvados.git] / sdk / cwl / tests / matcher.py
1 from builtins import object
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: Apache-2.0
5
6 import difflib
7 import json
8 import re
9
10
11 class JsonDiffMatcher(object):
12     """Raise AssertionError with a readable JSON diff when not __eq__().
13
14     Used with assert_called_with() so it's possible for a human to see
15     the differences between expected and actual call arguments that
16     include non-trivial data structures.
17     """
18     def __init__(self, expected):
19         self.expected = expected
20
21     def __eq__(self, actual):
22         expected_json = json.dumps(self.expected, sort_keys=True, indent=2)
23         actual_json = json.dumps(actual, sort_keys=True, indent=2)
24         if expected_json != actual_json:
25             raise AssertionError("".join(difflib.context_diff(
26                 expected_json.splitlines(1),
27                 actual_json.splitlines(1),
28                 fromfile="Expected", tofile="Actual")))
29         return True
30
31
32 def StripYAMLComments(yml):
33     return re.sub(r'(?ms)^(#.*?\n)*\n*', '', yml)