<commit_before>/* global describe, it, before, after */

import 'should';
import depcheck from '../src/index';
import fs from 'fs';
import path from 'path';

import importListParser from './fake_parsers/importList';
import exceptionDetector from './fake_detectors/exception';
import dependDetector from './fake_detectors/dependCallExpression';

describe('depcheck', () => {
  const spec = fs.readFileSync(__dirname + '/spec.json', { encoding: 'utf8' });
  const testCases = JSON.parse(spec);

  testCases.forEach(testCase => {
    it('should ' + testCase.name, done => {
      const testPath = path.resolve('test/fake_modules/' + testCase.module);
      const options = testCase.options;
      const expected = testCase.expected;

      depcheck(testPath, options, result => {
        result.dependencies.should.eql(expected.dependencies);
        result.devDependencies.should.eql(expected.devDependencies);
        done();
      });
    });
  });

  it('should ignore bad javascript', done => {
    const absolutePath = path.resolve('test/fake_modules/bad_js');

    depcheck(absolutePath, {}, unused => {
      unused.dependencies.should.deepEqual(['optimist']);

      const invalidFiles = Object.keys(unused.invalidFiles);
      invalidFiles.should.have.length(1);
      invalidFiles[0].should.endWith('/test/fake_modules/bad_js/index.js');

      const error = unused.invalidFiles[invalidFiles[0]];
      error.should.be.instanceof(SyntaxError);

      done();
    });
  });

  it('should allow dynamic package metadata', done => {
    const absolutePath = path.resolve('test/fake_modules/bad');

    depcheck(absolutePath, {
      'package': {
        'dependencies': {
          'optimist': '~0.6.0',
          'express': '^4.0.0',
        },
      },
    }, unused => {
      unused.dependencies.should.deepEqual(['optimist', 'express']);
      done();
    });
  });

  function testAccessUnreadableDirectory(
    module, unreadable, unusedDeps, unusedDevDeps) {
    const modulePath = path.resolve(__dirname, 'fake_modules', module);
    const unreadablePath = path.resolve(modulePath, unreadable);

    before(done => fs.mkdir(unreadablePath, '0000', done));

    it('should capture error', done =>
      depcheck(modulePath, {}, unused => {
        unused.dependencies.should.deepEqual(unusedDeps);
        unused.devDependencies.should.deepEqual(unusedDevDeps);

        const invalidDirs = Object.keys(unused.invalidDirs);
        invalidDirs.should.deepEqual([unreadablePath]);

        const error = unused.invalidDirs[invalidDirs[0]];
        error.should.be.instanceof(Error);
        error.toString().should.containEql('EACCES');

        done();
      }));

    after(done =>
      fs.chmod(unreadablePath, '0700', error =>
        error ? done(error) : fs.rmdir(unreadablePath, done)));
  }

  describe('access unreadable directory', () =>
    testAccessUnreadableDirectory(
      'unreadable',
      'unreadable',
      ['unreadable'],
      []));

  describe('access deep unreadable directory', () =>
    testAccessUnreadableDirectory(
      'unreadable_deep',
      'deep/nested/unreadable',
      [],
      []));

  function testAccessUnreadableFile(
    module, unreadable, unusedDeps, unusedDevDeps) {
    const modulePath = path.resolve(__dirname, 'fake_modules', module);
    const unreadablePath = path.resolve(modulePath, unreadable);

    before(done => fs.writeFile(unreadablePath, '', { mode: 0 }, done));

    it('should capture error', done =>
      depcheck(modulePath, {}, unused => {
        unused.dependencies.should.deepEqual(unusedDeps);
        unused.devDependencies.should.deepEqual(unusedDevDeps);

        const invalidFiles = Object.keys(unused.invalidFiles);
        invalidFiles.should.deepEqual([unreadablePath]);

        const error = unused.invalidFiles[invalidFiles[0]];
        error.should.be.instanceof(Error);
        error.toString().should.containEql('EACCES');

        done();
      }));

    after(done =>
      fs.chmod(unreadablePath, '0700', error =>
        error ? done(error) : fs.unlink(unreadablePath, done)));
  }

  describe('access unreadable file', () =>
    testAccessUnreadableFile(
      'unreadable',
      'unreadable.js',
      ['unreadable'],
      []));

  function testCustomPluggableComponents(module, options) {
    return depcheck(
      path.resolve('test/fake_modules', module),
      options,
      unused => {
        unused.dependencies.should.deepEqual([]);
        unused.devDependencies.should.deepEqual([]);
<commit_msg>For the path.resolve, relative to __dirname.<commit_after>/* global describe, it, before, after */

import 'should';
import depcheck from '../src/index';
import fs from 'fs';
import path from 'path';

import importListParser from './fake_parsers/importList';
import exceptionDetector from './fake_detectors/exception';
import dependDetector from './fake_detectors/dependCallExpression';

describe('depcheck', () => {
  const spec = fs.readFileSync(__dirname + '/spec.json', { encoding: 'utf8' });
  const testCases = JSON.parse(spec);

  testCases.forEach(testCase => {
    it('should ' + testCase.name, done => {
      const testPath = path.resolve(__dirname, 'fake_modules', testCase.module);
      const options = testCase.options;
      const expected = testCase.expected;

      depcheck(testPath, options, result => {
        result.dependencies.should.eql(expected.dependencies);
        result.devDependencies.should.eql(expected.devDependencies);
        done();
      });
    });
  });

  it('should ignore bad javascript', done => {
    const absolutePath = path.resolve(__dirname, 'fake_modules/bad_js');

    depcheck(absolutePath, {}, unused => {
      unused.dependencies.should.deepEqual(['optimist']);

      const invalidFiles = Object.keys(unused.invalidFiles);
      invalidFiles.should.have.length(1);
      invalidFiles[0].should.endWith('/test/fake_modules/bad_js/index.js');

      const error = unused.invalidFiles[invalidFiles[0]];
      error.should.be.instanceof(SyntaxError);

      done();
    });
  });

  it('should allow dynamic package metadata', done => {
    const absolutePath = path.resolve(__dirname, 'fake_modules/bad');

    depcheck(absolutePath, {
      'package': {
        'dependencies': {
          'optimist': '~0.6.0',
          'express': '^4.0.0',
        },
      },
    }, unused => {
      unused.dependencies.should.deepEqual(['optimist', 'express']);
      done();
    });
  });

  function testAccessUnreadableDirectory(
    module, unreadable, unusedDeps, unusedDevDeps) {
    const modulePath = path.resolve(__dirname, 'fake_modules', module);
    const unreadablePath = path.resolve(modulePath, unreadable);

    before(done => fs.mkdir(unreadablePath, '0000', done));

    it('should capture error', done =>
      depcheck(modulePath, {}, unused => {
        unused.dependencies.should.deepEqual(unusedDeps);
        unused.devDependencies.should.deepEqual(unusedDevDeps);

        const invalidDirs = Object.keys(unused.invalidDirs);
        invalidDirs.should.deepEqual([unreadablePath]);

        const error = unused.invalidDirs[invalidDirs[0]];
        error.should.be.instanceof(Error);
        error.toString().should.containEql('EACCES');

        done();
      }));

    after(done =>
      fs.chmod(unreadablePath, '0700', error =>
        error ? done(error) : fs.rmdir(unreadablePath, done)));
  }

  describe('access unreadable directory', () =>
    testAccessUnreadableDirectory(
      'unreadable',
      'unreadable',
      ['unreadable'],
      []));

  describe('access deep unreadable directory', () =>
    testAccessUnreadableDirectory(
      'unreadable_deep',
      'deep/nested/unreadable',
      [],
      []));

  function testAccessUnreadableFile(
    module, unreadable, unusedDeps, unusedDevDeps) {
    const modulePath = path.resolve(__dirname, 'fake_modules', module);
    const unreadablePath = path.resolve(modulePath, unreadable);

    before(done => fs.writeFile(unreadablePath, '', { mode: 0 }, done));

    it('should capture error', done =>
      depcheck(modulePath, {}, unused => {
        unused.dependencies.should.deepEqual(unusedDeps);
        unused.devDependencies.should.deepEqual(unusedDevDeps);

        const invalidFiles = Object.keys(unused.invalidFiles);
        invalidFiles.should.deepEqual([unreadablePath]);

        const error = unused.invalidFiles[invalidFiles[0]];
        error.should.be.instanceof(Error);
        error.toString().should.containEql('EACCES');

        done();
      }));

    after(done =>
      fs.chmod(unreadablePath, '0700', error =>
        error ? done(error) : fs.unlink(unreadablePath, done)));
  }

  describe('access unreadable file', () =>
    testAccessUnreadableFile(
      'unreadable',
      'unreadable.js',
      ['unreadable'],
      []));

  function testCustomPluggableComponents(module, options) {
    return depcheck(
      path.resolve(__dirname, 'fake_modules', module),
      options,
      unused => {
        unused.dependencies.should.deepEqual([]);
        unused.devDependencies.should.deepEqual([]);

{'fraction_non_alphanumeric': 0.14501045219496095, 'fraction_numerical': 0.005501155242600946, 'mean_word_length': 3.3285714285714287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '2169511', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2564, 'n_words': 461}