@@ -14,49 +14,56 @@ 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 {
21+ let Some ( render_interface) = render_interface else {
22+ return Ok ( ( ) ) ;
23+ } ;
24+
25+ let actual_image = {
2326 let mut player_lock = player. lock ( ) . unwrap ( ) ;
2427 player_lock. render ( ) ;
28+ render_interface. capture ( player_lock. renderer_mut ( ) )
29+ } ;
2530
26- let actual_image = render_interface. capture ( player_lock. renderer_mut ( ) ) ;
27-
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+ let expected_image = {
32+ let path = base_path. join ( format ! ( "{name}.expected.png" ) ) ?;
33+ if path. is_file ( ) ? {
34+ image:: load_from_memory ( & read_bytes ( & path) ?)
3135 . context ( "Failed to open expected image" ) ?
32- . into_rgba8 ( ) ;
33-
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 {
36+ . into_rgba8 ( )
37+ } else if image_comparison. known_failure {
38+ // If we're expecting this to be wrong, don't save a likely wrong image
39+ return Err ( anyhow ! ( "Image '{name}': No image to compare to!" ) ) ;
40+ } else {
41+ write_image ( & path, & actual_image, ImageFormat :: Png ) ?;
4442 return Err ( anyhow ! (
45- "No image to compare to, pretending this failed since we don't know if it worked ."
43+ "Image '{name}': No image to compare to! Saved actual image as expected ."
4644 ) ) ;
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 ) ?;
5045 }
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).
54- return Err ( anyhow ! (
55- "Not checking images, pretending this failed since we don't know if it worked."
56- ) ) ;
57- }
46+ } ;
5847
59- Ok ( ( ) )
48+ let result = test (
49+ & image_comparison,
50+ name,
51+ actual_image,
52+ expected_image,
53+ base_path,
54+ render_interface. name ( ) ,
55+ // If we're expecting failure, spamming files isn't productive.
56+ !image_comparison. known_failure ,
57+ ) ;
58+
59+ match ( result, image_comparison. known_failure ) {
60+ ( result, false ) => result,
61+ ( Ok ( ( ) ) , true ) => Err ( anyhow ! (
62+ "Image '{name}': Check was known to be failing, but now passes successfully. \
63+ Please update the test and remove `known_failure = true`!",
64+ ) ) ,
65+ ( Err ( _) , true ) => Ok ( ( ) ) ,
66+ }
6067}
6168
6269pub fn test (
@@ -66,13 +73,12 @@ pub fn test(
6673 expected_image : image:: RgbaImage ,
6774 test_path : & VfsPath ,
6875 environment_name : String ,
69- known_failure : bool ,
76+ save_failures : bool ,
7077) -> anyhow:: Result < ( ) > {
7178 use anyhow:: Context ;
7279
7380 let save_actual_image = || {
74- if !known_failure {
75- // If we're expecting failure, spamming files isn't productive.
81+ if save_failures {
7682 write_image (
7783 & test_path. join ( format ! ( "{name}.actual-{environment_name}.png" ) ) ?,
7884 & actual_image,
@@ -141,8 +147,7 @@ pub fn test(
141147 difference_color. extend_from_slice ( & p[ ..3 ] ) ;
142148 }
143149
144- if !known_failure {
145- // If we're expecting failure, spamming files isn't productive.
150+ if save_failures {
146151 let difference_image = image:: RgbImage :: from_raw (
147152 actual_image. width ( ) ,
148153 actual_image. height ( ) ,
@@ -163,8 +168,7 @@ pub fn test(
163168 difference_alpha. push ( p[ 3 ] )
164169 }
165170
166- if !known_failure {
167- // If we're expecting failure, spamming files isn't productive.
171+ if save_failures {
168172 let difference_image = image:: GrayImage :: from_raw (
169173 actual_image. width ( ) ,
170174 actual_image. height ( ) ,
0 commit comments