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