<commit_before>var ChildProcess = require('child_process'),
    Gulp = require('gulp'),
    Server = require('./server');

var RUN_USER = 'vmrun -gu IEUser -gp Passw0rd! ';

Gulp.task('test:vm', ['build:browser', 'test:vm:edge', 'test:vm:ie']);

Gulp.task('test:vm:edge', ['build:browser'], function(callback) {
  runVM(runEdge, callback);
});
Gulp.task('test:vm:ie', ['build:browser', 'test:vm:edge'], function(callback) {
  runVM(runIE, callback);
});


function runVM(run, callback) {
  var vmx = './browsers/MsEdge-Win10-VMware.vmwarevm';
  Server.start(function(uri) {
    startVM(vmx)
        .then(function() { return setExperimental(vmx); })
        .then(function() { return run(vmx, uri); })
        .catch(cleanup);
  }, function() {
    cleanup();
  });

  function cleanup() {
    // Kill the vm
    stopVM(vmx)
      .then(function() {
        Server.stop(function() {
          callback();
        });
      });
  }
}

function startVM(vmx) {
  return run('vmrun start "' + vmx + '"')
      // Introduce our own arbitrary wait as the Win10 VM seems to have issues when sending
      // VMTools commands immediately after the first startup.
      .then(function() {
        return new Promise(function(resolve) {
          // After it says we've started, put in a nice long timeout to allow the VM tools to startup
          // There appears to be some sort of hang that occurs if we do this too soon in the process.
          setTimeout(function() {
            resolve(vmx);
          }, 60 * 1000);
        });
      });
}

function setExperimental(vmx) {
  // Enable Edge experimental features
  var key = 'HKCU\\SOFTWARE\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppContainer\\Storage\\microsoft.microsoftedge_8wekyb3d8bbwe\\MicrosoftEdge\\ExperimentalFeatures';
  return run(RUN_USER + 'runProgramInGuest "' + vmx + '" "C:\\Windows\\System32\\reg.exe" ADD "' + key + '" /v ExperimentalJS /t REG_DWORD /d 1 /f');
}
function runEdge(vmx, uri) {
  return run(RUN_USER + 'runProgramInGuest "' + vmx + '" -interactive -activeWindow  "C:\\Windows\\explorer.exe" microsoft-edge:' + uri + '/?tag=prerelease');
}
function runIE(vmx, uri) {
  return run(RUN_USER + 'runScriptInGuest "' + vmx + '" -interactive -activeWindow -noWait "" "\\"C:\\Program Files\\Internet Explorer\\iexplore.exe\\" ' + uri + '/?tag=stable"');
}
function stopVM(vmx) {
  return run('vmrun stop "' + vmx + '" hard');
}

function run(command, options, counter) {
  counter = counter || 0;

  return new Promise(function(resolve, reject) {
    console.log('run', command);
    ChildProcess.exec(command, options, function(err, stdout, stderr) {
      if (counter < 5
          && (/The specified guest user must be logged in interactively to perform this operation/.test(stdout)
            || (/The VMware Tools are not running in the virtual machine/).test(stdout)
            || nonZero(/reg.exe/, command, stdout))) {
        // Allow retries if there is something that might be waiting for background processes like updates
        counter++;
        console.log('retry', counter, command);<commit_msg>Create delay helper function in vm runner<commit_after>var ChildProcess = require('child_process'),
    Gulp = require('gulp'),
    Server = require('./server');

var RUN_USER = 'vmrun -gu IEUser -gp Passw0rd! ';

Gulp.task('test:vm', ['build:browser', 'test:vm:edge', 'test:vm:ie']);

Gulp.task('test:vm:edge', ['build:browser'], function(callback) {
  runVM(runEdge, callback);
});
Gulp.task('test:vm:ie', ['build:browser', 'test:vm:edge'], function(callback) {
  runVM(runIE, callback);
});


function runVM(run, callback) {
  var vmx = './browsers/MsEdge-Win10-VMware.vmwarevm';
  Server.start(function(uri) {
    startVM(vmx)
        .then(function() { return setExperimental(vmx); })
        .then(function() { return run(vmx, uri); })
        .catch(cleanup);
  }, function() {
    cleanup();
  });

  function cleanup() {
    // Kill the vm
    stopVM(vmx)
      .then(function() {
        Server.stop(function() {
          callback();
        });
      });
  }
}

// Some of this sourced from the excellent https://gist.github.com/neovov/5372144
function startVM(vmx) {
  return run('vmrun start "' + vmx + '"')
      .then(delay(10));
}

function delay(seconds) {
  return function() {
    return new Promise(function(resolve) {
      setTimeout(function() {
        resolve();
      }, seconds * 1000);
    });
  };
}

function setExperimental(vmx) {
  // Enable Edge experimental features
  var key = 'HKCU\\SOFTWARE\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppContainer\\Storage\\microsoft.microsoftedge_8wekyb3d8bbwe\\MicrosoftEdge\\ExperimentalFeatures';
  return run(RUN_USER + 'runProgramInGuest "' + vmx + '" "C:\\Windows\\System32\\reg.exe" ADD "' + key + '" /v ExperimentalJS /t REG_DWORD /d 1 /f');
}
function runEdge(vmx, uri) {
  return run(RUN_USER + 'runProgramInGuest "' + vmx + '" -interactive -activeWindow  "C:\\Windows\\explorer.exe" microsoft-edge:' + uri + '/?tag=prerelease');
}
function runIE(vmx, uri) {
  return run(RUN_USER + 'runScriptInGuest "' + vmx + '" -interactive -activeWindow -noWait "" "\\"C:\\Program Files\\Internet Explorer\\iexplore.exe\\" ' + uri + '/?tag=stable"');
}
function stopVM(vmx) {
  return run('vmrun stop "' + vmx + '" hard');
}

function run(command, options, counter) {
  counter = counter || 0;

  return new Promise(function(resolve, reject) {
    console.log('run', command);
    ChildProcess.exec(command, options, function(err, stdout, stderr) {
      if (counter < 5
          && (/The specified guest user must be logged in interactively to perform this operation/.test(stdout)
            || (/The VMware Tools are not running in the virtual machine/).test(stdout)
            || nonZero(/reg.exe/, command, stdout))) {
        // Allow retries if there is something that might be waiting for background processes like updates
        counter++;
        console.log('retry', counter, command);
{'fraction_non_alphanumeric': 0.15980066445182725, 'fraction_numerical': 0.007142857142857143, 'mean_word_length': 3.2884615384615383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '1294291', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1840, 'n_words': 466}