question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Excluding files from preprocessor

See original GitHub issue

In my current code base I keep all files (including tests) for a module/component in the same folder, e.g.

menu

The problem is that I want test coverage for the module. Before I moved the specs into the module folder I used the following preprocessor:

preprocessors = {
    '**/resources/javascript/**/*.js': 'coverage'
};

Naturally that donโ€™t work anymore since it also includes all my tests (thereby increasing my code coverage metrics by quite a bit). As all the tests are named *Spec.js it should be quite simple to exclude them, but I canโ€™t seem to find a way given how the preprocessors are currently specified.

Excluding stuff when globbing appears to be quite difficult. Would it be possible to give the preprocessor an array of files instead? That would enable me to do the exclusion logic myself. Any thoughts on how this can be solved?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:1
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
lazdcommented, Jun 3, 2014

Hereโ€™s a complex example that combines a number of techniques found in various threads on the topic.

Imagine we have the following extermely contrived folder structure:

โ””โ”€โ”€ src/
    โ”œโ”€โ”€ index.js
    โ”œโ”€โ”€ index.js.text
    โ”œโ”€โ”€ index.test.js
    โ”œโ”€โ”€ README.txt
    โ”œโ”€โ”€ startuptest.js
    โ”œโ”€โ”€ lib/
    |   โ”œโ”€โ”€ util.js
    |   โ”œโ”€โ”€ util.test.js
    |   โ””โ”€โ”€ filters/
    |       โ”œโ”€โ”€ kalman.js
    |       โ””โ”€โ”€ lowpass.js
    โ”œโ”€โ”€ test/
    |   โ”œโ”€โ”€ main.js
    |   โ””โ”€โ”€ lib/
    |       โ”œโ”€โ”€ filters.js
    |       โ””โ”€โ”€ util.js
    โ””โ”€โ”€ vendor/
        โ”œโ”€โ”€ jquery.js
        โ””โ”€โ”€ three/
            โ”œโ”€โ”€ three.js
            โ””โ”€โ”€ three.fps.js

And weโ€™d like to preprocess all files that end with .js in src/ and its subdirectories, except those under src/vendor/ and src/test/ or any file in any folder containing .test. Phew!

So, we create a pattern that uses brace expansion to become two patterns, including a negation for the .test string and a .js to match only JavaScript files:

{src,src/!(test|vendor)/**}/!(*.test).js

This results in:

src/!(*test).js                     // Files in src/ that don't contain .test and end in .js
src/!(test|vendor)/**/!(*.test).js  // Files in all subfolders of src/ except test/ or vendor/ that don't contain .test and end in .js

Hereโ€™s a test for this pattern:

var Minimatch = require('minimatch').Minimatch;
var mm = new Minimatch('{src,src/!(test|vendor)/**}/!(*.test).js');

console.log(mm.match('src/index.js'), 'Should match files in src/ folder');
console.log(mm.match('src/startuptest.js'), 'Should match files in src/ folder with test in their name');
console.log(mm.match('src/lib/util.js'), 'Should match files in src/lib/ folder');
console.log(mm.match('src/lib/filters/kalman.js'), 'Should match files src/lib/ subfolders');
console.log(!mm.match('src/index.js.txt'), 'Should NOT match files in src/ folder that contain .js but do not end in it');
console.log(!mm.match('src/README.txt'), 'Should NOT match non .js files in src/ folder');
console.log(!mm.match('src/index.test.js'), 'Should NOT match files in src/ folder with .test in their names');
console.log(!mm.match('src/lib/util.test.js'), 'Should NOT match files in src/lib/ subfolders with .test in their names');
console.log(!mm.match('src/test/main.js'), 'Should NOT match files in src/test/ folder');
console.log(!mm.match('src/test/lib/filters.js'), 'Should NOT match files in src/test/ folder with .test in their names');
console.log(!mm.match('src/vendor/jquery.js'), 'Should NOT match files in src/vendor/ folder');
console.log(!mm.match('src/vendor/three/three.js'), 'Should NOT match files in src/vendor/ subfolders');

It works, but itโ€™s not pretty to look at. I hope this helps someone.

11reactions
thepiancommented, Feb 13, 2016

I always found it odd how some people instinctively separate implementation and tests into src and test directories, or other split out assets. They are usually closely related and you end up with parallel hierarchies. Then you need IDE support to relate them again on the assumption that test and source is called the same which is pushes you towards writing a test for each class / function which leads to box ticking rather than thinking about the problem at hand. Tools should get out of the way and let you structure a project in a way that works for the problem and project at hand.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I exclude files from karma code coverage report?
Another trick can be to use the ! operator to exclude specific paths: preprocessors: { // source files, that you wanna generate coverage...
Read more >
proper pattern for preprocessor directory exclusion
I'm looking for a pattern that will exclude everything in the test directory, but will instrument all the other files in the module...
Read more >
Exclude statements inside C header files
If the IBMยฎ Informixยฎ ESQL/C preprocessor has problems with certain statements in C header files, you can exclude specific lines from the preprocessing...
Read more >
Excluding files, not a pattern but a specific filename - fpm
It is for compilation. It is a library inside my code that I do not want to touch it but it contains duplicated...
Read more >
The C Preprocessor
Exactly like -include , except that any output produced by scanning file is thrown away. Macros it defines remain defined. This allows you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found