@@ -14,49 +14,53 @@ pub fn capture_and_compare_image(
1414 player : & Arc < Mutex < Player > > ,
1515 name : & String ,
1616 image_comparison : ImageComparison ,
17- known_failure : bool ,
1817 render_interface : Option < & dyn RenderInterface > ,
1918) -> anyhow:: Result < ( ) > {
2019 use anyhow:: Context ;
2120
22- if let Some ( render_interface) = render_interface {
23- let mut player_lock = player . lock ( ) . unwrap ( ) ;
24- player_lock . render ( ) ;
21+ let Some ( render_interface) = render_interface else {
22+ return Ok ( ( ) ) ;
23+ } ;
2524
26- let actual_image = render_interface. capture ( player_lock. renderer_mut ( ) ) ;
25+ let mut player_lock = player. lock ( ) . unwrap ( ) ;
26+ player_lock. render ( ) ;
2727
28- let expected_image_path = base_path. join ( format ! ( "{name}.expected.png" ) ) ?;
29- if expected_image_path. is_file ( ) ? {
30- let expected_image = image:: load_from_memory ( & read_bytes ( & expected_image_path) ?)
31- . context ( "Failed to open expected image" ) ?
32- . into_rgba8 ( ) ;
28+ let actual_image = render_interface. capture ( player_lock. renderer_mut ( ) ) ;
3329
34- test (
35- & image_comparison,
36- name,
37- actual_image,
38- expected_image,
39- base_path,
40- render_interface. name ( ) ,
41- known_failure,
42- ) ?;
43- } else if known_failure {
44- return Err ( anyhow ! (
45- "No image to compare to, pretending this failed since we don't know if it worked."
46- ) ) ;
47- } else {
48- // If we're expecting this to be wrong, don't save a likely wrong image
49- write_image ( & expected_image_path, & actual_image, ImageFormat :: Png ) ?;
50- }
51- } else if known_failure {
52- // It's possible that the trace output matched but the image might not.
53- // If we aren't checking the image, pretend the match failed (which makes it actually pass, since it's expecting failure).
30+ let expected_image_path = base_path. join ( format ! ( "{name}.expected.png" ) ) ?;
31+ let expected_image = if expected_image_path. is_file ( ) ? {
32+ image:: load_from_memory ( & read_bytes ( & expected_image_path) ?)
33+ . context ( "Failed to open expected image" ) ?
34+ . into_rgba8 ( )
35+ } else if image_comparison. known_failure {
36+ // If we're expecting this to be wrong, don't save a likely wrong image
37+ return Err ( anyhow ! ( "Image '{name}': No image to compare to!" ) ) ;
38+ } else {
39+ write_image ( & expected_image_path, & actual_image, ImageFormat :: Png ) ?;
5440 return Err ( anyhow ! (
55- "Not checking images, pretending this failed since we don't know if it worked ."
41+ "Image '{name}': No image to compare to! Saved actual image as expected ."
5642 ) ) ;
57- }
43+ } ;
5844
59- Ok ( ( ) )
45+ let result = test (
46+ & image_comparison,
47+ name,
48+ actual_image,
49+ expected_image,
50+ base_path,
51+ render_interface. name ( ) ,
52+ // If we're expecting failure, spamming files isn't productive.
53+ !image_comparison. known_failure ,
54+ ) ;
55+
56+ match ( result, image_comparison. known_failure ) {
57+ ( result, false ) => result,
58+ ( Ok ( ( ) ) , true ) => Err ( anyhow ! (
59+ "Image '{name}': Check was known to be failing, but now passes successfully. \
60+ Please update the test and remove `known_failure = true`!",
61+ ) ) ,
62+ ( Err ( _) , true ) => Ok ( ( ) ) ,
63+ }
6064}
6165
6266pub fn test (
@@ -66,13 +70,12 @@ pub fn test(
6670 expected_image : image:: RgbaImage ,
6771 test_path : & VfsPath ,
6872 environment_name : String ,
69- known_failure : bool ,
73+ save_failures : bool ,
7074) -> anyhow:: Result < ( ) > {
7175 use anyhow:: Context ;
7276
7377 let save_actual_image = || {
74- if !known_failure {
75- // If we're expecting failure, spamming files isn't productive.
78+ if save_failures {
7679 write_image (
7780 & test_path. join ( format ! ( "{name}.actual-{environment_name}.png" ) ) ?,
7881 & actual_image,
@@ -141,8 +144,7 @@ pub fn test(
141144 difference_color. extend_from_slice ( & p[ ..3 ] ) ;
142145 }
143146
144- if !known_failure {
145- // If we're expecting failure, spamming files isn't productive.
147+ if save_failures {
146148 let difference_image = image:: RgbImage :: from_raw (
147149 actual_image. width ( ) ,
148150 actual_image. height ( ) ,
@@ -163,8 +165,7 @@ pub fn test(
163165 difference_alpha. push ( p[ 3 ] )
164166 }
165167
166- if !known_failure {
167- // If we're expecting failure, spamming files isn't productive.
168+ if save_failures {
168169 let difference_image = image:: GrayImage :: from_raw (
169170 actual_image. width ( ) ,
170171 actual_image. height ( ) ,
0 commit comments