<commit_before>/*jshint node: true */

var url = require('url');
var fs = require('fs');
var cp = require('child_process');

var nano = require('nano');
var cors = require('./tests/CORS-Proxy/server.js');

var srcFiles = [
  "src/pouch.js", "src/pouch.collate.js", "src/pouch.merge.js",
  "src/pouch.replicate.js", "src/pouch.utils.js",
  "src/adapters/pouch.http.js", "src/adapters/pouch.idb.js",
  "src/adapters/pouch.websql.js", "src/plugins/pouchdb.mapreduce.js"
];

var testFiles = fs.readdirSync("./tests").filter(function(name){
  return (/^test\.([a-z0-9_])*\.js$/).test(name);
});

var browserConfig = [{
  browserName: 'chrome',
  platform: 'Windows 2003',
  name: 'win2003/chrome',
  'chrome.switches' : ['disable-file-system']
}, {
  browserName: 'firefox',
  version: '17',
  platform: 'Windows 2003',
  name: 'win2003/firefox'
}, {
  browserName: 'opera',
  version: '12',
  platform: 'Windows 2008',
  name: 'win2008/opera'
}];

module.exports = function(grunt) {

  var testStartTime = new Date();
  var testResults = {};

  grunt.initConfig({
    pkg: '<json:package.json>',
    concat: {
      amd: {
	src: grunt.utils._.flatten([
          "define('pouchdb',['jquery', 'simple-uuid', 'md5'], " +
            "function(jquery, uuid, md5) { ", 'src/pouch.amd.js', srcFiles,
          " return Pouch });"]),
	dest: 'pouch.amd.<%= pkg.release %>.js'
      },
      all: {
	src: grunt.utils._.flatten([
          "(function() { ", "src/deps/jquery-1.7.1.min.js",
          "src/deps/uuid.js","src/deps/polyfill.js", srcFiles, " })(this);"]),
	dest: 'pouch.<%= pkg.release %>.js'
      }
    },

    min: {
      dist: {
	src: grunt.utils._.flatten([
          "(function() { ", "src/deps/jquery-1.7.1.min.js", "src/deps/uuid.js",
          srcFiles, " })(this);"]),
	dest: 'pouch.<%= pkg.release %>.min.js'
      }
    },

    // Servers
    server: {
      base: '.',
      port: 8000
    },

    'cors-server': {
      base: 'http://127.0.0.1:5984',
      port: 2020
    },

    'node-qunit': {
      deps: './src/pouch.js',
      code: './src/adapters/pouch.leveldb.js',
      tests: testFiles.map(function (n) { return "./tests/" + n; }),
      done: function(err, res) {
        !err && (testResults['node'] = res);
	return true;
      }
    },

    'saucelabs-qunit': {
      all: {
	username: 'pouchdb',
	key: '<KEY>',
	testname: 'PouchDB Tests',
	tags: [process.env.TRAVIS_BRANCH || "unknown"],
	testTimeout: 1000 * 60 * 15, // 15 minutes
	testInterval: 1000 * 30, // 30 seconds
  tunnelTimeout: 1000 * 60 * 15, // 15 minutes
	urls: ["http://127.0.0.1:8000/tests/test.html?test=release-min&id=" +
               testStartTime.getTime() + "&testFiles=" + testFiles.join(',')],
	browsers: browserConfig,
	onTestComplete: function(status, page, config, browser) {
	  var done = this.async();
	  var browserDB = nano('http://127.0.0.1:5984').use('test_results');
          var retries = 0;
	  (function getResults() {
	    browser.eval("window.testReport", function(err, val) {
	      testResults[config.name] = err ? "No results" : val;
	      done(true);
	    });
	  }());
	}
      }
    },
    'publish-results': {
      server: 'http://pouchdb.iriscouch.com',
      db: 'test_results'
    }
  });

  // Custom tasks
  grunt.registerTask("cors-server", "Runs a CORS proxy", function(){
    var corsUrl = url.parse("http://127.0.0.1:" +
                            (arguments[0] || grunt.config("cors-server.port")));
    var couchUrl = url.parse(grunt.utils._.toArray(arguments).slice(1).join(":") ||
                             grunt.config("cors-server.base"));
    grunt.log.writeln("Starting CORS server " + url.format(corsUrl) +
                      " => " + url.format(couchUrl));
    cors.init(couchUrl, corsUrl);
  });

  grunt.registerTask("forever", "Runs a task forever, exits only on Ctrl+C", function(){
    this.async();
  });

  grunt.registerTask("publish-results",
                     "Publishes the results of the test to a server", function(){
    var done = this.async();
    cp.exec('git rev-list HEAD --max-count=1', function(err, stdout, stderr) {
      var results = {
	started: testStartTime,
	completed: new Date(),
	git_hash: stdout.replace(/[\n\r]/g, ''),
	passed: true,
	runs: {},
	runner: 'grunt'
      };
      for (var key in testResults) {
	results.runs[key] = {
	  started: testResults[key].started || "",
	  completed: testResults[key].completed || "",
	  passed: !!(testResults[key].passed),
	  report: testResults[key]
	};
  console.log("Test Result for %s is %s".yellow , key , results.runs[key].passed);
	results.passed = results.passed && results.runs[key].passed;
      }
      nano(grunt.config("publish-results.server"))
        .use(grunt.config("publish-results.db"))
        .insert(results, testStartTime.getTime() + "", function(err, body){
	  console.log(testStartTime.getTime(), err ? err.message : body);
	  done(results.passed && err == null);
        });
    });
  });

  grunt.loadNpmTasks('grunt-saucelabs');
  grunt.loadNpmTasks('grunt-node-qunit');

  grunt.registerTask("build", "concat min");
  grunt.registerTask("test", "build server cors-server node-qunit " +
                     "saucelabs-qunit publish-results");

  grunt.registerTask('default', 'build');
};
<commit_msg>and add polyfill when its minified<commit_after>/*jshint node: true */

