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 <marc.mutz@kdab.com>
This commit is contained in:
Edward Welbourne 2017-02-08 19:05:27 +01:00
parent 03903ec783
commit 2aec5c9b34

View File

@ -155,6 +155,29 @@ private:
QTime invalidTime() const { return QTime(-1, -1, -1); } QTime invalidTime() const { return QTime(-1, -1, -1); }
qint64 minJd() const { return QDateTimePrivate::minJd(); } qint64 minJd() const { return QDateTimePrivate::minJd(); }
qint64 maxJd() const { return QDateTimePrivate::maxJd(); } 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) Q_DECLARE_METATYPE(Qt::TimeSpec)
@ -1890,12 +1913,8 @@ void tst_QDateTime::operator_insert_extract()
QFETCH(QString, deserialiseAs); QFETCH(QString, deserialiseAs);
QFETCH(QDataStream::Version, dataStreamVersion); 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. // Start off in a certain timezone.
qputenv("TZ", serialiseAs.toLocal8Bit().constData()); TimeZoneRollback useZone(serialiseAs.toLocal8Bit());
tzset();
QDateTime dateTimeAsUTC(dateTime.toUTC()); QDateTime dateTimeAsUTC(dateTime.toUTC());
QByteArray byteArray; QByteArray byteArray;
@ -1920,8 +1939,7 @@ void tst_QDateTime::operator_insert_extract()
// Ensure that a change in timezone between serialisation and deserialisation // Ensure that a change in timezone between serialisation and deserialisation
// still results in identical UTC-converted datetimes. // still results in identical UTC-converted datetimes.
qputenv("TZ", deserialiseAs.toLocal8Bit().constData()); useZone.reset(deserialiseAs.toLocal8Bit());
tzset();
QDateTime expectedLocalTime(dateTimeAsUTC.toLocalTime()); QDateTime expectedLocalTime(dateTimeAsUTC.toLocalTime());
{ {
// Deserialise whole QDateTime at once. // Deserialise whole QDateTime at once.
@ -1972,12 +1990,6 @@ void tst_QDateTime::operator_insert_extract()
QCOMPARE(localDeserialized, dateTime); QCOMPARE(localDeserialized, dateTime);
} }
} }
if (previousTimeZone.isNull())
qunsetenv("TZ");
else
qputenv("TZ", previousTimeZone.constData());
tzset();
} }
void tst_QDateTime::toString_strformat() void tst_QDateTime::toString_strformat()