File: testfile.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (221 lines) | stat: -rw-r--r-- 5,460 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
    SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com>
    SPDX-FileCopyrightText: 2011 Milian Wolff <mail@milianw.de>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "testfile.h"

#include "testproject.h"

#include <QTemporaryFile>
#include <QElapsedTimer>
#include <QTest>

#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/backgroundparser/backgroundparser.h>
#include <interfaces/icore.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/ilanguagecontroller.h>
#include <project/projectmodel.h>

using namespace KDevelop;

class KDevelop::TestFilePrivate
{
public:
    TestFilePrivate()
    {
    }

    void updateReady(const IndexedString& _url, const ReferencedTopDUContext& _topContext)
    {
        Q_ASSERT(_url == url);
        Q_UNUSED(_url);
        topContext = _topContext;
        ready = true;
    }

    void init(const QString& fileName, const QString& contents, TestProject* _project)
    {
        file = fileName;

        setFileContents(contents);

        QFileInfo info(file);
        Q_ASSERT(info.exists());
        Q_ASSERT(info.isFile());
        url = IndexedString(info.absoluteFilePath());

        project = _project;
        if (project) {
            fileItem.reset(new ProjectFileItem(_project, Path(file), _project->projectItem()));
        }
    }

    void setFileContents(const QString& contents)
    {
        QFile file(this->file);
        file.open(QIODevice::WriteOnly | QIODevice::Truncate);
        Q_ASSERT(file.isOpen());
        Q_ASSERT(file.isWritable());
        file.write(contents.toUtf8());
        ready = false;
    }

    QString file;
    QString suffix;
    bool ready = false;
    ReferencedTopDUContext topContext;
    IndexedString url;
    TestProject* project;
    QScopedPointer<ProjectFileItem> fileItem;
    bool keepDUChainData = false;
};

TestFile::TestFile(const QString& contents, const QString& fileExtension,
                   TestProject* project, const QString& dir)
    : d_ptr(new TestFilePrivate())
{
    Q_D(TestFile);

    d->suffix = QLatin1Char('.') + fileExtension;

    QTemporaryFile file((!dir.isEmpty() ? dir : QDir::tempPath()) + QLatin1String("/testfile_XXXXXX") + d->suffix);
    file.setAutoRemove(false);
    file.open();
    Q_ASSERT(file.isOpen());

    d->init(file.fileName(), contents, project);
}

TestFile::TestFile(const QString& contents, const QString& fileExtension, const TestFile* base)
    : d_ptr(new TestFilePrivate)
{
    Q_D(TestFile);

    QString fileName = base->d_func()->file.mid(0, base->d_func()->file.length() - base->d_func()->suffix.length());
    d->suffix = QLatin1Char('.') + fileExtension;
    fileName += d->suffix;
    d->init(fileName, contents, base->d_func()->project);
}

TestFile::TestFile(const QString& contents, const QString& fileExtension, const QString& fileName,
                   KDevelop::TestProject* project, const QString& dir)
    : d_ptr(new TestFilePrivate)
{
    Q_D(TestFile);

    d->suffix = QLatin1Char('.') + fileExtension;
    const QString file = (!dir.isEmpty() ? dir : QDir::tempPath())
                    + QLatin1Char('/') + fileName + d->suffix;
    d->init(file, contents, project);
}


TestFile::~TestFile()
{
    Q_D(TestFile);

    if (auto* document = ICore::self()->documentController()->documentForUrl(d->url.toUrl())) {
        document->close(KDevelop::IDocument::Discard);
    }

    auto backgroundParser = ICore::self()->languageController()->backgroundParser();
    backgroundParser->removeDocument(d->url, this);
    QTRY_VERIFY(!backgroundParser->parseJobForDocument(d->url));

    if (d->topContext && !d->keepDUChainData) {
        DUChainWriteLocker lock;
        DUChain::self()->removeDocumentChain(d->topContext.data());
    }
    QFile::remove(d->file);
}

IndexedString TestFile::url() const
{
    Q_D(const TestFile);

    return d->url;
}

void TestFile::parse(TopDUContext::Features features, int priority)
{
    Q_D(TestFile);

    d->ready = false;
    DUChain::self()->updateContextForUrl(d->url, features, this, priority);
}

bool TestFile::parseAndWait(TopDUContext::Features features, int priority, int timeout)
{
    parse(features, priority);
    return waitForParsed(timeout);
}

bool TestFile::waitForParsed(int timeout)
{
    Q_D(TestFile);

    if (!d->ready) {
        // optimize: we don't want to wait the usual timeout before parsing documents here
        ICore::self()->languageController()->backgroundParser()->parseDocuments();
    }
    QElapsedTimer t;
    t.start();
    while (!d->ready && t.elapsed() < timeout) {
        QTest::qWait(10);
    }
    return d->ready;
}

bool TestFile::isReady() const
{
    Q_D(const TestFile);

    return d->ready;
}

ReferencedTopDUContext TestFile::topContext()
{
    Q_D(TestFile);

    waitForParsed();
    return d->topContext;
}

void TestFile::setFileContents(const QString& contents)
{
    Q_D(TestFile);

    d->setFileContents(contents);
}

QString TestFile::fileContents() const
{
    Q_D(const TestFile);

    QFile file(d->file);
    file.open(QIODevice::ReadOnly);
    Q_ASSERT(file.isOpen());
    Q_ASSERT(file.isReadable());
    return QString::fromUtf8(file.readAll());
}

void TestFile::setKeepDUChainData(bool keep)
{
    Q_D(TestFile);

    d->keepDUChainData = keep;
}

bool TestFile::keepDUChainData() const
{
    Q_D(const TestFile);

    return d->keepDUChainData;
}

#include "moc_testfile.cpp"