2960: Merge branch 'main' into 2960-keepstore-streaming
[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 from builtins import object
6
7 import difflib
8 import json
9 import re
10
11
12 class JsonDiffMatcher(object):
13     """Raise AssertionError with a readable JSON diff when not __eq__().
14
15     Used with assert_called_with() so it's possible for a human to see
16     the differences between expected and actual call arguments that
17     include non-trivial data structures.
18     """
19     def __init__(self, expected):
20         self.expected = expected
21
22     def __eq__(self, actual):
23         expected_json = json.dumps(self.expected, sort_keys=True, indent=2)
24         actual_json = json.dumps(actual, sort_keys=True, indent=2)
25         if expected_json != actual_json:
26             raise AssertionError("".join(difflib.context_diff(
27                 expected_json.splitlines(1),
28                 actual_json.splitlines(1),
29                 fromfile="Expected", tofile="Actual")))
30         return True
31
32
33 def StripYAMLComments(yml):
34     return re.sub(r'(?ms)^(#.*?\n)*\n*', '', yml)