|
| 1 | +import subprocess |
| 2 | + |
1 | 3 | import numpy as np
|
2 | 4 | from approvaltests.core import Comparator
|
3 | 5 | from approvaltests.namer import NamerFactory
|
4 | 6 |
|
| 7 | +import json |
| 8 | +from approvaltests import verify, Options |
| 9 | +from approvaltests.namer import NamerFactory |
| 10 | +from approvaltests.reporters import GenericDiffReporter, GenericDiffReporterConfig |
| 11 | + |
| 12 | + |
| 13 | +class WSLWindowsDiffReporter(GenericDiffReporter): |
| 14 | + def get_command(self, received, approved): |
| 15 | + # Convert WSL paths to Windows paths |
| 16 | + win_received = subprocess.check_output(['wslpath', '-w', received]).decode().strip() |
| 17 | + win_approved = subprocess.check_output(['wslpath', '-w', approved]).decode().strip() |
| 18 | + |
| 19 | + cmd = [self.path] + self.extra_args + [win_received, win_approved] |
| 20 | + return cmd |
| 21 | + |
| 22 | +def verify_json(item, name: str): |
| 23 | + |
| 24 | + config = GenericDiffReporterConfig( |
| 25 | + name="custom", |
| 26 | + path=r"pycharm", |
| 27 | + extra_args= ["diff"] |
| 28 | + ) |
| 29 | + |
| 30 | + parameters: Options = NamerFactory \ |
| 31 | + .with_parameters(name) \ |
| 32 | + .with_reporter( |
| 33 | + reporter=(WSLWindowsDiffReporter(config)) |
| 34 | + ) |
| 35 | + |
| 36 | + verify(item, options=parameters) |
| 37 | + |
5 | 38 |
|
6 | 39 | def gempy_verify_array(item, name: str, rtol: float = 1e-5, atol: float = 1e-5, ):
|
7 | 40 | # ! You will have to set the path to your diff tool
|
8 | 41 | reporter = GenericDiffReporter.create(
|
9 | 42 | diff_tool_path=r"/usr/bin/meld"
|
10 | 43 | )
|
11 |
| - |
| 44 | + |
12 | 45 | parameters: Options = NamerFactory \
|
13 | 46 | .with_parameters(name) \
|
14 | 47 | .with_comparator(
|
15 |
| - comparator=ArrayComparator(atol=atol, rtol=rtol) |
16 |
| - ).with_reporter( |
17 |
| - reporter=reporter |
| 48 | + comparator=ArrayComparator(atol=atol, rtol=rtol) |
| 49 | + ).with_reporter( |
| 50 | + reporter=reporter |
18 | 51 | )
|
19 |
| - |
20 |
| - |
21 |
| - verify(np.asarray(item), options=parameters) |
22 | 52 |
|
23 |
| -def verify_json(item, name: str): |
24 |
| - parameters: Options = NamerFactory \ |
25 |
| - .with_parameters(name) \ |
26 |
| - .with_reporter( |
27 |
| - reporter=GenericDiffReporter.create( |
28 |
| - diff_tool_path=r"/usr/bin/meld" |
29 |
| - ) |
30 |
| - ) |
31 |
| - |
32 |
| - verify(item, options=parameters) |
| 53 | + verify(np.asarray(item), options=parameters) |
33 | 54 |
|
34 | 55 |
|
35 | 56 | class ArrayComparator(Comparator):
|
@@ -66,10 +87,6 @@ def compare(self, received_path: str, approved_path: str) -> bool:
|
66 | 87 | return allclose
|
67 | 88 | except BaseException:
|
68 | 89 | return False
|
69 |
| -import json |
70 |
| -from approvaltests import verify, Options |
71 |
| -from approvaltests.namer import NamerFactory |
72 |
| -from approvaltests.reporters import GenericDiffReporter |
73 | 90 |
|
74 | 91 | class JsonSerializer:
|
75 | 92 | """Serializer that writes JSON with an indent and declares its own extension."""
|
|
0 commit comments