<commit_before>'use strict';

var assert = require('assert');
var babel = require('babel-core');
var fs = require('fs');
var path = require('path');
var rewire = require('rewire');
var sinon = require('sinon');

var preset = rewire('../index');
var rewireRevert;

module.exports = {
  setUp: function(done) {
    sinon.stub(console, 'warn');
    rewireRevert = preset.__set__('resolve', function(file) {
      if (file === 'fromMain') {
        return path.resolve('node_modules', 'fromMain/lib/index.js');
      } else if (file === 'fromMain/package.json') {
        throw new Error();
      } else if (file === 'bar') {
        return path.resolve('node_modules', 'bar/lib/index.js');
      } else if (file === 'bar/package.json') {
        return path.resolve('test/fixtures/mockPackage.json');
      } else {
        return path.resolve('node_modules', file);
      }
    });
    done();
  },

  tearDown: function(done) {
    console.warn.restore();
    rewireRevert();
    done();
  },

  testNpmImport: function(test) {
    var code = 'import foo from "bar/lib/foo";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});

    assert.notStrictEqual(-1, result.code.indexOf('../../../../node_modules/bar/lib/foo'));
    assert.strictEqual('../../../../node_modules/bar/lib/foo', result.metadata.modules.imports[0].source);
    test.done();
  },

  testNpmImportFromMain: function(test) {
    var code = 'import fromMain from "fromMain";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});

    assert.notStrictEqual(-1, result.code.indexOf('../../../../node_modules/fromMain/lib/index'));
    assert.strictEqual('../../../../node_modules/fromMain/lib/index', result.metadata.modules.imports[0].source);
    test.done();
  },

  testNpmImportFromBrowser: function(test) {
    sinon.stub(fs, 'existsSync').returns(true);

    var code = 'import bar from "bar";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});
    fs.existsSync.restore();

    assert.notStrictEqual(-1, result.code.indexOf('../../../../node_modules/bar/src/index'));
    assert.strictEqual('../../../../node_modules/bar/src/index', result.metadata.modules.imports[0].source);
    test.done();
  },

  testNpmImportMissingSourceFilename: function(test) {
    var code = 'import foo from "bar/lib/foo";';
    var result = babel.transform(code, {presets: [preset]});

    assert.strictEqual(1, console.warn.callCount);
    assert.notStrictEqual(-1, result.code.indexOf(path.resolve('node_modules', '/bar/lib/foo')));
    assert.strictEqual(path.resolve('node_modules', 'bar/lib/foo'), result.metadata.modules.imports[0].source);<commit_msg>Adds test case for when an npm module is not found<commit_after>'use strict';

var assert = require('assert');
var babel = require('babel-core');
var fs = require('fs');
var path = require('path');
var rewire = require('rewire');
var sinon = require('sinon');

var preset = rewire('../index');
var rewireRevert;

module.exports = {
  setUp: function(done) {
    sinon.stub(console, 'warn');
    rewireRevert = preset.__set__('resolve', function(file) {
      if (file === 'fromMain') {
        return path.resolve('node_modules', 'fromMain/lib/index.js');
      } else if (file === 'fromMain/package.json' || file === 'unknown') {
        throw new Error();
      } else if (file === 'bar') {
        return path.resolve('node_modules', 'bar/lib/index.js');
      } else if (file === 'bar/package.json') {
        return path.resolve('test/fixtures/mockPackage.json');
      } else {
        return path.resolve('node_modules', file);
      }
    });
    done();
  },

  tearDown: function(done) {
    console.warn.restore();
    rewireRevert();
    done();
  },

  testNpmImport: function(test) {
    var code = 'import foo from "bar/lib/foo";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});

    assert.notStrictEqual(-1, result.code.indexOf('../../../../node_modules/bar/lib/foo'));
    assert.strictEqual('../../../../node_modules/bar/lib/foo', result.metadata.modules.imports[0].source);
    test.done();
  },

  testNpmImportFromMain: function(test) {
    var code = 'import fromMain from "fromMain";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});

    assert.notStrictEqual(-1, result.code.indexOf('../../../../node_modules/fromMain/lib/index'));
    assert.strictEqual('../../../../node_modules/fromMain/lib/index', result.metadata.modules.imports[0].source);
    test.done();
  },

  testNpmImportFromBrowser: function(test) {
    sinon.stub(fs, 'existsSync').returns(true);

    var code = 'import bar from "bar";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});
    fs.existsSync.restore();

    assert.notStrictEqual(-1, result.code.indexOf('../../../../node_modules/bar/src/index'));
    assert.strictEqual('../../../../node_modules/bar/src/index', result.metadata.modules.imports[0].source);
    test.done();
  },

  testNpmUnknownImport: function(test) {
    var code = 'import foo from "unknown";';
    var result = babel.transform(code, {presets: [preset], filename: path.resolve('src/x/y/z/foo.js')});

    assert.notStrictEqual(-1, result.code.indexOf('unknown'));
    assert.strictEqual('../../../../unknown', result.metadata.modules.imports[0].source);
    assert.strictEqual(1, console.warn.callCount);
    test.done();
  },

  testNpmImportMissingSourceFilename: function(test) {
    var code = 'import foo from "bar/lib/foo";';
    var result = babel.transform(code, {presets: [preset]});

    assert.strictEqual(1, console.warn.callCount);
    assert.notStrictEqual(-1, result.code.indexOf(path.resolve('node_modules', '/bar/lib/foo')));
    assert.strictEqual(path.resolve('node_modules', 'bar/lib/foo'), result.metadata.modules.imports[0].source);
{'fraction_non_alphanumeric': 0.20136122177954846, 'fraction_numerical': 0.0034860557768924302, 'mean_word_length': 3.492915734526473, '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': '4757291', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1959, 'n_words': 339}