|
| 1 | +const core = require('@actions/core'); |
| 2 | +const path = require('node:path'); |
| 3 | + |
| 4 | +// Get project root directory (where package.json is) |
| 5 | +const projectRoot = path.dirname(path.dirname(__dirname)); |
| 6 | + |
| 7 | +// Store original methods |
| 8 | +const originalMethods = { |
| 9 | + info: core.info, |
| 10 | + debug: core.debug, |
| 11 | + warning: core.warning, |
| 12 | + error: core.error, |
| 13 | + setSecret: core.setSecret, |
| 14 | + setOutput: core.setOutput, |
| 15 | + setFailed: core.setFailed, |
| 16 | + notice: core.notice, |
| 17 | + startGroup: core.startGroup, |
| 18 | + endGroup: core.endGroup, |
| 19 | +}; |
| 20 | + |
| 21 | +// Store logs during test execution |
| 22 | +let logs = []; |
| 23 | + |
| 24 | +/** |
| 25 | + * Setup log suppression before each test |
| 26 | + */ |
| 27 | +const setupLogSuppression = () => { |
| 28 | + // Clear logs |
| 29 | + logs = []; |
| 30 | + |
| 31 | + // Mock core methods to capture logs instead of printing |
| 32 | + core.info = (...args) => { |
| 33 | + logs.push({ level: 'info', args }); |
| 34 | + }; |
| 35 | + |
| 36 | + core.debug = (...args) => { |
| 37 | + logs.push({ level: 'debug', args }); |
| 38 | + }; |
| 39 | + |
| 40 | + core.warning = (...args) => { |
| 41 | + logs.push({ level: 'warning', args }); |
| 42 | + }; |
| 43 | + |
| 44 | + core.error = (...args) => { |
| 45 | + logs.push({ level: 'error', args }); |
| 46 | + }; |
| 47 | + |
| 48 | + core.setSecret = () => { |
| 49 | + // Suppress ::add-mask:: output |
| 50 | + }; |
| 51 | + |
| 52 | + core.setOutput = () => { |
| 53 | + // Suppress ::set-output:: output |
| 54 | + }; |
| 55 | + |
| 56 | + core.setFailed = (...args) => { |
| 57 | + logs.push({ level: 'setFailed', args }); |
| 58 | + }; |
| 59 | + |
| 60 | + core.notice = (...args) => { |
| 61 | + logs.push({ level: 'notice', args }); |
| 62 | + }; |
| 63 | + |
| 64 | + core.startGroup = () => { |
| 65 | + // Suppress group output |
| 66 | + }; |
| 67 | + |
| 68 | + core.endGroup = () => { |
| 69 | + // Suppress group output |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Restore original methods and dump logs if test failed |
| 75 | + */ |
| 76 | +const restoreLogsOnFailure = function () { |
| 77 | + // Check if current test failed |
| 78 | + if (this.currentTest && this.currentTest.state === 'failed') { |
| 79 | + // Dump all captured logs |
| 80 | + const testName = this.currentTest.title; |
| 81 | + const testFile = path.relative(projectRoot, this.currentTest.file); |
| 82 | + console.log(`\n--- Logs from failed test: "${testName}" (${testFile}) ---`); |
| 83 | + for (const log of logs) { |
| 84 | + const message = log.args.join(' '); |
| 85 | + console.log(`[${log.level.toUpperCase()}] ${message}`); |
| 86 | + } |
| 87 | + console.log('--- End of logs ---\n'); |
| 88 | + } |
| 89 | + |
| 90 | + // Clear logs array |
| 91 | + logs = []; |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Restore original core methods |
| 96 | + */ |
| 97 | +const restoreMethods = () => { |
| 98 | + core.info = originalMethods.info; |
| 99 | + core.debug = originalMethods.debug; |
| 100 | + core.warning = originalMethods.warning; |
| 101 | + core.error = originalMethods.error; |
| 102 | + core.setSecret = originalMethods.setSecret; |
| 103 | + core.setOutput = originalMethods.setOutput; |
| 104 | + core.setFailed = originalMethods.setFailed; |
| 105 | + core.notice = originalMethods.notice; |
| 106 | + core.startGroup = originalMethods.startGroup; |
| 107 | + core.endGroup = originalMethods.endGroup; |
| 108 | +}; |
| 109 | + |
| 110 | +module.exports = { |
| 111 | + setupLogSuppression, |
| 112 | + restoreLogsOnFailure, |
| 113 | + restoreMethods, |
| 114 | +}; |
0 commit comments