forked from Shelex/cypress-allure-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.js
117 lines (110 loc) · 4.26 KB
/
writer.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
const path = require('path');
const fs = require('fs');
const uuid = require('uuid');
function allureWriter(on, config) {
// pass allure config from Cypress.env to process.env
// to get access from node context
process.env.allure = config.env.allure;
process.env.allureResultsPath =
config.env.allureResultsPath || 'allure-results';
on('task', {
writeAllureResults: ({ resultsDir, writer }) => {
const {
groups,
tests,
attachments,
envInfo,
categories,
executorInfo
} = writer;
try {
!fs.existsSync(resultsDir) &&
fs.mkdirSync(resultsDir, { recursive: true });
groups &&
groups.forEach((group) => {
if (group.children.length) {
const fileName = `${group.uuid}-container.json`;
const groupResultPath = path.join(
resultsDir,
fileName
);
!fs.existsSync(groupResultPath) &&
fs.writeFileSync(
groupResultPath,
JSON.stringify(group)
);
}
});
tests &&
tests.forEach((test) => {
const fileName = `${test.uuid}-result.json`;
const testResultPath = path.join(resultsDir, fileName);
!fs.existsSync(testResultPath) &&
fs.writeFileSync(
testResultPath,
JSON.stringify(test)
);
});
if (attachments) {
for (let [name, content] of Object.entries(attachments)) {
const attachmentPath = path.join(resultsDir, name);
!fs.existsSync(attachmentPath) &&
fs.writeFileSync(attachmentPath, content, {
encoding: 'binary'
});
}
}
writeInfoFile('categories.json', categories, resultsDir);
writeInfoFile('executor.json', executorInfo, resultsDir);
writeInfoFile('environment.properties', envInfo, resultsDir);
} catch (e) {
process.stdout.write(
`error while writing allure results: ${e}`
);
} finally {
return null;
}
}
});
on('after:screenshot', (details) => {
const resultsDir = process.env.allureResultsPath;
// As after:screenshot is an event when screenshot file is saved
// sometimes (when saving was after test finished)
// we cannot access Cypress or Cypress.Allure context here
if (process.env.allure === 'true') {
!fs.existsSync(resultsDir) &&
fs.mkdirSync(resultsDir, { recursive: true });
const allurePath = path.join(
resultsDir,
`${uuid.v4()}-attachment.png`
);
return new Promise((resolve, reject) => {
fs.copyFile(details.path, allurePath, (err) => {
if (err) {
return reject(err);
}
resolve({ path: allurePath });
});
});
}
});
}
const writeInfoFile = (fileName, data, resultsDir) => {
if (data) {
const isEnvProps = fileName === 'environment.properties';
isEnvProps &&
(data = Object.keys(data)
.map((key) => `${key}=${data[key]}`)
.join('\n'));
const filePath = path.join(resultsDir, fileName);
!fs.existsSync(filePath) &&
fs.writeFileSync(
filePath,
isEnvProps ? data : JSON.stringify(data),
{
encoding: 'binary'
}
);
}
};
module.exports = allureWriter;