-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupTree.js
222 lines (222 loc) · 8.14 KB
/
groupTree.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
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
// Serves the group-test-case report page.
const serveGroupReport = op => {
const {err, fs, reportPrep, reportScriptPrep, servePage} = op;
fs.readFile('groupReport.html', 'utf8')
.then(
htmlContent => {
fs.readFile('report.js', 'utf8')
.then(
jsContent => {
const newJSContent = reportScriptPrep(
jsContent, '/grouptally', ['total', 'changes', 'folderChanges', 'setChanges']
);
const newContent = reportPrep(htmlContent, newJSContent);
servePage(newContent, true);
},
error => err(error, 'reading report script')
);
},
error => err(error, 'reading groupReport page')
);
};
// Handles test-case grouping requests.
const groupHandle = (op, bodyObject) => {
const {err, getRef, globals, shorten} = op;
const {groupFolder, groupSet} = bodyObject;
if (! groupFolder && ! groupSet) {
err('Test folder and test set both missing', 'grouping test cases');
}
else {
// Get a reference to the test folder, if specified.
getRef('testfolder', groupFolder, 'test-case grouping')
.then(
// When the reference, if any, arrives:
ref => {
if (! globals.isError) {
// Set its global variable.
globals.groupFolderRef = shorten('testfolder', 'testfolder', ref);
if (! globals.isError) {
// Get a reference to the test set, if specified.
getRef('testset', groupSet, 'test-case grouping')
.then(
// When the reference, if any, arrives:
ref => {
if (! globals.isError) {
// Set its global variable.
globals.groupSetRef = shorten('testset', 'testset', ref);
// Serve a report on test-case creation.
serveGroupReport(op);
}
},
error => err(error, 'getting reference to test set')
);
}
}
},
error => err(error, 'getting reference to test folder')
);
}
};
// Groups test cases.
const groupCases = (op, cases) => {
const {globals, err, shorten, report, getCollectionData} = op;
if (cases.length && ! globals.isError) {
const firstCase = cases[0];
const firstRef = shorten('testcase', 'testcase', firstCase.ref);
if (! globals.isError) {
report([['total']]);
const folderRef = shorten('testfolder', 'testfolder', firstCase.testFolder);
// Determine what test-folder and test-set groupings are already known to be needed.
const needsFolder = globals.groupFolderRef && folderRef !== globals.groupFolderRef;
let needsSet = globals.groupSetRef && ! firstCase.testSets.count;
// If the need for a test-set grouping is still unknown, get data to determine it.
return (globals.groupSetRef && firstCase.testSets.count ? getCollectionData(
firstCase.testSets.ref, [], []
) : Promise.resolve([]))
.then(
// When the data, if needed, arrive:
sets => {
// Update the need for a test-set grouping if necessary.
if (sets.length && ! sets.map(
set => shorten('testset','testset', set.ref).includes(globals.groupSetRef)
)) {
needsSet = true;
}
// Group the test case into a test folder if necessary.
return (
needsFolder ? globals.restAPI.update({
ref: firstRef,
data: {
TestFolder: globals.groupFolderRef
}
}) : Promise.resolve('')
)
.then(
// When the test-folder grouping, if any, has been made:
() => {
// Group the test case into a test set if necessary.
return (
needsSet ? globals.restAPI.add({
ref: firstRef,
collection: 'TestSets',
data: [{_ref: globals.groupSetRef}],
fetch: ['_ref']
}) : Promise.resolve('')
)
.then(
// When the test-set grouping, if any, has been made:
() => {
if (needsFolder) {
report([['changes'], ['folderChanges']]);
}
if (needsSet) {
report([['changes'], ['setChanges']]);
}
// Process the remaining test cases.
return groupCases(op, cases.slice(1));
},
error => err(error, 'grouping test case into test set')
);
},
error => err(error, 'grouping test case into test folder')
);
},
error => err(error, 'getting initial data on grouping need')
);
}
else {
return Promise.resolve('');
}
}
else {
return Promise.resolve('');
}
};
// Recursively groups test cases in a tree or subtrees of user stories.
const groupTree = (op, storyRefs) => {
const {globals, err, shorten, getItemData, getCollectionData} = op;
if (storyRefs.length && ! globals.isError) {
const firstRef = shorten('userstory', 'hierarchicalrequirement', storyRefs[0]);
if (! globals.isError) {
// Get data on the first user story of the specified array.
return getItemData(firstRef, ['FormattedID'], ['Children', 'TestCases'])
.then(
// When the data arrive:
data => {
// FUNCTION DEFINITION START
// Processes child user stories and remaining user stories.
const groupChildrenAndSiblings = () => {
// If the user story has child user stories:
if (data.children.count) {
// Get data on them.
return getCollectionData(data.children.ref, [], [])
.then(
// When the data arrive:
children => {
// Process the child user stories.
return groupTree(op, children.map(child => child.ref))
.then(
// After they are processed, process the remaining user stories.
() => groupTree(op, storyRefs.slice(1)),
error => err(error, 'grouping test cases of child user stories')
);
},
error => err(error, 'getting data on child user stories')
);
}
// Otherwise, i.e. if the user story has no child user stories:
else {
// Process the remaining user stories.
return groupTree(op, storyRefs.slice(1));
}
};
// FUNCTION DEFINITION END
// Report progress in the console if requested.
if (globals.debug) {
console.log(`Processing ${data.formattedID}`);
}
// If the user story has test cases:
if (data.testCases.count) {
// Get data on them.
return getCollectionData(data.testCases.ref, ['TestFolder'], ['TestSets'])
.then(
// When the data arrive:
cases => {
// Process the test cases sequentially.
return groupCases(op, cases)
.then(
// After they are processed:
() => {
if (! globals.isError) {
// Process child user stories and the remaining user stories.
return groupChildrenAndSiblings();
}
else {
return '';
}
},
error => err(error, 'grouping test cases')
);
},
error => err(error, 'getting data on test cases')
);
}
// Otherwise, i.e. if the user story has no test cases:
else {
// Process child user stories and the remaining user stories.
return groupChildrenAndSiblings();
}
},
error => err(error, 'getting data on user story')
);
}
else {
return Promise.resolve('');
}
}
else {
return Promise.resolve('');
}
};
exports.groupHandle = groupHandle;
exports.groupTree = groupTree;