From 8cb7d68cf6664fe721828648bc8ed2bb621c8a1b Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Wed, 1 Jul 2015 11:47:17 +0200 Subject: CppTools: Tests: Fix QTemporaryDir warning on Windows ...coming from TemporaryCopiedDir. The warning was: QTemporaryDir: Unable to remove most likely due to the presence of read-only files. It occurred for plugin tests (e.g ClangCodeModel) that used a Qt resource path as source for TemporaryCopiedDir. Copying a qrc file makes it read-only, see also QTBUG-17163. Therefore, explicitly add the write permission. Change-Id: Idc04acd4844c5bc20e9b3de1efc78da3b06ac97c Reviewed-by: Christian Stenger Reviewed-by: Marco Bubke --- src/plugins/cpptools/cpptoolstestcase.cpp | 50 ++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 11 deletions(-) (limited to 'src/plugins/cpptools/cpptoolstestcase.cpp') diff --git a/src/plugins/cpptools/cpptoolstestcase.cpp b/src/plugins/cpptools/cpptoolstestcase.cpp index fa6bed8b51..8d2a3684a0 100644 --- a/src/plugins/cpptools/cpptoolstestcase.cpp +++ b/src/plugins/cpptools/cpptoolstestcase.cpp @@ -294,23 +294,51 @@ QString TemporaryDir::createFile(const QByteArray &relativePath, const QByteArra return filePath; } +static bool copyRecursively(const QString &sourceDirPath, + const QString &targetDirPath, + QString *error) +{ + auto copyHelper = [](QFileInfo sourceInfo, QFileInfo targetInfo, QString *error) -> bool { + const QString sourcePath = sourceInfo.absoluteFilePath(); + const QString targetPath = targetInfo.absoluteFilePath(); + if (!QFile::copy(sourcePath, targetPath)) { + if (error) { + *error = QString::fromLatin1("copyRecursively() failed: \"%1\" to \"%2\".") + .arg(sourcePath, targetPath); + } + return false; + } + + // Copied files from Qt resources are read-only. Make them writable + // so that their parent directory can be removed without warnings. + QFile file(targetPath); + return file.setPermissions(file.permissions() | QFile::WriteUser); + }; + + return Utils::FileUtils::copyRecursively(Utils::FileName::fromString(sourceDirPath), + Utils::FileName::fromString(targetDirPath), + error, + copyHelper); +} + TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath) { if (!m_isValid) return; - if (!sourceDirPath.isEmpty()) { - QFileInfo fi(sourceDirPath); - if (!fi.exists() || !fi.isReadable()) { - m_isValid = false; - return; - } + if (sourceDirPath.isEmpty()) + return; - if (!Utils::FileUtils::copyRecursively(Utils::FileName::fromString(sourceDirPath), - Utils::FileName::fromString(path()))) { - m_isValid = false; - return; - } + QFileInfo fi(sourceDirPath); + if (!fi.exists() || !fi.isReadable()) { + m_isValid = false; + return; + } + + QString errorMessage; + if (!copyRecursively(sourceDirPath, path(), &errorMessage)) { + QWARN(qPrintable(errorMessage)); + m_isValid = false; } } -- cgit v1.2.1