8
8
import time
9
9
import toml
10
10
import json
11
+ import pathlib
11
12
from shutil import copy , rmtree
12
13
from subprocess import check_call , check_output , CalledProcessError
13
14
@@ -24,6 +25,7 @@ def split(s):
24
25
parser .add_argument ('--publish' , action = 'store_true' , help = 'publish the crates' )
25
26
parser .add_argument ('--build' , action = 'store_true' , help = 'build the crates' )
26
27
parser .add_argument ('--test' , action = 'store_true' , help = 'test the crates' )
28
+ parser .add_argument ('--test_outputs' , action = 'store_true' , help = 'run the unittests' )
27
29
parser .add_argument ('--clean' , action = 'store_true' , help = 'clean the crates' )
28
30
parser .add_argument ('--doc' , action = 'store_true' , help = 'build the documentation' )
29
31
parser .add_argument ('--format' , action = 'store_true' , help = 'format all the non-sys crates' )
@@ -44,7 +46,7 @@ def cargo_cmd(*command):
44
46
print ('Processing' , cargo_toml )
45
47
46
48
for line in fileinput .input (cargo_toml , inplace = 1 ):
47
- line = re .sub ('version = "(=?).*" #auto' , 'version = "\g<1>' + args .version + '" #auto' , line )
49
+ line = re .sub ('version = "(=?).*" #auto' , r 'version = "\g<1>' + args .version + '" #auto' , line )
48
50
print (line , end = '' )
49
51
50
52
if args .publish :
@@ -93,6 +95,46 @@ def cargo_cmd(*command):
93
95
check_call (cargo_cmd ('test' ), cwd = crate )
94
96
check_call (cargo_cmd ('fmt' , '--check' ), cwd = crate )
95
97
98
+ if args .test_outputs :
99
+ import numpy as np
100
+ from PIL import Image
101
+
102
+ os .makedirs ('test_outputs' , exist_ok = True )
103
+ output_dir = os .path .abspath ('test_outputs' )
104
+ metadata = json .loads (check_output (cargo_cmd ('metadata' , '--format-version=1' , '--no-deps' ), cwd = 'gnuplot' ).decode ('utf8' ))
105
+ for target in metadata ['packages' ][0 ]['targets' ]:
106
+ if target ['kind' ] != ['example' ]:
107
+ continue
108
+
109
+ if target ['name' ] in [
110
+ 'animation_example' , # Special.
111
+ 'inverse_api' , # Special.
112
+ 'example3' , # Broken.
113
+ ]:
114
+ continue
115
+
116
+ check_call (cargo_cmd ('run' , '--example' , target ['name' ], '--' , '--no-show' , '--output-dir' , output_dir , '--save-png' ), cwd = 'gnuplot' )
117
+
118
+ golden_images = [pathlib .Path (f ) for f in glob .glob ('golden_outputs/*.png' )]
119
+ test_images = [pathlib .Path (f ) for f in glob .glob (f'{ output_dir } /*.png' )]
120
+
121
+ golden_filenames = set (f .name for f in golden_images )
122
+ test_filenames = set (f .name for f in test_images )
123
+ if golden_filenames != test_filenames :
124
+ missing = set (golden_filenames ) - set (test_filenames )
125
+ extra = set (test_filenames ) - set (golden_filenames )
126
+ assert False , f"Test images don't match golden images.\n Extra: { extra } \n Missing: { missing } "
127
+
128
+ for image_name in golden_images :
129
+ golden_image_path = pathlib .Path (image_name )
130
+ test_image_path = pathlib .Path (output_dir ) / golden_image_path .name
131
+ assert test_image_path .exists (), f"{ test_image_path } not found"
132
+
133
+ golden_image = np .array (Image .open (golden_image_path )).astype (np .float32 )
134
+ test_image = np .array (Image .open (test_image_path )).astype (np .float32 )
135
+ np .testing .assert_allclose (golden_image , test_image , atol = 5 , err_msg = image_name )
136
+
137
+
96
138
if args .clean :
97
139
crates_and_doc = ['doc' ]
98
140
crates_and_doc .extend (crate_list )
0 commit comments