<commit_before>/* global describe, it, context */

const { spawnSync } = require('child_process');
const path = require('path');
const exclude = require('../');

require('chai').should();

describe('testExclude', function() {
    it('should exclude the node_modules folder by default', function() {
        exclude()
            .shouldInstrument('./banana/node_modules/cat.js')
            .should.equal(false);
        exclude()
            .shouldInstrument('node_modules/cat.js')
            .should.equal(false);
    });

    it('ignores ./', function() {
        exclude()
            .shouldInstrument('./test.js')
            .should.equal(false);
        exclude()
            .shouldInstrument('./foo.test.js')
            .should.equal(false);
    });

    it('matches files in root with **/', function() {
        exclude()
            .shouldInstrument('__tests__/**')
            .should.equal(false);
    });

    it('does not instrument files outside cwd', function() {
        exclude({ include: ['../foo.js'] })
            .shouldInstrument('../foo.js')
            .should.equal(false);
    });

    it('can instrument files outside cwd if relativePath=false', function() {
        exclude({
            include: ['../foo.js'],
            relativePath: false
        })
            .shouldInstrument('../foo.js')
            .should.equal(true);
    });

    it('does not instrument files in the coverage folder by default', function() {
        exclude()
            .shouldInstrument('coverage/foo.js')
            .should.equal(false);
    });

    it('applies exclude rule ahead of include rule', function() {
        const e = exclude({
            include: ['test.js', 'foo.js'],
            exclude: ['test.js']
        });
        e.shouldInstrument('test.js').should.equal(false);
        e.shouldInstrument('foo.js').should.equal(true);
        e.shouldInstrument('banana.js').should.equal(false);
    });

    it('should handle gitignore-style excludes', function() {
        const e = exclude({
            exclude: ['dist']
        });

        e.shouldInstrument('dist/foo.js').should.equal(false);
        e.shouldInstrument('dist/foo/bar.js').should.equal(false);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('should handle gitignore-style includes', function() {
        const e = exclude({
            include: ['src']
        });

        e.shouldInstrument('src/foo.test.js').should.equal(false);
        e.shouldInstrument('src/foo.js').should.equal(true);
        e.shouldInstrument('src/foo/bar.js').should.equal(true);
    });

    it("handles folder '.' in path", function() {
        const e = exclude();
        e.shouldInstrument(
            'test/fixtures/basic/.next/dist/pages/async-props.js'
        ).should.equal(false);
    });

    it('excludes node_modules folder, even when empty exclude group is provided', function() {
        const e = exclude({
            exclude: []
        });

        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(false);
        e.shouldInstrument('node_modules/some/module/to/cover.js').should.equal(
            false
        );
        e.shouldInstrument('__tests__/a-test.js').should.equal(true);
        e.shouldInstrument('src/a.test.js').should.equal(true);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('allows node_modules folder to be included, if !node_modules is explicitly provided', function() {
        const e = exclude({
            exclude: ['!**/node_modules/**']
        });

        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(true);
        e.shouldInstrument('node_modules/some/module/to/cover.js').should.equal(
            true
        );
        e.shouldInstrument('__tests__/a-test.js').should.equal(true);
        e.shouldInstrument('src/a.test.js').should.equal(true);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('allows specific node_modules folder to be included, if !node_modules is explicitly provided', function() {
        const e = exclude({
            exclude: ['!**/node_modules/some/module/to/cover.js']
        });

        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(false);
        e.shouldInstrument('node_modules/some/module/to/cover.js').should.equal(
            true
        );
        e.shouldInstrument('__tests__/a-test.js').should.equal(true);
        e.shouldInstrument('src/a.test.js').should.equal(true);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('allows node_modules default exclusion glob to be turned off, if excludeNodeModules === false', function() {
        const e = exclude({
            excludeNodeModules: false,
            exclude: ['node_modules/**', '**/__test__/**']
        });

        e.shouldInstrument('node_modules/cat.js').should.equal(false);
        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(true);
        e.shouldInstrument(
            './banana/node_modules/__test__/cat.test.js'
        ).should.equal(false);
        e.shouldInstrument(
            './banana/node_modules/__test__/cat-test.js'
        ).should.equal(false);
        e.shouldInstrument(
            './banana/node_modules/__test__/cat.js'
        ).should.equal(false);
    });

    it('allows negated exclude patterns', function() {
        const e = exclude({
            exclude: ['foo/**', '!foo/bar.js']
        });

        e.shouldInstrument('./foo/fizz.js').should.equal(false);
        e.shouldInstrument('./foo/bar.js').should.equal(true);
    });

    it('allows negated include patterns', function() {
        const e = exclude({
            include: ['batman/**', '!batman/robin.js']
        });

        e.shouldInstrument('./batman/joker.js').should.equal(true);
        e.shouldInstrument('./batman/robin.js').should.equal(false);
    });

    it('handles extension option', function() {
        const js = exclude({
            extension: '.js'
        });

        js.shouldInstrument('file.js').should.equal(true);
        js.shouldInstrument('package.json').should.equal(false);

        const any = exclude();
        any.shouldInstrument('file.js').should.equal(true);
        any.shouldInstrument('package.json').should.equal(true);

        const multi = exclude({
            extension: ['.js', '.json']
        });
        multi.shouldInstrument('file.js').should.equal(true);
        multi.shouldInstrument('file.png').should.equal(false);
        multi.shouldInstrument('package.json').should.equal(true);<commit_msg>chore: Add test for negated exclude of file that is not included. (#320)<commit_after>/* global describe, it, context */

const { spawnSync } = require('child_process');
const path = require('path');
const exclude = require('../');

require('chai').should();

describe('testExclude', function() {
    it('should exclude the node_modules folder by default', function() {
        exclude()
            .shouldInstrument('./banana/node_modules/cat.js')
            .should.equal(false);
        exclude()
            .shouldInstrument('node_modules/cat.js')
            .should.equal(false);
    });

    it('ignores ./', function() {
        exclude()
            .shouldInstrument('./test.js')
            .should.equal(false);
        exclude()
            .shouldInstrument('./foo.test.js')
            .should.equal(false);
    });

    it('matches files in root with **/', function() {
        exclude()
            .shouldInstrument('__tests__/**')
            .should.equal(false);
    });

    it('does not instrument files outside cwd', function() {
        exclude({ include: ['../foo.js'] })
            .shouldInstrument('../foo.js')
            .should.equal(false);
    });

    it('can instrument files outside cwd if relativePath=false', function() {
        exclude({
            include: ['../foo.js'],
            relativePath: false
        })
            .shouldInstrument('../foo.js')
            .should.equal(true);
    });

    it('does not instrument files in the coverage folder by default', function() {
        exclude()
            .shouldInstrument('coverage/foo.js')
            .should.equal(false);
    });

    it('applies exclude rule ahead of include rule', function() {
        const e = exclude({
            include: ['test.js', 'foo.js'],
            exclude: ['test.js']
        });
        e.shouldInstrument('test.js').should.equal(false);
        e.shouldInstrument('foo.js').should.equal(true);
        e.shouldInstrument('banana.js').should.equal(false);
    });

    it('should handle gitignore-style excludes', function() {
        const e = exclude({
            exclude: ['dist']
        });

        e.shouldInstrument('dist/foo.js').should.equal(false);
        e.shouldInstrument('dist/foo/bar.js').should.equal(false);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('should handle gitignore-style includes', function() {
        const e = exclude({
            include: ['src']
        });

        e.shouldInstrument('src/foo.test.js').should.equal(false);
        e.shouldInstrument('src/foo.js').should.equal(true);
        e.shouldInstrument('src/foo/bar.js').should.equal(true);
    });

    it("handles folder '.' in path", function() {
        const e = exclude();
        e.shouldInstrument(
            'test/fixtures/basic/.next/dist/pages/async-props.js'
        ).should.equal(false);
    });

    it('excludes node_modules folder, even when empty exclude group is provided', function() {
        const e = exclude({
            exclude: []
        });

        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(false);
        e.shouldInstrument('node_modules/some/module/to/cover.js').should.equal(
            false
        );
        e.shouldInstrument('__tests__/a-test.js').should.equal(true);
        e.shouldInstrument('src/a.test.js').should.equal(true);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('allows node_modules folder to be included, if !node_modules is explicitly provided', function() {
        const e = exclude({
            exclude: ['!**/node_modules/**']
        });

        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(true);
        e.shouldInstrument('node_modules/some/module/to/cover.js').should.equal(
            true
        );
        e.shouldInstrument('__tests__/a-test.js').should.equal(true);
        e.shouldInstrument('src/a.test.js').should.equal(true);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('allows specific node_modules folder to be included, if !node_modules is explicitly provided', function() {
        const e = exclude({
            exclude: ['!**/node_modules/some/module/to/cover.js']
        });

        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(false);
        e.shouldInstrument('node_modules/some/module/to/cover.js').should.equal(
            true
        );
        e.shouldInstrument('__tests__/a-test.js').should.equal(true);
        e.shouldInstrument('src/a.test.js').should.equal(true);
        e.shouldInstrument('src/foo.js').should.equal(true);
    });

    it('allows node_modules default exclusion glob to be turned off, if excludeNodeModules === false', function() {
        const e = exclude({
            excludeNodeModules: false,
            exclude: ['node_modules/**', '**/__test__/**']
        });

        e.shouldInstrument('node_modules/cat.js').should.equal(false);
        e.shouldInstrument('./banana/node_modules/cat.js').should.equal(true);
        e.shouldInstrument(
            './banana/node_modules/__test__/cat.test.js'
        ).should.equal(false);
        e.shouldInstrument(
            './banana/node_modules/__test__/cat-test.js'
        ).should.equal(false);
        e.shouldInstrument(
            './banana/node_modules/__test__/cat.js'
        ).should.equal(false);
    });

    it('allows negated exclude patterns', function() {
        const e = exclude({
            exclude: ['foo/**', '!foo/bar.js']
        });

        e.shouldInstrument('./foo/fizz.js').should.equal(false);
        e.shouldInstrument('./foo/bar.js').should.equal(true);
    });

    it('allows negated include patterns', function() {
        const e = exclude({
            include: ['batman/**', '!batman/robin.js']
        });

        e.shouldInstrument('./batman/joker.js').should.equal(true);
        e.shouldInstrument('./batman/robin.js').should.equal(false);
    });

    it('negated exclude patterns only works for files that are covered by the `include` pattern', function() {
        const e = exclude({
            include: ['index.js'],
            exclude: ['!index2.js']
        });

        e.shouldInstrument('index.js').should.equal(true);
        e.shouldInstrument('index2.js').should.equal(false);
    });

    it('handles extension option', function() {
        const js = exclude({
            extension: '.js'
        });

        js.shouldInstrument('file.js').should.equal(true);
        js.shouldInstrument('package.json').should.equal(false);

        const any = exclude();
        any.shouldInstrument('file.js').should.equal(true);
        any.shouldInstrument('package.json').should.equal(true);

        const multi = exclude({
            extension: ['.js', '.json']
        });
        multi.shouldInstrument('file.js').should.equal(true);
        multi.shouldInstrument('file.png').should.equal(false);
        multi.shouldInstrument('package.json').should.equal(true);
{'fraction_non_alphanumeric': 0.1680270608132951, 'fraction_numerical': 0.00036767409368335905, 'mean_word_length': 2.3333333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4513496', 'n_tokens_mistral': 3949, 'n_tokens_neox': 3886, 'n_words': 627}