<commit_before>path   = require 'path'
fs     = require 'fs'
should = require 'should'
glob   = require 'glob'
rimraf = require 'rimraf'
Roots  = require 'roots'
W      = require 'when'
nodefn = require 'when/node/function'
_path  = path.join(__dirname, 'fixtures')
run = require('child_process').exec

# setup, teardown, and utils

should.file_exist = (path) ->
  fs.existsSync(path).should.be.ok

should.have_content = (path) ->
  fs.readFileSync(path).length.should.be.above(1)

should.contain = (path, content) ->
  fs.readFileSync(path, 'utf8').indexOf(content).should.not.equal(-1)

compile_fixture = (fixture_name, done) ->
  @path = path.join(_path, fixture_name)
  @public = path.join(@path, 'public')
  project = new Roots(@path)
  project.compile().on('error', done).on('done', done)

before (done) ->
  tasks = []
  for d in glob.sync("#{_path}/*/package.json")
    p = path.dirname(d)
    if fs.existsSync(path.join(p, 'node_modules')) then continue
    console.log "installing deps for #{d}"
    tasks.push nodefn.call(run, "cd #{p}; npm install")
  W.all(tasks).then(-> done())

after ->
  rimraf.sync(public_dir) for public_dir in glob.sync('test/fixtures/**/public')

# tests

describe 'errors', ->

  it 'should throw an error when no base is defined', ->
    @path = path.join(_path, 'error')
    project = (-> new Roots(@path)).should.throw("path does not exist")

describe 'basics', ->

  before (done) -> compile_fixture.call(@, 'basic', done)

  it 'should precompile a basic template', ->
    p = path.join(@public, 'tpl/all.js')
    should.file_exist(p)
    should.have_content(p)

  it 'should still compile other templates normally', ->
    p = path.join(@public, 'index.html')
    should.file_exist(p)
    should.have_content(p)

  it 'should compile templates under their local path key', ->
    p = path.join(@public, 'tpl/all.js')
    should.contain(p, 'template1')
    should.contain(p, 'cat/dog')

  it 'not compile templates that would break a normal jade compile', ->
    p = path.join(@public, 'tpl/all.js')
    should.contain(p, 'template2')

describe 'extract', ->

  before (done) -> compile_fixture.call(@, 'extract', done)

  it 'should compile a template to both client and static if extract is false', ->
    p1 = path.join(@public, 'tpl/all.js')
    should.file_exist(p1)
    should.have_content(p1)

    p2 = path.join(@public, 'tpl/template2.html')
    should.file_exist(p2)
    should.have_content(p2)

describe 'concat', ->

  before (done) -> compile_fixture.call(@, 'concat', done)

  it 'should compile templates separately if concat is false', ->
    p1 = path.join(@public, 'tpl/helper.js')
    should.file_exist(p1)
    should.have_content(p1)

    p2 = path.join(@public, 'tpl/template3.js')
    should.file_exist(p2)
    should.have_content(p2)

    p3 = path.join(@public, 'tpl/template4.js')
    should.file_exist(p3)
    should.have_content(p3)

describe 'no output', ->

  before (done) -> compile_fixture.call(@, 'no-out', done)

  it 'should compile templates with no output specified', ->
    p = path.join(@public, 'js/templates.js')
    should.file_exist(p)<commit_msg>refactor tests to use roots-util<commit_after>path   = require 'path'
fs     = require 'fs'
should = require 'should'
Roots  = require 'roots'
RootsUtil = require 'roots-util'

_path  = path.join(__dirname, 'fixtures')
h = new RootsUtil.Helpers(base: _path)

# utils

compile_fixture = (fixture_name, done) ->
  @public = path.join(fixture_name, 'public')
  h.project.compile(Roots, fixture_name, done)

before (done) ->
  h.project.install_dependencies('*', done)

after ->
  h.project.remove_folders('**/public')

# tests

describe 'errors', ->

  it 'should throw an error when no base is defined', ->
    @path = path.join(_path, 'error')
    project = (-> new Roots(@path)).should.throw("path does not exist")

describe 'basics', ->

  before (done) -> compile_fixture.call(@, 'basic', done)

  it 'should precompile a basic template', ->
    p = path.join(@public, 'tpl/all.js')
    h.file.exists(p).should.be.ok
    h.file.has_content(p).should.be.ok

  it 'should still compile other templates normally', ->
    p = path.join(@public, 'index.html')
    h.file.exists(p).should.be.ok
    h.file.has_content(p).should.be.ok

  it 'should compile templates under their local path key', ->
    p = path.join(@public, 'tpl/all.js')
    h.file.contains(p, 'template1').should.be.ok
    h.file.contains(p, 'cat/dog').should.be.ok

  it 'not compile templates that would break a normal jade compile', ->
    p = path.join(@public, 'tpl/all.js')
    h.file.contains(p, 'template2').should.be.ok

describe 'extract', ->

  before (done) -> compile_fixture.call(@, 'extract', done)

  it 'should compile a template to both client and static if extract is false', ->
    p1 = path.join(@public, 'tpl/all.js')
    h.file.exists(p1).should.be.ok
    h.file.has_content(p1).should.be.ok

    p2 = path.join(@public, 'tpl/template2.html')
    h.file.exists(p2).should.be.ok
    h.file.has_content(p2).should.be.ok

describe 'concat', ->

  before (done) -> compile_fixture.call(@, 'concat', done)

  it 'should compile templates separately if concat is false', ->
    p1 = path.join(@public, 'tpl/helper.js')
    h.file.exists(p1).should.be.ok
    h.file.has_content(p1).should.be.ok

    p2 = path.join(@public, 'tpl/template3.js')
    h.file.exists(p2).should.be.ok
    h.file.has_content(p2).should.be.ok

    p3 = path.join(@public, 'tpl/template4.js')
    h.file.exists(p3).should.be.ok
    h.file.has_content(p3).should.be.ok

describe 'no output', ->

  before (done) -> compile_fixture.call(@, 'no-out', done)

  it 'should compile templates with no output specified', ->
    p = path.join(@public, 'js/templates.js')
    h.file.exists(p).should.be.ok
{'fraction_non_alphanumeric': 0.16525496974935178, 'fraction_numerical': 0.007433016421780467, 'mean_word_length': 3.5775316455696204, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, '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': '6110483', 'n_tokens_mistral': 2059, 'n_tokens_neox': 2034, 'n_words': 465}