Commit 6cafc424 authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen

CppTools: Tests: Stop writing into source dir

Instead, always write into a unique temporary directory in
QDir::tempPath().

Where applicable, read the test source from files instead of first
writing the file.

Some clean ups in test_codegen*.

Change-Id: Id48dc50c6ca16252edfd9fc8a86ba0de9f9be486
Reviewed-by: default avatarChristian Stenger <christian.stenger@theqtcompany.com>
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@theqtcompany.com>
parent cb3c7538
This diff is collapsed.
......@@ -73,8 +73,10 @@ public:
m_source[m_position] = ' ';
// Write source to file
const QString fileName = QDir::tempPath() + QLatin1String("/file.h");
QVERIFY(writeFile(fileName, m_source));
m_temporaryDir.reset(new Tests::TemporaryDir());
QVERIFY(m_temporaryDir->isValid());
const QString fileName = m_temporaryDir->createFile("file.h", m_source);
QVERIFY(!fileName.isEmpty());
// Open in editor
m_editor = EditorManager::openEditor(fileName);
......@@ -150,6 +152,7 @@ private:
QByteArray m_source;
int m_position;
Snapshot m_snapshot;
QScopedPointer<Tests::TemporaryDir> m_temporaryDir;
TextEditorWidget *m_editorWidget;
QTextDocument *m_textDocument;
IEditor *m_editor;
......
......@@ -30,6 +30,7 @@
#include "cpptoolsplugin.h"
#include "cpptoolsreuse.h"
#include "cpptoolstestcase.h"
#include "cppfilesettingspage.h"
#include <utils/fileutils.h>
......@@ -60,15 +61,16 @@ void CppToolsPlugin::test_headersource()
QFETCH(QString, sourceFileName);
QFETCH(QString, headerFileName);
bool wasHeader;
const QString baseDir = baseTestDir();
QDir path = QDir(baseDir + _(QTest::currentDataTag()));
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QDir path = QDir(temporaryDir.path() + QLatin1Char('/') + _(QTest::currentDataTag()));
const QString sourcePath = path.absoluteFilePath(sourceFileName);
const QString headerPath = path.absoluteFilePath(headerFileName);
createTempFile(sourcePath);
createTempFile(headerPath);
bool wasHeader;
clearHeaderSourceCache();
QCOMPARE(correspondingHeaderOrSource(sourcePath, &wasHeader), headerPath);
QVERIFY(!wasHeader);
......
......@@ -46,7 +46,6 @@
#include <QDebug>
#include <QFileInfo>
#include <QTemporaryDir>
#include <QtTest>
using namespace CppTools;
......@@ -79,6 +78,15 @@ public:
{ return directory(_("sources")) + fileName; }
};
QStringList toAbsolutePaths(const QStringList &relativePathList,
const Tests::TemporaryCopiedDir &temporaryDir)
{
QStringList result;
foreach (const QString &file, relativePathList)
result << temporaryDir.absolutePath(file.toUtf8());
return result;
}
// TODO: When possible, use this helper class in all tests
class ProjectCreator
{
......@@ -478,8 +486,14 @@ void CppToolsPlugin::test_modelmanager_refresh_added_and_purge_removed()
void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_change()
{
QFETCH(QString, fileToChange);
QFETCH(QList<ProjectFile>, initialProjectFiles);
QFETCH(QList<ProjectFile>, finalProjectFiles);
QFETCH(QStringList, initialProjectFiles);
QFETCH(QStringList, finalProjectFiles);
Tests::TemporaryCopiedDir temporaryDir(
MyTestDataDir(QLatin1String("testdata_refresh2")).path());
fileToChange = temporaryDir.absolutePath(fileToChange.toUtf8());
initialProjectFiles = toAbsolutePaths(initialProjectFiles, temporaryDir);
finalProjectFiles = toAbsolutePaths(finalProjectFiles, temporaryDir);
ModelManagerTestHelper helper;
CppModelManager *mm = CppModelManager::instance();
......@@ -490,8 +504,8 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
ProjectPart::Ptr part(new ProjectPart);
part->languageVersion = ProjectPart::CXX14;
part->qtVersion = ProjectPart::Qt5;
foreach (const ProjectFile &file, initialProjectFiles)
part->files.append(file);
foreach (const QString &file, initialProjectFiles)
part->files.append(ProjectFile(file, ProjectFile::CXXSource));
pi = ProjectInfo(project);
pi.appendProjectPart(part);
pi.finish();
......@@ -504,9 +518,9 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
QCOMPARE(refreshedFiles.size(), initialProjectFiles.size());
snapshot = mm->snapshot();
foreach (const ProjectFile &file, initialProjectFiles) {
QVERIFY(refreshedFiles.contains(file.path));
QVERIFY(snapshot.contains(file.path));
foreach (const QString &file, initialProjectFiles) {
QVERIFY(refreshedFiles.contains(file));
QVERIFY(snapshot.contains(file));
}
document = snapshot.document(fileToChange);
......@@ -524,8 +538,8 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
// Add or remove source file. The configuration stays the same.
part->files.clear();
foreach (const ProjectFile &file, finalProjectFiles)
part->files.append(file);
foreach (const QString &file, finalProjectFiles)
part->files.append(ProjectFile(file, ProjectFile::CXXSource));
pi = ProjectInfo(project);
pi.appendProjectPart(part);
pi.finish();
......@@ -534,9 +548,9 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
QCOMPARE(refreshedFiles.size(), finalProjectFiles.size());
snapshot = mm->snapshot();
foreach (const ProjectFile &file, finalProjectFiles) {
QVERIFY(refreshedFiles.contains(file.path));
QVERIFY(snapshot.contains(file.path));
foreach (const QString &file, finalProjectFiles) {
QVERIFY(refreshedFiles.contains(file));
QVERIFY(snapshot.contains(file));
}
document = snapshot.document(fileToChange);
const QDateTime lastModifiedAfter = document->lastModified();
......@@ -549,19 +563,15 @@ void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_
void CppToolsPlugin::test_modelmanager_refresh_timeStampModified_if_sourcefiles_change_data()
{
QTest::addColumn<QString>("fileToChange");
QTest::addColumn<QList<ProjectFile> >("initialProjectFiles");
QTest::addColumn<QList<ProjectFile> >("finalProjectFiles");
QTest::addColumn<QStringList>("initialProjectFiles");
QTest::addColumn<QStringList>("finalProjectFiles");
const MyTestDataDir testDataDir(_("testdata_refresh2"));
const QString testCpp(testDataDir.file(_("source.cpp")));
const QString testCpp2(testDataDir.file(_("source2.cpp")));
const QString testCpp = QLatin1String("source.cpp");
const QString testCpp2 = QLatin1String("source2.cpp");
const QString fileToChange = testCpp;
QList<ProjectFile> projectFiles1 = QList<ProjectFile>()
<< ProjectFile(testCpp, ProjectFile::CXXSource);
QList<ProjectFile> projectFiles2 = QList<ProjectFile>()
<< ProjectFile(testCpp, ProjectFile::CXXSource)
<< ProjectFile(testCpp2, ProjectFile::CXXSource);
const QStringList projectFiles1 = QStringList() << testCpp;
const QStringList projectFiles2 = QStringList() << testCpp << testCpp2;
// Add a file
QTest::newRow("case: add project file") << fileToChange << projectFiles1 << projectFiles2;
......@@ -1059,7 +1069,7 @@ void CppToolsPlugin::test_modelmanager_renameIncludes()
} GCHelper;
Q_UNUSED(GCHelper); // do not warn about being unused
QTemporaryDir tmpDir;
TemporaryDir tmpDir;
QVERIFY(tmpDir.isValid());
const QDir workingDir(tmpDir.path());
......
......@@ -87,15 +87,18 @@ public:
}
// Write source to temprorary file
const QString filePath = QDir::tempPath() + QLatin1String("/file.h");
Document::Ptr document = Document::create(filePath);
QVERIFY(writeFile(document->fileName(), sourceWithoutCursorMarker.toUtf8()));
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
const QString filePath = temporaryDir.createFile("file.h",
sourceWithoutCursorMarker.toUtf8());
QVERIFY(!filePath.isEmpty());
// Preprocess source
Environment env;
Preprocessor preprocess(0, &env);
const QByteArray preprocessedSource = preprocess.run(filePath, sourceWithoutCursorMarker);
Document::Ptr document = Document::create(filePath);
document->setUtf8Source(preprocessedSource);
document->parse(parseMode);
document->check();
......
......@@ -63,22 +63,16 @@ public:
cleanUp();
}
Document::Ptr run(const QByteArray &source)
Document::Ptr run(const QString &filePath)
{
const QString fileName = TestIncludePaths::testFilePath();
FileWriterAndRemover scopedFile(fileName, source);
if (!scopedFile.writtenSuccessfully())
return Document::Ptr();
QScopedPointer<CppSourceProcessor> sourceProcessor(
CppModelManager::createSourceProcessor());
const ProjectPart::HeaderPath hp(TestIncludePaths::directoryOfTestFile(),
ProjectPart::HeaderPath::IncludePath);
sourceProcessor->setHeaderPaths(ProjectPart::HeaderPaths() << hp);
sourceProcessor->run(fileName);
sourceProcessor->run(filePath);
Document::Ptr document = m_cmm->document(fileName);
Document::Ptr document = m_cmm->document(filePath);
return document;
}
......@@ -101,14 +95,11 @@ private:
/// Check: Resolved and unresolved includes are properly tracked.
void CppToolsPlugin::test_cppsourceprocessor_includes_resolvedUnresolved()
{
QByteArray source =
"#include \"header.h\"\n"
"#include \"notresolvable.h\"\n"
"\n"
;
const QString testFilePath
= TestIncludePaths::testFilePath(QLatin1String("test_main_resolvedUnresolved.cpp"));
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
Document::Ptr document = processor.run(testFilePath);
QVERIFY(document);
const QList<Document::Include> resolvedIncludes = document->resolvedIncludes();
......@@ -167,15 +158,11 @@ void CppToolsPlugin::test_cppsourceprocessor_includes_cyclic()
/// Check: All include errors are reported as diagnostic messages.
void CppToolsPlugin::test_cppsourceprocessor_includes_allDiagnostics()
{
QByteArray source =
"#include <NotResolvable1>\n"
"#include <NotResolvable2>\n"
"#include \"/some/nonexisting/file123.h\"\n"
"\n"
;
const QString testFilePath
= TestIncludePaths::testFilePath(QLatin1String("test_main_allDiagnostics.cpp"));
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
Document::Ptr document = processor.run(testFilePath);
QVERIFY(document);
QCOMPARE(document->resolvedIncludes().size(), 0);
......@@ -185,15 +172,11 @@ void CppToolsPlugin::test_cppsourceprocessor_includes_allDiagnostics()
void CppToolsPlugin::test_cppsourceprocessor_macroUses()
{
QByteArray source =
"#define SOMEDEFINE 1\n"
"#if SOMEDEFINE == 1\n"
" int someNumber;\n"
"#endif\n"
;
const QString testFilePath
= TestIncludePaths::testFilePath(QLatin1String("test_main_macroUses.cpp"));
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
Document::Ptr document = processor.run(testFilePath);
QVERIFY(document);
const QList<Document::MacroUse> macroUses = document->macroUses();
QCOMPARE(macroUses.size(), 1);
......
......@@ -77,8 +77,12 @@ TestDocument::TestDocument(const QByteArray &fileName, const QByteArray &source,
QString TestDocument::filePath() const
{
if (!m_baseDirectory.isEmpty())
return QDir::cleanPath(m_baseDirectory + QLatin1Char('/') + m_fileName);
if (!QFileInfo(m_fileName).isAbsolute())
return QDir::tempPath() + QLatin1Char('/') + m_fileName;
return m_fileName;
}
......@@ -263,9 +267,25 @@ void ProjectOpenerAndCloser::onGcFinished()
m_gcFinished = true;
}
TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath)
TemporaryDir::TemporaryDir()
: m_temporaryDir(QDir::tempPath() + QLatin1String("/qtcreator-tests-XXXXXX"))
, m_isValid(m_temporaryDir.isValid())
{
}
QString TemporaryDir::createFile(const QByteArray &relativePath, const QByteArray &contents)
{
const QString relativePathString = QString::fromUtf8(relativePath);
if (relativePathString.isEmpty() || QFileInfo(relativePathString).isAbsolute())
return QString();
const QString filePath = m_temporaryDir.path() + QLatin1Char('/') + relativePathString;
if (!TestCase::writeFile(filePath, contents))
return QString();
return filePath;
}
TemporaryCopiedDir::TemporaryCopiedDir(const QString &sourceDirPath)
{
if (!m_isValid)
return;
......
......@@ -60,10 +60,13 @@ class CPPTOOLS_EXPORT TestDocument
public:
TestDocument(const QByteArray &fileName, const QByteArray &source, char cursorMarker = '@');
void setBaseDirectory(const QString &baseDirectory) { m_baseDirectory = baseDirectory; }
QString filePath() const;
bool writeToDisk() const;
public:
QString m_baseDirectory;
QString m_fileName;
QString m_source;
char m_cursorMarker;
......@@ -124,20 +127,33 @@ private:
QList<ProjectExplorer::Project *> m_openProjects;
};
class CPPTOOLS_EXPORT TemporaryCopiedDir
class TemporaryDir
{
Q_DISABLE_COPY(TemporaryDir)
public:
TemporaryCopiedDir(const QString &sourceDirPath);
TemporaryDir();
bool isValid() const { return m_isValid; }
QString path() const { return m_temporaryDir.path(); }
QString absolutePath(const QByteArray &relativePath) const;
private:
QString createFile(const QByteArray &relativePath, const QByteArray &contents);
protected:
QTemporaryDir m_temporaryDir;
bool m_isValid;
};
class CPPTOOLS_EXPORT TemporaryCopiedDir : public TemporaryDir
{
public:
TemporaryCopiedDir(const QString &sourceDirPath);
QString absolutePath(const QByteArray &relativePath) const;
private:
TemporaryCopiedDir();
};
class CPPTOOLS_EXPORT VerifyCleanCppModelManager
{
public:
......
......@@ -544,14 +544,8 @@ bool IncludeGroup::hasCommonIncludeDir() const
using namespace Tests;
using CppTools::Internal::CppToolsPlugin;
static QList<Include> includesForSource(const QByteArray &source)
static QList<Include> includesForSource(const QString &filePath)
{
const QString fileName = TestIncludePaths::testFilePath();
FileWriterAndRemover scopedFile(fileName, source);
if (!scopedFile.writtenSuccessfully())
return QList<Include>();
using namespace CppTools::Internal;
CppModelManager *cmm = CppModelManager::instance();
cmm->GC();
......@@ -560,44 +554,18 @@ static QList<Include> includesForSource(const QByteArray &source)
<< ProjectPart::HeaderPath(
TestIncludePaths::globalIncludePath(),
ProjectPart::HeaderPath::IncludePath));
sourceProcessor->run(fileName);
sourceProcessor->run(filePath);
Document::Ptr document = cmm->document(fileName);
Document::Ptr document = cmm->document(filePath);
return document->resolvedIncludes();
}
void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByNewLines()
{
// Source referencing those files
QByteArray source =
"#include \"header.h\"\n"
"\n"
"#include \"file.h\"\n"
"#include \"fileother.h\"\n"
"\n"
"#include <lib/fileother.h>\n"
"#include <lib/file.h>\n"
"\n"
"#include \"otherlib/file.h\"\n"
"#include \"otherlib/fileother.h\"\n"
"\n"
"#include \"utils/utils.h\"\n"
"\n"
"#include <QDebug>\n"
"#include <QDir>\n"
"#include <QString>\n"
"\n"
"#include <iostream>\n"
"#include <string>\n"
"#include <except>\n"
"\n"
"#include <iostream>\n"
"#include \"stuff\"\n"
"#include <except>\n"
"\n"
;
QList<Include> includes = includesForSource(source);
const QString testFilePath = TestIncludePaths::testFilePath(
QLatin1String("test_main_detectIncludeGroupsByNewLines.cpp"));
QList<Include> includes = includesForSource(testFilePath);
QCOMPARE(includes.size(), 17);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByNewLines(includes);
......@@ -636,20 +604,10 @@ void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByNewLines()
void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByIncludeDir()
{
QByteArray source =
"#include \"file.h\"\n"
"#include \"fileother.h\"\n"
"#include <lib/file.h>\n"
"#include <lib/fileother.h>\n"
"#include \"otherlib/file.h\"\n"
"#include \"otherlib/fileother.h\"\n"
"#include <iostream>\n"
"#include <string>\n"
"#include <except>\n"
"\n"
;
QList<Include> includes = includesForSource(source);
const QString testFilePath = TestIncludePaths::testFilePath(
QLatin1String("test_main_detectIncludeGroupsByIncludeDir.cpp"));
QList<Include> includes = includesForSource(testFilePath);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
......@@ -670,20 +628,10 @@ void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByIncludeDir()
void CppToolsPlugin::test_includeGroups_detectIncludeGroupsByIncludeType()
{
QByteArray source =
"#include \"file.h\"\n"
"#include \"fileother.h\"\n"
"#include <lib/file.h>\n"
"#include <lib/fileother.h>\n"
"#include \"otherlib/file.h\"\n"
"#include \"otherlib/fileother.h\"\n"
"#include <iostream>\n"
"#include <string>\n"
"#include <except>\n"
"\n"
;
QList<Include> includes = includesForSource(source);
const QString testFilePath = TestIncludePaths::testFilePath(
QLatin1String("test_main_detectIncludeGroupsByIncludeType.cpp"));
QList<Include> includes = includesForSource(testFilePath);
QCOMPARE(includes.size(), 9);
QList<IncludeGroup> includeGroups
= IncludeGroup::detectIncludeGroupsByIncludeDir(includes);
......
......@@ -103,11 +103,17 @@ public:
{
QVERIFY(succeededSoFar());
Tests::TemporaryDir temporaryDir;
QVERIFY(temporaryDir.isValid());
QList<Tests::TestDocument> documents_ = documents;
// Write files
QSet<QString> filePaths;
foreach (const Tests::TestDocument &document, documents) {
QVERIFY(document.writeToDisk());
filePaths << document.filePath();
for (int i = 0, size = documents_.size(); i < size; ++i) {
documents_[i].setBaseDirectory(temporaryDir.path());
QVERIFY(documents_[i].writeToDisk());
filePaths << documents_[i].filePath();
}
// Parse files
......@@ -115,7 +121,7 @@ public:
const Snapshot snapshot = globalSnapshot();
// Get class for which to generate the hierarchy
const Document::Ptr firstDocument = snapshot.document(documents.first().filePath());
const Document::Ptr firstDocument = snapshot.document(documents_.first().filePath());
QVERIFY(firstDocument);
QVERIFY(firstDocument->diagnosticMessages().isEmpty());
Class *clazz = FindFirstClassInDocument()(firstDocument);
......
......@@ -38,7 +38,6 @@
#include <cpptools/cpptoolstestcase.h>
#include <QFileInfo>
#include <QTemporaryDir>
#include <QTest>
using namespace CppTools;
......
#include <NotResolvable1>
#include <NotResolvable2>
#include "/some/nonexisting/file123.h"
#include "file.h"
#include "fileother.h"
#include <lib/file.h>
#include <lib/fileother.h>
#include "otherlib/file.h"
#include "otherlib/fileother.h"
#include <iostream>
#include <string>
#include <except>
#include "file.h"
#include "fileother.h"
#include <lib/file.h>
#include <lib/fileother.h>
#include "otherlib/file.h"
#include "otherlib/fileother.h"
#include <iostream>
#include <string>
#include <except>
#include "header.h"
#include "file.h"
#include "fileother.h"
#include <lib/fileother.h>
#include <lib/file.h>
#include "otherlib/file.h"
#include "otherlib/fileother.h"
#include "utils/utils.h"
#include <QDebug>
#include <QDir>
#include <QString>
#include <iostream>
#include <string>
#include <except>
#include <iostream>
#include "stuff"
#include <except>
#define SOMEDEFINE 1
#if SOMEDEFINE == 1
int someNumber;
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment