diff options
author | Jake Petroules <jake.petroules@qt.io> | 2017-10-23 07:30:35 -0700 |
---|---|---|
committer | Jake Petroules <jake.petroules@qt.io> | 2017-10-23 07:31:18 -0700 |
commit | 8433ee153e9cf1b7a4a536f807b64f988ce6c5cf (patch) | |
tree | 15eca8a3d36a69b52eb209b66b732ef5031159f2 /tests/auto/tools | |
parent | 5bb59e74070fdbf12c4b6c943385ef9a480b787c (diff) | |
parent | 9bbd8bec0491c332572941ad5545e207e9d6e2b8 (diff) |
Merge 1.10 into master
Change-Id: Ie23e92d1adbd73591444f725106301a63465f63d
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; }; |