Adding two tests for command parser.

* Test 1: quotes appear at start of command  and has a space in
* Test 2: quote appears in a command, and has a space.
This commit is contained in:
Paul Kinlan 2020-04-14 03:30:17 +01:00
parent b35eeba94c
commit 917880e1b7
4 changed files with 64 additions and 20 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules node_modules
dist dist
/.nyc_output /.nyc_output
.DS_Store

View File

@ -46,38 +46,54 @@ const FS = ({
}; };
const parseArgs = (command) => { const parseArgs = (command) => {
let args = []; const args = [];
let nextDelimiter = 0; let nextDelimiter = 0;
let prevDelimiter = 0; let prevDelimiter = 0;
while ((nextDelimiter = command.indexOf(' ', prevDelimiter)) >= 0) { while ((nextDelimiter = command.indexOf(' ', prevDelimiter)) >= 0) {
let arg = command.substring(prevDelimiter, nextDelimiter) let arg = command.substring(prevDelimiter, nextDelimiter);
let quoteIndex = arg.indexOf('\'');
let doubleQuoteIndex = arg.indexOf('"');
if (arg[0] === '\'' || arg[0] === '\"') { if (quoteIndex === 0 || doubleQuoteIndex === 0) {
/* The argument has a quote at the start i.e, 'id=0,streams=0 id=1,streams=1' */
const delimiter = arg[0]; const delimiter = arg[0];
const endDelimiter = command.indexOf(delimeter, prevDelimiter + 1); const endDelimiter = command.indexOf(delimiter, prevDelimiter + 1);
if (endDelimiter < 0) throw `Bad command espcape sequence ${delimeter} near ${nextDelimiter}` if (endDelimiter < 0) {
throw new Error(`Bad command escape sequence ${delimiter} near ${nextDelimiter}`);
}
arg = command.substring(prevDelimiter + 1, endDelimiter); arg = command.substring(prevDelimiter + 1, endDelimiter);
prevDelimiter = endDelimiter + 2; prevDelimiter = endDelimiter + 2;
args.push(arg);
} else if (quoteIndex > 0 || doubleQuoteIndex > 0) {
/* The argument has a quote in it, it must be ended correctly i,e. title='test' */
if (quoteIndex === -1) quoteIndex = Infinity;
if (doubleQuoteIndex === -1) doubleQuoteIndex = Infinity;
const delimiter = (quoteIndex < doubleQuoteIndex) ? '\'' : '"';
const endDelimiter = command.indexOf(delimiter, prevDelimiter + Math.min(quoteIndex, doubleQuoteIndex) + 1);
if (endDelimiter < 0) {
throw new Error(`Bad command escape sequence ${delimiter} near ${nextDelimiter}`);
} }
else {
arg = command.substring(prevDelimiter, endDelimiter + 1);
prevDelimiter = endDelimiter + 2;
args.push(arg);
} else if (arg !== '') {
args.push(arg);
prevDelimiter = nextDelimiter + 1;
} else {
prevDelimiter = nextDelimiter + 1; prevDelimiter = nextDelimiter + 1;
if (arg === "") {
continue;
} }
} }
args.push(arg) if (prevDelimiter !== command.length) {
} args.push(command.substring(prevDelimiter));
if (prevDelimiter != command.length) {
args.push(command.substring(prevDelimiter))
} }
return args; return args;
} };
const run = ({ const run = ({
payload: { payload: {

View File

@ -4,8 +4,10 @@ const IS_BROWSER = typeof window !== 'undefined' && typeof window.document !== '
const OPTIONS = { const OPTIONS = {
corePath: '../node_modules/@ffmpeg/core/ffmpeg-core.js', corePath: '../node_modules/@ffmpeg/core/ffmpeg-core.js',
...(IS_BROWSER ? { workerPath: '../dist/worker.dev.js' } : {}), ...(IS_BROWSER ? { workerPath: '../dist/worker.dev.js' } : {}),
logger: ({ message }) => console.log(message),
}; };
const FLAME_MP4_LENGTH = 100374; const FLAME_MP4_LENGTH = 100374;
const META_FLAME_MP4_LENGTH = 100408;
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports = { module.exports = {
@ -14,5 +16,6 @@ if (typeof module !== 'undefined') {
IS_BROWSER, IS_BROWSER,
OPTIONS, OPTIONS,
FLAME_MP4_LENGTH, FLAME_MP4_LENGTH,
META_FLAME_MP4_LENGTH,
}; };
} }

View File

@ -18,3 +18,27 @@ describe('transcode()', () => {
)); ));
}); });
}); });
describe('run()', () => {
describe('should run a command with quoted parameters at start and a space in between', () => {
['flame.avi'].forEach((name) => (
it(`run ${name}`, async () => {
await worker.write(name, `${BASE_URL}/${name}`);
await worker.run(`-y -i ${name} -metadata 'title="my title"' output.mp4`);
const { data } = await worker.read('output.mp4');
expect(data.length).to.be(META_FLAME_MP4_LENGTH);
}).timeout(TIMEOUT)
));
});
describe('should run a command with name quoted parameters and a space in between', () => {
['flame.avi'].forEach((name) => (
it(`run ${name}`, async () => {
await worker.write(name, `${BASE_URL}/${name}`);
await worker.run(`-y -i ${name} -metadata title="my title" output.mp4`);
const { data } = await worker.read('output.mp4');
expect(data.length).to.be(META_FLAME_MP4_LENGTH);
}).timeout(TIMEOUT)
));
});
});