From 94cc5d6b4f8a3a271feecb0db91ac53deb6ae2f8 Mon Sep 17 00:00:00 2001 From: Aaron Bauman Date: Fri, 28 Aug 2020 17:05:14 -0400 Subject: [PATCH 1/2] Re-add max-depth param --- README.md | 2 ++ index.js | 2 ++ lib/crawl.js | 4 +++ test/fixtures/max-depth-1.json | 34 +++++++++++++++++++++ test/fixtures/max-depth-2.json | 54 ++++++++++++++++++++++++++++++++ test/integration.js | 56 ++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 test/fixtures/max-depth-1.json create mode 100644 test/fixtures/max-depth-2.json diff --git a/README.md b/README.md index 6f0ab79..c677351 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ $ npm install --global backstop-crawl --limit-similar[=3] Limits the number of similar URLs to a set number Defaults to 3 e.g /blog/1, /blog/2, /blog/3 + --max-depth Sets the maximum crawl depth. + Defaults to 0 (no limit) --reference-url Allows a reference URL to be used in testing Examples diff --git a/index.js b/index.js index c708c76..a9e6fdd 100755 --- a/index.js +++ b/index.js @@ -30,6 +30,8 @@ const cli = meow( --limit-similar[=3] Limits the number of similar URLs to a set number Defaults to 3 e.g /blog/1, /blog/2, /blog/3 + --max-depth Sets the maximum crawl depth. + Defaults to 0 (no limit) --reference-url Allows a reference URL to be used in testing Examples diff --git a/lib/crawl.js b/lib/crawl.js index e525ac7..6de5611 100644 --- a/lib/crawl.js +++ b/lib/crawl.js @@ -55,6 +55,10 @@ function crawl(url, flags) { crawler.ignoreInvalidSSL = true; } + if (flags.maxDepth) { + crawler.maxDepth = flags.maxDepth; + } + if (flags.outfile) { outfile = flags.outfile; } diff --git a/test/fixtures/max-depth-1.json b/test/fixtures/max-depth-1.json new file mode 100644 index 0000000..0d18b50 --- /dev/null +++ b/test/fixtures/max-depth-1.json @@ -0,0 +1,34 @@ +{ + "viewports": [ + { + "name": "Screen", + "width": 1440, + "height": 900 + } + ], + "scenarios": [ + { + "label": "/", + "url": "http://0.0.0.0:8080/", + "selectors": [ + "document" + ], + "hideSelectors": [ + "iframe" + ] + } + ], + "paths": { + "bitmaps_reference": "backstop_data/bitmaps_reference", + "bitmaps_test": "backstop_data/bitmaps_test", + "casper_scripts": "backstop_data/casper_scripts", + "html_report": "backstop_data/html_report", + "ci_report": "backstop_data/ci_report" + }, + "casperFlags": [], + "engine": "phantomjs", + "report": [ + "browser" + ], + "debug": false +} diff --git a/test/fixtures/max-depth-2.json b/test/fixtures/max-depth-2.json new file mode 100644 index 0000000..0d360f7 --- /dev/null +++ b/test/fixtures/max-depth-2.json @@ -0,0 +1,54 @@ +{ + "viewports": [ + { + "name": "Screen", + "width": 1440, + "height": 900 + } + ], + "scenarios": [ + { + "label": "/", + "url": "http://0.0.0.0:8080/", + "selectors": [ + "document" + ], + "hideSelectors": [ + "iframe" + ] + }, + { + "label": "/test1.html", + "url": "http://0.0.0.0:8080/test1.html", + "selectors": [ + "document" + ], + "hideSelectors": [ + "iframe" + ] + }, + { + "label": "/blog/", + "url": "http://0.0.0.0:8080/blog/", + "selectors": [ + "document" + ], + "hideSelectors": [ + "iframe" + ] + } + ], + "paths": { + "bitmaps_reference": "backstop_data/bitmaps_reference", + "bitmaps_test": "backstop_data/bitmaps_test", + "casper_scripts": "backstop_data/casper_scripts", + "html_report": "backstop_data/html_report", + "ci_report": "backstop_data/ci_report" + }, + "casperFlags": [], + "engine": "phantomjs", + "report": [ + "browser" + ], + "debug": false +} diff --git a/test/integration.js b/test/integration.js index 8a287f6..304f3cc 100644 --- a/test/integration.js +++ b/test/integration.js @@ -208,3 +208,59 @@ test('Can limit similar urls lower than default (4)', async t => { ); t.deepEqual(file, expected); }); + +test('Default usage', async t => { + const [file, expected] = await getFiles( + './backstop.json', + './fixtures/default-test.json' + ); + return t.deepEqual(file, expected); +}); + +test('Using max depth zero yields same results as default', async t => { + await execa(crawl, [ + 'http://0.0.0.0:8080', + '--max-depth=0' + ]); + const [file, expected] = await getFiles( + './backstop.json', + './fixtures/default-test.json' + ); + return t.deepEqual(file, expected); +}); + +test('Using max depth one includes only the top level.', async t => { + await execa(crawl, [ + 'http://0.0.0.0:8080', + '--max-depth=1' + ]); + const [file, expected] = await getFiles( + './backstop.json', + './fixtures/max-depth-1.json' + ); + return t.deepEqual(file, expected); +}); + +test('Using max depth two includes only two levels.', async t => { + await execa(crawl, [ + 'http://0.0.0.0:8080', + '--max-depth=2' + ]); + const [file, expected] = await getFiles( + './backstop.json', + './fixtures/max-depth-2.json' + ); + return t.deepEqual(file, expected); +}); + +test('Using max depth three yields same results as default.', async t => { + await execa(crawl, [ + 'http://0.0.0.0:8080', + '--max-depth=3' + ]); + const [file, expected] = await getFiles( + './backstop.json', + './fixtures/default-test.json' + ); + return t.deepEqual(file, expected); +}); From 78ed8c81536c3b75940983c88d87005c6afcf811 Mon Sep 17 00:00:00 2001 From: Aaron Bauman Date: Fri, 28 Aug 2020 17:15:20 -0400 Subject: [PATCH 2/2] Add input check from b21480a69501e8d4ac05c1881c8edb5b1ba8833b --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index a9e6fdd..296bb21 100755 --- a/index.js +++ b/index.js @@ -51,6 +51,15 @@ if (cli.flags.limitSimilar) { } } +if (cli.flags.maxDepth) { + if (!Number.isInteger(cli.flags.maxDepth)) { + console.error( + `> Error: "${cli.flags.maxDepth}" isn't a valid depth` + ); + process.exit(1); + } +} + if (cli.flags.referenceUrl) { if (!validurl(cli.flags.referenceUrl)) {