|
| 1 | +const fs = require('../util/fs'); |
| 2 | +const path = require('path'); |
| 3 | +const logger = require('../util/logger')('Xray report'); |
| 4 | +const _ = require('lodash'); |
| 5 | +const cloneDeep = require('lodash/cloneDeep'); |
| 6 | +const { stat } = require('fs'); |
| 7 | + |
| 8 | +module.exports = function (config, reporter) { |
| 9 | + const jsonReporter = cloneDeep(reporter); |
| 10 | + |
| 11 | + function toAbsolute (p) { |
| 12 | + return path.isAbsolute(p) ? p : path.join(config.projectPath, p); |
| 13 | + } |
| 14 | + |
| 15 | + function transformTestCases (testCases) { |
| 16 | + const transformedTestCases = []; |
| 17 | + |
| 18 | + for (const testName in testCases) { |
| 19 | + let testStatus = 'PASSED'; |
| 20 | + const testCase = testCases[testName]; |
| 21 | + const xrayTestResult = { |
| 22 | + 'iterations': [], |
| 23 | + 'testInfo': {} |
| 24 | + }; |
| 25 | + |
| 26 | + testCase.forEach((testedViewport) => { |
| 27 | + let { pair: { viewportLabel: name }, status } = testedViewport; |
| 28 | + |
| 29 | + status = `${status}ed`.toUpperCase(); |
| 30 | + if (status === 'FAILED') { |
| 31 | + testStatus = status; |
| 32 | + } |
| 33 | + xrayTestResult.iterations.push({ name, status }); |
| 34 | + }); |
| 35 | + |
| 36 | + xrayTestResult.status = testStatus; |
| 37 | + xrayTestResult.testInfo.requirementKeys = testCase[0].pair.metadata; |
| 38 | + xrayTestResult.testInfo.summary = testCase[0].pair.label; |
| 39 | + xrayTestResult.testInfo.type = "Generic"; |
| 40 | + transformedTestCases.push( |
| 41 | + xrayTestResult |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + return transformedTestCases; |
| 46 | + } |
| 47 | + |
| 48 | + function transformToXrayJson (json) { |
| 49 | + const results = {} |
| 50 | + const namedTestCases = _.groupBy(json, 'pair.label'); |
| 51 | + return results.tests = transformTestCases(namedTestCases); |
| 52 | + } |
| 53 | + |
| 54 | + logger.log('Writing Xray json report'); |
| 55 | + |
| 56 | + return fs.ensureDir(toAbsolute(config.json_report)).then(function () { |
| 57 | + const res = transformToXrayJson(jsonReporter.tests); |
| 58 | + |
| 59 | + return fs.writeFile(toAbsolute(config.compareJsonFileName), JSON.stringify(res, null, 2)).then( |
| 60 | + function () { |
| 61 | + logger.log('Wrote Xray Json report to: ' + toAbsolute(config.compareJsonFileName)); |
| 62 | + }, |
| 63 | + function (err) { |
| 64 | + logger.error('Failed writing Xray Json report to: ' + toAbsolute(config.compareJsonFileName)); |
| 65 | + throw err; |
| 66 | + } |
| 67 | + ); |
| 68 | + }); |
| 69 | +}; |
0 commit comments