Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autotest: make utilities, pyscript tests run independently #8097

Merged
merged 19 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
632e271
CI: avoid hang on Windows Conda testing related to https://github.com…
rouault Jul 19, 2023
a32e4ff
autotest: fix execution of commands on Windows with backslashes
dbaston Jul 13, 2023
5174cf9
autotest test_gdal_contour.py: make tests independent
dbaston Jul 7, 2023
5fb68dc
autotest test_gdal_grid.py: make tests independent
dbaston Jul 7, 2023
5ea47f4
autotest test_gdal_grid.py: split and parametrize tests, use tmp_path
dbaston Jul 7, 2023
192f081
autotest test_gdaladdo.py: make tests independent
dbaston Jul 7, 2023
4a3321e
autotest test_gdaldem.py: make tests independent
dbaston Jul 7, 2023
7dbfaa5
autotest test_nearblack.py: make tests independent
dbaston Jul 7, 2023
e973e92
autotest test_ogrlineref.py: make tests independent
dbaston Jul 7, 2023
772060a
autotest test_gdal_translate.py: make tests independent
dbaston Jul 7, 2023
500ad39
autotest test_gdalwarp.py: make tests independent
dbaston Jul 7, 2023
b799335
autotest test_gdalwarp_lib.py: make tests independent
dbaston Jul 7, 2023
fc6e3e2
autotest test_ogrtindex.py: move cleanup into fixture
dbaston Jul 7, 2023
f5de524
autotest test_gdalbuildvrt.py: make tests independent, use tmp_path
dbaston Jul 12, 2023
7e6b7c2
autotest: disable random order for test_gdaltindex and test_gdalgnmutils
dbaston Jul 12, 2023
d17c8da
autotest test_gdal_merge.py: make tests independent, use tmp_path
dbaston Jul 12, 2023
07e85f0
gdal_pansharpen.py: avoid error trying to generate relative path
dbaston Jul 14, 2023
bc57477
autotest test_gdal_pansharpen.py: make tests independent, use tmp_path
dbaston Jul 12, 2023
2afea0a
autotest test_gdal_retile.py: make tests independent, use tmp_path
dbaston Jul 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/cmake_builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ jobs:
shell: bash -l {0}
run: |
conda install --yes --quiet curl libiconv icu python=3.10 swig numpy pytest pytest-env filelock zlib lxml jsonschema
# FIXME: remove libnetcdf=4.9.2=nompi_h5902ca5_107 pinning as soon as https://github.com/conda-forge/libnetcdf-feedstock/issues/182 is resolved
conda install --yes --quiet proj geos hdf4 hdf5 kealib \
libnetcdf openjpeg poppler libtiff libpng xerces-c expat libxml2 kealib json-c \
libnetcdf=4.9.2=nompi_h5902ca5_107 openjpeg poppler libtiff libpng xerces-c expat libxml2 kealib json-c \
cfitsio freexl geotiff libjpeg-turbo libpq libspatialite libwebp-base pcre pcre2 postgresql \
sqlite tiledb zstd cryptopp cgal doxygen librttopo libkml openssl xz \
openjdk ant qhull armadillo blas blas-devel libblas libcblas liblapack liblapacke blosc libarchive \
Expand Down
10 changes: 8 additions & 2 deletions autotest/pymod/gdaltest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,10 @@ def runexternal(
encoding="latin1",
):
# pylint: disable=unused-argument
command = shlex.split(cmd)
if sys.platform == "win32":
command = cmd
else:
command = shlex.split(cmd)
if strin is None:
p = subprocess.Popen(command, stdout=subprocess.PIPE)
else:
Expand Down Expand Up @@ -1949,7 +1952,10 @@ def _read_in_thread(f, q):

def runexternal_out_and_err(cmd, check_memleak=True, encoding="ascii"):
# pylint: disable=unused-argument
command = shlex.split(cmd)
if sys.platform == "win32":
command = cmd
else:
command = shlex.split(cmd)
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if p.stdout is not None:
Expand Down
4 changes: 3 additions & 1 deletion autotest/pyscripts/test_gdal_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ def test_gdal_edit_py_3(script_path):
shutil.copy(test_py_scripts.get_data_path("gcore") + "byte.tif", filename)

