diff options
author | Jake Petroules <jake.petroules@qt.io> | 2017-10-17 12:29:53 +0200 |
---|---|---|
committer | Jake Petroules <jake.petroules@qt.io> | 2017-10-20 12:27:03 +0000 |
commit | 2d99ec0838109240871dce5fd0b9ac646d7d1a99 (patch) | |
tree | 3fbd562a005e5b7eced39ea732ff5dac8634a524 /tests/auto/tools | |
parent | 19abf216fdd8a54c2b8ec101631f378ce6b5bc6d (diff) |
Fix FileSaver and add some autotests to make sure it works
Change-Id: I707edb703068868014b4434b7508bfa41617383c
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'tests/auto/tools')
-rw-r--r-- | tests/auto/tools/tst_tools.cpp | 127 | ||||
-rw-r--r-- | tests/auto/tools/tst_tools.h | 7 |
2 files changed, 133 insertions, 1 deletions
diff --git a/tests/auto/tools/tst_tools.cpp b/tests/auto/tools/tst_tools.cpp index dca88f438..b29e2007c 100644 --- a/tests/auto/tools/tst_tools.cpp +++ b/tests/auto/tools/tst_tools.cpp @@ -46,6 +46,7 @@ #include <tools/buildoptions.h> #include <tools/error.h> #include <tools/fileinfo.h> +#include <tools/filesaver.h> #include <tools/hostosinfo.h> #include <tools/processutils.h> #include <tools/profile.h> @@ -67,7 +68,8 @@ using namespace qbs; using namespace qbs::Internal; -TestTools::TestTools(Settings *settings) : m_settings(settings) +TestTools::TestTools(Settings *settings) + : m_settings(settings), testDataDir(testWorkDir("tools")) { } @@ -76,6 +78,129 @@ TestTools::~TestTools() qDeleteAll(m_tmpDirs); } +void TestTools::initTestCase() +{ + QDir().mkpath(testDataDir); +} + +void TestTools::fileSaver() +{ + QVERIFY(QDir::setCurrent(testDataDir)); + + static const char *fn = "foo.txt"; + const auto run = [](const std::function<void()> &func) { + if (QFile::exists(fn)) + QVERIFY(QFile::remove(fn)); + func(); + if (QFile::exists(fn)) + QVERIFY(QFile::remove(fn)); + }; + + // failing to open the file means nothing works + run([] { + Internal::FileSaver fs(fn); + QVERIFY(!fs.device()); + QVERIFY(!fs.write("hello")); + QVERIFY(!fs.commit()); + QVERIFY(!QFile::exists(fn)); + }); + + // verify that correct usage creates a file with the right contents + run([] { + Internal::FileSaver fs(fn); + QVERIFY(fs.open()); + QVERIFY(fs.device()); + QVERIFY(fs.write("hello")); + QVERIFY(fs.commit()); + QVERIFY(QFile::exists(fn)); + QFile f(fn); + QVERIFY(f.open(QIODevice::ReadOnly)); + QCOMPARE(f.readAll(), QByteArrayLiteral("hello")); + }); + + // failing to commit writes nothing + run([] { + Internal::FileSaver fs(fn); + QVERIFY(fs.open()); + QVERIFY(fs.device()); + QVERIFY(fs.write("hello")); + QVERIFY(!QFile::exists(fn)); + }); + + // verify that correct usage creates a file with the right contents and does not overwrite + run([] { + { + Internal::FileSaver fs(fn); + QVERIFY(fs.open()); + QVERIFY(fs.device()); + QVERIFY(fs.write("hello")); + QVERIFY(fs.commit()); + QVERIFY(QFile::exists(fn)); + QFile f(fn); + QVERIFY(f.open(QIODevice::ReadOnly)); + QCOMPARE(f.readAll(), QByteArrayLiteral("hello")); + } + + const auto lm = QFileInfo(fn).lastModified(); + QVERIFY(lm.isValid()); + + waitForNewTimestamp("."); + + { + Internal::FileSaver fs(fn); + QVERIFY(fs.open()); + QVERIFY(fs.device()); + QVERIFY(fs.write("hello")); + QVERIFY(fs.commit()); + QVERIFY(QFile::exists(fn)); + } + + const auto lm2 = QFileInfo(fn).lastModified(); + QVERIFY(lm2.isValid()); + + QCOMPARE(lm, lm2); // timestamps should be the same since content did not change + + waitForNewTimestamp("."); + + { + Internal::FileSaver fs(fn); + QVERIFY(fs.open()); + QVERIFY(fs.device()); + QVERIFY(fs.write("hello2")); + QVERIFY(fs.commit()); + QVERIFY(QFile::exists(fn)); + QFile f(fn); + QVERIFY(f.open(QIODevice::ReadOnly)); + QCOMPARE(f.readAll(), QByteArrayLiteral("hello2")); + } + + const auto lm3 = QFileInfo(fn).lastModified(); + QVERIFY(lm3.isValid()); + + QVERIFY(lm != lm3); // timestamps should differ since the content changed + + waitForNewTimestamp("."); + + { + // Test overwriteIfUnchanged + Internal::FileSaver fs(fn, true); + QVERIFY(fs.open()); + QVERIFY(fs.device()); + QVERIFY(fs.write("hello2")); + QVERIFY(fs.commit()); + QVERIFY(QFile::exists(fn)); + QFile f(fn); + QVERIFY(f.open(QIODevice::ReadOnly)); + QCOMPARE(f.readAll(), QByteArrayLiteral("hello2")); + } + + const auto lm4 = QFileInfo(fn).lastModified(); + QVERIFY(lm4.isValid()); + + QVERIFY(lm3 != lm4); // timestamps should differ since we always overwrite + }); +} + void TestTools::testFileInfo() { QCOMPARE(FileInfo::fileName("C:/waffl/copter.exe"), QString("copter.exe")); diff --git a/tests/auto/tools/tst_tools.h b/tests/auto/tools/tst_tools.h index e80d5fbf1..bd8538be2 100644 --- a/tests/auto/tools/tst_tools.h +++ b/tests/auto/tools/tst_tools.h @@ -56,7 +56,12 @@ public: TestTools(qbs::Settings *settings); ~TestTools(); +public slots: + virtual void initTestCase(); + private slots: + void fileSaver(); + void fileCaseCheck(); void testBuildConfigMerging(); void testFileInfo(); @@ -101,4 +106,6 @@ private: qbs::Settings * const m_settings; QList<QTemporaryDir *> m_tmpDirs; + + const QString testDataDir; }; |