Glob Pattern for all files in directory and files within sub directories (recursively)
Asked Answered
F

1

6

I am working on setting up test runner tasks for our project and ran into something unexpected. Our task wasn't getting any of the files using the below pattern:

'/tests/**/*.spec.js'

Our directory structure is like this:

--Scripts
     --app
     --tests
         -test.spec.js

     -gulp.js
     -karma.conf.js
     etc.

However, this was bring forward 0 test cases. When I changed the pattern to this, instead:

'/tests/*.spec.js'

Then it works as expected. Now, I know the /**/*spec.js is a pattern for "files that end with 'spec.js' in zero or more sub directories". But, it feels like there should be a simple pattern to say "all files that end with 'spec.js' in this directory and in zero or more sub-directories". Otherwise, I know have to use both patterns if my structure looks like this:

--Scripts
     --app
     --tests
         -test.spec.js
         --subTests
             -test2.spec.js
     -gulp.js
     -karma.conf.js
     etc.

The above would require my gulp task to look something like below:

gulp.task('unit-test', ['lint'], function (done) {
    gulputil.log('Running unit tests...');
    //return gulp.src(testFilesDirect).pipe(jasmine({ verbose: true, includeStackTrace: true }));
    new KarmaServer({
        configFile: __dirname + '/karma.conf.js',
        files: ['/tests/*.spec.js', '/tests/**/*.spec.js'],
        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false
    }, done).start();
});

Is that correct or am I missing something fundamental that wouldn't require two test patterns?

There is another article that talks about symlinks here: Gulp - Target all files in a folder and its subfolders

But that seems to be directed towards a more localized setup whereas my situation needs to be part of an automated build system, additionally, those solutions involved Linux or Mac and our systems are Windows based.

Frayda answered 25/4, 2016 at 15:52 Comment(3)
Could be a NodeJS or Gulp version issue? I set up a glob in the same structure you have and it was able to stream with both files with just '/scripts/**/*.spec.js'. This is on node v5.6.0 and gulp 3.9.0Anapest
Nice thought, I just checked my versions: Gulp 3.9.0 and Node v5.7.0....Frayda
So frustrating that this never got a good answer!Vexatious
B
0

I ran into the same issue. I think that what you want is:

'/tests/{*.spec.js,**/*.spec.js}'

This would expand into 2 paths, where the first gives you files in the /test/ directory and the 2nd gives you files in subdirectories:

/tests/*.spec.js
/tests/**/*.spec.js

I agree that there should be an even simpler way for this common pattern, but this is still pretty compact.

Bergeman answered 12/9, 2024 at 8:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.