From cfe3c0be34b63f8c4326f2d01cf880f6c28c9eb5 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Fri, 13 Jun 2014 12:59:24 -0400 Subject: CppTools: Remove test file after test In test_includeGroups_detectIncludeGroupsByIncludeType the test file was not removed and thus subsequent and isolated executions of this test failed. Change-Id: I5eeb4f004f5b146ac794ca913b4092629131fca3 Reviewed-by: Erik Verbruggen --- src/plugins/cpptools/cppsourceprocessor_test.cpp | 7 +++---- src/plugins/cpptools/cpptoolstestcase.cpp | 23 +++++++++++++++++++++++ src/plugins/cpptools/cpptoolstestcase.h | 12 ++++++++++++ src/plugins/cpptools/includeutils.cpp | 6 ++++-- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/plugins/cpptools/cppsourceprocessor_test.cpp b/src/plugins/cpptools/cppsourceprocessor_test.cpp index 73a3754f44..a0b4022682 100644 --- a/src/plugins/cpptools/cppsourceprocessor_test.cpp +++ b/src/plugins/cpptools/cppsourceprocessor_test.cpp @@ -64,17 +64,16 @@ public: Document::Ptr run(const QByteArray &source) { const QString fileName = TestIncludePaths::testFilePath(); - if (QFileInfo(fileName).exists()) - return Document::Ptr(); // Test file was not removed. - TestCase::writeFile(fileName, source); + FileWriterAndRemover scopedFile(fileName, source); + if (!scopedFile.writtenSuccessfully()) + return Document::Ptr(); CppSourceProcessor sourceProcessor((QPointer(m_cmm))); sourceProcessor.setIncludePaths(QStringList(TestIncludePaths::directoryOfTestFile())); sourceProcessor.run(fileName); Document::Ptr document = m_cmm->document(fileName); - QFile(fileName).remove(); return document; } diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index 05eb856821..554a993596 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -189,5 +189,28 @@ bool TestCase::writeFile(const QString &filePath, const QByteArray &contents) return true; } +FileWriterAndRemover::FileWriterAndRemover(const QString &filePath, const QByteArray &contents) + : m_filePath(filePath) +{ + if (QFileInfo(filePath).exists()) { + const QString warning = QString::fromLatin1( + "Will not overwrite existing file: \"%1\"." + " If this file is left over due to a(n) abort/crash, please remove manually.") + .arg(m_filePath); + QWARN(qPrintable(warning)); + m_writtenSuccessfully = false; + } else { + m_writtenSuccessfully = TestCase::writeFile(filePath, contents); + } +} + +FileWriterAndRemover::~FileWriterAndRemover() +{ + if (m_writtenSuccessfully && !QFile::remove(m_filePath)) { + const QString warning = QLatin1String("Failed to remove file from disk: ") + m_filePath; + QWARN(qPrintable(warning)); + } +} + } // namespace Tests } // namespace CppTools diff --git a/src/plugins/cpptools/cpptoolstestcase.h b/src/plugins/cpptools/cpptoolstestcase.h index 1871c96e68..c80438122d 100644 --- a/src/plugins/cpptools/cpptoolstestcase.h +++ b/src/plugins/cpptools/cpptoolstestcase.h @@ -96,6 +96,18 @@ private: bool m_runGarbageCollector; }; +class FileWriterAndRemover +{ +public: + FileWriterAndRemover(const QString &filePath, const QByteArray &contents); // Writes file + bool writtenSuccessfully() const { return m_writtenSuccessfully; } + ~FileWriterAndRemover(); // Removes file + +private: + const QString m_filePath; + bool m_writtenSuccessfully; +}; + } // namespace Tests } // namespace CppTools diff --git a/src/plugins/cpptools/includeutils.cpp b/src/plugins/cpptools/includeutils.cpp index aa1aae123a..c8077ab8de 100644 --- a/src/plugins/cpptools/includeutils.cpp +++ b/src/plugins/cpptools/includeutils.cpp @@ -509,10 +509,12 @@ using CppTools::Internal::CppToolsPlugin; static QList includesForSource(const QByteArray &source) { const QString fileName = TestIncludePaths::testFilePath(); - CppTools::Tests::TestCase::writeFile(fileName, source); - using namespace CppTools::Internal; + FileWriterAndRemover scopedFile(fileName, source); + if (!scopedFile.writtenSuccessfully()) + return QList(); + using namespace CppTools::Internal; CppModelManager *cmm = CppModelManager::instance(); cmm->GC(); CppSourceProcessor sourceProcessor((QPointer(cmm))); -- cgit v1.2.1