<commit_before>'use strict';

var defineModule = require('../');
var gutil = require('gulp-util');
var path = require('path');
var fs = require('fs');

require('mocha');
require('should');

var fixtureFile = function(filePath) {
  var fixturesDir = path.join('test', 'fixtures');
  var fixturePath = path.join(fixturesDir, filePath);
  var file = new gutil.File({
    path: fixturePath,
    cwd: fixturesDir,
    base: path.dirname(fixturePath),
    contents: fs.readFileSync(fixturePath)
  });
  return file;
};

var streamThroughDefineModule = function(type, options, filePath, cb) {
  var stream = defineModule(type, options);
  var file = fixtureFile(filePath);
  cb(stream);
  stream.write(file);
  stream.end();
};

var fileShouldMatchExpected = function(file, filePath) {
  var expectedDir = path.join('test', 'expected');
  var expectedPath = path.join(expectedDir, filePath);
  file.contents.toString().should.equal(fs.readFileSync(expectedPath).toString());
};

describe('gulp-define-module', function() {
  describe('defineModule()', function() {

    var basic = function(type, options, variant) {
      return function(done) {
        streamThroughDefineModule(type, options, 'basic.js', function(stream) {
          stream.on('data', function(file) {
            var expectedName = 'basic_' + type;
            if (variant) { expectedName += '_' + variant; }
            fileShouldMatchExpected(file, expectedName + '.js');
            done();
          });
        });
      };
    };

    it('makes AMD modules', basic('amd'));
    it('makes CommonJS modules', basic('commonjs'));
    it('makes Node modules', basic('node'));
    it('makes plain modules', basic('plain'));

    it('throws when the the type is unsupported', function() {
      basic('invalid').should.throw(); // throws before it becomes async
    });

    var requireOptions = { require: { Library: 'library' } };
    it('hanldes require for AMD', basic('amd', requireOptions, 'require'));
    it('hanldes require for Node', basic('node', requireOptions, 'require'));
    it('ignores require for CommonJS', basic('commonjs', requireOptions));
    it('ignores require for plain', basic('plain', requireOptions));

    it('accepts wrapper option',
      basic('plain', { wrapper: 'App.TEMPLATES = TemplateWrapper(<%= contents %>)' }, 'wrapped'));

    it('accepts options.context as a string',
      basic('plain', {
        context: { customContents: '<%= contents %>' },
        wrapper: '<%= customContents %>'
      }));

    it('accepts options.context as a function',
      basic('plain', {
        context: function(context) {
          return {
            customContents: 'App.TEMPLATES = TemplateWrapper(' + context.contents + ')'
          };
        },
        wrapper: '<%= customContents %>'
      }, 'wrapped'));

    it('accepts options through incoming file', function(done) {
      var stream = defineModule('amd');
      var file = fixtureFile('basic.js');
      file.moduleOptions = requireOptions;
      stream.on('data', function(file) {
        fileShouldMatchExpected(file, 'basic_amd_require.js');
        done();
      });
      stream.write(file);
      stream.end();
    });

    describe('wrapper context', function() {
      it('defines name', basic('plain', {
        wrapper: 'App["<%= name %>"] = <%= contents %>'
      }, 'context_name'));
      it('defines file', basic('plain', {
        wrapper: 'App["<%= file.path %>"] = <%= contents %>'
      }, 'context_file'));
      it('defines contents', basic('plain', {
        wrapper: '<%= contents %>'
      }));
      it('handles nested paths for name', function(done) {
        var stream = defineModule('plain', {
          wrapper: 'App["<%= name %>"] = <%= contents %>'
        });
        var file = fixtureFile('nested/basic.js');
        stream.on('data', function(file) {
          fileShouldMatchExpected(file, 'basic_plain_context_name.js');<commit_msg>Fixed file option name to match implementation.<commit_after>'use strict';

var defineModule = require('../');
var gutil = require('gulp-util');
var path = require('path');
var fs = require('fs');

require('mocha');
require('should');

var fixtureFile = function(filePath) {
  var fixturesDir = path.join('test', 'fixtures');
  var fixturePath = path.join(fixturesDir, filePath);
  var file = new gutil.File({
    path: fixturePath,
    cwd: fixturesDir,
    base: path.dirname(fixturePath),
    contents: fs.readFileSync(fixturePath)
  });
  return file;
};

var streamThroughDefineModule = function(type, options, filePath, cb) {
  var stream = defineModule(type, options);
  var file = fixtureFile(filePath);
  cb(stream);
  stream.write(file);
  stream.end();
};

var fileShouldMatchExpected = function(file, filePath) {
  var expectedDir = path.join('test', 'expected');
  var expectedPath = path.join(expectedDir, filePath);
  file.contents.toString().should.equal(fs.readFileSync(expectedPath).toString());
};

describe('gulp-define-module', function() {
  describe('defineModule()', function() {

    var basic = function(type, options, variant) {
      return function(done) {
        streamThroughDefineModule(type, options, 'basic.js', function(stream) {
          stream.on('data', function(file) {
            var expectedName = 'basic_' + type;
            if (variant) { expectedName += '_' + variant; }
            fileShouldMatchExpected(file, expectedName + '.js');
            done();
          });
        });
      };
    };

    it('makes AMD modules', basic('amd'));
    it('makes CommonJS modules', basic('commonjs'));
    it('makes Node modules', basic('node'));
    it('makes plain modules', basic('plain'));

    it('throws when the the type is unsupported', function() {
      basic('invalid').should.throw(); // throws before it becomes async
    });

    var requireOptions = { require: { Library: 'library' } };
    it('hanldes require for AMD', basic('amd', requireOptions, 'require'));
    it('hanldes require for Node', basic('node', requireOptions, 'require'));
    it('ignores require for CommonJS', basic('commonjs', requireOptions));
    it('ignores require for plain', basic('plain', requireOptions));

    it('accepts wrapper option',
      basic('plain', { wrapper: 'App.TEMPLATES = TemplateWrapper(<%= contents %>)' }, 'wrapped'));

    it('accepts options.context as a string',
      basic('plain', {
        context: { customContents: '<%= contents %>' },
        wrapper: '<%= customContents %>'
      }));

    it('accepts options.context as a function',
      basic('plain', {
        context: function(context) {
          return {
            customContents: 'App.TEMPLATES = TemplateWrapper(' + context.contents + ')'
          };
        },
        wrapper: '<%= customContents %>'
      }, 'wrapped'));

    it('accepts options through incoming file', function(done) {
      var stream = defineModule('amd');
      var file = fixtureFile('basic.js');
      file.defineModuleOptions = requireOptions;
      stream.on('data', function(file) {
        fileShouldMatchExpected(file, 'basic_amd_require.js');
        done();
      });
      stream.write(file);
      stream.end();
    });

    describe('wrapper context', function() {
      it('defines name', basic('plain', {
        wrapper: 'App["<%= name %>"] = <%= contents %>'
      }, 'context_name'));
      it('defines file', basic('plain', {
        wrapper: 'App["<%= file.path %>"] = <%= contents %>'
      }, 'context_file'));
      it('defines contents', basic('plain', {
        wrapper: '<%= contents %>'
      }));
      it('handles nested paths for name', function(done) {
        var stream = defineModule('plain', {
          wrapper: 'App["<%= name %>"] = <%= contents %>'
        });
        var file = fixtureFile('nested/basic.js');
        stream.on('data', function(file) {
          fileShouldMatchExpected(file, 'basic_plain_context_name.js');
{'fraction_non_alphanumeric': 0.17059270516717326, 'fraction_numerical': 0.0, 'mean_word_length': 3.2502691065662, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '2520817', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2205, 'n_words': 527}