summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Benjamin Barenblat <bbaren@google.com>2022-06-06 12:28:04 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-06-06 12:28:41 -0700
commitba9f2f66095702db6b9580b40e8602ef64ec922d (patch)
treed5da0a175e6698c39007439b37d40fcf9598f272
parent91b8bfab44baf5c89efabd6f6b8b33f6a42f39b6 (diff)
Ignore invalid TZ settings in tests
For portability, absl_time_test builds a small, incomplete tzdata database into the test binary and uses that instead of the system tzdata database. (absl_time_test needs to run on some platforms that lack a system tzdata database.) However, this causes issues if TZ is set to something that isn’t in the test database. To address them, fall back to America/Los_Angeles if TZ is set to an unknown value during testing. Bug: https://bugs.debian.org/1012194 PiperOrigin-RevId: 453257912 Change-Id: I293d0f96876b31c32a2847468a3377bb49f3aa15
-rw-r--r--absl/time/internal/test_util.cc17
1 files changed, 13 insertions, 4 deletions
diff --git a/absl/time/internal/test_util.cc b/absl/time/internal/test_util.cc
index 9a485a07..24cff138 100644
--- a/absl/time/internal/test_util.cc
+++ b/absl/time/internal/test_util.cc
@@ -17,6 +17,7 @@
#include <algorithm>
#include <cstddef>
#include <cstring>
+#include <memory>
#include "absl/base/config.h"
#include "absl/base/internal/raw_logging.h"
@@ -103,6 +104,12 @@ class TestZoneInfoSource : public cctz::ZoneInfoSource {
const char* const end_;
};
+std::unique_ptr<cctz::ZoneInfoSource> EmbeddedTestZone(const char* data,
+ std::size_t size) {
+ return std::unique_ptr<cctz::ZoneInfoSource>(
+ new TestZoneInfoSource(data, size));
+}
+
std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
const std::string& name,
const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
@@ -110,12 +117,14 @@ std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
for (const ZoneInfo& zoneinfo : kZoneInfo) {
if (name == zoneinfo.name) {
if (zoneinfo.data == nullptr) return nullptr;
- return std::unique_ptr<cctz::ZoneInfoSource>(
- new TestZoneInfoSource(zoneinfo.data, zoneinfo.length));
+ return EmbeddedTestZone(zoneinfo.data, zoneinfo.length);
}
}
- ABSL_RAW_LOG(FATAL, "Unexpected time zone \"%s\" in test", name.c_str());
- return nullptr;
+
+ // The embedded tzdata database knows nothing about this zone. Return
+ // something so the tests can proceed.
+ return EmbeddedTestZone(reinterpret_cast<char*>(America_Los_Angeles),
+ America_Los_Angeles_len);
}
} // namespace