|
| 1 | +import {afterAll, beforeAll, describe, expect, it} from '@jest/globals'; |
| 2 | +import {spawnSync} from 'child_process'; |
| 3 | +import fs from 'fs'; |
| 4 | +import os from 'os'; |
| 5 | +import path from 'path'; |
| 6 | +import {fileURLToPath, pathToFileURL} from 'url'; |
| 7 | + |
| 8 | +const dist = fileURLToPath(new URL('../dist/', import.meta.url)); |
| 9 | +let tempDir: string; |
| 10 | +let linkedDist: string; |
| 11 | + |
| 12 | +beforeAll(() => { |
| 13 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-java-entrypoints-')); |
| 14 | + linkedDist = path.join(tempDir, 'linked # dist'); |
| 15 | + fs.symlinkSync(dist, linkedDist, 'junction'); |
| 16 | +}); |
| 17 | + |
| 18 | +afterAll(() => { |
| 19 | + fs.rmSync(tempDir, {recursive: true, force: true}); |
| 20 | +}); |
| 21 | + |
| 22 | +function execute(args: string[], input?: string) { |
| 23 | + return spawnSync(process.execPath, args, { |
| 24 | + encoding: 'utf8', |
| 25 | + input, |
| 26 | + timeout: 10000, |
| 27 | + env: { |
| 28 | + PATH: process.env.PATH, |
| 29 | + SystemRoot: process.env.SystemRoot |
| 30 | + } |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +describe.each([ |
| 35 | + ['setup', 1, 'java-version or java-version-file input expected'], |
| 36 | + ['cleanup', 0, ''] |
| 37 | +] as const)('%s entrypoint', (name, exitCode, output) => { |
| 38 | + it.each(['direct', 'symlink', 'preserved symlink'])( |
| 39 | + 'executes through a %s path', |
| 40 | + mode => { |
| 41 | + const entry = path.join( |
| 42 | + mode === 'direct' ? dist : linkedDist, |
| 43 | + name, |
| 44 | + 'index.js' |
| 45 | + ); |
| 46 | + const args = |
| 47 | + mode === 'preserved symlink' |
| 48 | + ? ['--preserve-symlinks-main', entry] |
| 49 | + : [entry]; |
| 50 | + const result = execute(args); |
| 51 | + |
| 52 | + expect(result.error).toBeUndefined(); |
| 53 | + expect(result.status).toBe(exitCode); |
| 54 | + expect(result.stderr).toBe(''); |
| 55 | + expect(result.stdout).not.toContain('skipping the execution'); |
| 56 | + if (output) { |
| 57 | + expect(result.stdout).toContain(output); |
| 58 | + } else { |
| 59 | + expect(result.stdout).toBe(''); |
| 60 | + } |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + it.each(['eval', 'stdin', 'file'])( |
| 65 | + 'does not execute when imported from %s', |
| 66 | + mode => { |
| 67 | + const moduleUrl = pathToFileURL(path.join(dist, name, 'index.js')).href; |
| 68 | + const source = `const {run} = await import(${JSON.stringify(moduleUrl)}); console.log(typeof run);`; |
| 69 | + const importer = path.join(tempDir, `${name}-importer.mjs`); |
| 70 | + fs.writeFileSync(importer, source); |
| 71 | + const args = |
| 72 | + mode === 'file' |
| 73 | + ? [importer] |
| 74 | + : mode === 'eval' |
| 75 | + ? ['--input-type=module', '-e', source] |
| 76 | + : ['--input-type=module', '-']; |
| 77 | + const result = execute(args, mode === 'stdin' ? source : undefined); |
| 78 | + |
| 79 | + expect(result.error).toBeUndefined(); |
| 80 | + expect(result.status).toBe(0); |
| 81 | + expect(result.stderr).toBe(''); |
| 82 | + expect(result.stdout).toContain('skipping the execution'); |
| 83 | + expect(result.stdout).toContain('function'); |
| 84 | + expect(result.stdout).not.toContain('::error::'); |
| 85 | + } |
| 86 | + ); |
| 87 | +}); |
0 commit comments