Skip to content

Commit

Permalink
Add pytest test suite (#36)
Browse files Browse the repository at this point in the history
The testing infrastructure is taken from GDAL master branch "3.9.0dev" in
January 2024 (https://github.com/OSGeo/gdal/tree/master/autotest). It has
been slightly modified to work with GDAL <3.7 too, which is what is currently
available on CI runners.

The test data for GRASS raster is taken from GDAL 3.4 release branch and
the test data for vector is taken from GRASS main branch "8.4.0dev".
  • Loading branch information
nilason authored Jan 31, 2024
1 parent bd2269b commit 33d6548
Show file tree
Hide file tree
Showing 35 changed files with 3,323 additions and 31 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/build_ubuntu_cmake.sh

This file was deleted.

42 changes: 35 additions & 7 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
- push
- pull_request

env:
GDAL_AUTOLOAD_DIR: $HOME/gdalplugins
GDAL_AUTOLOAD_CMAKE_DIR: $HOME/gdalplugins_cmake
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -21,19 +24,44 @@ jobs:
--no-install-suggests
- name: Create GDAL_AUTOLOAD directory
run: |
mkdir $HOME/gdalplugins
mkdir ${{ env.GDAL_AUTOLOAD_DIR }}
- name: Setup test data
run: |
mkdir -p $HOME/grassdata
cd $HOME/grassdata/
wget -c --quiet https://grass.osgeo.org/sampledata/north_carolina/nc_spm_08_micro.zip
unzip nc_spm_08_micro.zip
rm -f nc_spm_08_micro.zip
rm -f nc_spm_08_micro.zip
- name: Build
run: .github/workflows/build_ubuntu.sh $HOME/gdalplugins
run: .github/workflows/build_ubuntu.sh ${{ env.GDAL_AUTOLOAD_DIR }}
- name: Test executing of GDAL with driver on GRASS maps
run: .github/workflows/test_simple.sh $HOME/gdalplugins
- name: Build with CMake
run: .github/workflows/build_ubuntu_cmake.sh $HOME/gdalplugins_cmake
run: .github/workflows/test_simple.sh ${{ env.GDAL_AUTOLOAD_DIR }}
- name: Create GDAL_AUTOLOAD directory for CMake
run: |
mkdir ${{ env.GDAL_AUTOLOAD_CMAKE_DIR }}
- name: Set up Python for tests
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pytest-env pytest-sugar gdal==3.6.4
- name: CMake configure
run: |
cmake -B build -DAUTOLOAD_DIR=${{ env.GDAL_AUTOLOAD_CMAKE_DIR }}
- name: CMake build
run: |
cmake --build build
- name: CTest
run: |
export GDAL_DRIVER_PATH=${GITHUB_WORKSPACE}/build
export LD_LIBRARY_PATH=$(grass --config path)/lib
cd build
ctest --output-on-failure
- name: Install with CMake
run: |
cmake --install build
- name: Test executing of GDAL with driver on GRASS maps (CMake)
run: .github/workflows/test_simple.sh $HOME/gdalplugins_cmake
run: |
.github/workflows/test_simple.sh ${{ env.GDAL_AUTOLOAD_CMAKE_DIR }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*~
config.log
*.log
config.status*
autom*.cache
*.o
Expand All @@ -10,3 +10,4 @@ Makefile
# CMake files (CMakeSettings.json is generated by Visual Studio)
/build
CMakeSettings.json
*.pyc
16 changes: 16 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(COMPLETE_VERSION
${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}
)
project(gdal-grass VERSION ${COMPLETE_VERSION})
include(CTest)

# ##############################################################################
# Dependencies and flags
Expand Down Expand Up @@ -65,3 +66,18 @@ target_include_directories(
${GRASS_INCLUDE} ${PROJINC})
target_link_libraries(ogr_grass PUBLIC ${GDAL_LIBRARY} ${G_LIBS})
install(TARGETS ogr_grass DESTINATION ${AUTOLOAD_DIR})

# ##############################################################################
# Tests

if (Python_LOOKUP_VERSION)
set(Python_FIND_STRATEGY VERSION)
find_package(Python ${Python_LOOKUP_VERSION} EXACT COMPONENTS Interpreter Development NumPy)
else ()
set(Python_FIND_STRATEGY LOCATION)
find_package(Python 3.11 COMPONENTS Interpreter Development NumPy)
endif ()

if (BUILD_TESTING AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/autotest")
add_subdirectory(autotest)
endif ()
99 changes: 99 additions & 0 deletions autotest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# CMake4GDAL project is distributed under MIT license. See accompanying file LICENSE.txt.

#[[
option(AUTOTEST_DOWNLOAD_TEST_DATA "Autotest to download test data" OFF)
option(AUTOTEST_SLOW_TEST "Autotest to run slow test" OFF)
if (NOT DEFINED ENV{CTEST_PARALLEL_LEVEL})
set(PARALLEL_OPTION "-j1")
endif ()
]]

if (Python_Interpreter_FOUND)

if (WIN32)
# If running GDAL as a CustomBuild Command os MSBuild, "ERROR bla:" is considered as failing the job. This is rarely
# the intended behavior
list(APPEND PYTHON_RUN_ENV "CPL_ERROR_SEPARATOR=\\;")
endif ()

# Set TEST_ENV that goes into pytest.ini

# Set GDAL_DATA
if(WIN32)
file(TO_NATIVE_PATH "${PROJECT_BINARY_DIR}/data" GDAL_DATA)
else()
set(GDAL_DATA "${PROJECT_BINARY_DIR}/data")
endif()
set(TEST_ENV_ GDAL_DATA=${GDAL_DATA})

if (GDAL_DOWNLOAD_TEST_DATA)
list(APPEND TEST_ENV_ GDAL_DOWNLOAD_TEST_DATA=YES)
else ()
list(APPEND TEST_ENV_ "#GDAL_DOWNLOAD_TEST_DATA=YES")
endif ()
if (GDAL_SLOW_TESTS)
list(APPEND TEST_ENV_ GDAL_RUN_SLOW_TESTS=YES)
else ()
list(APPEND TEST_ENV_ "#GDAL_RUN_SLOW_TESTS=YES")
endif ()
# Conda enable PROJ_NETWORK but this does interfere with some of our tests due to some unexpected grid being used
list(APPEND TEST_ENV_ PROJ_NETWORK=OFF)
list(APPEND TEST_ENV_ "GDAL_DRIVER_PATH=${PROJECT_BINARY_DIR}")

string(REPLACE ";" "\n " TEST_ENV "${TEST_ENV_}")

set(AUTOTEST_LOG_FILE "${CMAKE_CURRENT_BINARY_DIR}/autotest.log")
set(PYTEST_INI_HEADER_MESSAGE "This file was generated from ${GDAL_CMAKE_TEMPLATE_PATH}/pytest.ini.in using ${CMAKE_CURRENT_LIST_FILE}")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/pytest.ini.in ${CMAKE_CURRENT_BINARY_DIR}/pytest.ini @ONLY)
unset(PYTEST_INI_HEADER_MESSAGE)

function (copy_file_or_dir source dest)
if (IS_DIRECTORY ${source})
message(STATUS "Copying contents of ${source} to ${destination}")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory ${source} ${destination})
else()
message(STATUS "Copying ${source} to ${destination}")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${source} ${destination})
endif()
endfunction()

