<commit_before>var assert = require('chai').assert,
    Path = require('path'),
    fs = require('fs'),
    npmPackages = require('../../lib/linters/npm-packages');

/*global describe, it*/

describe('npm-packages', function () {
    describe('in a project with an installed package listed under optionalDependencies', function () {
        var projectDirectory = Path.resolve(__dirname, 'npm-packages', 'optionalDependencies'),
            packageJsonPath = Path.resolve(projectDirectory, 'package.json'),
            packageJsonStr;

        before(function (done) {
            fs.readFile(packageJsonPath, 'utf-8', function (err, contents) {
                packageJsonStr = contents;
                done(err);
            });
        });
        it('should output zero errors', function (done) {
            npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
                assert.isArray(results);
                assert.equal(results.length, 0);
                done(err);
            });
        });
    });

    describe('in a project with no dependencies listed in package.json and no node_modules folder', function () {
        var projectDirectory = Path.resolve(__dirname, 'npm-packages', 'noDependenciesAndNoNodeModules'),
            packageJsonPath = Path.resolve(projectDirectory, 'package.json'),
            packageJsonStr;

        before(function (done) {
            fs.readFile(packageJsonPath, 'utf-8', function (err, contents) {
                packageJsonStr = contents;
                done(err);
            });
        });
        it('should output zero errors', function (done) {
            npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
                assert.isArray(results);
                assert.equal(results.length, 0);
                done(err);
            });
        });
    });

    describe('in a project with a package.json that has a dependencies property with a string value', function () {
        var projectDirectory = Path.resolve(__dirname, 'npm-packages', 'nonObjectDependencies'),
            packageJsonPath = Path.resolve(projectDirectory, 'package.json'),
            packageJsonStr;

        before(function (done) {
            fs.readFile(packageJsonPath, 'utf-8', function (err, contents) {
                packageJsonStr = contents;
                done(err);
            });
        });
        it('should output one error', function (done) {
            npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
                assert.isArray(results);
                assert.equal(results.length, 1);
                assert.equal(results[0].message, 'The dependencies property should be an object if present');
                done(err);
            });
        });
    });

    describe('in a project with a package.json that has the same package listed under dependencies and devDependencies', function () {
        var projectDirectory = Path.resolve(__dirname, 'npm-packages', 'sameDependencyListedTwice'),
            packageJsonPath = Path.resolve(projectDirectory, 'package.json'),
            packageJsonStr;

        before(function (done) {
            fs.readFile(packageJsonPath, 'utf-8', function (err, contents) {
                packageJsonStr = contents;
                done(err);
            });
        });
        it('should output two errors', function (done) {
            npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
                assert.isArray(results);
                assert.equal(results.length, 2);
                assert.equal(results[0].message, 'devDependencies: The package a was already listed in the dependencies section, and with a different version specifier (1.2.3 vs. 1.4.7)');
                assert.equal(results[1].message, 'optionalDependencies: The package b was already listed in the dependencies section');
                done(err);
            });
        });
    });

    describe('in a project with a dependency that is not installed in node_modules', function () {
        var projectDirectory = Path.resolve(__dirname, 'npm-packages', 'dependencyNotInstalled'),
            packageJsonPath = Path.resolve(projectDirectory, 'package.json'),
            packageJsonStr;

        before(function (done) {
            fs.readFile(packageJsonPath, 'utf-8', function (err, contents) {
                packageJsonStr = contents;
                done(err);
            });
        });
        it('should output one error', function (done) {
            npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
                assert.isArray(results);
                assert.equal(results.length, 1);
                assert.equal(results[0].message, 'Package b is not installed (expected version: 2.0.0).');
                done(err);
            });
        });
    });

    describe('in a project with an optional dependency that is not installed in node_modules', function () {
        var projectDirectory = Path.resolve(__dirname, 'npm-packages', 'optionalDependencyNotInstalled'),
            packageJsonPath = Path.resolve(projectDirectory, 'package.json'),
            packageJsonStr;

        before(function (done) {
            fs.readFile(packageJsonPath, 'utf-8', function (err, contents) {
                packageJsonStr = contents;
                done(err);
            });
        });
        it('should output zero errors', function (done) {
            npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
                assert.isArray(results);
                assert.equal(results.length, 0);
                done(err);
            });
        });
    });
});<commit_msg>npm-packages: Remove duplication in tests.<commit_after>var assert = require('chai').assert,
    Path = require('path'),
    fs = require('fs'),
    npmPackages = require('../../lib/linters/npm-packages');

/*global describe, it*/

function loadTestCase(testName, cb) {
    var projectDirectory = Path.resolve(__dirname, 'npm-packages', testName),
        packageJsonPath = Path.resolve(projectDirectory, 'package.json');

    fs.readFile(packageJsonPath, 'utf-8', function (err, packageJsonStr) {
        if (err) {
            return cb(err);
        }
        npmPackages(packageJsonPath, packageJsonStr, {}, function (err, results) {
            cb(err, results);
        });
    });
}

describe('npm-packages', function () {
    it('should produce no errors when an installed package is listed under optionalDependencies', function (done) {
        loadTestCase('optionalDependencies', function (err, results) {
            assert.isArray(results);
            assert.equal(results.length, 0);
            done(err);
        });
    });

    it('should produce no errors when no dependencies are listed in package.json and there is no node_modules folder', function (done) {
        loadTestCase('noDependenciesAndNoNodeModules', function (err, results) {
            assert.isArray(results);
            assert.equal(results.length, 0);
            done(err);
        });
    });

    it('should produce an error when the dependencies property has a non-object value', function (done) {
        loadTestCase('nonObjectDependencies', function (err, results) {
            assert.isArray(results);
            assert.equal(results.length, 1);
            assert.equal(results[0].message, 'The dependencies property should be an object if present');
            done(err);
        });
    });

    it('should produce errors when the same packages are listed twice in different sections', function (done) {
        loadTestCase('sameDependencyListedTwice', function (err, results) {
            assert.isArray(results);
            assert.equal(results.length, 2);
            assert.equal(results[0].message, 'devDependencies: The package a was already listed in the dependencies section, and with a different version specifier (1.2.3 vs. 1.4.7)');
            assert.equal(results[1].message, 'optionalDependencies: The package b was already listed in the dependencies section');
            done(err);
        });
    });

    it('should produce an error when a listed dependency is not installed in node_modules', function (done) {
        loadTestCase('dependencyNotInstalled', function (err, results) {
            assert.isArray(results);
            assert.equal(results.length, 1);
            assert.equal(results[0].message, 'Package b is not installed (expected version: 2.0.0).');
            done(err);
        });
    });

    it('should produce no errors when an optional dependency is not installed in node_modules', function (done) {
        loadTestCase('optionalDependencyNotInstalled', function (err, results) {
            assert.isArray(results);
            assert.equal(results.length, 0);
            done(err);
        });
    });
});
{'fraction_non_alphanumeric': 0.11854512797485406, 'fraction_numerical': 0.005051638976201167, 'mean_word_length': 2.3404574428196474, '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': '1335386', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2163, 'n_words': 592}