Skip to content

Commit 8e313f2

Browse files
committed
autotest test_pct.py: make tests independent, use tmp_path
1 parent ca65520 commit 8e313f2

File tree

1 file changed

+50
-56
lines changed

1 file changed

+50
-56
lines changed

autotest/pyscripts/test_pct.py

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
###############################################################################
3030

3131

32-
import os
3332
import struct
3433

3534
import gdaltest
@@ -46,44 +45,67 @@
4645
)
4746

4847

49-
@pytest.fixture()
48+
@pytest.fixture(scope="module")
5049
def script_path():
5150
return test_py_scripts.get_py_script("rgb2pct")
5251

5352

54-
###############################################################################
55-
# Test rgb2pct
53+
@pytest.fixture(scope="module")
54+
def rgb2pct1_tif(script_path, tmp_path_factory):
55+
56+
tif_fname = str(tmp_path_factory.mktemp("tmp") / "test_rgb2pct_1.tif")
57+
58+
test_py_scripts.run_py_script(
59+
script_path,
60+
"rgb2pct",
61+
test_py_scripts.get_data_path("gcore") + f"rgbsmall.tif {tif_fname}",
62+
)
5663

64+
yield tif_fname
5765

58-
def test_rgb2pct_1(script_path):
66+
67+
@pytest.fixture(scope="module")
68+
def rgb2pct2_tif(script_path, tmp_path_factory):
69+
70+
tif_fname = str(tmp_path_factory.mktemp("tmp") / "test_rgb2pct_2.tif")
5971

6072
test_py_scripts.run_py_script(
6173
script_path,
6274
"rgb2pct",
63-
test_py_scripts.get_data_path("gcore") + "rgbsmall.tif tmp/test_rgb2pct_1.tif",
75+
"-n 16 " + test_py_scripts.get_data_path("gcore") + f"rgbsmall.tif {tif_fname}",
6476
)
6577

66-
ds = gdal.Open("tmp/test_rgb2pct_1.tif")
67-
assert ds.GetRasterBand(1).Checksum() == 31231
68-
ds = None
78+
yield tif_fname
79+
80+
81+
###############################################################################
82+
# Test rgb2pct
83+
84+
85+
def test_rgb2pct_1(rgb2pct1_tif):
86+
87+
with gdal.Open(rgb2pct1_tif) as ds:
88+
assert ds.GetRasterBand(1).Checksum() == 31231
6989

7090

7191
###############################################################################
7292
# Test pct2rgb
7393

7494

75-
def test_pct2rgb_1(script_path):
95+
def test_pct2rgb_1(script_path, tmp_path, rgb2pct1_tif):
7696
gdal_array = pytest.importorskip("osgeo.gdal_array")
7797
try:
7898
gdal_array.BandRasterIONumPy
7999
except AttributeError:
80100
pytest.skip("osgeo.gdal_array.BandRasterIONumPy is unavailable")
81101

102+
output_tif = str(tmp_path / "test_pct2rgb_1.tif")
103+
82104
test_py_scripts.run_py_script(
83-
script_path, "pct2rgb", "tmp/test_rgb2pct_1.tif tmp/test_pct2rgb_1.tif"
105+
script_path, "pct2rgb", f"{rgb2pct1_tif} {output_tif}"
84106
)
85107

86-
ds = gdal.Open("tmp/test_pct2rgb_1.tif")
108+
ds = gdal.Open(output_tif)
87109
assert ds.GetRasterBand(1).Checksum() == 20963
88110

89111
ori_ds = gdal.Open(test_py_scripts.get_data_path("gcore") + "rgbsmall.tif")
@@ -98,17 +120,9 @@ def test_pct2rgb_1(script_path):
98120
# Test rgb2pct -n option
99121

100122

101-
def test_rgb2pct_2(script_path):
123+
def test_rgb2pct_2(script_path, rgb2pct2_tif):
102124

103-
test_py_scripts.run_py_script(
104-
script_path,
105-
"rgb2pct",
106-
"-n 16 "
107-
+ test_py_scripts.get_data_path("gcore")
108-
+ "rgbsmall.tif tmp/test_rgb2pct_2.tif",
109-
)
110-
111-
ds = gdal.Open("tmp/test_rgb2pct_2.tif")
125+
ds = gdal.Open(rgb2pct2_tif)
112126
assert ds.GetRasterBand(1).Checksum() == 16596
113127

