var tape                = require('tape');
var path                = require('path');
var fs                  = require('fs');
var broccoli            = require('broccoli');
var walkSync            = require('walk-sync');
var quickTemp           = require('quick-temp');
var copyDereferenceSync = require('copy-dereference').sync;
var _                   = require('lodash');

module.exports.setup = setup;
module.exports.teardown = teardown;
module.exports.setupSrcDir = setupSrcDir;
module.exports.bundleExists = bundleExists;
module.exports.bundleContains = bundleContains;
module.exports.bundleDoesntContain = bundleDoesntContain;

function setup(t, relativePath, brocfunc) {
  var srcDir = setupSrcDir(t, relativePath);
  var tree = brocfunc(srcDir);
  var builder = new broccoli.Builder(tree);

  return {
    srcDir: srcDir,
    tree: tree,
    builder: builder
  };
}

function setupSrcDir(t, relativePath) {
  var srcDir = path.join(__dirname, relativePath);
  var tmpDir = path.join(quickTemp.makeOrRemake(t, "tmpSrcDir"), "copy");
  copyDereferenceSync(srcDir, tmpDir);

  return tmpDir;
}

function teardown(t, testBroc) {
  quickTemp.remove(t, "tmpSrcDir");
}

function bundleExists(t, tree, bundlePath) {
  var destDir = tree.destDir;
  var absluteBundlePath = path.join(destDir, bundlePath);

  t.ok(fs.existsSync(absluteBundlePath), bundlePath + " bundle exists");
}

function bundleContains(t, tree, bundlePath, regexes) {
  if (! _.isArray(regexes)) {
    regexes = [regexes];
  }

  var destDir = tree.destDir;
  var absluteBundlePath = path.join(destDir, bundlePath);
  var bundleString = fs.readFileSync(absluteBundlePath).toString();

  regexes.forEach(function(regex) {
    t.ok(bundleString.match(regex), bundlePath + " contains " + regex.toString());
  });
}

function bundleDoesntContain(t, tree, bundlePath, regexes) {
  if (! _.isArray(regexes)) {
    regexes = [regexes];
  }

  var destDir = tree.destDir;
  var absluteBundlePath = path.join(destDir, bundlePath);
  var bundleExists = fs.existsSync(absluteBundlePath);

  var bundleString = fs.readFileSync(absluteBundlePath).toString();
  regexes.forEach(function(regex) {
    t.notOk(bundleString.match(regex), bundlePath + " doesn't contain " + regex.toString());
  });
}

@@ -629,16 +629,767 @@
 ntain;%0A%0A
+/**%0A * Sets up a test environment with copies of the required test's files in a tmp%0A * directory.%0A *%0A * t: The Tape context variable%0A * relativePath: The path relative to the test directory that contains the files%0A *               for this test. These files are copied into a tmp directory%0A * brocfunc: This function is called with the newly created tmp directory (with%0A *           the files from %60relativePath%60 copied) and you should return a%0A *           Broccoli tree built with the broccoli module you want to test%0A *%0A * Returns%0A *%0A * A hash %7B srcDir, tree, builder %7D, where srcDir is the tmp directory created%0A * by this function, tree is the tree built by the %60brocfunc%60, and builder is%0A * the broccoli builder created by this function.%0A *%0A */%0A
 function

Add a comment describing the test setup function