From 9a4beb4d36dc0c613e59aa5d88060d521462f56c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 2 Apr 2014 14:08:29 +0200 Subject: [PATCH] Improve handling of temporary directories in SQL tests. Initialize directory delayed in shared code and add checks to verify that it is valid. Close attached / cloned databases to prevent locks on files and leaking temporary directories caused by SQLite: QTemporaryDir: Unable to remove "...\Temp\tst_qsqldatabase-P1XkOA" most likely due to the presence of read-only files. QTemporaryDir: Unable to remove "...\Temp\tst_qsqltablemodel-P1XkOA" most likely due to the presence of read-only files. QWARN : tst_QSql::concurrentAccess() QTemporaryDir: Unable to remove "...\Temp\tst_qsql-l0VAKJ" most likely due to the presence of read-only files. Change-Id: If85bbaed04bb1a32e427d642be332996d967f796 Reviewed-by: Andy Shaw Reviewed-by: Joerg Bornemann --- tests/auto/sql/kernel/qsql/tst_qsql.cpp | 11 +++--- .../sql/kernel/qsqldatabase/tst_databases.h | 34 +++++++++++++++---- .../kernel/qsqldatabase/tst_qsqldatabase.cpp | 5 +-- .../sql/kernel/qsqldriver/tst_qsqldriver.cpp | 4 +-- .../sql/kernel/qsqlquery/tst_qsqlquery.cpp | 4 +-- .../sql/kernel/qsqlthread/tst_qsqlthread.cpp | 4 +-- .../qsqlquerymodel/tst_qsqlquerymodel.cpp | 4 +-- .../tst_qsqlrelationaltablemodel.cpp | 4 +-- .../qsqltablemodel/tst_qsqltablemodel.cpp | 5 +-- 9 files changed, 50 insertions(+), 25 deletions(-) diff --git a/tests/auto/sql/kernel/qsql/tst_qsql.cpp b/tests/auto/sql/kernel/qsql/tst_qsql.cpp index 24f418af267..2dcd75427be 100644 --- a/tests/auto/sql/kernel/qsql/tst_qsql.cpp +++ b/tests/auto/sql/kernel/qsql/tst_qsql.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -117,7 +117,7 @@ void tst_QSql::basicDriverTest() char *argv[] = { const_cast(QTest::currentAppName()) }; QCoreApplication app(argc, argv, false); tst_Databases dbs; - dbs.open(); + QVERIFY(dbs.open()); foreach (const QString& dbName, dbs.dbNames) { QSqlDatabase db = QSqlDatabase::database(dbName); @@ -161,7 +161,7 @@ void tst_QSql::open() QCoreApplication app(argc, argv, false); tst_Databases dbs; - dbs.open(); + QVERIFY(dbs.open()); if (count == -1) // first iteration: see how many dbs are open count = (int) dbs.dbNames.count(); @@ -188,7 +188,7 @@ void tst_QSql::concurrentAccess() QCoreApplication app(argc, argv, false); tst_Databases dbs; - dbs.open(); + QVERIFY(dbs.open()); foreach (const QString& dbName, dbs.dbNames) { QSqlDatabase db = QSqlDatabase::database(dbName); QVERIFY(db.isValid()); @@ -204,6 +204,7 @@ void tst_QSql::concurrentAccess() QVERIFY_SQL(ndb, open()); QCOMPARE(db.tables(), ndb.tables()); + ndb.close(); } // no database servers installed - don't fail QVERIFY(1); @@ -217,7 +218,7 @@ void tst_QSql::openErrorRecovery() QCoreApplication app(argc, argv, false); tst_Databases dbs; - dbs.addDbs(); + QVERIFY(dbs.addDbs()); if (dbs.dbNames.isEmpty()) QSKIP("No database drivers installed"); foreach (const QString& dbName, dbs.dbNames) { diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_databases.h b/tests/auto/sql/kernel/qsqldatabase/tst_databases.h index 5c8b3ef728f..db011352300 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_databases.h +++ b/tests/auto/sql/kernel/qsqldatabase/tst_databases.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -248,7 +249,7 @@ public: dbNames.append( cName ); } - void addDbs() + bool addDbs() { //addDb("QOCI", "localhost", "system", "penandy"); // addDb( "QOCI8", "//horsehead.qt-project.org:1521/pony.troll.no", "scott", "tiger" ); // Oracle 9i on horsehead @@ -302,7 +303,10 @@ public: // use in-memory database to prevent local files // addDb("QSQLITE", ":memory:"); - addDb( "QSQLITE", QDir::toNativeSeparators(dbDir.path() + "/foo.db") ); + QTemporaryDir *sqLiteDir = dbDir(); + if (!sqLiteDir) + return false; + addDb( QStringLiteral("QSQLITE"), QDir::toNativeSeparators(sqLiteDir->path() + QStringLiteral("/foo.db")) ); // addDb( "QSQLITE2", QDir::toNativeSeparators(dbDir.path() + "/foo2.db") ); // addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.qt-project.org\\ICEBLINK", "troll", "trond", "" ); // addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.qt-project.org\\SQLEXPRESS", "troll", "trond", "" ); @@ -319,11 +323,14 @@ public: // addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2008-x86-01.qt-project.org;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" ); // addDb( "QODBC", "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dbs\\access\\testdb.mdb", "", "", "" ); // addDb( "QODBC", "DRIVER={Postgresql};SERVER=bq-pgsql84.qt-project.org;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" ); + return true; } - void open() + // 'false' return indicates a system error, for example failure to create a temporary directory. + bool open() { - addDbs(); + if (!addDbs()) + return false; QStringList::Iterator it = dbNames.begin(); @@ -341,6 +348,7 @@ public: } } } + return true; } void close() @@ -589,7 +597,21 @@ public: QStringList dbNames; int counter; - QTemporaryDir dbDir; + +private: + QTemporaryDir *dbDir() + { + if (m_dbDir.isNull()) { + m_dbDir.reset(new QTemporaryDir); + if (!m_dbDir->isValid()) { + qWarning() << Q_FUNC_INFO << "Unable to create a temporary directory: " << QDir::toNativeSeparators(m_dbDir->path()); + m_dbDir.reset(); + } + } + return m_dbDir.data(); + } + + QScopedPointer m_dbDir; }; #endif diff --git a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp index 6e199d5a71c..fa5db999d4d 100644 --- a/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/sql/kernel/qsqldatabase/tst_qsqldatabase.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -394,7 +394,7 @@ void tst_QSqlDatabase::populateTestTables(QSqlDatabase db) void tst_QSqlDatabase::initTestCase() { qRegisterMetaType("QSqlDriver::NotificationSource"); - dbs.open(); + QVERIFY(dbs.open()); for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) { QSqlDatabase db = QSqlDatabase::database((*it)); @@ -2229,6 +2229,7 @@ void tst_QSqlDatabase::sqlite_enable_cache_mode() QSqlQuery q(db), q2(db2); QVERIFY_SQL(q, exec("select * from " + qTableName("qtest", __FILE__, db))); QVERIFY_SQL(q2, exec("select * from " + qTableName("qtest", __FILE__, db))); + db2.close(); } QTEST_MAIN(tst_QSqlDatabase) diff --git a/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp b/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp index 53e5451e064..1775aecb201 100644 --- a/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp +++ b/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -69,7 +69,7 @@ private slots: void tst_QSqlDriver::initTestCase_data() { - dbs.open(); + QVERIFY(dbs.open()); if (dbs.fillTestTable() == 0) QSKIP("No database drivers are available in this Qt configuration"); } diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp index a497fac79ab..55cb67eed9a 100644 --- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -257,7 +257,7 @@ tst_QSqlQuery::~tst_QSqlQuery() void tst_QSqlQuery::initTestCase() { - dbs.open(); + QVERIFY(dbs.open()); for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) { QSqlDatabase db = QSqlDatabase::database(( *it ) ); diff --git a/tests/auto/sql/kernel/qsqlthread/tst_qsqlthread.cpp b/tests/auto/sql/kernel/qsqlthread/tst_qsqlthread.cpp index 1be74dc5fa0..e85c64db8ee 100644 --- a/tests/auto/sql/kernel/qsqlthread/tst_qsqlthread.cpp +++ b/tests/auto/sql/kernel/qsqlthread/tst_qsqlthread.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -350,7 +350,7 @@ void tst_QSqlThread::recreateTestTables() void tst_QSqlThread::initTestCase() { - dbs.open(); + QVERIFY(dbs.open()); recreateTestTables(); } diff --git a/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp b/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp index fe0f6abd9d9..1e4ef731985 100644 --- a/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp +++ b/tests/auto/sql/models/qsqlquerymodel/tst_qsqlquerymodel.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -124,7 +124,7 @@ tst_QSqlQueryModel::~tst_QSqlQueryModel() void tst_QSqlQueryModel::initTestCase() { - dbs.open(); + QVERIFY(dbs.open()); for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) { QSqlDatabase db = QSqlDatabase::database((*it)); CHECK_DATABASE(db); diff --git a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp index 37c08e51015..75a403e4b6e 100644 --- a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp +++ b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -98,7 +98,7 @@ private: void tst_QSqlRelationalTableModel::initTestCase_data() { - dbs.open(); + QVERIFY(dbs.open()); if (dbs.fillTestTable() == 0) QSKIP("No database drivers are available in this Qt configuration"); } diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp index 628a34f6f4d..61583f0e20c 100644 --- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -157,7 +157,7 @@ private: tst_QSqlTableModel::tst_QSqlTableModel() { - dbs.open(); + QVERIFY(dbs.open()); } tst_QSqlTableModel::~tst_QSqlTableModel() @@ -1968,6 +1968,7 @@ void tst_QSqlTableModel::sqlite_attachedDatabase() QCOMPARE(model.rowCount(), 1); QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 3); QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("main")); + attachedDb.close(); }