summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2018-05-10 17:33:56 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-05-10 17:33:56 +0000
commitf4f19b51b2d588abe80df8493c23d708ec63f1b7 (patch)
tree5ec813a95688a3215d1be614211abbb82d3fc45e /testing
parent2db7eda9f51a4aa19df529ab12530542513c22a1 (diff)
downloadpdfium-f4f19b51b2d588abe80df8493c23d708ec63f1b7.tar.xz
Make GetTestDataDir() work in a non-standalone checkout.
Set the test data dir path correctly if PDFium is living inside another project as third_party/pdfium. BUG=chromium:841513 Change-Id: I565f7d97157e1769be8b7910f3c77d6d00015543 Reviewed-on: https://pdfium-review.googlesource.com/32314 Reviewed-by: dsinclair <dsinclair@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'testing')
-rw-r--r--testing/utils/path_service.cpp62
-rw-r--r--testing/utils/path_service.h5
2 files changed, 61 insertions, 6 deletions
diff --git a/testing/utils/path_service.cpp b/testing/utils/path_service.cpp
index 1030c2bb17..2ded0ed3b1 100644
--- a/testing/utils/path_service.cpp
+++ b/testing/utils/path_service.cpp
@@ -8,8 +8,10 @@
#include <Windows.h>
#elif defined(__APPLE__)
#include <mach-o/dyld.h>
+#include <sys/stat.h>
#else // Linux
#include <linux/limits.h>
+#include <sys/stat.h>
#include <unistd.h>
#endif // _WIN32
@@ -17,6 +19,38 @@
#include "core/fxcrt/fx_system.h"
+namespace {
+
+#if defined(__APPLE__) || (defined(ANDROID) && __ANDROID_API__ < 21)
+using stat_wrapper_t = struct stat;
+
+int CallStat(const char* path, stat_wrapper_t* sb) {
+ return stat(path, sb);
+}
+#elif !_WIN32
+using stat_wrapper_t = struct stat64;
+
+int CallStat(const char* path, stat_wrapper_t* sb) {
+ return stat64(path, sb);
+}
+#endif
+
+bool DirectoryExists(const std::string& path) {
+#ifdef _WIN32
+ DWORD fileattr = GetFileAttributesA(path.c_str());
+ if (fileattr != INVALID_FILE_ATTRIBUTES)
+ return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0;
+ return false;
+#else
+ stat_wrapper_t file_info;
+ if (CallStat(path.c_str(), &file_info) != 0)
+ return false;
+ return S_ISDIR(file_info.st_mode);
+#endif
+}
+
+} // namespace
+
// static
bool PathService::EndsWithSeparator(const std::string& path) {
return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
@@ -88,10 +122,30 @@ bool PathService::GetTestDataDir(std::string* path) {
if (!EndsWithSeparator(*path))
path->push_back(PATH_SEPARATOR);
- path->append("testing");
- path->push_back(PATH_SEPARATOR);
- path->append("resources");
- return true;
+
+ std::string potential_path = *path;
+ potential_path.append("testing");
+ potential_path.push_back(PATH_SEPARATOR);
+ potential_path.append("resources");
+ if (DirectoryExists(potential_path)) {
+ *path = potential_path;
+ return true;
+ }
+
+ potential_path = *path;
+ potential_path.append("third_party");
+ potential_path.push_back(PATH_SEPARATOR);
+ potential_path.append("pdfium");
+ potential_path.push_back(PATH_SEPARATOR);
+ potential_path.append("testing");
+ potential_path.push_back(PATH_SEPARATOR);
+ potential_path.append("resources");
+ if (DirectoryExists(potential_path)) {
+ *path = potential_path;
+ return true;
+ }
+
+ return false;
}
// static
diff --git a/testing/utils/path_service.h b/testing/utils/path_service.h
index 96fd69e889..fc0042ca17 100644
--- a/testing/utils/path_service.h
+++ b/testing/utils/path_service.h
@@ -23,12 +23,13 @@ class PathService {
static bool GetExecutableDir(std::string* path);
// Retrieve the root directory of the source tree.
- // Assume executables always run from out/<Debug|Release>/, the source
+ // Assume executables always run from out/<build_dir_name>/, so the source
// directory is two levels above the executable directory.
static bool GetSourceDir(std::string* path);
// Retrieve the test data directory where test files are stored.
- // Currently, the test dir is under <source_dir>/testing/resources/.
+ // Try <source_dir>/testing/resources/ first. If it does not exist, try
+ // checking <source_dir>/third_party/pdfium/testing/resources/.
static bool GetTestDataDir(std::string* path);
// Get the full path for a test file under the test data directory.