|
| 1 | +const assert = require('assert'); |
| 2 | +const fs = require('fs'); |
| 3 | +const os = require('os'); |
| 4 | +const path = require('path'); |
| 5 | +const { spawnSync } = require('child_process'); |
| 6 | + |
| 7 | +const repoRoot = path.resolve(__dirname, '../../..'); |
| 8 | +const checkDiffScript = path.join(repoRoot, '.github/scripts/check-diff-async.mjs'); |
| 9 | +const countAsyncScript = path.join(repoRoot, '.github/scripts/count-async-functions.mjs'); |
| 10 | + |
| 11 | +function run(command, args, cwd, extraEnv = {}) { |
| 12 | + const result = spawnSync(command, args, { |
| 13 | + cwd, |
| 14 | + env: { ...process.env, ...extraEnv }, |
| 15 | + encoding: 'utf8', |
| 16 | + }); |
| 17 | + assert.strictEqual(result.status, 0, `command failed: ${command} ${args.join(' ')}\n${result.stderr}`); |
| 18 | + return result; |
| 19 | +} |
| 20 | + |
| 21 | +function initTempRepo() { |
| 22 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cloudserver-script-tests-')); |
| 23 | + run('git', ['init'], dir); |
| 24 | + run('git', ['config', 'user.name', 'Copilot Test'], dir); |
| 25 | + run('git', ['config', 'user.email', 'copilot-test@example.com'], dir); |
| 26 | + |
| 27 | + fs.writeFileSync(path.join(dir, 'README.md'), 'test\n'); |
| 28 | + run('git', ['add', 'README.md'], dir); |
| 29 | + run('git', ['commit', '-m', 'init'], dir); |
| 30 | + return dir; |
| 31 | +} |
| 32 | + |
| 33 | +function runNodeScript(scriptPath, cwd, extraEnv = {}) { |
| 34 | + return spawnSync(process.execPath, [scriptPath], { |
| 35 | + cwd, |
| 36 | + env: { ...process.env, ...extraEnv }, |
| 37 | + encoding: 'utf8', |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +describe('CI async migration scripts', () => { |
| 42 | + const tempDirs = []; |
| 43 | + |
| 44 | + after(() => { |
| 45 | + tempDirs.forEach(dir => fs.rmSync(dir, { recursive: true, force: true })); |
| 46 | + }); |
| 47 | + |
| 48 | + it('check-diff-async exits successfully when no JS files changed', () => { |
| 49 | + const dir = initTempRepo(); |
| 50 | + tempDirs.push(dir); |
| 51 | + |
| 52 | + const result = runNodeScript(checkDiffScript, dir); |
| 53 | + assert.strictEqual(result.status, 0, result.stderr); |
| 54 | + assert(result.stdout.includes('No changed JS files to check.')); |
| 55 | + }); |
| 56 | + |
| 57 | + it('check-diff-async ignores non-source JS changes', () => { |
| 58 | + const dir = initTempRepo(); |
| 59 | + tempDirs.push(dir); |
| 60 | + |
| 61 | + fs.mkdirSync(path.join(dir, 'tests/unit'), { recursive: true }); |
| 62 | + fs.writeFileSync(path.join(dir, 'tests/unit/newTest.js'), 'module.exports = () => {};\n'); |
| 63 | + run('git', ['add', 'tests/unit/newTest.js'], dir); |
| 64 | + |
| 65 | + const result = runNodeScript(checkDiffScript, dir); |
| 66 | + assert.strictEqual(result.status, 0, result.stderr); |
| 67 | + assert(result.stdout.includes('No source JS files in diff')); |
| 68 | + }); |
| 69 | + |
| 70 | + it('check-diff-async fails on newly added callback-style function', () => { |
| 71 | + const dir = initTempRepo(); |
| 72 | + tempDirs.push(dir); |
| 73 | + |
| 74 | + fs.mkdirSync(path.join(dir, 'lib'), { recursive: true }); |
| 75 | + fs.writeFileSync(path.join(dir, 'lib/newFile.js'), [ |
| 76 | + 'function badStyle(param, cb) {', |
| 77 | + ' return cb(null, param);', |
| 78 | + '}', |
| 79 | + '', |
| 80 | + ].join('\n')); |
| 81 | + run('git', ['add', 'lib/newFile.js'], dir); |
| 82 | + |
| 83 | + const result = runNodeScript(checkDiffScript, dir); |
| 84 | + assert.strictEqual(result.status, 1); |
| 85 | + assert(result.stderr.includes('function has callback parameter')); |
| 86 | + assert(result.stderr.includes('lib/newFile.js')); |
| 87 | + }); |
| 88 | + |
| 89 | + it('count-async-functions runs and prints summary', function countAsyncTest() { |
| 90 | + this.timeout(120000); |
| 91 | + const result = runNodeScript(countAsyncScript, repoRoot); |
| 92 | + |
| 93 | + assert.strictEqual(result.status, 0, result.stderr); |
| 94 | + assert(result.stdout.includes('=== Async/Await Migration Progress ===')); |
| 95 | + assert(result.stdout.includes('Total functions:')); |
| 96 | + }); |
| 97 | +}); |
0 commit comments