var url = require('url');
var fs = require('fs');
var cp = require('child_process');

var nano = require('nano');
var cors = require('./tests/CORS-Proxy/server.js');

var srcFiles = [
  "src/pouch.js", "src/pouch.collate.js", "src/pouch.merge.js",
  "src/pouch.replicate.js", "src/pouch.utils.js",
  "src/adapters/pouch.http.js", "src/adapters/pouch.idb.js",
  "src/adapters/pouch.websql.js", "src/plugins/pouchdb.mapreduce.js"
];

var testFiles = fs.readdirSync("./tests").filter(function(name){
  return (/^test\.([a-z0-9_])*\.js$/).test(name);
});

var browserConfig = [{
  browserName: 'chrome',
  platform: 'Windows 2003',
  name: 'win2003/chrome',
  'chrome.switches' : ['disable-file-system']
}, {
  browserName: 'firefox',
  version: '17',
  platform: 'Windows 2003',
  name: 'win2003/firefox'
}, {
  browserName: 'opera',
  version: '12',
  platform: 'Windows 2008',
  name: 'win2008/opera'
}];

module.exports = function(grunt) {

  var testStartTime = new Date();
  var testResults = {};

  grunt.initConfig({
    pkg: '<json:package.json>',
    concat: {
      amd: {
	src: grunt.utils._.flatten([
          "define('pouchdb',['jquery', 'simple-uuid', 'md5'], " +
            "function(jquery, uuid, md5) { ", 'src/pouch.amd.js', srcFiles,
          " return Pouch });"]),
	dest: 'pouch.amd.<%= pkg.release %>.js'
      },
      all: {
	src: grunt.utils._.flatten([
          "(function() { ", "src/deps/jquery-1.7.1.min.js",
          "src/deps/uuid.js","src/deps/polyfill.js", srcFiles, " })(this);"]),
	dest: 'pouch.<%= pkg.release %>.js'
      }
    },

    min: {
      dist: {
	src: grunt.utils._.flatten([
          "(function() { ", "src/deps/jquery-1.7.1.min.js", "src/deps/uuid.js","src/deps/polyfill.js",
          srcFiles, " })(this);"]),
	dest: 'pouch.<%= pkg.release %>.min.js'
      }
    },

    // Servers
    server: {
      base: '.',
      port: 8000
    },

    'cors-server': {
      base: 'http://127.0.0.1:5984',
      port: 2020
    },

    'node-qunit': {
      deps: './src/pouch.js',
      code: './src/adapters/pouch.leveldb.js',
      tests: testFiles.map(function (n) { return "./tests/" + n; }),
      done: function(err, res) {
        !err && (testResults['node'] = res);
	return true;
      }
    },

    'saucelabs-qunit': {
      all: {
	username: 'pouchdb',
	key: '<KEY>',
	testname: 'PouchDB Tests',
	tags: [process.env.TRAVIS_BRANCH || "unknown"],
	testTimeout: 1000 * 60 * 15, // 15 minutes
	testInterval: 1000 * 30, // 30 seconds
  tunnelTimeout: 1000 * 60 * 15, // 15 minutes
	urls: ["http://127.0.0.1:8000/tests/test.html?test=release-min&id=" +
               testStartTime.getTime() + "&testFiles=" + testFiles.join(',')],
	browsers: browserConfig,
	onTestComplete: function(status, page, config, browser) {
	  var done = this.async();
	  var browserDB = nano('http://127.0.0.1:5984').use('test_results');
          var retries = 0;
	  (function getResults() {
	    browser.eval("window.testReport", function(err, val) {
	      testResults[config.name] = err ? "No results" : val;
	      done(true);
	    });
	  }());
	}
      }
    },
    'publish-results': {
      server: 'http://pouchdb.iriscouch.com',
      db: 'test_results'
    }
  });

  // Custom tasks
  grunt.registerTask("cors-server", "Runs a CORS proxy", function(){
    var corsUrl = url.parse("http://127.0.0.1:" +
                            (arguments[0] || grunt.config("cors-server.port")));
    var couchUrl = url.parse(grunt.utils._.toArray(arguments).slice(1).join(":") ||
                             grunt.config("cors-server.base"));
    grunt.log.writeln("Starting CORS server " + url.format(corsUrl) +
                      " => " + url.format(couchUrl));
    cors.init(couchUrl, corsUrl);
  });

  grunt.registerTask("forever", "Runs a task forever, exits only on Ctrl+C", function(){
    this.async();
  });

  grunt.registerTask("publish-results",
                     "Publishes the results of the test to a server", function(){
    var done = this.async();
    cp.exec('git rev-list HEAD --max-count=1', function(err, stdout, stderr) {
      var results = {
	started: testStartTime,
	completed: new Date(),
	git_hash: stdout.replace(/[\n\r]/g, ''),
	passed: true,
	runs: {},
	runner: 'grunt'
      };
      for (var key in testResults) {
	results.runs[key] = {
	  started: testResults[key].started || "",
	  completed: testResults[key].completed || "",
	  passed: !!(testResults[key].passed),
	  report: testResults[key]
	};
  console.log("Test Result for %s is %s".yellow , key , results.runs[key].passed);
	results.passed = results.passed && results.runs[key].passed;
      }
      nano(grunt.config("publish-results.server"))
        .use(grunt.config("publish-results.db"))
        .insert(results, testStartTime.getTime() + "", function(err, body){
	  console.log(testStartTime.getTime(), err ? err.message : body);
	  done(results.passed && err == null);
        });
    });
  });

  grunt.loadNpmTasks('grunt-saucelabs');
  grunt.loadNpmTasks('grunt-node-qunit');

  grunt.registerTask("build", "concat min");
  grunt.registerTask("test", "build server cors-server node-qunit " +
                     "saucelabs-qunit publish-results");

  grunt.registerTask('default', 'build');
};

{'fraction_non_alphanumeric': 0.1989399962142722, 'fraction_numerical': 0.021578648495173196, 'mean_word_length': 2.824466159971046, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '6280296', 'n_tokens_mistral': 4028, 'n_tokens_neox': 3757, 'n_words': 673}