-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathallure.ts
171 lines (152 loc) · 6.1 KB
/
allure.ts
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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as child_process from 'child_process'
import * as fs from 'fs/promises'
import * as path from 'path'
import { platform } from 'process'
// @ts-ignore lib provides no types
import decompress from '@xhmikosr/decompress'
// @ts-ignore lib provides no types
import decompressTargz from '@xhmikosr/decompress-targz'
import { allureReport } from './report_allure.js'
import { isFileExist } from './isFileExists.js'
export const writeExecutorJson = async (
sourceReportDir: string,
{
buildUrl,
buildOrder,
reportUrl,
runUniqueId,
}: {
buildUrl: string
runUniqueId: string
buildOrder: number
reportUrl: string
}
) => {
const dataFile = path.join(sourceReportDir, 'executor.json')
const dataJson: AllureExecutor = {
// type is required, otherwise allure fails with java.lang.NullPointerException
type: 'github',
// adds link to GitHub Actions Run
name: 'GitHub Actions',
buildName: `Run ${runUniqueId}`,
buildUrl,
// required to open previous report in TREND
reportUrl,
buildOrder,
}
await fs.writeFile(dataFile, JSON.stringify(dataJson, null, 2))
}
export const extractAllure = async (allureCliDir: string, allureArchiveName: string) => {
await decompress(allureArchiveName, '.', {
map: (file: { data: Buffer; mode: number; mtime: string; path: string; type: string }) => {
file.path = `${allureCliDir}${file.path.substring(file.path.indexOf('/'))}`
return file
},
plugins: [decompressTargz()],
})
console.log('successfully extracted', allureArchiveName, 'to', allureCliDir)
}
export const downloadAllure = async (allureRelease: string, allureArchiveName: string) => {
const downloadUrl = `https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/${allureRelease}/allure-commandline-${allureRelease}.tgz`
const res = await fetch(downloadUrl)
const blob = await res.blob()
// @ts-ignore some nodejs bug converting stream to stream
await fs.writeFile(allureArchiveName, blob.stream())
console.log('successfully downloaded allure', allureRelease)
}
export const deleteAllure = async (allureCliDir: string) => {
try {
await fs.rm(allureCliDir, { recursive: true, force: true })
} catch (err) {
console.error(err)
}
}
export const spawnAllure = async (allureCliDir: string, allureResultsDir: string, allureReportDir: string) => {
const cliArgs = ['generate', '--clean', allureResultsDir, '-o', allureReportDir]
let allureChildProcess: child_process.ChildProcess
if (platform === 'win32') {
allureChildProcess = child_process.spawn('cmd', ['/c', path.join(allureCliDir, 'bin', 'allure.bat'), ...cliArgs], {
stdio: 'inherit',
})
} else {
allureChildProcess = child_process.spawn(`${allureCliDir}/bin/allure`, cliArgs, { stdio: 'inherit' })
}
const generation = new Promise<void>((resolve, reject) => {
allureChildProcess.once('error', reject)
allureChildProcess.once('exit', (code: unknown) => (code === 0 ? resolve() : reject(code)))
})
return generation
}
export const getLastRunId = async (reportBaseDir: string) => {
const dataFile = path.join(reportBaseDir, 'lastRun.json')
if (await isFileExist(dataFile)) {
const lastRun: LastRunJson = JSON.parse((await fs.readFile(dataFile)).toString('utf-8'))
return `${lastRun.runId}_${lastRun.runTimestamp}`
} else {
return null
}
}
export const writeLastRunId = async (reportBaseDir: string, runId: number, runTimestamp: number) => {
const dataFile = path.join(reportBaseDir, 'lastRun.json')
const dataJson: LastRunJson = { runId, runTimestamp }
await fs.writeFile(dataFile, JSON.stringify(dataJson, null, 2))
}
export const updateDataJson = async (reportBaseDir: string, reportDir: string, runId: number, runUniqueId: string) => {
const summaryJson: AllureSummaryJson = JSON.parse(
(await fs.readFile(path.join(reportDir, 'widgets', 'summary.json'))).toString('utf-8')
)
const dataFile = path.join(reportBaseDir, 'data.json')
let dataJson: AllureRecord[]
if (await isFileExist(dataFile)) {
dataJson = JSON.parse((await fs.readFile(dataFile)).toString('utf-8'))
} else {
dataJson = []
}
const failedTests = summaryJson.statistic.broken + summaryJson.statistic.failed
const testResult: AllureRecordTestResult = failedTests > 0 ? 'FAIL' : summaryJson.statistic.passed > 0 ? 'PASS' : 'UNKNOWN'
const record: AllureRecord = {
runId,
runUniqueId,
testResult,
timestamp: summaryJson.time.start,
summary: {
statistic: summaryJson.statistic,
time: summaryJson.time,
},
}
dataJson.unshift(record)
await fs.writeFile(dataFile, JSON.stringify(dataJson, null, 2))
return {
testResult,
passed: summaryJson.statistic.passed,
failed: failedTests,
total: summaryJson.statistic.total,
}
}
export const getTestResultIcon = (testResult: AllureRecordTestResult) => {
if (testResult === 'PASS') {
return '✅'
}
if (testResult === 'FAIL') {
return '❌'
}
return '❔'
}
export const writeAllureListing = async (reportBaseDir: string) => fs.writeFile(path.join(reportBaseDir, 'index.html'), allureReport)
export const isAllureResultsOk = async (sourceReportDir: string) => {
const allureResultExt = ['.json', '.xml']
if (await isFileExist(sourceReportDir)) {
const listfiles = (await fs.readdir(sourceReportDir, { withFileTypes: true })).filter((d) => {
const fileName = d.name.toLowerCase()
return d.isFile() && allureResultExt.some((ext) => fileName.endsWith(ext))
})
if (listfiles.length > 0) {
return true
}
console.log('allure-results folder has no json or xml files:', sourceReportDir)
return false
}
console.log("allure-results folder doesn't exist:", sourceReportDir)
return false
}