<commit_before>'use strict';

var assert = require('assert');
var rimraf = require('rimraf');
var assign = require('object-assign');
var expect = require('expect.js');
var mkdirp = require('mkdirp');
var fs = require('fs');
var path = require('path');
var webpack = require('webpack');
var execSync = require('child_process').execSync;
var fsExtra = require('fs-extra');

describe('Opal loader', function(){
  var opalLoader = path.resolve(__dirname, '../');
  var outputDir = path.resolve(__dirname, './output/loader');
  var globalConfig = {
      output: {
        path: outputDir,
        filename: '[id].loader.js',
      },
      module: {
        loaders: [{ test: /\.rb$/, loader: opalLoader }],
      },
    };

  beforeEach(function(done) {
    fsExtra.copySync('./test/fixtures/dependency.rb', './test/fixtures/dependency.rb.backup', {clobber: true})
    rimraf(outputDir, function(err) {
      if (err) { return done(err); }
      mkdirp(outputDir, done);
    });
  });

  afterEach(function(done) {
    // cleanup
    fsExtra.copy('./test/fixtures/dependency.rb.backup', './test/fixtures/dependency.rb', {clobber: true}, done)
  })

  it("loads correctly", function (done) {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/basic.js'
    });
    webpack(config, function(err, stats) {
      expect(err).to.be(null);

      fs.readdir(outputDir, function(err, files) {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), function(err, data) {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

          return done();
        });
      })
    });
  });

  it("reloads dependencies properly", function (done) {
    this.timeout(6000)
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/requires.js'
    });
    webpack(config, function(err, stats) {
      expect(err).to.be(null)
      fs.writeFileSync('./test/fixtures/dependency.rb', 'HELLO=456')
      setTimeout(function() {
        fs.readdir(outputDir, function(err, files) {
          expect(err).to.be(null);
          expect(files.length).to.equal(1);
          fs.readFile(path.resolve(outputDir, files[0]), function(err, data) {
            var subject = data.toString();

            expect(err).to.be(null);
            expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

            return done();
          })
        })
      }, 3000)
    });
  });

  it("loads requires correctly", function (done) {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/requires.js'
    });
    webpack(config, function(err, stats) {
      expect(err).to.be(null);

      fs.readdir(outputDir, function(err, files) {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), function(err, data) {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

          return done();
        });
      })
    });
  });

  it("loads require_tree", function (done) {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/tree.js'
    });
    webpack(config, function(err, stats) {
      expect(err).to.be(null);

      fs.readdir(outputDir, function(err, files) {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), function(err, data) {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'THERE', 456\)/);

          return done();
        });
      })
    });
  });

  it("loads require_relative", function (done) {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/relative.js'
    });
    webpack(config, function(err, stats) {
      expect(err).to.be(null);

      fs.readdir(outputDir, function(err, files) {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), function(err, data) {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

          return done();
        });
      })
    });
  });

  it("outputs correct source maps", function (done) {
    this.timeout(10000);
    execSync("bundle exec opal -c -e \"require 'opal'\" > test/output/loader/opal.js");

    const config = assign({}, globalConfig, {
      entry: './test/fixtures/source_maps.js',
      devtool: 'source-map'
    });
    webpack(config, function(err, stats) {
      expect(err).to.be(null);

      fs.readdir(outputDir, function(err, files) {
        expect(err).to.be(null);
        expect(files.length).to.equal(3);
        var output = execSync("node -r ./test/output/loader/opal.js -r ./test/fixtures/load_source_maps.js ./test/output/loader/0.loader.js 2>&1 || true").toString()
        // ruby output, might need some more work since we're 1 line off
        // expecting test/fixtures/source_maps.rb:4:in `hello': source map location (RuntimeError)
        expect(output).to.match(/test\/output\/loader\/webpack:\/test\/fixtures\/source_maps\.rb:3:1\)/)
        expect(output).to.match(/test\/output\/loader\/webpack:\/test\/fixtures\/source_maps.rb:7:1\)/)
        return done();
      })
    });
  });
});<commit_msg>DRY up code, use arrow functions when possible<commit_after>'use strict';

const assert = require('assert');
const rimraf = require('rimraf');
const assign = require('object-assign');
const expect = require('expect.js');
const mkdirp = require('mkdirp');
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const execSync = require('child_process').execSync;
const fsExtra = require('fs-extra');

