From 00de5046a4df6289f2d244e459c47efdb329c3fc Mon Sep 17 00:00:00 2001 From: Tamay Eser Uysal Date: Thu, 9 Jun 2022 02:43:09 +0300 Subject: [PATCH] feat: run all recording files in directory with cli --- src/CLIUtils.ts | 39 +++++++++++++++++++++++++++++-- src/cli.ts | 7 +++--- test/cli_test.ts | 41 ++++++++++++++++++++++++++++++++- test/resources/replay-fail.json | 8 +++++++ 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 test/resources/replay-fail.json diff --git a/src/CLIUtils.ts b/src/CLIUtils.ts index 472de115..ec27583b 100644 --- a/src/CLIUtils.ts +++ b/src/CLIUtils.ts @@ -15,12 +15,47 @@ */ import { parse, createRunner } from '../lib/main.js'; -import { readFileSync } from 'fs'; -import { join, isAbsolute } from 'path'; +import { readFileSync, readdirSync, lstatSync } from 'fs'; +import { join, isAbsolute, extname } from 'path'; import { pathToFileURL } from 'url'; import { cwd } from 'process'; import { PuppeteerRunnerOwningBrowserExtension } from '../lib/main.js'; +export function getJSONFilesFromFolder(path: string): string[] { + return readdirSync(path) + .filter((file) => extname(file) === '.json') + .map((file) => join(path, file)); +} + +export function getRecordingPaths( + paths: string[], + log: boolean = true +): string[] { + const recordingPaths: string[] = []; + + for (const path of paths) { + let isDirectory; + try { + isDirectory = lstatSync(path).isDirectory(); + } catch (err) { + log && console.error(`Couldn't find file/folder: ${path}`, err); + + continue; + } + + if (isDirectory) { + const filesInFolder = getJSONFilesFromFolder(path); + + if (!filesInFolder.length) + log && console.error(`There is no recordings in: ${path}`); + + recordingPaths.push(...filesInFolder); + } else recordingPaths.push(path); + } + + return recordingPaths; +} + export function getHeadlessEnvVar(headless?: string) { if (!headless) { return true; diff --git a/src/cli.ts b/src/cli.ts index e5c3de59..8bcb005f 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -16,8 +16,7 @@ import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; - -import { getHeadlessEnvVar, runFiles } from './CLIUtils.js'; +import { getHeadlessEnvVar, getRecordingPaths, runFiles } from './CLIUtils.js'; interface Arguments { files: string[]; @@ -32,7 +31,9 @@ yargs(hideBin(process.argv)) () => {}, async (argv) => { const args = argv as unknown as Arguments; - await runFiles(args.files, { + const recordingPaths = getRecordingPaths(args.files); + + await runFiles(recordingPaths, { log: true, headless: getHeadlessEnvVar( args.headless || process.env.PUPPETEER_HEADLESS diff --git a/test/cli_test.ts b/test/cli_test.ts index 4314b012..017d6850 100644 --- a/test/cli_test.ts +++ b/test/cli_test.ts @@ -14,7 +14,12 @@ limitations under the License. */ -import { runFiles, getHeadlessEnvVar } from '../src/CLIUtils.js'; +import { + runFiles, + getHeadlessEnvVar, + getRecordingPaths, + getJSONFilesFromFolder, +} from '../src/CLIUtils.js'; import { assert } from 'chai'; import path from 'path'; import url from 'url'; @@ -42,6 +47,20 @@ describe('cli', () => { ); }); + it('is not able to run', async () => { + assert.isFalse( + await runFiles([path.join(__dirname, 'resources', 'replay-fail.json')]) + ); + }); + + it('is able to run able to run folder of recordings', async () => { + const recordings = getJSONFilesFromFolder( + path.join(__dirname, 'resources') + ); + + assert.isFalse(await runFiles([...recordings])); + }); + it('is able to run successfully with an extension', async () => { assert.isTrue( await runFiles([path.join(__dirname, 'resources', 'replay.json')], { @@ -52,4 +71,24 @@ describe('cli', () => { ); }); }); + + describe('getRecordingPaths', () => { + it('is able to get recordings from a directory', () => { + const recordingsFolderPath = 'test/resources'; + const recordingPaths = getRecordingPaths([recordingsFolderPath]); + + assert.sameMembers(recordingPaths, [ + `${recordingsFolderPath}/replay.json`, + `${recordingsFolderPath}/replay-fail.json`, + ]); + }); + }); + + describe('getJSONFilesFromFolder', () => { + it('is able to return json files from a directory', () => { + const files = getJSONFilesFromFolder(path.join(__dirname, 'resources')); + + assert.isTrue(files.every((file) => file.endsWith('.json'))); + }); + }); }); diff --git a/test/resources/replay-fail.json b/test/resources/replay-fail.json new file mode 100644 index 00000000..a1897f05 --- /dev/null +++ b/test/resources/replay-fail.json @@ -0,0 +1,8 @@ +{ + "steps": [ + { + "type": "navigate", + "url": "https://www.wikipedia.org/" + } + ] +} \ No newline at end of file