Skip to content

Commit

Permalink
ORC-1663: [C++] Enable TestTimezone.testMissingTZDB on Windows
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Enable TestTimezone.testMissingTZDB unit test to run on Windows.

### Why are the changes needed?

When /usr/share/zoneinfo is unavailable and TZDIR env is unset, creating C++ ORC reader will crash on Windows. We need to better deal with this case. See context from the Apache Arrow community: apache/arrow#36026 and apache/arrow#40633

### How was this patch tested?

Make sure the test passes on Windows.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #1856 from wgtmac/win_tz_test.

Authored-by: Gang Wu <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
  • Loading branch information
wgtmac authored and dongjoon-hyun committed Mar 22, 2024
1 parent 4eaf306 commit ed71575
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions c++/test/TestTimezone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "wrap/gmock.h"
#include "wrap/gtest-wrapper.h"

#include <cstdlib>
#include <iostream>
#include <vector>

Expand Down Expand Up @@ -403,20 +404,37 @@ namespace orc {
EXPECT_EQ(1699164000 + 8 * 3600, la->convertFromUTC(1699164000));
}

#ifndef _MSC_VER
bool setEnv(const char* name, const char* value) {
#ifdef _MSC_VER
return _putenv_s(name, value) == 0;
#else
return setenv(name, value, 1) == 0;
#endif
}

bool delEnv(const char* name) {
#ifdef _MSC_VER
return _putenv_s(name, "") == 0;
#else
return unsetenv(name) == 0;
#endif
}

TEST(TestTimezone, testMissingTZDB) {
const char* tzDirBackup = std::getenv("TZDIR");
setenv("TZDIR", "/path/to/wrong/tzdb", 1);
if (tzDirBackup != nullptr) {
ASSERT_TRUE(delEnv("TZDIR"));
}
ASSERT_TRUE(setEnv("TZDIR", "/path/to/wrong/tzdb"));
EXPECT_THAT([]() { getTimezoneByName("America/Los_Angeles"); },
testing::ThrowsMessage<TimezoneError>(testing::HasSubstr(
"Time zone file /path/to/wrong/tzdb/America/Los_Angeles does not exist."
" Please install IANA time zone database and set TZDIR env.")));
if (tzDirBackup != nullptr) {
setenv("TZDIR", tzDirBackup, 1);
ASSERT_TRUE(setEnv("TZDIR", tzDirBackup));
} else {
unsetenv("TZDIR");
ASSERT_TRUE(delEnv("TZDIR"));
}
}
#endif

} // namespace orc

0 comments on commit ed71575

Please sign in to comment.