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

Fix and refactor tests #79

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
88 changes: 45 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ set(shp_LIB_VERSIONINFO "4:0:0")
set(PROJECT_VERSION
"${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")

set(EG_DATA ${CMAKE_CURRENT_BINARY_DIR}/shape_eg_data CACHE STRING "")
set(EG_DATA_URI http://dl.maptools.org/dl/shapelib/shape_eg_data.zip)
flederwiesel marked this conversation as resolved.
Show resolved Hide resolved
set(EG_DATA_SHA256 991d9c5ca5995245cbc9b94a70f01ca4ef689ad8ae44ea703765ea460b753ee9)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# libraries are all shared by default.
Expand Down Expand Up @@ -175,29 +179,37 @@ endif()
if(MSVC)
# TODO(schwehr): How to test on Windows?
set(BUILD_TESTING OFF CACHE BOOL "")
else()
# TODO(schwehr): Temporary work around.
cmake_policy(SET CMP0026 OLD)
endif()

find_program(BASH_EXECUTABLE bash)
find_program(SED_EXECUTABLE sed)
if(BASH_EXECUTABLE AND SED_EXECUTABLE)
if(BASH_EXECUTABLE)
set(BUILD_TESTING ON CACHE BOOL "")
else()
message(STATUS "WARNING: sed or bash not available so disabling testing")
message(STATUS "WARNING: bash not available so disabling testing")
endif()

# For the first series of tests, the user needs to have downloaded
# from http://dl.maptools.org/dl/shapelib/shape_eg_data.zip, unpacked
# that file, and specified the location of that directory with
# the cmake option, -DEG_DATA:PATH=whatever
# For the first series of tests, the user needs to have downloaded and unpacked
# shape data from http://dl.maptools.org/dl/shapelib/shape_eg_data.zip.
# The location to the unpacked files can be changed with the cmake option
# -DEG_DATA:PATH=whatever. If this does not exists, the archive is being
# downloaded and unpacked at configure time.
if(BUILD_TESTING)
if(EG_DATA)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/script.sed "s?/u/www/projects/shapelib/eg_data?${EG_DATA}?\n")
else()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/script.sed "")
message(STATUS "WARNING: EG_DATA:PATH not set to point to downloaded eg_data directory so the eg_data part of testing will be ignored.")
if(NOT IS_DIRECTORY ${EG_DATA})
message(STATUS "Downloading ${EG_DATA_URI}...")
file(DOWNLOAD ${EG_DATA_URI} shape_eg_data.zip
EXPECTED_HASH SHA256=${EG_DATA_SHA256}
TIMEOUT 60
STATUS DL_RESULT
)

if(DL_RESULT EQUAL 0)
file(ARCHIVE_EXTRACT
INPUT shape_eg_data.zip
DESTINATION ${EG_DATA}
)
else()
file(REMOVE shape_eg_data.zip)
endif()
endif()
endif()

Expand All @@ -221,10 +233,6 @@ foreach(executable ${executables})
set_target_properties(${TOOLS} PROPERTIES
INSTALL_RPATH "@loader_path/${RELATIVE_LIBDIR}")
endif()
if(BUILD_TESTING)
get_target_property(${executable}_LOC ${executable} LOCATION)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/script.sed "s?\\./${executable}?${${executable}_LOC}?\n")
endif()
if(NOT MSVC)
target_compile_options(${executable} PRIVATE -Wall -Wextra)
endif()
Expand All @@ -248,46 +256,40 @@ if(BUILD_TESTING)
foreach(executable shptest shputils)
add_executable(${executable} ${executable}.c)
target_link_libraries(${executable} ${PACKAGE})
get_target_property(${executable}_LOC ${executable} LOCATION)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/script.sed "s?\\./${executable}?${${executable}_LOC}?\n")
endforeach(executable shptest shputils)

