/
upgradetesthelper.cpp
256 lines (211 loc) · 7.01 KB
/
upgradetesthelper.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* @file upgradetesthelper.cpp
* @copyright 2013 Jolla Ltd.
* @author Martin Kampas <martin.kampas@tieto.com>
* @date 2013
*/
#include "upgradetesthelper.h"
#include <QtCore/QBuffer>
#include <QtCore/QDebug>
#include <QtCore/qmath.h>
#include <QtCore/QTemporaryFile>
#include <QtCore/QTextStream>
#include "libssu/ssusettings_p.h"
/**
* @class UpgradeTestHelper
* @brief Utilities to generate upgrade-test data
*
* It is driven by recipe in following format.
*
* Every line consists of three values delimited by colon:
*
* @verbatim
* <history>:<current>:<expected>
* @endverbatim
*
* - history of changes of the key. A word of length HistoryLength made up
* of letters [SKRN] -- (S)et value, (K)eep value, (R)emove key, (N)oop
* - current value (settings at revision CurrentVersion)
* - value expected after upgrade. The special value "@NOTSET@" means the key
* is expected to not be set after upgrade.
*
* Action (S)et results in assigning "v<revision>-default" to the key, e.g.,
* "v5-default" when 'S' appears as 5th letter in the history word.
*
* The key is built as "<history>__<current>".
*
* Example:
*
* @verbatim
* SKNRN:v1-default:@NOTSET@
* SSNSN:v2-default:v4-default
* @endverbatim
*
* Generated settings:
*
* @verbatim
* [Global]
* configVersion = 2
* SKNRN__v1-default = v1-default
* SSNSN__v2-default = v2-default
*
* [groupA]
* SKNRN__v1-default = v1-default
* SSNSN__v2-default = v2-default
* @endverbatim
*
* Generated default settings:
*
* @verbatim
* [Global]
* configVersion = 5
*
* [1]
* SKNRN__v1-default = v1-default
* groupA\SKNRN__v1-default = v1-default
* SSNSN__v2-default = v1-default
* groupA\SSNSN__v2-default = v1-default
*
* [2]
* SKNRN__v1-default = v1-default
* groupA\SKNRN__v1-default = v1-default
* SSNSN__v2-default = v2-default
* groupA\SSNSN__v2-default = v2-default
*
* [3]
*
* [4]
* cmd-remove = SKNRN__v1-default, groupA/SKNRN__v1-default
* SSNSN__v2-default = v4-default
* groupA\SSNSN__v2-default = v4-default
*
* [5]
* @endverbatim
*/
QList<UpgradeTestHelper::TestCase> UpgradeTestHelper::readRecipe(QIODevice *recipe){
QList<TestCase> testCases;
while (!recipe->atEnd()){
const QString line = recipe->readLine().trimmed();
if (line.startsWith('#') || line.isEmpty())
continue;
const QStringList splitted = line.split(':');
Q_ASSERT_X(splitted.count() == 3, Q_FUNC_INFO,
qPrintable(QString("Inalid recipe line '%1'").arg(line)));
testCases.append(TestCase(splitted.at(0), splitted.at(1), splitted.at(2)));
}
return testCases;
}
void UpgradeTestHelper::fillSettings(QSettings *settings, const QList<TestCase> &testCases){
settings->setValue("configVersion", CurrentVersion);
foreach (const QString &group, groups()){
settings->beginGroup(group);
foreach (const TestCase &testCase, testCases){
if (!testCase.current().isEmpty()){
settings->setValue(testCase.key(), testCase.current());
}
}
settings->endGroup();
}
settings->sync();
}
void UpgradeTestHelper::fillDefaultSettings(QSettings *defaultSettings, const QList<TestCase>
&testCases){
defaultSettings->setValue("configVersion", HistoryLength);
QHash<QString, QString> lastSetValue; // for the (K)eep action; no need to qualify with group
for (int revision = 1; revision <= HistoryLength; ++revision){
defaultSettings->beginGroup(QString::number(revision));
QStringList keysToRemove;
foreach (const QString &group, groups()){
defaultSettings->beginGroup(group);
foreach (const TestCase &testCase, testCases){
switch (testCase.history().at(revision - 1).toLatin1()){
case 'S': // (S)et value
lastSetValue[testCase.key()] = QString("v%1-default").arg(revision);
defaultSettings->setValue(testCase.key(), lastSetValue[testCase.key()]);
break;
case 'K': // (K)eep value
Q_ASSERT_X(!lastSetValue[testCase.key()].isEmpty(), Q_FUNC_INFO,
qPrintable(QString("Inalid TestCase::history: '%1'").arg(testCase.history())));
defaultSettings->setValue(testCase.key(), lastSetValue[testCase.key()]);
break;
case 'R': // (R)emove key
keysToRemove.append((group.isEmpty() ? group : group + "/") + testCase.key());
lastSetValue.remove(testCase.key());
break;
case 'N': // (N)oop
break;
default:
Q_ASSERT_X(false, Q_FUNC_INFO, qPrintable(QString(
"Inalid TestCase::history: '%1': invalid command-code '%2'")
.arg(testCase.history())
.arg(testCase.history().at(revision - 1))));
}
}
defaultSettings->endGroup();
}
if (!keysToRemove.isEmpty()){
defaultSettings->setValue("cmd-remove", keysToRemove);
}
defaultSettings->endGroup();
}
defaultSettings->sync();
}
bool UpgradeTestHelper::generateSnapshotRecipe(QTextStream *out){
const QString actions = "SKRN";
QBuffer buf;
buf.open(QIODevice::ReadWrite);
QTextStream stream(&buf);
// for all "valid" variations of the letters "SKRN" of length HistoryLength
for (int i = 0; i < qPow(actions.count(), HistoryLength); ++i){
QString history = QString::number(i, actions.count());
// Left pad to HistoryLength
history.prepend(QString(HistoryLength - history.length(), '0'));
for (int revision = 0; revision < history.length(); ++revision){
history.replace(revision, 1, actions.at(history.at(revision).digitValue()));
}
static const QRegExp invalidSequence("(^[^S]*K|R[^S]*K|^R)");
if (history.contains(invalidSequence)){
continue;
}
for (int revision = 0; revision < HistoryLength; ++revision){
stream << history << QString(":v%1-default:\n").arg(revision);
}
stream << history << ":custom:\n";
}
stream.flush();
// Read recipe
buf.seek(0);
QList<TestCase> testCases = readRecipe(&buf);
// Generate settings file according to recipe
QTemporaryFile settingsFile;
if (!settingsFile.open()){
return false;
}
QSettings settings(settingsFile.fileName(), QSettings::IniFormat);
fillSettings(&settings, testCases);
// Generate defaults file according to recipe
QTemporaryFile defaultSettingsFile;
if (!defaultSettingsFile.open()){
return false;
}
QSettings defaultSettings(defaultSettingsFile.fileName(), QSettings::IniFormat);
fillDefaultSettings(&defaultSettings, testCases);
// Parse settings -- do upgrade
SsuSettings ssuSettings(settingsFile.fileName(), QSettings::IniFormat,
defaultSettingsFile.fileName());
// Output recipe
foreach (const UpgradeTestHelper::TestCase &testCase, testCases){
const QString expected = ssuSettings.contains(testCase.key())
? ssuSettings.value(testCase.key()).toString()
: "@NOTSET@";
*out << QString("%1:%2:%3\n")
.arg(testCase.history())
.arg(testCase.current())
.arg(expected);
}
return true;
}
QStringList UpgradeTestHelper::groups(){
static const QStringList groups = QStringList() << "" /* General */ << "groupA";
return groups;
}