<commit_before>var chai = require('chai');
var sinon = require('sinon');
var expect = chai.expect;
var Mugshot = require('../lib/mugshot.js');

chai.use(require('sinon-chai'));

describe('With selector', function() {
  var withSelector = {
        name: 'path',
        selector: 'a dom selector'
      },
      rect = {
        width: 100,
        height: 100,
        top: 50,
        left: 50
      },
      screenshot = new Buffer('ZmxvcmVudGlu'),
      error = new Error('Fatal Error'),
      callback, mugshot, browser, PNGProcessor, FS;

  beforeEach(function() {
    browser = {
      takeScreenshot: sinon.stub().yields(null, screenshot),
      getBoundingClientRect: sinon.stub()
    };

    FS = {
      mkdir: sinon.stub().yields(null)
    };

    PNGProcessor = {
      crop: sinon.stub()
    };

    callback = sinon.spy();

    var options = {
      fs: FS,
      PNGProcessor: PNGProcessor
    };

    mugshot = new Mugshot(browser, options);
  });

  it('should get the bounding rect', function() {
    mugshot.test(withSelector, callback);

    expect(browser.getBoundingClientRect).to.have.been.calledWith(
      withSelector.selector, sinon.match.func);
  });

  it('should call the cb with error if the bounding rect couldn\'t be ' +
     'calculated', function() {
    browser.getBoundingClientRect.yields(error);

    mugshot.test(withSelector, callback);

    expect(callback).to.have.been.calledWithExactly(error);
  });

  it('should crop the image', function() {
    browser.getBoundingClientRect.yields(null, rect);

    mugshot.test(withSelector, callback);

    expect(PNGProcessor.crop).to.have.been.calledWith(screenshot, rect,
      sinon.match.func);
  });

  it('should call the cb with error if the screenshot couldn\'t be cropped',
     function() {
    browser.getBoundingClientRect.yields(null, rect);
    PNGProcessor.crop.yields(error);

    mugshot.test(withSelector, callback);

    expect(callback).to.have.been.calledWithExactly(error);
  });
});
<commit_msg>add tests to check fs.exists call<commit_after>var chai = require('chai');
var sinon = require('sinon');
var expect = chai.expect;
var Mugshot = require('../lib/mugshot.js');
var path = require('path');

chai.use(require('sinon-chai'));

describe('With selector', function() {
  var withSelector = {
        name: 'path',
        selector: 'a dom selector'
      },
      rect = {
        width: 100,
        height: 100,
        top: 50,
        left: 50
      },
      screenshot = new Buffer('ZmxvcmVudGlu'),
      error = new Error('Fatal Error'),
      rootDirectory = 'visual-tests',
      extension = '.png',
      baselinePath = path.join(rootDirectory, withSelector.name + extension),
      callback, mugshot, browser, PNGProcessor, FS;

  beforeEach(function() {
    browser = {
      takeScreenshot: sinon.stub().yields(null, screenshot),
      getBoundingClientRect: sinon.stub()
    };

    FS = {
      mkdir: sinon.stub().yields(null),
      exists: sinon.stub()
    };

    PNGProcessor = {
      crop: sinon.stub()
    };

    callback = sinon.spy();

    var options = {
      fs: FS,
      PNGProcessor: PNGProcessor
    };

    mugshot = new Mugshot(browser, options);
  });

  it('should get the bounding rect', function() {
    mugshot.test(withSelector, callback);

    expect(browser.getBoundingClientRect).to.have.been.calledWith(
      withSelector.selector, sinon.match.func);
  });

  it('should call the cb with error if the bounding rect couldn\'t be ' +
     'calculated', function() {
    browser.getBoundingClientRect.yields(error);

    mugshot.test(withSelector, callback);

    expect(callback).to.have.been.calledWithExactly(error);
  });

  it('should crop the image', function() {
    browser.getBoundingClientRect.yields(null, rect);

    mugshot.test(withSelector, callback);

    expect(PNGProcessor.crop).to.have.been.calledWith(screenshot, rect,
      sinon.match.func);
  });

  it('should call the cb with error if the screenshot couldn\'t be cropped',
     function() {
    browser.getBoundingClientRect.yields(null, rect);
    PNGProcessor.crop.yields(error);

    mugshot.test(withSelector, callback);

    expect(callback).to.have.been.calledWithExactly(error);
  });

  it('should check if a baseline exists', function() {
    browser.getBoundingClientRect.yields(null, rect);
    PNGProcessor.crop.yields(null, rect);

    mugshot.test(withSelector, callback);

    expect(FS.exists).to.have.been.calledWith(baselinePath, sinon.match.func);
  });
});

{'fraction_non_alphanumeric': 0.14067006878189484, 'fraction_numerical': 0.004437541601952518, 'mean_word_length': 3.128205128205128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4634407', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1419, 'n_words': 293}