@@ -39,6 +39,33 @@ def pytest_configure(config):
3939 )
4040
4141
42+ def skip_image_decoder_test (codec ):
43+ # Whether to skip a test that needs the given image codec: we skip when the
44+ # backing library isn't available, unless we're told to fail loudly (on CI
45+ # we are, so a missing library surfaces as a failure rather than a silent
46+ # skip).
47+ #
48+ # FAIL_WITHOUT_IMAGE_DECODERS is a catch-all default covering every image
49+ # codec. A per-codec FAIL_WITHOUT_<CODEC> overrides the catch-all, so a CI
50+ # job can enable the catch-all and still opt a single codec out, e.g.
51+ # FAIL_WITHOUT_IMAGE_DECODERS=1 FAIL_WITHOUT_HEIC=0.
52+ if {
53+ "jpeg" : jpeg_is_available ,
54+ "png" : png_is_available ,
55+ "webp" : webp_is_available ,
56+ "avif" : avif_is_available ,
57+ "heic" : heic_is_available ,
58+ }[codec ]():
59+ return False
60+ override = os .environ .get (f"FAIL_WITHOUT_{ codec .upper ()} " )
61+ fail_without = (
62+ override
63+ if override is not None
64+ else os .environ .get ("FAIL_WITHOUT_IMAGE_DECODERS" )
65+ )
66+ return fail_without in (None , "0" )
67+
68+
4269def pytest_collection_modifyitems (items ):
4370 # This hook is called by pytest after it has collected the tests (google its
4471 # name to check out its doc!). We can ignore some tests as we see fit here,
@@ -95,52 +122,20 @@ def pytest_collection_modifyitems(items):
95122 # those for whatever reason, we need to know.
96123 item .add_marker (pytest .mark .skip (reason = "CUDA not available." ))
97124
98- # Same rationale as needs_cuda: skip when libjpeg support isn't built in,
99- # unless FAIL_WITHOUT_JPEG is set (on CI it is, so a missing libjpeg
100- # surfaces as a failure rather than a silent skip).
101- if (
102- needs_jpeg
103- and not jpeg_is_available ()
104- and os .environ .get ("FAIL_WITHOUT_JPEG" ) is None
105- ):
125+ # Same rationale as needs_cuda; see skip_image_decoder_test().
126+ if needs_jpeg and skip_image_decoder_test ("jpeg" ):
106127 item .add_marker (pytest .mark .skip (reason = "libjpeg support not available." ))
107128
108- # Same rationale as needs_jpeg: skip when libpng support isn't built in,
109- # unless FAIL_WITHOUT_PNG is set (on CI it is, so a missing libpng
110- # surfaces as a failure rather than a silent skip).
111- if (
112- needs_png
113- and not png_is_available ()
114- and os .environ .get ("FAIL_WITHOUT_PNG" ) is None
115- ):
129+ if needs_png and skip_image_decoder_test ("png" ):
116130 item .add_marker (pytest .mark .skip (reason = "libpng support not available." ))
117131
118- # Same rationale as needs_jpeg: skip when libwebp support isn't built in,
119- # unless FAIL_WITHOUT_WEBP is set (on CI it is, so a missing libwebp
120- # surfaces as a failure rather than a silent skip).
121- if (
122- needs_webp
123- and not webp_is_available ()
124- and os .environ .get ("FAIL_WITHOUT_WEBP" ) is None
125- ):
132+ if needs_webp and skip_image_decoder_test ("webp" ):
126133 item .add_marker (pytest .mark .skip (reason = "libwebp support not available." ))
127134
128- # Same rationale as needs_jpeg: skip when libavif support isn't built in,
129- # unless FAIL_WITHOUT_AVIF is set (on CI it is, so a missing libavif
130- # surfaces as a failure rather than a silent skip).
131- if (
132- needs_avif
133- and not avif_is_available ()
134- and os .environ .get ("FAIL_WITHOUT_AVIF" ) is None
135- ):
135+ if needs_avif and skip_image_decoder_test ("avif" ):
136136 item .add_marker (pytest .mark .skip (reason = "libavif support not available." ))
137137
138- # Same rationale as needs_jpeg
139- if (
140- needs_heic
141- and not heic_is_available ()
142- and os .environ .get ("FAIL_WITHOUT_HEIC" ) is None
143- ):
138+ if needs_heic and skip_image_decoder_test ("heic" ):
144139 item .add_marker (pytest .mark .skip (reason = "libheif support not available." ))
145140
146141 out_items .append (item )
0 commit comments