<commit_before>'use strict';

const assert = require('assert');
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
const request = require('request');
const util = require('util');
const uuid = require('node-uuid');

describe('nodejs-docker', () => {

  it('serves traffic on 8080', (done) => {
    runDocker('test/express', 8080, (host, callback) => {
      setTimeout(() => {
        request(`http://${host}:8080`, (err, res, body) => {
          if (err) {
            console.error(`Error requesting content: ${util.inspect(err)}`);
            throw err;
          }
          assert.equal(body, 'Hello World!');
          callback(() => {
            done();
          });
        });
      }, 3000);
    });
  });
});

/**
 * Start a docker process for the given test
 */
function runDocker(tag, port, callback) {
  let name = uuid.v4();
  let d = spawn('docker', [
    'run', '-i', '--name', name, '-p', `${port}:${port}`, tag
  ]);

  d.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
  });

  d.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
  });

  d.on('error', (err) => { 
    console.error(`Error spawning docker process: ${util.inspect(err)}`); 
    throw err;
  });

  // if using docker-machine, grab the hostname
  let host = 'localhost';
  if (process.env.DOCKER_HOST) {
    host = process.env.DOCKER_HOST.split('//')[1].split(':')[0];
  }

  console.log(`host: ${host}`);<commit_msg>Remove test containers on exit<commit_after>'use strict';

const assert = require('assert');
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
const request = require('request');
const util = require('util');
const uuid = require('node-uuid');

describe('nodejs-docker', () => {

  it('serves traffic on 8080', (done) => {
    runDocker('test/express', 8080, (host, callback) => {
      setTimeout(() => {
        request(`http://${host}:8080`, (err, res, body) => {
          if (err) {
            console.error(`Error requesting content: ${util.inspect(err)}`);
            throw err;
          }
          assert.equal(body, 'Hello World!');
          callback(() => {
            done();
          });
        });
      }, 3000);
    });
  });
});

/**
 * Start a docker process for the given test
 */
function runDocker(tag, port, callback) {
  let name = uuid.v4();
  let d = spawn('docker', [
    'run', '--rm', '-i', '--name', name, '-p', `${port}:${port}`, tag
  ]);

  d.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
  });

  d.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
  });

  d.on('error', (err) => { 
    console.error(`Error spawning docker process: ${util.inspect(err)}`); 
    throw err;
  });

  // if using docker-machine, grab the hostname
  let host = 'localhost';
  if (process.env.DOCKER_HOST) {
    host = process.env.DOCKER_HOST.split('//')[1].split(':')[0];
  }

  console.log(`host: ${host}`);
{'fraction_non_alphanumeric': 0.21980595516895282, 'fraction_numerical': 0.012713282034125126, 'mean_word_length': 2.863049095607235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '1620409', 'n_tokens_mistral': 1021, 'n_tokens_neox': 982, 'n_words': 216}