Skip to content

Commit f0115af

Browse files
committed
Use explicit per-test filenames in save tests
Replace the module-level `filename_gen` counter in test_ggsave.py and test_save_as_pdf_pages.py with explicit, test-specific filenames. The shared generator caused random failures under `pytest -n auto`: each xdist worker reset its counter to zero, so workers raced on the same `filename-N.png` paths in CWD. Naming each file after the test that writes it makes basenames unique within a module, and matching the extension to the actual format (.pdf in test_save_as_pdf_pages.py) removes the cross-module collision on `test_save_method`.
1 parent 424ba9f commit f0115af

2 files changed

Lines changed: 34 additions & 51 deletions

File tree

tests/test_ggsave.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@
2121
p = ggplot(mtcars, aes(x="wt", y="mpg", label="name")) + geom_text()
2222

2323

24-
def sequential_filenames():
25-
"""
26-
Generate filenames for the tests
27-
"""
28-
for i in range(100):
29-
yield Path(f"filename-{i}.png")
30-
31-
32-
filename_gen = sequential_filenames()
33-
34-
3524
def assert_exist_and_clean(filename, msg=None):
25+
if isinstance(filename, str):
26+
filename = Path(filename)
27+
3628
if not msg:
3729
msg = f"File {filename} does not exist"
3830
assert filename.exists(), msg
@@ -46,27 +38,26 @@ def test_default_filename(self):
4638
assert_exist_and_clean(fn, "default filename")
4739

4840
def test_save_method(self):
49-
fn = next(filename_gen)
41+
fn1 = "save_method-01.png"
5042
with pytest.warns(PlotnineWarning) as record:
51-
p.save(fn)
43+
p.save(fn1)
5244

53-
assert_exist_and_clean(fn, "save method")
45+
assert_exist_and_clean(fn1, "save method")
5446

5547
res = ("saving" in str(item.message).lower() for item in record)
5648
assert any(res)
5749

5850
res = ("filename" in str(item.message).lower() for item in record)
5951
assert any(res)
6052

61-
# verbose
62-
fn = next(filename_gen)
53+
fn2 = "save_method-02.png"
6354
with warnings.catch_warnings(record=True) as record:
64-
p.save(fn, verbose=False)
65-
assert_exist_and_clean(fn, "save method")
55+
p.save(fn2, verbose=False)
56+
assert_exist_and_clean(fn2, "save method")
6657
assert not record, "Issued an unexpected warning"
6758

6859
def test_filename_plot_path(self):
69-
fn = next(filename_gen)
60+
fn = "filename_plot_path.png"
7061
p.save(fn, path=".", verbose=False)
7162
assert_exist_and_clean(fn, "fn, plot and path")
7263

@@ -76,7 +67,7 @@ def test_format_png(self):
7667
assert_exist_and_clean(fn, "format png")
7768

7869
def test_dpi(self):
79-
fn = next(filename_gen)
70+
fn = "dpi.png"
8071
p.save(fn, dpi=100, verbose=False)
8172
assert_exist_and_clean(fn, "dpi = 100")
8273

@@ -86,24 +77,21 @@ def test_ggsave(self):
8677
assert_exist_and_clean(fn, "default filename")
8778

8879
def test_save_big(self):
89-
fn = next(filename_gen)
9080
# supplying the ggplot object will work without
9181
# printing it first! 26 is the current limit, just go
9282
# over it to not use too much memory
93-
p.save(fn, width=26, height=26, limitsize=False, verbose=False)
94-
assert_exist_and_clean(fn, "big height and width")
83+
fn1 = "save_big-01.png"
84+
p.save(fn1, width=26, height=26, limitsize=False, verbose=False)
85+
assert_exist_and_clean(fn1, "big height and width")
9586

9687
# Using the global option
97-
fn = next(filename_gen)
88+
fn2 = "save_big-02.png"
9889
set_option("limitsize", False)
99-
p.save(fn, width=26, height=26, verbose=False)
90+
p.save(fn2, width=26, height=26, verbose=False)
10091
set_option("limitsize", True)
101-
assert_exist_and_clean(fn, "big height and width")
92+
assert_exist_and_clean(fn2, "big height and width")
10293

10394
def test_dpi_theme_xkcd(self):
104-
fn1 = next(filename_gen)
105-
fn2 = next(filename_gen)
106-
10795
data = pd.DataFrame({"x": range(4), "y": range(4), "b": list("aabb")})
10896

10997
p = (
@@ -112,9 +100,11 @@ def test_dpi_theme_xkcd(self):
112100
+ facet_wrap("b")
113101
+ theme_xkcd()
114102
)
103+
fn1 = "dpi_theme_xkcd-01.png"
115104
p.save(fn1, verbose=False)
116105
assert_exist_and_clean(fn1, "Saving with theme_xkcd and dpi (1)")
117106

