import assert from 'node:assert/strict'; import path from 'node:path'; import {fileURLToPath} from 'node:url'; import test from 'node:test'; import {assertThrows} from '../helpers/assert.js'; import { execa, execaSync, execaNode, $, } from '../../index.js'; import {setFixtureDirectory, FIXTURES_DIRECTORY_URL} from '../helpers/fixtures-directory.js'; import {foobarString} from '../helpers/input.js'; setFixtureDirectory(); const testFileUrl = async execaMethod => { const command = new URL('noop.js', FIXTURES_DIRECTORY_URL); const {stdout} = await execaMethod(command); assert.equal(stdout, foobarString); }; test('execa()\'s command argument can be a file URL', () => testFileUrl(execa)); test('execaSync()\'s command argument can be a file URL', () => testFileUrl(execaSync)); test('execaNode()\'s command argument can be a file URL', () => testFileUrl(execaNode)); test('$\'s command argument can be a file URL', () => testFileUrl($)); test('$.sync\'s command argument can be a file URL', () => testFileUrl($.sync)); const testInvalidFileUrl = async execaMethod => { const invalidUrl = new URL('https://invalid.com'); assertThrows(() => { execaMethod(invalidUrl); }, {code: 'ERR_INVALID_URL_SCHEME'}); }; test('execa()\'s command argument cannot be a non-file URL', () => testInvalidFileUrl(execa)); test('execaSync()\'s command argument cannot be a non-file URL', () => testInvalidFileUrl(execaSync)); test('execaNode()\'s command argument cannot be a non-file URL', () => testInvalidFileUrl(execaNode)); test('$\'s command argument cannot be a non-file URL', () => testInvalidFileUrl($)); test('$.sync\'s command argument cannot be a non-file URL', () => testInvalidFileUrl($.sync)); const testInvalidCommand = async (commandArgument, execaMethod) => { assertThrows(() => { execaMethod(commandArgument); }, {message: /First argument must be a string or a file URL/}); }; test('execa()\'s first argument must be defined', () => testInvalidCommand(undefined, execa)); test('execaSync()\'s first argument must be defined', () => testInvalidCommand(undefined, execaSync)); test('execaNode()\'s first argument must be defined', () => testInvalidCommand(undefined, execaNode)); test('$\'s first argument must be defined', () => testInvalidCommand(undefined, $)); test('$.sync\'s first argument must be defined', () => testInvalidCommand(undefined, $.sync)); test('execa()\'s first argument must be valid', () => testInvalidCommand(true, execa)); test('execaSync()\'s first argument must be valid', () => testInvalidCommand(true, execaSync)); test('execaNode()\'s first argument must be valid', () => testInvalidCommand(true, execaNode)); test('$\'s first argument must be valid', () => testInvalidCommand(true, $)); test('$.sync\'s first argument must be valid', () => testInvalidCommand(true, $.sync)); test('execa()\'s command argument must be a string or file URL', () => testInvalidCommand(['command', 'arg'], execa)); test('execaSync()\'s command argument must be a string or file URL', () => testInvalidCommand(['command', 'arg'], execaSync)); test('execaNode()\'s command argument must be a string or file URL', () => testInvalidCommand(['command', 'arg'], execaNode)); test('$\'s command argument must be a string or file URL', () => testInvalidCommand(['command', 'arg'], $)); test('$.sync\'s command argument must be a string or file URL', () => testInvalidCommand(['command', 'arg'], $.sync)); const testRelativePath = async execaMethod => { // @todo: use import.meta.dirname after dropping support for Node <20.11.0 const rootDirectory = path.basename(fileURLToPath(new URL('../..', import.meta.url))); const pathViaParentDirectory = path.join('..', rootDirectory, 'test', 'fixtures', 'noop.js'); const {stdout} = await execaMethod(pathViaParentDirectory); assert.equal(stdout, foobarString); }; test('execa() use relative path with \'..\' chars', () => testRelativePath(execa)); test('execaSync() use relative path with \'..\' chars', () => testRelativePath(execaSync)); test('execaNode() use relative path with \'..\' chars', () => testRelativePath(execaNode)); test('$ use relative path with \'..\' chars', () => testRelativePath($)); test('$.sync use relative path with \'..\' chars', () => testRelativePath($.sync));