From 77fdce87e42456137fea122c1e70bfa43060f6ae Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 21 Mar 2024 14:21:43 +0800 Subject: [PATCH 1/6] WIP: Enable TestTimezone.testMissingTZDB on Windows --- c++/test/TestTimezone.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc index 69f3b854e0..b76508f199 100644 --- a/c++/test/TestTimezone.cc +++ b/c++/test/TestTimezone.cc @@ -403,7 +403,6 @@ namespace orc { EXPECT_EQ(1699164000 + 8 * 3600, la->convertFromUTC(1699164000)); } -#ifndef _MSC_VER TEST(TestTimezone, testMissingTZDB) { const char* tzDirBackup = std::getenv("TZDIR"); setenv("TZDIR", "/path/to/wrong/tzdb", 1); @@ -417,6 +416,5 @@ namespace orc { unsetenv("TZDIR"); } } -#endif } // namespace orc From 906383d4f73750cad0404e6cb24232e7d54e10b4 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 21 Mar 2024 15:16:59 +0800 Subject: [PATCH 2/6] add windows function --- c++/test/TestTimezone.cc | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc index b76508f199..c01a324c95 100644 --- a/c++/test/TestTimezone.cc +++ b/c++/test/TestTimezone.cc @@ -21,6 +21,10 @@ #include "wrap/gmock.h" #include "wrap/gtest-wrapper.h" +#ifdef _MSC_VER +#include "processenv.h" +#endif + #include #include @@ -403,17 +407,33 @@ namespace orc { EXPECT_EQ(1699164000 + 8 * 3600, la->convertFromUTC(1699164000)); } + bool setEnv(const char* name, const char* value) { +#ifdef _MSC_VER + return SetEnvironmentVariableA(name, value); +#else + return setenv(name, value, 1) == 0; +#endif + } + + bool delEnv(const char* name) { +#ifdef _MSC_VER + return SetEnvironmentVariableA(name, nullptr)); +#else + return unsetenv(name) == 0; +#endif + } + TEST(TestTimezone, testMissingTZDB) { const char* tzDirBackup = std::getenv("TZDIR"); - setenv("TZDIR", "/path/to/wrong/tzdb", 1); + setEnv("TZDIR", "/path/to/wrong/tzdb"); EXPECT_THAT([]() { getTimezoneByName("America/Los_Angeles"); }, testing::ThrowsMessage(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); + setEnv("TZDIR", tzDirBackup); } else { - unsetenv("TZDIR"); + delEnv("TZDIR"); } } From 0cc676551ac47e8d4159eb9d7b7365a53d881a27 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 21 Mar 2024 15:38:39 +0800 Subject: [PATCH 3/6] add missing windows.h --- c++/test/TestTimezone.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc index c01a324c95..5bab3f9a93 100644 --- a/c++/test/TestTimezone.cc +++ b/c++/test/TestTimezone.cc @@ -22,7 +22,10 @@ #include "wrap/gtest-wrapper.h" #ifdef _MSC_VER +// clang-format off +#include "windows.h" #include "processenv.h" +// clang-format on #endif #include @@ -417,7 +420,7 @@ namespace orc { bool delEnv(const char* name) { #ifdef _MSC_VER - return SetEnvironmentVariableA(name, nullptr)); + return SetEnvironmentVariableA(name, nullptr); #else return unsetenv(name) == 0; #endif From b856bffc28b7ca07c4c477834945236c4d0daf26 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 21 Mar 2024 16:43:04 +0800 Subject: [PATCH 4/6] refine test --- c++/test/TestTimezone.cc | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc index 5bab3f9a93..2912883408 100644 --- a/c++/test/TestTimezone.cc +++ b/c++/test/TestTimezone.cc @@ -428,15 +428,18 @@ namespace orc { TEST(TestTimezone, testMissingTZDB) { const char* tzDirBackup = std::getenv("TZDIR"); - setEnv("TZDIR", "/path/to/wrong/tzdb"); + if (tzDirBackup != nullptr) { + ASSERT_TRUE(delEnv("TZDIR")); + } + ASSERT_TRUE(setEnv("TZDIR", "/path/to/wrong/tzdb")); EXPECT_THAT([]() { getTimezoneByName("America/Los_Angeles"); }, testing::ThrowsMessage(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); + ASSERT_TRUE(setEnv("TZDIR", tzDirBackup)); } else { - delEnv("TZDIR"); + ASSERT_TRUE(delEnv("TZDIR")); } } From f04842de31f1566c677d92282cfc25eaa24bc100 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 21 Mar 2024 22:21:21 +0800 Subject: [PATCH 5/6] Update c++/test/TestTimezone.cc --- c++/test/TestTimezone.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc index 2912883408..4e85b9bf1e 100644 --- a/c++/test/TestTimezone.cc +++ b/c++/test/TestTimezone.cc @@ -412,7 +412,7 @@ namespace orc { bool setEnv(const char* name, const char* value) { #ifdef _MSC_VER - return SetEnvironmentVariableA(name, value); + return _putenv_s(name, value) == 0; #else return setenv(name, value, 1) == 0; #endif @@ -420,7 +420,7 @@ namespace orc { bool delEnv(const char* name) { #ifdef _MSC_VER - return SetEnvironmentVariableA(name, nullptr); + return _putenv_s(name, "") == 0; #else return unsetenv(name) == 0; #endif From b36063786c24e44774a400dae43855efecf0b5a6 Mon Sep 17 00:00:00 2001 From: Gang Wu Date: Thu, 21 Mar 2024 22:21:27 +0800 Subject: [PATCH 6/6] Update c++/test/TestTimezone.cc --- c++/test/TestTimezone.cc | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc index 4e85b9bf1e..2330fcfb04 100644 --- a/c++/test/TestTimezone.cc +++ b/c++/test/TestTimezone.cc @@ -21,13 +21,7 @@ #include "wrap/gmock.h" #include "wrap/gtest-wrapper.h" -#ifdef _MSC_VER -// clang-format off -#include "windows.h" -#include "processenv.h" -// clang-format on -#endif - +#include #include #include