114128
ct = ds.GetRasterBand(1).GetRasterColorTable()
@@ -125,17 +139,19 @@ def test_rgb2pct_2(script_path):
125139
# Test rgb2pct -pct option
126140

127141

128-
def test_rgb2pct_3(script_path):
142+
def test_rgb2pct_3(script_path, tmp_path, rgb2pct2_tif):
143+
144+
output_tif = str(tmp_path / "test_rgb2pct_3.tif")
129145

130146
test_py_scripts.run_py_script(
131147
script_path,
132148
"rgb2pct",
133-
"-pct tmp/test_rgb2pct_2.tif "
149+
f"-pct {rgb2pct2_tif} "
134150
+ test_py_scripts.get_data_path("gcore")
135-
+ "rgbsmall.tif tmp/test_rgb2pct_3.tif",
151+
+ f"rgbsmall.tif {output_tif}",
136152
)
137153

138-
ds = gdal.Open("tmp/test_rgb2pct_3.tif")
154+
ds = gdal.Open(output_tif)
139155
assert ds.GetRasterBand(1).Checksum() == 16596
140156

141157
ct = ds.GetRasterBand(1).GetRasterColorTable()
@@ -152,22 +168,22 @@ def test_rgb2pct_3(script_path):
152168
# Test pct2rgb with big CT (>256 entries)
153169

154170

155-
def test_pct2rgb_4(script_path):
171+
def test_pct2rgb_4(script_path, tmp_path):
156172
gdal_array = pytest.importorskip("osgeo.gdal_array")
157173
try:
158174
gdal_array.BandRasterIONumPy
159175
except AttributeError:
160176
pytest.skip("osgeo.gdal_array.BandRasterIONumPy is unavailable")
161177

178+
output_tif = str(tmp_path / "test_pct2rgb_4.tif")
179+
162180
test_py_scripts.run_py_script(
163181
script_path,
164182
"pct2rgb",
165-
"-rgba "
166-
+ test_py_scripts.get_data_path("gcore")
167-
+ "rat.img tmp/test_pct2rgb_4.tif",
183+
"-rgba " + test_py_scripts.get_data_path("gcore") + f"rat.img {output_tif}",
168184
)
169185

170-
ds = gdal.Open("tmp/test_pct2rgb_4.tif")
186+
ds = gdal.Open(output_tif)
171187
ori_ds = gdal.Open(test_py_scripts.get_data_path("gcore") + "rat.img")
172188

173189
ori_data = struct.unpack(
@@ -189,10 +205,10 @@ def test_pct2rgb_4(script_path):
189205
ori_ds = None
190206

191207

192-
def test_gdalattachpct_1():
193-
pct_filename = "tmp/test_rgb2pct_2.tif"
208+
def test_gdalattachpct_1(tmp_path, rgb2pct2_tif):
209+
pct_filename = rgb2pct2_tif
194210
src_filename = test_py_scripts.get_data_path("gcore") + "rgbsmall.tif"
195-
pct_filename4 = "tmp/test_gdalattachpct_1_4.txt"
211+
pct_filename4 = str(tmp_path / "test_gdalattachpct_1_4.txt")
196212

197213
# pct from raster
198214
ct0 = color_table.get_color_table(pct_filename)
@@ -247,25 +263,3 @@ def test_gdalattachpct_1():
247263
ct3 = None
248264
ct4 = None
249265
ct5 = None
250-
251-
252-
###############################################################################
253-
# Cleanup
254-
255-
256-
def test_rgb2pct_cleanup():
257-
258-
lst = [
259-
"tmp/test_rgb2pct_1.tif",
260-
"tmp/test_pct2rgb_1.tif",
261-
"tmp/test_rgb2pct_2.tif",
262-
"tmp/test_rgb2pct_3.tif",
263-
"tmp/test_pct2rgb_1.tif",
264-
"tmp/test_pct2rgb_4.tif",
265-
"tmp/test_gdalattachpct_1_4.txt",
266-
]
267-
for filename in lst:
268-
try:
269-
os.remove(filename)
270-
except OSError:
271-
pass

0 commit comments

Comments
 (0)