-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
179 lines (150 loc) · 6.19 KB
/
util.js
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
const insertLine = require('insert-line');
const fs = require('fs');
const tagRegex = /\@[\w\d\=\-\_\(\)\.\:\&]*[\w\d]/g;
const suiteRegex = /\@S[\w\d]{8}/g;
const testRegex = /\@T[\w\d]{8}/g;
const getSpace = (code) => {
const lines = code.split('\n');
let line = lines[0];
if (lines[1] && lines[0].trim().startsWith('@')) line = lines[1];
return line.search(/\S|$/);
};
const getTitle = (name) => {
return name.replace(tagRegex, '').trim();
}
const parseTest = testTitle => {
const captures = testTitle.match(testRegex);
if (captures) {
return captures[1];
}
return null;
};
const parseSuite = suiteTitle => {
const captures = suiteTitle.match(suiteRegex);
if (captures) {
return captures[1];
}
return null;
};
const insertLineToFile = (file, line, opts = { at: 1, overwrite: false }) => {
const fileContent = fs.readFileSync(file, 'utf8').toString();
const lines = fileContent.split(/\r\n|\r|\n/g);
lines.splice(opts.at - 1, opts.overwrite ? 1 : 0, line);
fs.writeFileSync(file, lines.join("\n"));
}
const getLine = (file, line) => {
const fileContent = fs.readFileSync(file, 'utf8').toString();
const lines = fileContent.split(/\r\n|\r|\n/g);
return lines[line];
}
function updateFiles(features, testomatioMap, workDir) {
const files = [];
let hasOtherIds = false;
for (const suite of features) {
if (!suite.scenario) continue;
if (!suite.scenario.length) continue;
let lineInc = 0;
const featureFile = `${workDir}/${suite.scenario[0].file}`;
files.push(featureFile);
const suiteName = getTitle(suite.feature);
const fileName = suite.scenario[0].file;
let needsSuiteUpdate = true;
if (!testomatioMap.suites[suiteName]) needsSuiteUpdate = false;
if (suite.tags.includes(testomatioMap.suites[suiteName])) needsSuiteUpdate = false;
if (suite.feature.includes(testomatioMap.suites[suiteName])) needsSuiteUpdate = false;
if (needsSuiteUpdate) {
let id = testomatioMap.suites[suiteName];
if (testomatioMap.suites[`${fileName}#${suiteName}`]) id = testomatioMap.suites[`${fileName}#${suiteName}`];
const at = suite.line || 1;
if (process.env.TESTOMATIO_TITLE_IDS) {
// ignore suite ids
} else if (suite.tags.length) {
const hasId = suite.tags.map(t => '@' + t).find(t => t === id);
if (hasId) continue;
if (suite.tags.find(t => t.match(suiteRegex))) {
hasOtherIds = true;
continue;
}
const tags = getLine(featureFile, at - 1).split(' ').filter(v => v.match(tagRegex))
insertLineToFile(featureFile, `${tags.join(' ')} ${id}`, { overwrite: true, at });
} else {
insertLineToFile(featureFile, `${id}`, { at });
lineInc = 1;
}
}
for (const scenario of suite.scenario) {
const spaceCount = getSpace(scenario.code);
const file = `${workDir}/${scenario.file}`;
const name = getTitle(scenario.name);
if (!testomatioMap.tests[name]) continue;
if (scenario.tags.includes(testomatioMap.tests[name])) continue;
if (scenario.name.includes(testomatioMap.tests[name])) continue;
let id = testomatioMap.tests[name];
if (testomatioMap.tests[`${suiteName}#${name}`]) id = testomatioMap.tests[`${suiteName}#${name}`];
if (testomatioMap.tests[`${fileName}#${suiteName}#${name}`]) id = testomatioMap.tests[`${fileName}#${suiteName}#${name}`];
if (process.env.TESTOMATIO_TITLE_IDS) {
const lineInc = scenario.code.split('\n').findIndex(line => line.trim().startsWith('Scenario'));
const at = scenario.line + lineInc;
const scenarioTitle = getLine(featureFile, at);
insertLineToFile(file, `${scenarioTitle} ${id}`, { overwrite: true, at: at + 1 });
continue;
}
if (scenario.tags.length) {
const hasId = scenario.tags.map(t => '@' + t).find(t => t === id);
if (hasId) continue;
if (suite.tags.find(t => t.match(suiteRegex))) {
hasOtherIds = true;
continue;
}
const at = scenario.line + 1 + lineInc;
const prevLine = getLine(file, at - 1)
const tags = prevLine.split(' ').filter(v => v.match(tagRegex))
insertLineToFile(file, ' '.repeat(spaceCount) + (`${tags.join(' ')} ${id}`.trim()), { overwrite: true, at });
} else {
insertLineToFile(file, `\n${' '.repeat(spaceCount)}${id}`, { overwrite: true, at: scenario.line + lineInc });
lineInc += 1;
}
}
}
if (hasOtherIds) console.log('WARNING: Some tests have IDs from another project. New IDs were ignored. Clean up old IDs with --clean-ids and re-run this command again.');
return files;
}
function cleanFiles(features, testomatioMap = {}, workDir, dangerous = false) {
const testIds = testomatioMap.tests ? Object.values(testomatioMap.tests) : [];
const suiteIds = testomatioMap.suites ? Object.values(testomatioMap.suites) : [];
const files = [];
for (const suite of features) {
if (!suite) continue;
if (!suite.scenario) continue;
if (!suite.scenario.length) continue;
const file = `${workDir}/${suite.scenario[0].file}`;
let fileContent = fs.readFileSync(file, { encoding: 'utf8' });
const suiteTitle = suite.feature;
const suiteId = `@S${parseSuite(suiteTitle)}`;
if (!dangerous) {
/*
Suite and test ids are always added with a new line,
therefore, when we want to remove ids - the whole line shold be removed.
The regex below includes:
1. [ \t]* – unlimited amount of spaces or tabs
2. suite or test id (sid/tid)
2. \\s - new line (or other whitespace characters)
*/
suiteIds.forEach(sid => fileContent = fileContent.replace(new RegExp('[ \\t]*' + sid + '\\s'), ''))
testIds.forEach(tid => fileContent = fileContent.replace(new RegExp('[ \\t]*' + tid + '\\s'), ''))
} else {
fileContent = fileContent.replace(suiteRegex, '');
fileContent = fileContent.replace(testRegex, '');
}
fileContent = fileContent.split('\n').map(l => l.replace(/\s+$/, '')).join('\n');
files.push(file);
fs.writeFileSync(file, fileContent, (err) => {
if (err) throw err;
});
}
return files;
}
module.exports = {
updateFiles,
cleanFiles,
};