From 194fa056f2910d220226a4cfa5713f22c5a6df88 Mon Sep 17 00:00:00 2001 From: sparshithNR Date: Sun, 17 Mar 2019 10:51:44 -0700 Subject: [PATCH] CLI: Avoid directory scanning for arguments that are known files Check if the argument is path to a simple known file, if so do not traverse the entire folder for that glob only to find self. Closes https://github.com/qunitjs/qunit/pull/1385. Co-authored-by: Robert Jackson --- src/cli/utils.js | 22 ++++++++++++++++------ test/cli/fixtures/expected/tap-outputs.js | 18 +++++++++--------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/cli/utils.js b/src/cli/utils.js index d6522bf24..79cf0f085 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -54,26 +54,36 @@ function findFiles( baseDir, options ) { } function getFilesFromArgs( args ) { - let globs = args.slice(); + const globs = args.slice(); // Default to files in the test directory if ( !globs.length ) { globs.push( "test/**/*.js" ); } + const files = []; + const filteredGlobs = []; + // For each of the potential globs, we check if it is a directory path and // update it so that it matches the JS files in that directory. - globs = globs.map( glob => { + globs.forEach( glob => { const stat = existsStat( glob ); - if ( stat && stat.isDirectory() ) { - return `${glob}/**/*.js`; + if ( stat && stat.isFile() ) { + + // Remember known files to avoid (slow) directory-wide glob scanning. + // https://github.com/qunitjs/qunit/pull/1385 + files.push( glob ); + } else if ( stat && stat.isDirectory() ) { + filteredGlobs.push( `${glob}/**/*.js` ); } else { - return glob; + filteredGlobs.push( glob ); } } ); - const files = findFiles( process.cwd(), { match: globs } ); + if ( filteredGlobs.length ) { + files.push.apply( files, findFiles( process.cwd(), { match: filteredGlobs } ) ); + } if ( !files.length ) { error( "No files were found matching: " + args.join( ", " ) ); diff --git a/test/cli/fixtures/expected/tap-outputs.js b/test/cli/fixtures/expected/tap-outputs.js index 598e289d7..568b28813 100644 --- a/test/cli/fixtures/expected/tap-outputs.js +++ b/test/cli/fixtures/expected/tap-outputs.js @@ -33,9 +33,9 @@ ok 1 Single > has a test "qunit single.js double.js": `TAP version 13 -ok 1 Double > has a test -ok 2 Double > has another test -ok 3 Single > has a test +ok 1 Single > has a test +ok 2 Double > has a test +ok 3 Double > has another test 1..3 # pass 3 # skip 0 @@ -71,9 +71,9 @@ not ok 1 Throws match > bad "qunit test single.js 'glob/**/*-test.js'": `TAP version 13 -ok 1 A-Test > derp -ok 2 Nested-Test > herp -ok 3 Single > has a test +ok 1 Single > has a test +ok 2 A-Test > derp +ok 3 Nested-Test > herp ok 4 First > 1 ok 5 Second > 1 1..5 @@ -85,10 +85,10 @@ ok 5 Second > 1 "qunit --seed 's33d' test single.js 'glob/**/*-test.js'": `Running tests with seed: s33d TAP version 13 ok 1 Second > 1 -ok 2 Single > has a test +ok 2 Nested-Test > herp ok 3 First > 1 -ok 4 Nested-Test > herp -ok 5 A-Test > derp +ok 4 A-Test > derp +ok 5 Single > has a test 1..5 # pass 5 # skip 0