Skip to content

Commit

Permalink
Avoid using stat() when searching for testdata directory
Browse files Browse the repository at this point in the history
  • Loading branch information
catamorphism committed Aug 1, 2024
1 parent f4e4cdb commit b8dfad4
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions icu4c/source/test/intltest/intltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <string.h>
#include <cmath>
#include <math.h>
#include <sys/stat.h>

#include "unicode/ctest.h" // for str_timeDelta
#include "unicode/curramt.h"
Expand Down Expand Up @@ -1680,24 +1679,36 @@ const char *IntlTest::getSourceTestData(UErrorCode& /*err*/) {
return srcDataDir;
}

static bool directoryExists(const char* dirName) {
struct stat sb;
return (stat(dirName, &sb) == 0) && ((sb.st_mode & S_IFMT) == S_IFDIR);
static bool fileExists(const char* fileName) {
// Test for `srcDataDir` existing by checking for `srcDataDir`/message2/valid-tests.json
U_ASSERT(fileName != nullptr);
FILE *f = fopen(fileName, "r");
if (f) {
fclose(f);
return true;
}
return false;
}

/**
* Returns the path to icu/testdata/
*/
const char *IntlTest::getSharedTestData(UErrorCode& err) {
#define SOURCE_TARBALL_TOP U_TOPSRCDIR U_FILE_SEP_STRING ".." U_FILE_SEP_STRING
#define REPO_TOP SOURCE_TARBALL_TOP ".." U_FILE_SEP_STRING
#define FILE_NAME U_FILE_SEP_STRING "message2" U_FILE_SEP_STRING "valid-tests.json"
const char *srcDataDir = nullptr;
const char *testFile = nullptr;
if (U_SUCCESS(err)) {
#ifdef U_TOPSRCDIR
// Try U_TOPSRCDIR/../testdata (source tarball)
srcDataDir = U_TOPSRCDIR U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
if (!directoryExists(srcDataDir)) {
srcDataDir = SOURCE_TARBALL_TOP "testdata" U_FILE_SEP_STRING;
testFile = SOURCE_TARBALL_TOP "testdata" FILE_NAME;
if (!fileExists(testFile)) {
// If that doesn't exist, try U_TOPSRCDIR/../../testdata (in-repo)
srcDataDir = U_TOPSRCDIR U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
if (!directoryExists(srcDataDir)) {
srcDataDir = REPO_TOP "testdata" U_FILE_SEP_STRING;
testFile = REPO_TOP "testdata" FILE_NAME;
if (!fileExists(testFile)) {
// If neither exists, return null
err = U_FILE_ACCESS_ERROR;
srcDataDir = nullptr;
Expand All @@ -1706,11 +1717,14 @@ const char *IntlTest::getSharedTestData(UErrorCode& err) {
#else
// Try ../../../../testdata (if we're in icu/source/test/intltest)
// and ../../../../../../testdata (if we're in icu/source/test/intltest/Platform/(Debug|Release)
srcDataDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
if (!directoryExists(srcDataDir)) {
srcDataDir = ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "testdata" U_FILE_SEP_STRING;
if (!directoryExists(srcDataDir)) {
#define TOP ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING
#define TOP_TOP ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING TOP
srcDataDir = TOP "testdata" U_FILE_SEP_STRING;
testFile = TOP "testdata" FILE_NAME
if (!fileExists(testFile)) {
srcDataDir = TOP_TOP "testdata" U_FILE_SEP_STRING;
testFile = TOP_TOP "testdata" FILE_NAME;
if (!fileExists(testFile)) {
err = U_FILE_ACCESS_ERROR;
srcDataDir = nullptr;
}
Expand Down

0 comments on commit b8dfad4

Please sign in to comment.