<commit_before>module.exports = function(grunt) {

  require('load-grunt-tasks')(grunt);

  var distFiles = ['routes/*.js', 'tools/*.js', 'util/*.js', 'util/types/*.js', 'server.js'];

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    env : {
      dist : {
        SIS_RUN_LONG_TESTS : 'true',
        JUNIT_REPORT_PATH : grunt.option('report_path') || '_reports/report.xml'
      }
    },
    copy: {
      dist: {
        files : [
          {
            src: distFiles,
            dest: 'dist/',
          },
          {
            src: ["package.json", "npm-shrinkwrap.json"],
            dest: "dist/"
          }
        ]
      }
    },
    jshint: {
      files: distFiles.concat(["test/**/*.js"]),
      options: {
        newcap: false,
        node : true
      },
      dist: distFiles.map(function(f) { return "dist/" + f; })
    },
    // Empties folders to start fresh
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            'dist/*',
            '_reports/*',
            grunt.option('coverage_out') || '_reports/coverage.html',
            grunt.option('report_path') || '_reports/report.xml'
          ]
        }]
      },
      server: '.tmp'
    },
    mochaTest: {
      test: {
        options: {
          reporter: 'mocha-jenkins-reporter',
          clearRequireCache: true,
          timeout: 60000,
          require: 'test/fixtures/coverage-blanket'
        },
        src: ['test/init-tests.js', 'test/test-tokenmanager.js', 'test/test-*.js']
      },
      coverage: {
        options: {
          reporter: 'html-cov',
          // use the quiet flag to suppress the mocha console output
          quiet: true,
          // specify a destination file to capture the mocha
          // output (the quiet option does not suppress this)
          captureFile: grunt.option('coverage_out') || '_reports/coverage.html'
        },
        src: ['test/init-tests.js', 'test/test-*.js']
      },
      remote : {
        options: {
          reporter: 'mocha-jenkins-reporter',
          timeout: 60000,
          grep: '@API',
          clearRequireCache: true
        },
        src: ['test/test-*.js']
      },
      repl : {
        options : {
            reporter: 'mocha-jenkins-reporter',
            timeout : 60000,
            clearRequireCache : true
        },
        src : ['test/replication-tests/init-seed-data.js',
               'test/replication-tests/test-repl-*.js',
               'test/replication-tests/verify-seed-data.js']
      }
    }
  });

  grunt.registerTask('buildjson', function(target) {
    var outfile = 'build.json';
    var buildNum = process.env.BUILD_NUMBER || 'local-build';
    var githash = process.env.GIT_COMMIT_HASH || 'dev-hash';
    var buildId = process.env.BUILD_ID || grunt.template.date(Date.now(), 'yyyy-mm-dd_HH-MM-ss');

    var output = {
        build_num : buildNum,
        git_hash : githash,
        build_id : buildId,
        version : grunt.config.get('pkg.version')
    };
    output = JSON.stringify(output);
    grunt.file.write(outfile, output);
    if (target == 'dist') {
        // write to dist as well
        outfile = 'dist/' + outfile;
        grunt.file.write(outfile, output);
    }
  });

  var getWebInstancesFromInventory = function(inventory) {
    var ini = require('ini');
    var webInstances = [];
    function parseGroup(conf, group) {
        if (!conf || !conf[group]) {
            return;
        }
        for (var k in conf[group]) {
            var v = conf[group][k];
            var line = k + '=' + v;
            var splits = line.split(' ');
            var host = splits.shift();
            /* jshint loopfunc: true */
            splits = splits.map(function(s) {
                return s.split('=');
            });
            for (var i = 0; i < splits.length; ++i) {
                if (splits[i][0] == 'ansible_ssh_host') {
                    webInstances.push({ host : host, ip : splits[i][1], group : group });
                    break;
                }
            }
        }
    }
    function parseFile(file) {
        var conf = ini.parse(grunt.file.read(file));
        parseGroup(conf, 'sis-web');
        parseGroup(conf, 'sis-proxy');
    }
    if (grunt.file.isDir(inventory)) {
        grunt.file.recurse(inventory, parseFile);
    } else {
        parseFile(inventory);
    }
    return webInstances;
  };

  grunt.registerTask('apitest', 'Run remote api tests', function(inventory) {
    if (!inventory || !grunt.file.exists(inventory)) {
        return grunt.fail.fatal("inventory does not exist");
    }
    var webInstances = getWebInstancesFromInventory(inventory);
    // update the configs
    for (var i = 0; i < webInstances.length; ++i) {
        var host = webInstances[i];
        var host_fixed = host.host.replace(/\./g, '_');
        grunt.config.set('env.' + host_fixed, {
            SIS_REMOTE_USERNAME : 'sistest',
            SIS_REMOTE_PASSWORD : '<PASSWORD>',
            SIS_REMOTE_URL : 'https://' + host.ip,
            JUNIT_REPORT_PATH : 'report_apitest_' + host_fixed + '.xml',
            NODE_TLS_REJECT_UNAUTHORIZED : "0"
        });
        grunt.task.run('env:' + host_fixed);
        grunt.task.run('mochaTest:remote');
    }
  });

  grunt.registerTask('repltest', 'Run replication tests', function(inventory) {<commit_msg>Update grunt file to take in ansible-inventory as option for tests<commit_after>module.exports = function(grunt) {

  require('load-grunt-tasks')(grunt);

  var distFiles = ['routes/*.js', 'tools/*.js', 'util/*.js', 'util/types/*.js', 'server.js'];

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    env : {
      dist : {
        SIS_RUN_LONG_TESTS : 'true',
        JUNIT_REPORT_PATH : grunt.option('report_path') || '_reports/report.xml'
      }
    },
    copy: {
      dist: {
        files : [
          {
            src: distFiles,
            dest: 'dist/',
          },
          {
            src: ["package.json", "npm-shrinkwrap.json"],
            dest: "dist/"
          }
        ]
      }
    },
    jshint: {
      files: distFiles.concat(["test/**/*.js"]),
      options: {
        newcap: false,
        node : true
      },
      dist: distFiles.map(function(f) { return "dist/" + f; })
    },
    // Empties folders to start fresh
    clean: {
      dist: {
        files: [{
          dot: true,
          src: [
            'dist/*',
            '_reports/*',
            grunt.option('coverage_out') || '_reports/coverage.html',
            grunt.option('report_path') || '_reports/report.xml'
          ]
        }]
      },
      server: '.tmp'
    },
    mochaTest: {
      test: {
        options: {
          reporter: 'mocha-jenkins-reporter',
          clearRequireCache: true,
          timeout: 60000,
          require: 'test/fixtures/coverage-blanket'
        },
        src: ['test/init-tests.js', 'test/test-tokenmanager.js', 'test/test-*.js']
      },
      coverage: {
        options: {
          reporter: 'html-cov',
          // use the quiet flag to suppress the mocha console output
          quiet: true,
          // specify a destination file to capture the mocha
          // output (the quiet option does not suppress this)
          captureFile: grunt.option('coverage_out') || '_reports/coverage.html'
        },
        src: ['test/init-tests.js', 'test/test-*.js']
      },
      remote : {
        options: {
          reporter: 'mocha-jenkins-reporter',
          timeout: 60000,
          grep: '@API',
          clearRequireCache: true
        },
        src: ['test/test-*.js']
      },
      repl : {
        options : {
            reporter: 'mocha-jenkins-reporter',
            timeout : 60000,
            clearRequireCache : true
        },
        src : ['test/replication-tests/init-seed-data.js',
               'test/replication-tests/test-repl-*.js',
               'test/replication-tests/verify-seed-data.js']
      }
    }
  });

  grunt.registerTask('buildjson', function(target) {
    var outfile = 'build.json';
    var buildNum = process.env.BUILD_NUMBER || 'local-build';
    var githash = process.env.GIT_COMMIT_HASH || 'dev-hash';
    var buildId = process.env.BUILD_ID || grunt.template.date(Date.now(), 'yyyy-mm-dd_HH-MM-ss');

    var output = {
        build_num : buildNum,
        git_hash : githash,
        build_id : buildId,
        version : grunt.config.get('pkg.version')
    };
    output = JSON.stringify(output);
    grunt.file.write(outfile, output);
    if (target == 'dist') {
        // write to dist as well
        outfile = 'dist/' + outfile;
        grunt.file.write(outfile, output);
    }
  });

  var getWebInstancesFromInventory = function(inventory) {
    var ini = require('ini');
    var webInstances = [];
    function parseGroup(conf, group) {
        if (!conf || !conf[group]) {
            return;
        }
        for (var k in conf[group]) {
            var v = conf[group][k];
            var line = k + '=' + v;
            var splits = line.split(' ');
            var host = splits.shift();
            /* jshint loopfunc: true */
            splits = splits.map(function(s) {
                return s.split('=');
            });
            for (var i = 0; i < splits.length; ++i) {
                if (splits[i][0] == 'ansible_ssh_host') {
                    webInstances.push({ host : host, ip : splits[i][1], group : group });
                    break;
                }
            }
        }
    }
    function parseFile(file) {
        var conf = ini.parse(grunt.file.read(file));
        parseGroup(conf, 'sis-web');
        parseGroup(conf, 'sis-proxy');
    }
    if (grunt.file.isDir(inventory)) {
        grunt.file.recurse(inventory, parseFile);
    } else {
        parseFile(inventory);
    }
    return webInstances;
  };

  grunt.registerTask('apitest', 'Run remote api tests', function() {
    var inventory = grunt.option("ansible-inventory");
    if (!inventory || !grunt.file.exists(inventory)) {
        return grunt.fail.fatal("inventory does not exist - set ansible-inventory");
    }
    var webInstances = getWebInstancesFromInventory(inventory);
    // update the configs
    for (var i = 0; i < webInstances.length; ++i) {
        var host = webInstances[i];
        var host_fixed = host.host.replace(/\./g, '_');
        grunt.config.set('env.' + host_fixed, {
            SIS_REMOTE_USERNAME : 'sistest',
            SIS_REMOTE_PASSWORD : '<PASSWORD>',
            SIS_REMOTE_URL : 'https://' + host.ip,
            JUNIT_REPORT_PATH : 'report_apitest_' + host_fixed + '.xml',
            NODE_TLS_REJECT_UNAUTHORIZED : "0"
        });
        grunt.task.run('env:' + host_fixed);
        grunt.task.run('mochaTest:remote');
    }
  });

  grunt.registerTask('repltest', 'Run replication tests', function() {
    var inventory = grunt.option("ansible-inventory");
    if (!inventory || !grunt.file.exists(inventory)) {
        return grunt.fail.fatal("inventory does not exist - set ansible-inventory");
    }
{'fraction_non_alphanumeric': 0.14989603110026217, 'fraction_numerical': 0.003616309556098002, 'mean_word_length': 1.8140422284406004, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3769884', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3479, 'n_words': 662}