|
| 1 | +import * as path from 'node:path' |
| 2 | +import * as os from 'node:os' |
| 3 | +import * as fs from 'node:fs' |
| 4 | +import {callScriptFile, resolveScriptFilePath} from '../src/script-file' |
| 5 | + |
| 6 | +describe('resolveScriptFilePath', () => { |
| 7 | + test('rejects file:// protocol', () => { |
| 8 | + expect(() => resolveScriptFilePath('file:///some/path.js')).toThrow( |
| 9 | + '"script-file" must not use the "file://" protocol' |
| 10 | + ) |
| 11 | + }) |
| 12 | + |
| 13 | + test('returns absolute path as-is', () => { |
| 14 | + const abs = '/absolute/path/to/script.js' |
| 15 | + expect(resolveScriptFilePath(abs)).toEqual(abs) |
| 16 | + }) |
| 17 | + |
| 18 | + test('resolves relative path against GITHUB_WORKSPACE when set', () => { |
| 19 | + const original = process.env['GITHUB_WORKSPACE'] |
| 20 | + process.env['GITHUB_WORKSPACE'] = '/workspace' |
| 21 | + try { |
| 22 | + expect(resolveScriptFilePath('scripts/run.js')).toEqual( |
| 23 | + '/workspace/scripts/run.js' |
| 24 | + ) |
| 25 | + } finally { |
| 26 | + if (original === undefined) { |
| 27 | + delete process.env['GITHUB_WORKSPACE'] |
| 28 | + } else { |
| 29 | + process.env['GITHUB_WORKSPACE'] = original |
| 30 | + } |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + test('resolves relative path against cwd when GITHUB_WORKSPACE is unset', () => { |
| 35 | + const original = process.env['GITHUB_WORKSPACE'] |
| 36 | + delete process.env['GITHUB_WORKSPACE'] |
| 37 | + try { |
| 38 | + expect(resolveScriptFilePath('scripts/run.js')).toEqual( |
| 39 | + path.resolve(process.cwd(), 'scripts/run.js') |
| 40 | + ) |
| 41 | + } finally { |
| 42 | + if (original !== undefined) { |
| 43 | + process.env['GITHUB_WORKSPACE'] = original |
| 44 | + } |
| 45 | + } |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +describe('callScriptFile', () => { |
| 50 | + let tmpDir: string |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-script-test-')) |
| 54 | + }) |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + fs.rmSync(tmpDir, {recursive: true, force: true}) |
| 58 | + }) |
| 59 | + |
| 60 | + test('calls the exported function with args', async () => { |
| 61 | + const scriptPath = path.join(tmpDir, 'script.js') |
| 62 | + fs.writeFileSync( |
| 63 | + scriptPath, |
| 64 | + 'module.exports = async ({core}) => core.value' |
| 65 | + ) |
| 66 | + |
| 67 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 68 | + const result = await callScriptFile(scriptPath, require, { |
| 69 | + core: {value: 42} |
| 70 | + } as never) |
| 71 | + |
| 72 | + expect(result).toEqual(42) |
| 73 | + }) |
| 74 | + |
| 75 | + test('forwards all injected args to the exported function', async () => { |
| 76 | + const scriptPath = path.join(tmpDir, 'all-args.js') |
| 77 | + fs.writeFileSync( |
| 78 | + scriptPath, |
| 79 | + `module.exports = async (args) => Object.keys(args).sort()` |
| 80 | + ) |
| 81 | + |
| 82 | + const args = { |
| 83 | + github: {}, |
| 84 | + octokit: {}, |
| 85 | + getOctokit: () => null, |
| 86 | + context: {}, |
| 87 | + core: {}, |
| 88 | + exec: {}, |
| 89 | + glob: {}, |
| 90 | + io: {}, |
| 91 | + require: require, |
| 92 | + __original_require__: require |
| 93 | + } |
| 94 | + |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 96 | + const result = await callScriptFile(scriptPath, require, args as never) |
| 97 | + |
| 98 | + expect(result).toEqual(Object.keys(args).sort()) |
| 99 | + }) |
| 100 | + |
| 101 | + test('throws when file does not export a function', async () => { |
| 102 | + const scriptPath = path.join(tmpDir, 'not-a-fn.js') |
| 103 | + fs.writeFileSync(scriptPath, 'module.exports = 42') |
| 104 | + |
| 105 | + await expect( |
| 106 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 107 | + callScriptFile(scriptPath, require, {} as never) |
| 108 | + ).rejects.toThrow('"script-file" must export a function, got number') |
| 109 | + }) |
| 110 | + |
| 111 | + test('throws when file does not exist', async () => { |
| 112 | + const scriptPath = path.join(tmpDir, 'nonexistent.js') |
| 113 | + |
| 114 | + await expect( |
| 115 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 116 | + callScriptFile(scriptPath, require, {} as never) |
| 117 | + ).rejects.toThrow() |
| 118 | + }) |
| 119 | + |
| 120 | + test('propagates rejection from the exported function', async () => { |
| 121 | + const scriptPath = path.join(tmpDir, 'throws.js') |
| 122 | + fs.writeFileSync( |
| 123 | + scriptPath, |
| 124 | + "module.exports = async () => { throw new Error('boom') }" |
| 125 | + ) |
| 126 | + |
| 127 | + await expect( |
| 128 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 129 | + callScriptFile(scriptPath, require, {} as never) |
| 130 | + ).rejects.toThrow('boom') |
| 131 | + }) |
| 132 | + |
| 133 | + test('rejects file:// path before loading', async () => { |
| 134 | + await expect( |
| 135 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 136 | + callScriptFile('file:///some/path.js', require, {} as never) |
| 137 | + ).rejects.toThrow('"script-file" must not use the "file://" protocol') |
| 138 | + }) |
| 139 | +}) |
0 commit comments