107+
fn2 = "dpi_theme_xkcd-02.png"
118108
p.save(fn2, dpi=72, verbose=False)
119109
assert_exist_and_clean(fn2, "Saving with theme_xkcd and dpi (2)")
120110

@@ -149,7 +139,7 @@ def test_bad_units(self):
149139
# "leakages" due to the tests in this test module.
150140
def test_ggsave_closes_plot():
151141
assert plt.get_fignums() == [], "There are unsaved test plots"
152-
fn = next(filename_gen)
142+
fn = "ggsave_closes_plot.png"
153143
p.save(fn, verbose=False)
154144
assert_exist_and_clean(fn, "exist")
155145
assert plt.get_fignums() == [], "ggplot.save did not close the plot"

tests/test_save_as_pdf_pages.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,10 @@ def p(N=3):
1717
yield template + ggtitle("%d of %d" % (i, N))
1818

1919

20-
def sequential_filenames():
21-
"""
22-
Generate filenames for the tests
23-
"""
24-
for i in range(100):
25-
yield Path(f"filename-{i}.png")
26-
27-
28-
filename_gen = sequential_filenames()
29-
30-
3120
def assert_exist_and_clean(filename, msg=None):
21+
if isinstance(filename, str):
22+
filename = Path(filename)
23+
3224
if not msg:
3325
msg = f"File {filename} does not exist"
3426
assert filename.exists(), msg
@@ -42,8 +34,8 @@ def test_default_filename(self):
4234
fn = plots[0]._save_filename("pdf")
4335
assert_exist_and_clean(fn, "default filename")
4436

45-
def test_save_method(self):
46-
fn = next(filename_gen)
37+
def test_save_as_pdf_method(self):
38+
fn = "save_as_pdf_method-01.pdf"
4739
with pytest.warns(UserWarning) as record:
4840
save_as_pdf_pages(p(), fn)
4941

@@ -53,7 +45,7 @@ def test_save_method(self):
5345
assert any(res)
5446

5547
# verbose
56-
fn = next(filename_gen)
48+
fn = "save_as_pdf_method-02.pdf"
5749
with warnings.catch_warnings(record=True) as record:
5850
save_as_pdf_pages(p(), fn, verbose=False)
5951
assert_exist_and_clean(fn, "save method")
@@ -63,7 +55,7 @@ def test_save_method(self):
6355
assert not any(res)
6456

6557
def test_filename_plot_path(self):
66-
fn = next(filename_gen)
58+
fn = "filename_plot_path.pdf"
6759
with pytest.warns(PlotnineWarning):
6860
save_as_pdf_pages(p(), fn, path=".")
6961
assert_exist_and_clean(fn, "fn, plot and path")
@@ -76,7 +68,7 @@ def test_height_width(self):
7668
plots = []
7769
for i, plot in enumerate(p()):
7870
plots.append(plot + theme(figure_size=(8 + i, 6 + i)))
79-
fn = next(filename_gen)
71+
fn = "height_width.pdf"
8072
with pytest.warns(PlotnineWarning):
8173
save_as_pdf_pages(plots, fn)
8274
# assert False, "Check %s" % fn # Uncomment to check
@@ -85,24 +77,25 @@ def test_height_width(self):
8577
class TestExceptions:
8678
def test_plot_exception(self):
8779
# Force an error in drawing
88-
fn = next(filename_gen)
80+
fn = "plot_exception.pdf"
8981
plots = list(p())
9082
plots[0] += aes(color="unknown")
9183
with pytest.raises(PlotnineError):
9284
save_as_pdf_pages(plots, fn, verbose=False)
9385

86+
fn_path = Path(fn)
9487
# TODO: Remove when MPL>=3.10.0
95-
if fn.exists():
96-
fn.unlink()
88+
if fn_path.exists():
89+
fn_path.unlink()
9790

98-
assert not fn.exists()
91+
assert not fn_path.exists()
9992

10093

10194
# This should be the last function in the file since it can catch
10295
# "leakages" due to the tests in this test module.
10396
def test_save_as_pdf_pages_closes_plots():
10497
assert plt.get_fignums() == [], "There are unsaved test plots"
105-
fn = next(filename_gen)
98+
fn = "save_as_pdf_pages_closes_plots.pdf"
10699
with pytest.warns(PlotnineWarning):
107100
save_as_pdf_pages(p(), fn)
108101
assert_exist_and_clean(fn, "exist")

0 commit comments

Comments
 (0)