Commit ba9f2f66 by Benjamin Barenblat Committed by Copybara-Service

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
parent 91b8bfab
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstring> #include <cstring>
#include <memory>
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
...@@ -103,6 +104,12 @@ class TestZoneInfoSource : public cctz::ZoneInfoSource { ...@@ -103,6 +104,12 @@ class TestZoneInfoSource : public cctz::ZoneInfoSource {
const char* const end_; 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( std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
const std::string& name, const std::string& name,
const std::function<std::unique_ptr<cctz::ZoneInfoSource>( const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
...@@ -110,12 +117,14 @@ std::unique_ptr<cctz::ZoneInfoSource> TestFactory( ...@@ -110,12 +117,14 @@ std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
for (const ZoneInfo& zoneinfo : kZoneInfo) { for (const ZoneInfo& zoneinfo : kZoneInfo) {
if (name == zoneinfo.name) { if (name == zoneinfo.name) {
if (zoneinfo.data == nullptr) return nullptr; if (zoneinfo.data == nullptr) return nullptr;
return std::unique_ptr<cctz::ZoneInfoSource>( return EmbeddedTestZone(zoneinfo.data, zoneinfo.length);
new TestZoneInfoSource(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 } // namespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment