<commit_before>'use strict';

var _ = require('lodash');
var expect = require('expect.js');
var fs = require('fs');
var zlib = require('zlib');

var ExternalBuild = require('../src/ExternalBuild');
var boscoMock = require('./boscoMock');

describe('ExternalBuild', function() {
  this.timeout(2000);
  this.slow(500);

  it('should do nothing without build config', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath('')
    };

    doBuild(service, {}, null, function(err) {
      if (err) return done(err);

      expect(localBosco).to.not.have.property('_log');
      expect(localBosco).to.not.have.property('_error');
      expect(localBosco).to.not.have.property('_warn');
      expect(localBosco.console).to.not.have.property('_log');
      expect(localBosco.console).to.not.have.property('_error');
      expect(localBosco.console).to.not.have.property('_warn');
      done();
    });
  });

  it('should run build command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'echo hi'
      }
    };

    doBuild(service, {}, null, function(err) {
      if (err) return done(err);

      expect(localBosco.console).to.not.have.property('_log');
      done();
    });
  });

  it('should error and log failed command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: ['bash', '-c', 'echo -n hi; echo -n bye >&2;false'],
      }
    };

    doBuild(service, {}, null, function(err) {
      expect(err).to.be.an(Error);
      expect(err).to.have.property('code', 1);
      expect(localBosco.console._log).to.eql(['hi']);
      expect(localBosco._error).to.have.length(2);
      expect(localBosco._error[0]).to.contain('with code 1');
      expect(localBosco._error[1]).to.be('bye');
      done();
    });
  });

  it('should error with exit code of failed command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: ['bash', '-c', 'exit 7'],
      }
    };

    doBuild(service, {}, null, function(err) {
      expect(err).to.be.an(Error);
      expect(err).to.have.property('code', 7);
      done();
    });
  });

  it('should not run if watchBuilds and reloadOnly', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: ['bash', '-c', 'echo -n hi; echo -n bye >&2;false'],
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./,
      reloadOnly: true
    };

    doBuild(service, options, null, function(err) {
      if (err) return done(err);
      expect(localBosco).to.not.have.property('_log');
      expect(localBosco).to.not.have.property('_error');
      expect(localBosco.console).to.not.have.property('_log');
      done();
    });
  });

  it('should run build command as watch command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'echo hi; sleep 1',
        watch: {
          ready: 'hi',
        }
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./
    };

    doBuild(service, options, null, function(err) {
      if (err) return done(err);
      expect(localBosco.console).to.not.have.property('_log');
      expect(localBosco._log).to.be.an('array');
      expect(localBosco._log[0]).to.contain('echo hi; sleep 1');
      done();
    });
  });

  it('should error and log if watch exits', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'bash -c echo',
        watch: {
          ready: 'hi',
        }
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./
    };

    doBuild(service, options, null, function(err) {
      expect(err).to.be.an(Error);
      expect(err).to.have.property('code', 0);
      expect(localBosco._error).to.be.an('array');
      expect(localBosco._error).to.have.length(1);
      expect(localBosco.process.stderr._data).to.have.length(1);
      expect(localBosco.process.stderr._data[0]).to.contain('with code 0');
      done();
    });
  });

  it('should error and log if watch times out', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'echo hello; sleep 1',
        watch: {
          timeout: 1
        }
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./,
      watchCallback: function(err, service, output) {
        expect(output.state).to.be('timeout');
        expect(localBosco.process.stderr._data).to.be.an('array');
        expect(localBosco.process.stderr._data.length).to.be.greaterThan(0);
        expect(localBosco.process.stderr._data[0]).to.contain('timed out');
        done();
      }
    };<commit_msg>Larger difference in test to avoid intermittent test failures re #186<commit_after>'use strict';

var _ = require('lodash');
var expect = require('expect.js');
var fs = require('fs');
var zlib = require('zlib');

var ExternalBuild = require('../src/ExternalBuild');
var boscoMock = require('./boscoMock');

describe('ExternalBuild', function() {
  this.timeout(2000);
  this.slow(500);

  it('should do nothing without build config', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath('')
    };

    doBuild(service, {}, null, function(err) {
      if (err) return done(err);

      expect(localBosco).to.not.have.property('_log');
      expect(localBosco).to.not.have.property('_error');
      expect(localBosco).to.not.have.property('_warn');
      expect(localBosco.console).to.not.have.property('_log');
      expect(localBosco.console).to.not.have.property('_error');
      expect(localBosco.console).to.not.have.property('_warn');
      done();
    });
  });

  it('should run build command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'echo hi'
      }
    };

    doBuild(service, {}, null, function(err) {
      if (err) return done(err);

      expect(localBosco.console).to.not.have.property('_log');
      done();
    });
  });

  it('should error and log failed command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: ['bash', '-c', 'echo -n hi; echo -n bye >&2;false'],
      }
    };

    doBuild(service, {}, null, function(err) {
      expect(err).to.be.an(Error);
      expect(err).to.have.property('code', 1);
      expect(localBosco.console._log).to.eql(['hi']);
      expect(localBosco._error).to.have.length(2);
      expect(localBosco._error[0]).to.contain('with code 1');
      expect(localBosco._error[1]).to.be('bye');
      done();
    });
  });

  it('should error with exit code of failed command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: ['bash', '-c', 'exit 7'],
      }
    };

    doBuild(service, {}, null, function(err) {
      expect(err).to.be.an(Error);
      expect(err).to.have.property('code', 7);
      done();
    });
  });

  it('should not run if watchBuilds and reloadOnly', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: ['bash', '-c', 'echo -n hi; echo -n bye >&2;false'],
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./,
      reloadOnly: true
    };

    doBuild(service, options, null, function(err) {
      if (err) return done(err);
      expect(localBosco).to.not.have.property('_log');
      expect(localBosco).to.not.have.property('_error');
      expect(localBosco.console).to.not.have.property('_log');
      done();
    });
  });

  it('should run build command as watch command', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'echo hi; sleep 1',
        watch: {
          ready: 'hi',
        }
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./
    };

    doBuild(service, options, null, function(err) {
      if (err) return done(err);
      expect(localBosco.console).to.not.have.property('_log');
      expect(localBosco._log).to.be.an('array');
      expect(localBosco._log[0]).to.contain('echo hi; sleep 1');
      done();
    });
  });

  it('should error and log if watch exits', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'bash -c echo',
        watch: {
          ready: 'hi',
        }
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./
    };

    doBuild(service, options, null, function(err) {
      expect(err).to.be.an(Error);
      expect(err).to.have.property('code', 0);
      expect(localBosco._error).to.be.an('array');
      expect(localBosco._error).to.have.length(1);
      expect(localBosco.process.stderr._data).to.have.length(1);
      expect(localBosco.process.stderr._data[0]).to.contain('with code 0');
      done();
    });
  });

  it('should error and log if watch times out', function(done) {
    var localBosco = boscoMock();
    var doBuild = ExternalBuild(localBosco).doBuild;
    var service = {
      name: 'service',
      repoPath: localBosco.getRepoPath(''),
      build: {
        command: 'echo hello; sleep 5',
        watch: {
          timeout: 100
        }
      }
    };
    var options = {
      watchBuilds: true,
      watchRegex: /./,
      watchCallback: function(err, service, output) {
        expect(output.state).to.be('timeout');
        expect(localBosco.process.stderr._data).to.be.an('array');
        expect(localBosco.process.stderr._data.length).to.be.greaterThan(0);
        expect(localBosco.process.stderr._data[0]).to.contain('timed out');
        done();
      }
    };
{'fraction_non_alphanumeric': 0.16623399100034614, 'fraction_numerical': 0.005278643129110419, 'mean_word_length': 2.681745778910481, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5256472', 'n_tokens_mistral': 3912, 'n_tokens_neox': 3808, 'n_words': 718}