function (symlink_or_copy source destination)
file(CREATE_LINK ${source} ${destination}
RESULT res
SYMBOLIC)
if (NOT res EQUAL 0)
copy_file_or_dir(${source} ${destination})
endif ()
endfunction ()

symlink_or_copy(${CMAKE_CURRENT_SOURCE_DIR}/conftest.py ${CMAKE_CURRENT_BINARY_DIR}/conftest.py)

if (NOT "${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
foreach (subdir IN ITEMS pymod) # proj_grids cpp/data)
if (SKIP_COPYING_AUTOTEST_SUBDIRS)
message(STATUS "Skipping copying ${CMAKE_CURRENT_SOURCE_DIR}/${subdir}")
else ()
symlink_or_copy(${CMAKE_CURRENT_SOURCE_DIR}/${subdir} ${CMAKE_CURRENT_BINARY_DIR}/${subdir})
endif ()
endforeach ()
endif()

foreach (tgt IN ITEMS ogr gdrivers)
if (NOT "${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}")
if (SKIP_COPYING_AUTOTEST_SUBDIRS)
message(STATUS "Skipping copying ${CMAKE_CURRENT_SOURCE_DIR}/${tgt}")
else ()
symlink_or_copy(${CMAKE_CURRENT_SOURCE_DIR}/${tgt} ${CMAKE_CURRENT_BINARY_DIR}/${tgt})
endif ()
endif()
add_custom_target(
autotest_${tgt}
COMMAND ${CMAKE_COMMAND} -E env ${PYTHON_RUN_ENV} ${Python_EXECUTABLE} -m pytest -c
${CMAKE_CURRENT_BINARY_DIR}/pytest.ini ${tgt}
DEPENDS ${GDAL_LIB_TARGET_NAME} gdalapps python_binding)
add_test(NAME autotest_${tgt} COMMAND ${Python_EXECUTABLE} -m pytest -c ${CMAKE_CURRENT_BINARY_DIR}/pytest.ini
${tgt})
set_property(TEST autotest_${tgt} PROPERTY ENVIRONMENT "${PYTHON_RUN_ENV}")
endforeach ()

endif ()
47 changes: 47 additions & 0 deletions autotest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# GDAL-GRASS driver test suite

## How to run tests

1. You need to install `pytest` to run the test suite. This should do it:

```bash
pip install --upgrade pip
pip install pytest pytest-env pytest-sugar gdal==$(gdal-config --version)
```

2. Configure

```bash
cd gdal-grass
cmake -B build
cd build
cmake build .
```

3. Then, run tests with:

```bash
ctest
```
or:

```bash
cd autotest # i.e gdal-grass/build/autotest
pytest
```

4. Some quick usage tips:

```bash
# get more verbose output; don't capture stdout/stdin
pytest -vvs

# run all the ogr tests
pytest ogr

# run a particular module only
pytest ogr/ogr_grass.py

# run a particular test case in a module
pytest ogr/ogr_grass.py::test_ogr_grass_point2
```
Loading

0 comments on commit 33d6548

Please sign in to comment.