Moved test data to paths to environment variables

Added environment variables to remove relative paths from the code
This commit is contained in:
Bohdan Tyshchenko 2020-10-16 09:15:11 -07:00
parent 5711a66d77
commit 5159b67d7d
5 changed files with 45 additions and 14 deletions

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.12)
project(GDALSandbox CXX C)
@ -95,6 +95,7 @@ target_link_libraries(utils PUBLIC
sandbox2::temp_file
sandbox2::fileops
sandbox2::util
sandbox2::file_base
)
add_library(gtiff_converter STATIC
@ -118,15 +119,23 @@ target_link_libraries(raster_to_gtiff
)
if (ENABLE_TESTS)
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
include(GoogleTest)
enable_testing()
add_executable(tests tests.cc)
target_link_libraries(tests PRIVATE
data_retriever
gtiff_converter
utils
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES}
gtest
gtest_main
sandbox2::testing
sandbox2::file_base
)
gtest_discover_tests(tests PROPERTIES
ENVIRONMENT "TEST_TMPDIR=/tmp/"
ENVIRONMENT "TEST_SRCDIR=${PROJECT_SOURCE_DIR}"
)
endif()

View File

@ -91,6 +91,11 @@ You can run it in the following way:
```
After that, you can compare both files using the `gdalinfo` utility.
Also, there are unit tests that automatically convert a few files and then compare input and output raster data to make sure that they are equal.
To run tests your CMake build must use `-DENABLE_TESTS=ON`, then you can run tests using `./tests`.
To run tests your CMake build must use `-DENABLE_TESTS=ON`, then you can run tests using `ctest`.
Note that it will also run Sandboxed API related tests. To run tests manually you will need to specify a few environmental variables and then run `tests` executable.
```
export TEST_TMPDIR=/tmp/
export TEST_SRCDIR=/path/to/project/source
```
All test data is from [osgeo samples](http://download.osgeo.org/geotiff/samples/).

View File

@ -16,7 +16,9 @@
#include <string>
#include "gtest/gtest.h"
#include "sandboxed_api/sandbox2/testing.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/path.h"
#include "gdal_sandbox.h" // NOLINT(build/include)
#include "get_raster_data.h" // NOLINT(build/include)
@ -27,16 +29,16 @@ namespace {
inline constexpr absl::string_view kTempFilePrefix = "temp_data";
inline constexpr absl::string_view kFirstTestDataPath =
"../testdata/cea.tif";
"testdata/cea.tif";
inline constexpr absl::string_view kSecondTestDataPath =
"../testdata/SP27GTIF.tif";
"testdata/SP27GTIF.tif";
} // namespace
class TestGTiffProcessor : public testing::TestWithParam<absl::string_view> {
public:
TestGTiffProcessor()
: tempfile_(kTempFilePrefix)
: tempfile_(sandbox2::GetTestTempPath())
{}
protected:
@ -44,21 +46,24 @@ class TestGTiffProcessor : public testing::TestWithParam<absl::string_view> {
};
TEST_P(TestGTiffProcessor, TestProcessorOnGTiffData) {
std::string filename = std::string(GetParam());
std::string file_path = gdal::sandbox::utils::GetTestDataPath(GetParam());
ASSERT_TRUE(sandbox2::file_util::fileops::Exists(file_path, false))
<< "Error finding input dataset";
ASSERT_TRUE(tempfile_.HasValue())
<< "Error creating temporary output file";
gdal::sandbox::parser::RasterDataset original_bands_data =
gdal::sandbox::parser::GetRasterBandsFromFile(filename);
gdal::sandbox::parser::GetRasterBandsFromFile(file_path);
std::optional<std::string> proj_db_path =
gdal::sandbox::utils::FindProjDbPath();
ASSERT_TRUE(proj_db_path != std::nullopt)
<< "Specified proj.db does not exist";
gdal::sandbox::RasterToGTiffProcessor processor(absl::StrCat(
sandbox2::file_util::fileops::GetCWD(), "/", tempfile_.GetPath()),
gdal::sandbox::RasterToGTiffProcessor processor(tempfile_.GetPath(),
std::move(proj_db_path.value()), original_bands_data);
ASSERT_EQ(processor.Run(), absl::OkStatus())

View File

@ -17,6 +17,7 @@
#include <unistd.h>
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/path.h"
#include "sandboxed_api/sandbox2/util/temp_file.h"
namespace gdal::sandbox::utils {
@ -70,4 +71,11 @@ std::optional<std::string> FindProjDbPath() {
return proj_db_path;
}
std::string GetTestDataPath(absl::string_view testdata_path) {
const char* test_srcdir = std::getenv("TEST_SRCDIR");
return sandbox2::file::JoinPath(test_srcdir == nullptr ?
sandbox2::file_util::fileops::GetCWD() : test_srcdir, testdata_path);
}
} // namespace gdal::sandbox::utils

View File

@ -43,6 +43,10 @@ class TempFile {
// Helper function to retrieve potential proj.db path from environment variable
std::optional<std::string> FindProjDbPath();
// Tries to get test folder path from the TEST_SRCDIR environment variable and
// uses CWD path otherwise to join it with the testdata_path
std::string GetTestDataPath(absl::string_view testdata_path);
} // namespace gdal::sandbox::utils
#endif // RASTER_TO_GTIFF_UTILS_H_