# Write this as a shell script since execute_process cannot handle
# anything like redirection.
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/script.sh "${SED_EXECUTABLE} -f script.sed < $1 >| $2")
execute_process(
COMMAND
${BASH_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/script.sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test1.sh ${CMAKE_CURRENT_BINARY_DIR}/sed_scripted_test1.sh
)
# Set environment variables defining path to executables being used
function(declare_test_executable TEST TARGETS)
foreach(TARGET ${TARGETS})
string(TOUPPER ${TARGET} NAME)
list(APPEND TEST_ENV ${NAME}=$<TARGET_FILE:${TARGET}>)
endforeach()

execute_process(
COMMAND
${BASH_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/script.sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test2.sh ${CMAKE_CURRENT_BINARY_DIR}/sed_scripted_test2.sh
)

execute_process(
COMMAND
${BASH_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/script.sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test3.sh ${CMAKE_CURRENT_BINARY_DIR}/sed_scripted_test3.sh
)
set_tests_properties(${TEST} PROPERTIES ENVIRONMENT "${TEST_ENV}")
endfunction()

if(EG_DATA)
# These tests correspond to everything in test1.sh
add_test(
NAME test1
COMMAND ${BASH_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/sed_scripted_test1.sh
COMMAND
${BASH_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tests/test1.sh ${PROJECT_SOURCE_DIR}/tests/expect1.out ${EG_DATA}
)
declare_test_executable(test1 "shpdump;dbfdump")
endif()
# These tests correspond to everything in test2.sh

add_test(
NAME test2
COMMAND ${BASH_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/sed_scripted_test2.sh
COMMAND
${BASH_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tests/test2.sh ${PROJECT_SOURCE_DIR}/tests/expect2.out
)
declare_test_executable(test2 "dbfadd;dbfcreate;dbfdump;shpadd;shpcreate;shpdump;shptest")

# These tests correspond to everything in test3.sh
add_test(
NAME test3
COMMAND ${BASH_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/sed_scripted_test3.sh
COMMAND
${BASH_EXECUTABLE} ${PROJECT_SOURCE_DIR}/tests/test3.sh ${PROJECT_SOURCE_DIR}/tests/expect3.out
)
declare_test_executable(test3 "dbfadd;dbfcreate;dbfdump;shpadd;shpcreate;shpdump")
endif()

include(cmake/contrib.cmake)
Expand Down
8 changes: 8 additions & 0 deletions cmake/contrib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ if(BUILD_SHAPELIB_CONTRIB)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
if(BUILD_TESTING)
add_test(
NAME contrib
COMMAND
${BASH_EXECUTABLE} ${PROJECT_SOURCE_DIR}/contrib/tests/shpproj.sh ${PROJECT_SOURCE_DIR}/contrib/tests/expect.out
)
declare_test_executable(contrib "dbfadd;dbfcreate;shpadd;shpcreate;shpdump")
endif()
endif()
29 changes: 29 additions & 0 deletions contrib/tests/expect.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Shapefile Type: Point # of Shapes: 5

File Bounds: (-84.15596,34.54927,0,0)
to (-83.521622,34.992767,0,0)

Shape:0 (Point) nVertices=1, nParts=0
Bounds:(-83.5495,34.992401, 0)
to (-83.5495,34.992401, 0)
(-83.5495,34.992401, 0)

Shape:1 (Point) nVertices=1, nParts=0
Bounds:(-83.521622,34.992767, 0)
to (-83.521622,34.992767, 0)
(-83.521622,34.992767, 0)

Shape:2 (Point) nVertices=1, nParts=0
Bounds:(-84.016815,34.67276, 0)
to (-84.016815,34.67276, 0)
(-84.016815,34.67276, 0)

Shape:3 (Point) nVertices=1, nParts=0
Bounds:(-84.15596,34.648624, 0)
to (-84.15596,34.648624, 0)
(-84.15596,34.648624, 0)

Shape:4 (Point) nVertices=1, nParts=0
Bounds:(-83.619515,34.54927, 0)
to (-83.619515,34.54927, 0)
(-83.619515,34.54927, 0)
36 changes: 17 additions & 19 deletions contrib/tests/shpproj.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
#!/bin/bash

testdir="$(dirname "$(readlink -f $0)")"
readonly SCRIPTDIR=$(dirname "${BASH_SOURCE[0]}")
readonly TOP_BUILDDIR="$SCRIPTDIR/../.."

rm -f "$testdir/test*"
$top_builddir/shpcreate "$testdir/test" point
"${SHPCREATE:-$TOP_BUILDDIR/shpcreate}" "test" point

$top_builddir/shpadd "$testdir/test" -83.54949956 34.992401
$top_builddir/shpadd "$testdir/test" -83.52162155 34.99276748
$top_builddir/shpadd "$testdir/test" -84.01681518 34.67275985
$top_builddir/shpadd "$testdir/test" -84.15596023 34.64862437
$top_builddir/shpadd "$testdir/test" -83.61951463 34.54927047
"${SHPADD:-$TOP_BUILDDIR/shpadd}" "test" -83.54949956 34.992401
"${SHPADD:-$TOP_BUILDDIR/shpadd}" "test" -83.52162155 34.99276748
"${SHPADD:-$TOP_BUILDDIR/shpadd}" "test" -84.01681518 34.67275985
"${SHPADD:-$TOP_BUILDDIR/shpadd}" "test" -84.15596023 34.64862437
"${SHPADD:-$TOP_BUILDDIR/shpadd}" "test" -83.61951463 34.54927047

$top_builddir/dbfcreate "$testdir/test" -s fd 30
$top_builddir/dbfadd "$testdir/test" "1"
$top_builddir/dbfadd "$testdir/test" "2"
$top_builddir/dbfadd "$testdir/test" "3"
$top_builddir/dbfadd "$testdir/test" "4"
$top_builddir/dbfadd "$testdir/test" "5"
"${DBFCREATE:-$TOP_BUILDDIR/dbfcreate}" "test" -s fd 30
"${DBFADD:-$TOP_BUILDDIR/dbfadd}" "test" "1"
"${DBFADD:-$TOP_BUILDDIR/dbfadd}" "test" "2"
"${DBFADD:-$TOP_BUILDDIR/dbfadd}" "test" "3"
"${DBFADD:-$TOP_BUILDDIR/dbfadd}" "test" "4"
"${DBFADD:-$TOP_BUILDDIR/dbfadd}" "test" "5"

$top_builddir/shpdump -precision 8 "$testdir/test" > "$testdir/test.out"
"${SHPDUMP:-$TOP_BUILDDIR/shpdump}" -precision 8 "test" > "test.out"

result="$(diff "$testdir/test.out")"
if [ "$result" == "" ]; then

if result=$(diff "$SCRIPTDIR/expect.out" "test.out"); then
echo "******* Test Succeeded *********"
rm -f "$testdir/test*"
exit 0
else
echo "******* Test Failed *********"
echo "$result"
rm -f "$testdir/test*"
exit 1
fi
Loading