-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess-junit-results
executable file
·87 lines (75 loc) · 2.07 KB
/
postprocess-junit-results
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
#!/usr/bin/env node
const yargs = require('yargs')
const argv = yargs
.usage('Usage: $0 [options]')
.help('help')
.option('search-folder', {
string: true,
demandOption: true,
requiresArg: true,
describe: 'Directory to search for JUnit XML results'
})
.option('test-results-files', {
string: true,
demandOption: true,
requiresArg: true,
describe: 'Glob that matches JUnit XML files within searchFolder'
})
.wrap(yargs.terminalWidth())
.argv
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const cheerio = require('cheerio')
function discoverTestFiles() {
return new Promise((resolve, reject) => {
glob(argv.testResultsFiles, {cwd: argv.searchFolder}, (err, paths) => {
if (err) {
reject(err)
} else {
resolve(paths)
}
})
})
}
async function postProcessJUnitXML(junitXmlPath) {
const fullPath = path.resolve(argv.searchFolder, junitXmlPath)
const friendlyName = path.basename(junitXmlPath, '.xml').replace(/-+/g, ' ')
console.log(`${fullPath}: loading`)
const original = await new Promise((resolve, reject) => {
fs.readFile(fullPath, {encoding: 'utf8'}, (err, content) => {
if (err) {
reject(err)
} else {
resolve(content)
}
})
})
const $ = cheerio.load(original, { xmlMode: true })
$('testcase').attr('name', (i, oldName) => `[${friendlyName}] ${oldName}`)
const modified = $.xml()
await new Promise((resolve, reject) => {
fs.writeFile(fullPath, modified, {encoding: 'utf8'}, err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
console.log(`${fullPath}: complete`)
}
;(async function() {
const testResultFiles = await discoverTestFiles()
console.log(`Post-processing ${testResultFiles.length} JUnit XML files`)
await Promise.all(
testResultFiles.map(postProcessJUnitXML)
)
console.log(`${testResultFiles.length} JUnit XML files complete`)
})().then(
() => process.exit(0),
err => {
console.error(err.stack || err)
process.exit(1)
}
)