Skip to content

Commit b066651

Browse files
committed
Tidy formatting of options code
using patterns from #150
1 parent d71296f commit b066651

File tree

1 file changed

+111
-102
lines changed

1 file changed

+111
-102
lines changed

pytest_mpl/plugin.py

+111-102
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444

4545
from pytest_mpl.summary.html import generate_summary_basic_html, generate_summary_html
4646

47-
SUPPORTED_FORMATS = {'html', 'json', 'basic-html'}
47+
DEFAULT_STYLE = "classic"
48+
DEFAULT_TOLERANCE: float = 2
49+
DEFAULT_BACKEND = "agg"
50+
51+
SUPPORTED_FORMATS = {"html", "json", "basic-html"}
4852

4953
SHAPE_MISMATCH_ERROR = """Error: Image dimensions did not match.
5054
Expected shape: {expected_shape}
@@ -125,60 +129,71 @@ def pytest_report_header(config, startdir):
125129

126130
def pytest_addoption(parser):
127131
group = parser.getgroup("matplotlib image comparison")
128-
group.addoption('--mpl', action='store_true',
129-
help="Enable comparison of matplotlib figures to reference files")
130-
group.addoption('--mpl-generate-path',
131-
help="directory to generate reference images in, relative "
132-
"to location where py.test is run", action='store')
133-
group.addoption('--mpl-generate-hash-library',
134-
help="filepath to save a generated hash library, relative "
135-
"to location where py.test is run", action='store')
136-
137-
baseline_path_help = (
132+
133+
msg = "Enable comparison of matplotlib figures to reference files"
134+
group.addoption("--mpl", help=msg, action="store_true")
135+
136+
msg = "directory to generate reference images in, relative to location where py.test is run"
137+
group.addoption("--mpl-generate-path", help=msg, action="store")
138+
139+
msg = "filepath to save a generated hash library, relative to location where py.test is run"
140+
group.addoption("--mpl-generate-hash-library", help=msg, action="store")
141+
142+
msg = (
138143
"directory containing baseline images, relative to "
139144
"location where py.test is run unless --mpl-baseline-relative is given. "
140145
"This can also be a URL or a set of comma-separated URLs (in case "
141146
"mirrors are specified)"
142147
)
143-
group.addoption("--mpl-baseline-path", help=baseline_path_help, action="store")
144-
parser.addini("mpl-baseline-path", help=baseline_path_help)
148+
option = "mpl-baseline-path"
149+
group.addoption(f"--{option}", help=msg, action="store")
150+
parser.addini(option, help=msg)
145151

146-
group.addoption("--mpl-baseline-relative", help="interpret the baseline directory as "
147-
"relative to the test location.", action="store_true")
148-
mpl_hash_library_help = "json library of image hashes, relative to location where py.test is run"
149-
group.addoption('--mpl-hash-library', help=mpl_hash_library_help, action='store')
150-
parser.addini("mpl-hash-library", help=mpl_hash_library_help)
152+
msg = "interpret the baseline directory as relative to the test location."
153+
group.addoption("--mpl-baseline-relative", help=msg, action="store_true")
151154

152-
mpl_generate_summary_help = (
155+
msg = "json library of image hashes, relative to location where py.test is run"
156+
option = "mpl-hash-library"
157+
group.addoption(f"--{option}", help=msg, action="store")
158+
parser.addini(option, help=msg)
159+
160+
msg = (
153161
"Generate a summary report of any failed tests"
154162
", in --mpl-results-path. The type of the report should be "
155163
"specified. Supported types are `html`, `json` and `basic-html`. "
156164
"Multiple types can be specified separated by commas."
157165
)
158-
group.addoption("--mpl-generate-summary", help=mpl_generate_summary_help, action="store")
159-
parser.addini("mpl-generate-summary", help=mpl_generate_summary_help)
166+
option = "mpl-generate-summary"
167+
group.addoption(f"--{option}", help=msg, action="store")
168+
parser.addini(option, help=msg)
160169

161-
results_path_help = "directory for test results, relative to location where py.test is run"
162-
group.addoption('--mpl-results-path', help=results_path_help, action='store')
163-
parser.addini('mpl-results-path', help=results_path_help)
170+
msg = "directory for test results, relative to location where py.test is run"
171+
option = "mpl-results-path"
172+
group.addoption(f"--{option}", help=msg, action="store")
173+
parser.addini(option, help=msg)
164174

165-
results_always_help = ("Always compare to baseline images and save result images, even for passing tests. "
166-
"This option is automatically applied when generating a HTML summary.")
167-
group.addoption('--mpl-results-always', action='store_true',
168-
help=results_always_help)
169-
parser.addini('mpl-results-always', help=results_always_help)
175+
msg = (
176+
"Always compare to baseline images and save result images, even for passing tests. "
177+
"This option is automatically applied when generating a HTML summary."
178+
)
179+
option = "mpl-results-always"
180+
group.addoption(f"--{option}", help=msg, action="store_true")
181+
parser.addini(option, help=msg)
170182

171-
use_full_test_name_help = "use fully qualified test name as the filename."
172-
group.addoption("--mpl-use-full-test-name", help=use_full_test_name_help, action="store_true")
173-
parser.addini('mpl-use-full-test-name', help=use_full_test_name_help, type='bool')
183+
msg = "use fully qualified test name as the filename."
184+
option = "mpl-use-full-test-name"
185+
group.addoption(f"--{option}", help=msg, action="store_true")
186+
parser.addini(option, help=msg, type="bool")
174187

175-
style_help = "default style to use for tests, unless specified in the mpl_image_compare decorator"
176-
group.addoption('--mpl-default-style', help=style_help, action='store')
177-
parser.addini('mpl-default-style', help=style_help)
188+
msg = "default style to use for tests, unless specified in the mpl_image_compare decorator"
189+
option = "mpl-default-style"
190+
group.addoption(f"--{option}", help=msg, action="store")
191+
parser.addini(option, help=msg)
178192

179-
tolerance_help = "default tolerance to use for tests, unless specified in the mpl_image_compare decorator"
180-
group.addoption('--mpl-default-tolerance', help=tolerance_help, action='store')
181-
parser.addini('mpl-default-tolerance', help=tolerance_help)
193+
msg = "default tolerance to use for tests, unless specified in the mpl_image_compare decorator"
194+
option = "mpl-default-tolerance"
195+
group.addoption(f"--{option}", help=msg, action="store")
196+
parser.addini(option, help=msg)
182197

183198
msg = "default backend to use for tests, unless specified in the mpl_image_compare decorator"
184199
option = "mpl-default-backend"
@@ -188,36 +203,40 @@ def pytest_addoption(parser):
188203

189204
def pytest_configure(config):
190205

191-
config.addinivalue_line('markers',
192-
"mpl_image_compare: Compares matplotlib figures "
193-
"against a baseline image")
206+
config.addinivalue_line(
207+
"markers",
208+
"mpl_image_compare: Compares matplotlib figures against a baseline image",
209+
)
210+
211+
if (
212+
config.getoption("--mpl")
213+
or config.getoption("--mpl-generate-path") is not None
214+
or config.getoption("--mpl-generate-hash-library") is not None
215+
):
194216

195-
if (config.getoption("--mpl") or
196-
config.getoption("--mpl-generate-path") is not None or
197-
config.getoption("--mpl-generate-hash-library") is not None):
217+
def get_cli_or_ini(name, default=None):
218+
return config.getoption(f"--{name}") or config.getini(name) or default
198219

199-
baseline_dir = (config.getoption("--mpl-baseline-path") or
200-
config.getini("mpl-baseline-path"))
201220
generate_dir = config.getoption("--mpl-generate-path")
202221
generate_hash_lib = config.getoption("--mpl-generate-hash-library")
203-
results_dir = config.getoption("--mpl-results-path") or config.getini("mpl-results-path")
204-
hash_library = config.getoption("--mpl-hash-library") or config.getini("mpl-hash-library")
205-
_hash_library_from_cli = bool(config.getoption("--mpl-hash-library")) # for backwards compatibility
206-
generate_summary = (config.getoption("--mpl-generate-summary") or
207-
config.getini("mpl-generate-summary"))
208-
results_always = (config.getoption("--mpl-results-always") or
209-
config.getini("mpl-results-always"))
210-
use_full_test_name = (config.getoption("--mpl-use-full-test-name") or
211-
config.getini("mpl-use-full-test-name"))
212222

223+
baseline_dir = get_cli_or_ini("mpl-baseline-path")
213224
if config.getoption("--mpl-baseline-relative"):
214225
baseline_relative_dir = config.getoption("--mpl-baseline-path")
215226
else:
216227
baseline_relative_dir = None
228+
use_full_test_name = get_cli_or_ini("mpl-use-full-test-name")
229+
230+
hash_library = get_cli_or_ini("mpl-hash-library")
231+
_hash_library_from_cli = bool(config.getoption("--mpl-hash-library")) # for backwards compatibility
217232

218-
# Note that results_dir is an empty string if not specified
219-
if not results_dir:
220-
results_dir = None
233+
default_tolerance = float(get_cli_or_ini("mpl-default-tolerance", DEFAULT_TOLERANCE))
234+
default_style = get_cli_or_ini("mpl-default-style", DEFAULT_STYLE)
235+
default_backend = get_cli_or_ini("mpl-default-backend", DEFAULT_BACKEND)
236+
237+
results_dir = get_cli_or_ini("mpl-results-path")
238+
results_always = get_cli_or_ini("mpl-results-always")
239+
generate_summary = get_cli_or_ini("mpl-generate-summary")
221240

222241
if generate_dir is not None:
223242
if baseline_dir is not None:
@@ -234,35 +253,25 @@ def pytest_configure(config):
234253
if not _hash_library_from_cli:
235254
hash_library = os.path.abspath(hash_library)
236255

237-
default_style = (config.getoption("--mpl-default-style") or
238-
config.getini("mpl-default-style") or
239-
"classic")
240-
241-
default_tolerance = float(config.getoption("--mpl-default-tolerance") or
242-
config.getini("mpl-default-tolerance") or
243-
"2")
244-
245-
default_backend = (config.getoption("--mpl-default-backend") or
246-
config.getini("mpl-default-backend") or
247-
"agg")
248-
249-
config.pluginmanager.register(ImageComparison(config,
250-
baseline_dir=baseline_dir,
251-
baseline_relative_dir=baseline_relative_dir,
252-
generate_dir=generate_dir,
253-
results_dir=results_dir,
254-
hash_library=hash_library,
255-
generate_hash_library=generate_hash_lib,
256-
generate_summary=generate_summary,
257-
results_always=results_always,
258-
use_full_test_name=use_full_test_name,
259-
default_style=default_style,
260-
default_tolerance=default_tolerance,
261-
default_backend=default_backend,
262-
_hash_library_from_cli=_hash_library_from_cli))
256+
plugin = ImageComparison(
257+
config,
258+
baseline_dir=baseline_dir,
259+
baseline_relative_dir=baseline_relative_dir,
260+
generate_dir=generate_dir,
261+
results_dir=results_dir,
262+
hash_library=hash_library,
263+
generate_hash_library=generate_hash_lib,
264+
generate_summary=generate_summary,
265+
results_always=results_always,
266+
use_full_test_name=use_full_test_name,
267+
default_style=default_style,
268+
default_tolerance=default_tolerance,
269+
default_backend=default_backend,
270+
_hash_library_from_cli=_hash_library_from_cli,
271+
)
272+
config.pluginmanager.register(plugin)
263273

264274
else:
265-
266275
config.pluginmanager.register(FigureCloser(config))
267276

268277

@@ -305,23 +314,23 @@ def path_is_not_none(apath):
305314

306315

307316
class ImageComparison:
308-
309-
def __init__(self,
310-
config,
311-
baseline_dir=None,
312-
baseline_relative_dir=None,
313-
generate_dir=None,
314-
results_dir=None,
315-
hash_library=None,
316-
generate_hash_library=None,
317-
generate_summary=None,
318-
results_always=False,
319-
use_full_test_name=False,
320-
default_style='classic',
321-
default_tolerance=2,
322-
default_backend='agg',
323-
_hash_library_from_cli=False, # for backwards compatibility
324-
):
317+
def __init__(
318+
self,
319+
config,
320+
baseline_dir=None,
321+
baseline_relative_dir=None,
322+
generate_dir=None,
323+
results_dir=None,
324+
hash_library=None,
325+
generate_hash_library=None,
326+
generate_summary=None,
327+
results_always=False,
328+
use_full_test_name=False,
329+
default_style=DEFAULT_STYLE,
330+
default_tolerance=DEFAULT_TOLERANCE,
331+
default_backend=DEFAULT_BACKEND,
332+
_hash_library_from_cli=False, # for backwards compatibility
333+
):
325334
self.config = config
326335
self.baseline_dir = baseline_dir
327336
self.baseline_relative_dir = path_is_not_none(baseline_relative_dir)

0 commit comments

Comments
 (0)