|
21 | 21 | #include "wrap/gmock.h"
|
22 | 22 | #include "wrap/gtest-wrapper.h"
|
23 | 23 |
|
| 24 | +#include <cstdlib> |
24 | 25 | #include <iostream>
|
25 | 26 | #include <vector>
|
26 | 27 |
|
@@ -403,20 +404,37 @@ namespace orc {
|
403 | 404 | EXPECT_EQ(1699164000 + 8 * 3600, la->convertFromUTC(1699164000));
|
404 | 405 | }
|
405 | 406 |
|
406 |
| -#ifndef _MSC_VER |
| 407 | + bool setEnv(const char* name, const char* value) { |
| 408 | +#ifdef _MSC_VER |
| 409 | + return _putenv_s(name, value) == 0; |
| 410 | +#else |
| 411 | + return setenv(name, value, 1) == 0; |
| 412 | +#endif |
| 413 | + } |
| 414 | + |
| 415 | + bool delEnv(const char* name) { |
| 416 | +#ifdef _MSC_VER |
| 417 | + return _putenv_s(name, "") == 0; |
| 418 | +#else |
| 419 | + return unsetenv(name) == 0; |
| 420 | +#endif |
| 421 | + } |
| 422 | + |
407 | 423 | TEST(TestTimezone, testMissingTZDB) {
|
408 | 424 | const char* tzDirBackup = std::getenv("TZDIR");
|
409 |
| - setenv("TZDIR", "/path/to/wrong/tzdb", 1); |
| 425 | + if (tzDirBackup != nullptr) { |
| 426 | + ASSERT_TRUE(delEnv("TZDIR")); |
| 427 | + } |
| 428 | + ASSERT_TRUE(setEnv("TZDIR", "/path/to/wrong/tzdb")); |
410 | 429 | EXPECT_THAT([]() { getTimezoneByName("America/Los_Angeles"); },
|
411 | 430 | testing::ThrowsMessage<TimezoneError>(testing::HasSubstr(
|
412 | 431 | "Time zone file /path/to/wrong/tzdb/America/Los_Angeles does not exist."
|
413 | 432 | " Please install IANA time zone database and set TZDIR env.")));
|
414 | 433 | if (tzDirBackup != nullptr) {
|
415 |
| - setenv("TZDIR", tzDirBackup, 1); |
| 434 | + ASSERT_TRUE(setEnv("TZDIR", tzDirBackup)); |
416 | 435 | } else {
|
417 |
| - unsetenv("TZDIR"); |
| 436 | + ASSERT_TRUE(delEnv("TZDIR")); |
418 | 437 | }
|
419 | 438 | }
|
420 |
| -#endif |
421 | 439 |
|
422 | 440 | } // namespace orc
|
0 commit comments