describe('Opal loader', function(){
  const dependencyMain = './test/fixtures/dependency.rb'
  const dependencyBackup = './test/fixtures/dependency.rb.backup'
  const opalLoader = path.resolve(__dirname, '../');
  const outputDir = path.resolve(__dirname, './output/loader');
  const globalConfig = {
      output: {
        path: outputDir,
        filename: '[id].loader.js',
      },
      module: {
        loaders: [{ test: /\.rb$/, loader: opalLoader }],
      },
    };

  beforeEach(done => {
    fsExtra.copySync(dependencyMain, dependencyBackup, {clobber: true})
    rimraf(outputDir, function(err) {
      if (err) { return done(err); }
      mkdirp(outputDir, done);
    });
  });

  afterEach(done => {
    // cleanup
    fsExtra.copy(dependencyBackup, dependencyMain, {clobber: true}, done)
  })

  it("loads correctly", done => {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/basic.js'
    });
    webpack(config, (err, stats) => {
      expect(err).to.be(null);

      fs.readdir(outputDir, (err, files) => {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), (err, data) => {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

          return done();
        });
      })
    });
  });

  it("reloads dependencies properly", function (done) {
    this.timeout(6000)
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/requires.js'
    });
    webpack(config, (err, stats) => {
      expect(err).to.be(null)
      fs.writeFileSync(dependencyMain, 'HELLO=456')
      setTimeout(() => {
        fs.readdir(outputDir, (err, files) => {
          expect(err).to.be(null);
          expect(files.length).to.equal(1);
          fs.readFile(path.resolve(outputDir, files[0]), (err, data) => {
            var subject = data.toString();

            expect(err).to.be(null);
            expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

            return done();
          })
        })
      }, 3000)
    });
  });

  it("loads requires correctly", done =>{
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/requires.js'
    });
    webpack(config, (err, stats) => {
      expect(err).to.be(null);

      fs.readdir(outputDir, (err, files) => {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), (err, data) => {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

          return done();
        });
      })
    });
  });

  it("loads require_tree", done => {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/tree.js'
    });
    webpack(config, (err, stats) => {
      expect(err).to.be(null);

      fs.readdir(outputDir, (err, files) => {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), (err, data) => {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'THERE', 456\)/);

          return done();
        });
      })
    });
  });

  it("loads require_relative", done => {
    const config = assign({}, globalConfig, {
      entry: './test/fixtures/relative.js'
    });
    webpack(config, (err, stats) => {
      expect(err).to.be(null);

      fs.readdir(outputDir, (err, files) => {
        expect(err).to.be(null);
        expect(files.length).to.equal(1);
        fs.readFile(path.resolve(outputDir, files[0]), (err, data) => {
          var subject = data.toString();

          expect(err).to.be(null);
          expect(subject).to.match(/Opal\.cdecl\(\$scope, 'HELLO', 123\)/);

          return done();
        });
      })
    });
  });

  it("outputs correct source maps", function (done) {
    this.timeout(10000);
    execSync("bundle exec opal -c -e \"require 'opal'\" > test/output/loader/opal.js");

    const config = assign({}, globalConfig, {
      entry: './test/fixtures/source_maps.js',
      devtool: 'source-map'
    });
    webpack(config, (err, stats) => {
      expect(err).to.be(null);

      fs.readdir(outputDir, (err, files) => {
        expect(err).to.be(null);
        expect(files.length).to.equal(3);
        var output = execSync("node -r ./test/output/loader/opal.js -r ./test/fixtures/load_source_maps.js ./test/output/loader/0.loader.js 2>&1 || true").toString()
        // ruby output, might need some more work since we're 1 line off
        // expecting test/fixtures/source_maps.rb:4:in `hello': source map location (RuntimeError)
        expect(output).to.match(/test\/output\/loader\/webpack:\/test\/fixtures\/source_maps\.rb:3:1\)/)
        expect(output).to.match(/test\/output\/loader\/webpack:\/test\/fixtures\/source_maps.rb:7:1\)/)
        return done();
      })
    });
  });
});
{'fraction_non_alphanumeric': 0.18952684742158427, 'fraction_numerical': 0.009569377990430622, 'mean_word_length': 2.550487574709028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '468945', 'n_tokens_mistral': 3753, 'n_tokens_neox': 3593, 'n_words': 629}