Merge branch 'main' into copilot/macos-self-hosted-runners-fix

This commit is contained in:
Bruno Borges
2026-09-09 02:20:38 -04:00
committed by GitHub
10 changed files with 226 additions and 8 deletions
+87
View File
@@ -0,0 +1,87 @@
import {afterAll, beforeAll, describe, expect, it} from '@jest/globals';
import {spawnSync} from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {fileURLToPath, pathToFileURL} from 'url';
const dist = fileURLToPath(new URL('../dist/', import.meta.url));
let tempDir: string;
let linkedDist: string;
beforeAll(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-java-entrypoints-'));
linkedDist = path.join(tempDir, 'linked # dist');
fs.symlinkSync(dist, linkedDist, 'junction');
});
afterAll(() => {
fs.rmSync(tempDir, {recursive: true, force: true});
});
function execute(args: string[], input?: string) {
return spawnSync(process.execPath, args, {
encoding: 'utf8',
input,
timeout: 10000,
env: {
PATH: process.env.PATH,
SystemRoot: process.env.SystemRoot
}
});
}
describe.each([
['setup', 1, 'java-version or java-version-file input expected'],
['cleanup', 0, '']
] as const)('%s entrypoint', (name, exitCode, output) => {
it.each(['direct', 'symlink', 'preserved symlink'])(
'executes through a %s path',
mode => {
const entry = path.join(
mode === 'direct' ? dist : linkedDist,
name,
'index.js'
);
const args =
mode === 'preserved symlink'
? ['--preserve-symlinks-main', entry]
: [entry];
const result = execute(args);
expect(result.error).toBeUndefined();
expect(result.status).toBe(exitCode);
expect(result.stderr).toBe('');
expect(result.stdout).not.toContain('skipping the execution');
if (output) {
expect(result.stdout).toContain(output);
} else {
expect(result.stdout).toBe('');
}
}
);
it.each(['eval', 'stdin', 'file'])(
'does not execute when imported from %s',
mode => {
const moduleUrl = pathToFileURL(path.join(dist, name, 'index.js')).href;
const source = `const {run} = await import(${JSON.stringify(moduleUrl)}); console.log(typeof run);`;
const importer = path.join(tempDir, `${name}-importer.mjs`);
fs.writeFileSync(importer, source);
const args =
mode === 'file'
? [importer]
: mode === 'eval'
? ['--input-type=module', '-e', source]
: ['--input-type=module', '-'];
const result = execute(args, mode === 'stdin' ? source : undefined);
expect(result.error).toBeUndefined();
expect(result.status).toBe(0);
expect(result.stderr).toBe('');
expect(result.stdout).toContain('skipping the execution');
expect(result.stdout).toContain('function');
expect(result.stdout).not.toContain('::error::');
}
);
});
+52
View File
@@ -0,0 +1,52 @@
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
import fs from 'fs';
import {isMainModule} from '../src/is-main-module.js';
describe('main module detection', () => {
const originalArgv = process.argv;
beforeEach(() => {
process.argv = [process.execPath, 'entrypoint.js'];
});
afterEach(() => {
process.argv = originalArgv;
jest.restoreAllMocks();
});
it.each([undefined, '-'])(
'skips filesystem access when argv[1] is %s',
entrypoint => {
process.argv =
entrypoint === undefined
? [process.execPath]
: [process.execPath, entrypoint];
const realpath = jest.spyOn(fs, 'realpathSync');
expect(isMainModule(import.meta.url)).toBe(false);
expect(realpath).not.toHaveBeenCalled();
}
);
it.each(['ENOENT', 'ENOTDIR'])(
'treats a non-file entrypoint returning %s as an import',
code => {
jest.spyOn(fs, 'realpathSync').mockImplementation(() => {
throw Object.assign(new Error('No file-based entrypoint'), {code});
});
expect(isMainModule(import.meta.url)).toBe(false);
}
);
it('propagates unexpected filesystem errors', () => {
const error = Object.assign(new Error('Permission denied'), {
code: 'EACCES'
});
jest.spyOn(fs, 'realpathSync').mockImplementation(() => {
throw error;
});
expect(() => isMainModule(import.meta.url)).toThrow(error);
});
});
@@ -27,6 +27,7 @@ jest.unstable_mockModule('@actions/core', () => ({
jest.unstable_mockModule('fs', () => ({
default: {
...jest.requireActual<typeof import('fs')>('fs'),
readFileSync: jest.fn()
}
}));
+1
View File
@@ -27,6 +27,7 @@ jest.unstable_mockModule('@actions/core', () => ({
jest.unstable_mockModule('fs', () => ({
default: {
...jest.requireActual<typeof import('fs')>('fs'),
readFileSync: jest.fn()
}
}));