diff --git a/oss-internship-2020/libpng/README.md b/oss-internship-2020/libpng/README.md index c649592..f71b776 100644 --- a/oss-internship-2020/libpng/README.md +++ b/oss-internship-2020/libpng/README.md @@ -38,7 +38,8 @@ output: `images/rgbtobgr_red_ball.png` #### Tests: -You should add `-DLIBPNG_SAPI_ENABLE_TESTS=ON` to use tests and run: +You should add `-DLIBPNG_SAPI_ENABLE_TESTS=ON` to use tests and do: ``` -./tests/tests +cd tests +ctest . ``` diff --git a/oss-internship-2020/libpng/tests/CMakeLists.txt b/oss-internship-2020/libpng/tests/CMakeLists.txt index 8feaad7..ea4af40 100644 --- a/oss-internship-2020/libpng/tests/CMakeLists.txt +++ b/oss-internship-2020/libpng/tests/CMakeLists.txt @@ -32,5 +32,4 @@ target_link_libraries(tests PRIVATE sapi::sapi ) -gtest_discover_tests(tests) - +gtest_discover_tests(tests PROPERTIES ENVIRONMENT "TEST_SRCDIR=${PROJECT_SOURCE_DIR}") diff --git a/oss-internship-2020/libpng/tests/basic_test.cc b/oss-internship-2020/libpng/tests/basic_test.cc index 4c4f97d..6b6b6d2 100644 --- a/oss-internship-2020/libpng/tests/basic_test.cc +++ b/oss-internship-2020/libpng/tests/basic_test.cc @@ -28,7 +28,7 @@ using ::testing::Eq; using ::testing::IsTrue; TEST(SandboxTest, ReadWrite) { - std::string infile = GetTestFilePath("pngtest.png"); + std::string infile = GetFilePath("pngtest.png"); absl::StatusOr status_or_path = sandbox2::CreateNamedTempFileAndClose("output.png"); diff --git a/oss-internship-2020/libpng/tests/extended_test.cc b/oss-internship-2020/libpng/tests/extended_test.cc index deda14e..a2e67e3 100644 --- a/oss-internship-2020/libpng/tests/extended_test.cc +++ b/oss-internship-2020/libpng/tests/extended_test.cc @@ -191,8 +191,8 @@ void WritePng(LibPNGApi& api, absl::string_view outfile, Data& data) { } TEST(SandboxTest, ReadModifyWrite) { - std::string infile = GetTestFilePath("red_ball.png"); - std::string outfile = GetTestFilePath("test_output.png"); + std::string infile = GetFilePath("red_ball.png"); + std::string outfile = GetFilePath("test_output.png"); LibPNGSapiSandbox sandbox; ASSERT_THAT(sandbox.Init(), IsOk()); diff --git a/oss-internship-2020/libpng/tests/helper.cc b/oss-internship-2020/libpng/tests/helper.cc index b349c6b..84ab30c 100644 --- a/oss-internship-2020/libpng/tests/helper.cc +++ b/oss-internship-2020/libpng/tests/helper.cc @@ -13,28 +13,12 @@ // limitations under the License. #include "helper.h" // NOLINT(build/include) + #include "../sandboxed.h" // NOLINT(build/include) -#include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/path.h" -std::string GetImagesFolder() { - std::string cwd = sandbox2::file_util::fileops::GetCWD(); - auto find = cwd.rfind("/build"); - if (find == std::string::npos) { - LOG(ERROR) << "Something went wrong: CWD don't contain build dir. " - << "Please run tests from build dir, path might be incorrect\n"; +std::string GetSourcePath() { return getenv("TEST_SRCDIR"); } - return sandbox2::file::JoinPath(cwd, "images"); - } - - return sandbox2::file::JoinPath(cwd.substr(0, find), "images"); +std::string GetFilePath(absl::string_view filename) { + return sandbox2::file::JoinPath(GetSourcePath(), "images", filename); } - -std::string GetTestFilePath(const std::string& filename) { - static std::string* images_folder_path = nullptr; - if (!images_folder_path) { - images_folder_path = new std::string(GetImagesFolder()); - } - return sandbox2::file::JoinPath(*images_folder_path, filename); -} - diff --git a/oss-internship-2020/libpng/tests/helper.h b/oss-internship-2020/libpng/tests/helper.h index 9590f61..7a2a97c 100644 --- a/oss-internship-2020/libpng/tests/helper.h +++ b/oss-internship-2020/libpng/tests/helper.h @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - -std::string GetTestFilePath(const std::string& filename); +#include "absl/strings/str_join.h" +std::string GetFilePath(absl::string_view filename);