try:
test_py_scripts.run_py_script(script_path, "gdal_edit", filename + " -a_srs ''")
test_py_scripts.run_py_script(
script_path, "gdal_edit", filename + " -a_srs None"
)

ds = gdal.Open(filename)
wkt = ds.GetProjectionRef()
Expand Down
178 changes: 83 additions & 95 deletions autotest/pyscripts/test_gdal_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,57 @@ def script_path():
return test_py_scripts.get_py_script("gdal_merge")


@pytest.fixture(scope="module")
def sample_tifs(tmp_path_factory):
tmpdir = tmp_path_factory.mktemp("tmp")

drv = gdal.GetDriverByName("GTiff")
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS("WGS84")
wkt = srs.ExportToWkt()

sample1_tif = str(tmpdir / "in1.tif")
with drv.Create(sample1_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(0)

sample2_tif = str(tmpdir / "in2.tif")
with drv.Create(sample2_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(63)

sample3_tif = str(tmpdir / "in3.tif")
with drv.Create(sample3_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(127)

sample4_tif = str(tmpdir / "in4.tif")
with drv.Create(sample4_tif, 10, 10, 1) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(255)

yield (sample1_tif, sample2_tif, sample3_tif, sample4_tif)


###############################################################################
# Basic test


def test_gdal_merge_1(script_path):
def test_gdal_merge_1(script_path, tmp_path):

output_tif = str(tmp_path / "test_gdal_merge_1.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-o tmp/test_gdal_merge_1.tif "
+ test_py_scripts.get_data_path("gcore")
+ "byte.tif",
f"-o {output_tif} " + test_py_scripts.get_data_path("gcore") + "byte.tif",
)

ds = gdal.Open("tmp/test_gdal_merge_1.tif")
ds = gdal.Open(output_tif)
assert ds.GetRasterBand(1).Checksum() == 4672
ds = None

Expand All @@ -70,44 +106,17 @@ def test_gdal_merge_1(script_path):
# Merge 4 tiles


def test_gdal_merge_2(script_path):

drv = gdal.GetDriverByName("GTiff")
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS("WGS84")
wkt = srs.ExportToWkt()

ds = drv.Create("tmp/in1.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(0)
ds = None

ds = drv.Create("tmp/in2.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(63)
ds = None
def test_gdal_merge_2(script_path, tmp_path, sample_tifs):

ds = drv.Create("tmp/in3.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(127)
ds = None

ds = drv.Create("tmp/in4.tif", 10, 10, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([3, 0.1, 0, 48, 0, -0.1])
ds.GetRasterBand(1).Fill(255)
ds = None
output_tif = str(tmp_path / "test_gdal_merge_2.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-q -o tmp/test_gdal_merge_2.tif tmp/in1.tif tmp/in2.tif tmp/in3.tif tmp/in4.tif",
f"-q -o {output_tif} {' '.join(sample_tifs)}",
)

ds = gdal.Open("tmp/test_gdal_merge_2.tif")
ds = gdal.Open(output_tif)
assert ds.GetProjectionRef().find("WGS 84") != -1, "Expected WGS 84\nGot : %s" % (
ds.GetProjectionRef()
)
Expand All @@ -133,15 +142,17 @@ def test_gdal_merge_2(script_path):
# Test -separate and -v options


def test_gdal_merge_3(script_path):
def test_gdal_merge_3(script_path, tmp_path, sample_tifs):

output_tif = str(tmp_path / "test_gdal_merge_3.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-separate -v -o tmp/test_gdal_merge_3.tif tmp/in1.tif tmp/in2.tif tmp/in3.tif tmp/in4.tif",
f"-separate -v -o {output_tif} {' '.join(sample_tifs)}",
)

ds = gdal.Open("tmp/test_gdal_merge_3.tif")
ds = gdal.Open(output_tif)
assert ds.GetProjectionRef().find("WGS 84") != -1, "Expected WGS 84\nGot : %s" % (
ds.GetProjectionRef()
)
Expand All @@ -167,15 +178,17 @@ def test_gdal_merge_3(script_path):
# Test -init option


def test_gdal_merge_4(script_path):
def test_gdal_merge_4(script_path, tmp_path, sample_tifs):

output_tif = str(tmp_path / "test_gdal_merge_4.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
"-init 255 -o tmp/test_gdal_merge_4.tif tmp/in2.tif tmp/in3.tif",
f"-init 255 -o {output_tif} {sample_tifs[1]} {sample_tifs[2]}",
)

ds = gdal.Open("tmp/test_gdal_merge_4.tif")
ds = gdal.Open(output_tif)

assert ds.GetRasterBand(1).Checksum() == 4725, "Wrong checksum"

Expand All @@ -184,7 +197,7 @@ def test_gdal_merge_4(script_path):
# Test merging with alpha band (#3669)


def test_gdal_merge_5(script_path):
def test_gdal_merge_5(script_path, tmp_path):
gdal_array = pytest.importorskip("osgeo.gdal_array")
try:
gdal_array.BandRasterIONumPy
Expand All @@ -196,71 +209,46 @@ def test_gdal_merge_5(script_path):
srs.SetWellKnownGeogCS("WGS84")
wkt = srs.ExportToWkt()

ds = drv.Create("tmp/in5.tif", 10, 10, 4)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(255)
ds = None
input1_tif = str(tmp_path / "in5.tif")
with drv.Create(input1_tif, 10, 10, 4) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(1).Fill(255)

ds = drv.Create("tmp/in6.tif", 10, 10, 4)
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(2).Fill(255)
ds.GetRasterBand(4).Fill(255)
cs = ds.GetRasterBand(4).Checksum()
ds = None
input2_tif = str(tmp_path / "in6.tif")
with drv.Create(input2_tif, 10, 10, 4) as ds:
ds.SetProjection(wkt)
ds.SetGeoTransform([2, 0.1, 0, 49, 0, -0.1])
ds.GetRasterBand(2).Fill(255)
ds.GetRasterBand(4).Fill(255)
cs = ds.GetRasterBand(4).Checksum()

output_tif = str(tmp_path / "test_gdal_merge_5.tif")

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
" -o tmp/test_gdal_merge_5.tif tmp/in5.tif tmp/in6.tif",
f" -o {output_tif} {input1_tif} {input2_tif}",
)

ds = gdal.Open("tmp/test_gdal_merge_5.tif")
with gdal.Open(output_tif) as ds:

assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"
ds = None
assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"

os.unlink("tmp/test_gdal_merge_5.tif")
os.unlink(output_tif)

test_py_scripts.run_py_script(
script_path,
"gdal_merge",
" -o tmp/test_gdal_merge_5.tif tmp/in6.tif tmp/in5.tif",
f" -o {output_tif} {input1_tif} {input2_tif}",
)

ds = gdal.Open("tmp/test_gdal_merge_5.tif")
with gdal.Open(output_tif) as ds:

assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"


###############################################################################
# Cleanup


def test_gdal_merge_cleanup():

lst = [
"tmp/test_gdal_merge_1.tif",
"tmp/test_gdal_merge_2.tif",
"tmp/test_gdal_merge_3.tif",
"tmp/test_gdal_merge_4.tif",
"tmp/test_gdal_merge_5.tif",
"tmp/in1.tif",
"tmp/in2.tif",
"tmp/in3.tif",
"tmp/in4.tif",
"tmp/in5.tif",
"tmp/in6.tif",
]
for filename in lst:
try:
os.remove(filename)
except OSError:
pass
assert ds.GetRasterBand(1).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(2).Checksum() == cs, "Wrong checksum"
assert ds.GetRasterBand(3).Checksum() == 0, "Wrong checksum"
assert ds.GetRasterBand(4).Checksum() == cs, "Wrong checksum"
Loading