From 2aec5c9b3495ecf3c6534dbf47ae09e60b59c146 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 8 Feb 2017 19:05:27 +0100 Subject: [PATCH] Package transient zone setting in test to ensure restore on fail tst_QDateTime::operator_insert_extract() was setting the time-zone and taking care to restore it at the end of the test; however, if the test were to fail, the restore would be skipped. Package the zone-setting and restore in a class instance, so that premature return can't bypass the restore. Change-Id: I3df63260da17e481ef4d0d107d9f0fdea3e147e7 Reviewed-by: Marc Mutz --- .../corelib/tools/qdatetime/tst_qdatetime.cpp | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 4604e664b0..6ff23487af 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -155,6 +155,29 @@ private: QTime invalidTime() const { return QTime(-1, -1, -1); } qint64 minJd() const { return QDateTimePrivate::minJd(); } qint64 maxJd() const { return QDateTimePrivate::maxJd(); } + + class TimeZoneRollback + { + const QByteArray prior; + public: + // Save the previous timezone so we can restore it afterwards, otherwise + // later tests may break: + explicit TimeZoneRollback(const QByteArray &zone) : prior(qgetenv("TZ")) + { reset(zone); } + void reset(const QByteArray &zone) + { + qputenv("TZ", zone.constData()); + tzset(); + } + ~TimeZoneRollback() + { + if (prior.isNull()) + qunsetenv("TZ"); + else + qputenv("TZ", prior.constData()); + tzset(); + } + }; }; Q_DECLARE_METATYPE(Qt::TimeSpec) @@ -1890,12 +1913,8 @@ void tst_QDateTime::operator_insert_extract() QFETCH(QString, deserialiseAs); QFETCH(QDataStream::Version, dataStreamVersion); - // Save the previous timezone so we can restore it afterwards, otherwise later tests will break - QByteArray previousTimeZone = qgetenv("TZ"); - // Start off in a certain timezone. - qputenv("TZ", serialiseAs.toLocal8Bit().constData()); - tzset(); + TimeZoneRollback useZone(serialiseAs.toLocal8Bit()); QDateTime dateTimeAsUTC(dateTime.toUTC()); QByteArray byteArray; @@ -1920,8 +1939,7 @@ void tst_QDateTime::operator_insert_extract() // Ensure that a change in timezone between serialisation and deserialisation // still results in identical UTC-converted datetimes. - qputenv("TZ", deserialiseAs.toLocal8Bit().constData()); - tzset(); + useZone.reset(deserialiseAs.toLocal8Bit()); QDateTime expectedLocalTime(dateTimeAsUTC.toLocalTime()); { // Deserialise whole QDateTime at once. @@ -1972,12 +1990,6 @@ void tst_QDateTime::operator_insert_extract() QCOMPARE(localDeserialized, dateTime); } } - - if (previousTimeZone.isNull()) - qunsetenv("TZ"); - else - qputenv("TZ", previousTimeZone.constData()); - tzset(); } void tst_QDateTime::toString_strformat()