Skip to content
Myr

normalize.js

This file

Path

test/arguments/normalize.js

183 lines · 11.4 KiB · JavaScript

Last changed by

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

History of this file

Take it

Allow template strings with all methods (#933)ehmickybefore adoption8074d85

lines 1–183why, one step back
1import {join, basename} from 'node:path';
2import {fileURLToPath} from 'node:url';3import test from 'ava';4import {execa, execaSync, execaCommand, execaCommandSync, execaNode, $} from '../../index.js';5import {setFixtureDir, FIXTURES_DIR, FIXTURES_DIR_URL} from '../helpers/fixtures-dir.js';6import {foobarString} from '../helpers/input.js';78setFixtureDir();9const NOOP_PATH = join(FIXTURES_DIR, 'noop.js');1011const testFileUrl = async (t, execaMethod) => {12 const command = new URL('noop.js', FIXTURES_DIR_URL);13 const {stdout} = await execaMethod(command);14 t.is(stdout, foobarString);15};1617test('execa()\'s command argument can be a file URL', testFileUrl, execa);18test('execaSync()\'s command argument can be a file URL', testFileUrl, execaSync);19test('execaCommand()\'s command argument can be a file URL', testFileUrl, execaCommand);20test('execaCommandSync()\'s command argument can be a file URL', testFileUrl, execaCommandSync);21test('execaNode()\'s command argument can be a file URL', testFileUrl, execaNode);22test('$\'s command argument can be a file URL', testFileUrl, $);23test('$.sync\'s command argument can be a file URL', testFileUrl, $.sync);2425const testInvalidFileUrl = async (t, execaMethod) => {26 const invalidUrl = new URL('https://invalid.com');27 t.throws(() => {28 execaMethod(invalidUrl);29 }, {code: 'ERR_INVALID_URL_SCHEME'});30};3132test('execa()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execa);33test('execaSync()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaSync);34test('execaCommand()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaCommand);35test('execaCommandSync()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaCommandSync);36test('execaNode()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaNode);37test('$\'s command argument cannot be a non-file URL', testInvalidFileUrl, $);38test('$.sync\'s command argument cannot be a non-file URL', testInvalidFileUrl, $.sync);3940const testInvalidCommand = async (t, arg, execaMethod) => {41 t.throws(() => {42 execaMethod(arg);43 }, {message: /First argument must be a string or a file URL/});44};4546test('execa()\'s first argument must be defined', testInvalidCommand, undefined, execa);47test('execaSync()\'s first argument must be defined', testInvalidCommand, undefined, execaSync);48test('execaCommand()\'s first argument must be defined', testInvalidCommand, undefined, execaCommand);49test('execaCommandSync()\'s first argument must be defined', testInvalidCommand, undefined, execaCommandSync);50test('execaNode()\'s first argument must be defined', testInvalidCommand, undefined, execaNode);51test('$\'s first argument must be defined', testInvalidCommand, undefined, $);52test('$.sync\'s first argument must be defined', testInvalidCommand, undefined, $.sync);53test('execa()\'s first argument must be valid', testInvalidCommand, true, execa);54test('execaSync()\'s first argument must be valid', testInvalidCommand, true, execaSync);55test('execaCommand()\'s first argument must be valid', testInvalidCommand, true, execaCommand);56test('execaCommandSync()\'s first argument must be valid', testInvalidCommand, true, execaCommandSync);57test('execaNode()\'s first argument must be valid', testInvalidCommand, true, execaNode);58test('$\'s first argument must be valid', testInvalidCommand, true, $);59test('$.sync\'s first argument must be valid', testInvalidCommand, true, $.sync);60test('execa()\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], execa);61test('execaSync()\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], execaSync);62test('execaCommand()\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], execaCommand);63test('execaCommandSync()\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], execaCommandSync);64test('execaNode()\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], execaNode);65test('$\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], $);66test('$.sync\'s command argument must be a string or file URL', testInvalidCommand, ['command', 'arg'], $.sync);6768const testInvalidArgs = async (t, execaMethod) => {69 t.throws(() => {70 execaMethod('echo', true);71 }, {message: /Second argument must be either/});72};7374test('execa()\'s second argument must be valid', testInvalidArgs, execa);75test('execaSync()\'s second argument must be valid', testInvalidArgs, execaSync);76test('execaCommand()\'s second argument must be valid', testInvalidArgs, execaCommand);77test('execaCommandSync()\'s second argument must be valid', testInvalidArgs, execaCommandSync);78test('execaNode()\'s second argument must be valid', testInvalidArgs, execaNode);79test('$\'s second argument must be valid', testInvalidArgs, $);80test('$.sync\'s second argument must be valid', testInvalidArgs, $.sync);8182const testInvalidArgsItems = async (t, execaMethod) => {83 t.throws(() => {84 execaMethod('echo', [{}]);85 }, {message: 'Second argument must be an array of strings: [object Object]'});86};8788test('execa()\'s second argument must not be objects', testInvalidArgsItems, execa);89test('execaSync()\'s second argument must not be objects', testInvalidArgsItems, execaSync);90test('execaCommand()\'s second argument must not be objects', testInvalidArgsItems, execaCommand);91test('execaCommandSync()\'s second argument must not be objects', testInvalidArgsItems, execaCommandSync);92test('execaNode()\'s second argument must not be objects', testInvalidArgsItems, execaNode);93test('$\'s second argument must not be objects', testInvalidArgsItems, $);94test('$.sync\'s second argument must not be objects', testInvalidArgsItems, $.sync);9596const testNullByteArg = async (t, execaMethod) => {97 t.throws(() => {98 execaMethod('echo', ['a\0b']);99 }, {message: /null bytes/});100};101102test('execa()\'s second argument must not include \\0', testNullByteArg, execa);103test('execaSync()\'s second argument must not include \\0', testNullByteArg, execaSync);104test('execaCommand()\'s second argument must not include \\0', testNullByteArg, execaCommand);105test('execaCommandSync()\'s second argument must not include \\0', testNullByteArg, execaCommandSync);106test('execaNode()\'s second argument must not include \\0', testNullByteArg, execaNode);107test('$\'s second argument must not include \\0', testNullByteArg, $);108test('$.sync\'s second argument must not include \\0', testNullByteArg, $.sync);109110const testSerializeArg = async (t, arg, execaMethod) => {111 const {stdout} = await execaMethod(NOOP_PATH, [arg]);112 t.is(stdout, String(arg));113};114115test('execa()\'s arguments can be numbers', testSerializeArg, 1, execa);116test('execa()\'s arguments can be booleans', testSerializeArg, true, execa);117test('execa()\'s arguments can be NaN', testSerializeArg, Number.NaN, execa);118test('execa()\'s arguments can be Infinity', testSerializeArg, Number.POSITIVE_INFINITY, execa);119test('execa()\'s arguments can be null', testSerializeArg, null, execa);120test('execa()\'s arguments can be undefined', testSerializeArg, undefined, execa);121test('execa()\'s arguments can be bigints', testSerializeArg, 1n, execa);122test('execa()\'s arguments can be symbols', testSerializeArg, Symbol('test'), execa);123test('execaSync()\'s arguments can be numbers', testSerializeArg, 1, execaSync);124test('execaSync()\'s arguments can be booleans', testSerializeArg, true, execaSync);125test('execaSync()\'s arguments can be NaN', testSerializeArg, Number.NaN, execaSync);126test('execaSync()\'s arguments can be Infinity', testSerializeArg, Number.POSITIVE_INFINITY, execaSync);127test('execaSync()\'s arguments can be null', testSerializeArg, null, execaSync);128test('execaSync()\'s arguments can be undefined', testSerializeArg, undefined, execaSync);129test('execaSync()\'s arguments can be bigints', testSerializeArg, 1n, execaSync);130test('execaSync()\'s arguments can be symbols', testSerializeArg, Symbol('test'), execaSync);131test('execaNode()\'s arguments can be numbers', testSerializeArg, 1, execaNode);132test('execaNode()\'s arguments can be booleans', testSerializeArg, true, execaNode);133test('execaNode()\'s arguments can be NaN', testSerializeArg, Number.NaN, execaNode);134test('execaNode()\'s arguments can be Infinity', testSerializeArg, Number.POSITIVE_INFINITY, execaNode);135test('execaNode()\'s arguments can be null', testSerializeArg, null, execaNode);136test('execaNode()\'s arguments can be undefined', testSerializeArg, undefined, execaNode);137test('execaNode()\'s arguments can be bigints', testSerializeArg, 1n, execaNode);138test('execaNode()\'s arguments can be symbols', testSerializeArg, Symbol('test'), execaNode);139test('$\'s arguments can be numbers', testSerializeArg, 1, $);140test('$\'s arguments can be booleans', testSerializeArg, true, $);141test('$\'s arguments can be NaN', testSerializeArg, Number.NaN, $);142test('$\'s arguments can be Infinity', testSerializeArg, Number.POSITIVE_INFINITY, $);143test('$\'s arguments can be null', testSerializeArg, null, $);144test('$\'s arguments can be undefined', testSerializeArg, undefined, $);145test('$\'s arguments can be bigints', testSerializeArg, 1n, $);146test('$\'s arguments can be symbols', testSerializeArg, Symbol('test'), $);147test('$.sync\'s arguments can be numbers', testSerializeArg, 1, $.sync);148test('$.sync\'s arguments can be booleans', testSerializeArg, true, $.sync);149test('$.sync\'s arguments can be NaN', testSerializeArg, Number.NaN, $.sync);150test('$.sync\'s arguments can be Infinity', testSerializeArg, Number.POSITIVE_INFINITY, $.sync);151test('$.sync\'s arguments can be null', testSerializeArg, null, $.sync);152test('$.sync\'s arguments can be undefined', testSerializeArg, undefined, $.sync);153test('$.sync\'s arguments can be bigints', testSerializeArg, 1n, $.sync);154test('$.sync\'s arguments can be symbols', testSerializeArg, Symbol('test'), $.sync);155156const testInvalidOptions = async (t, execaMethod) => {157 t.throws(() => {158 execaMethod('echo', [], new Map());159 }, {message: /Last argument must be an options object/});160};161162test('execa()\'s third argument must be a plain object', testInvalidOptions, execa);163test('execaSync()\'s third argument must be a plain object', testInvalidOptions, execaSync);164test('execaCommand()\'s third argument must be a plain object', testInvalidOptions, execaCommand);165test('execaCommandSync()\'s third argument must be a plain object', testInvalidOptions, execaCommandSync);166test('execaNode()\'s third argument must be a plain object', testInvalidOptions, execaNode);167test('$\'s third argument must be a plain object', testInvalidOptions, $);168test('$.sync\'s third argument must be a plain object', testInvalidOptions, $.sync);169170const testRelativePath = async (t, execaMethod) => {171 const rootDir = basename(fileURLToPath(new URL('../..', import.meta.url)));172 const pathViaParentDir = join('..', rootDir, 'test', 'fixtures', 'noop.js');173 const {stdout} = await execaMethod(pathViaParentDir);174 t.is(stdout, foobarString);175};176177test('execa() use relative path with \'..\' chars', testRelativePath, execa);178test('execaSync() use relative path with \'..\' chars', testRelativePath, execaSync);179test('execaCommand() use relative path with \'..\' chars', testRelativePath, execaCommand);180test('execaCommandSync() use relative path with \'..\' chars', testRelativePath, execaCommandSync);181test('execaNode() use relative path with \'..\' chars', testRelativePath, execaNode);182test('$ use relative path with \'..\' chars', testRelativePath, $);183test('$.sync use relative path with \'..\' chars', testRelativePath, $.sync);