/* global describe, it, beforeEach */

var path = require('path')

var expect = require('chai').expect
var checkModules = require('../../lib/check_modules')

describe('Checking modules', function () {
  var fixturePath = path.resolve(__dirname, '..', 'fixtures')
  var fixtureName

  function basePath () {
    return path.join(fixturePath, fixtureName)
  }

  function packageInfo () {
    return require(path.join(basePath(), 'package.json'))
  }

  function dependencyPath () {
    return path.join(basePath(), 'node_modules')
  }

  function run () {
    return checkModules(packageInfo(), dependencyPath(), true)
  }

  beforeEach(function () {
    fixtureName = null
  })

  describe('when they are valid', function () {
    it('does not throw an exception', function () {
      fixtureName = 'valid_dependencies'
      run()
    })
  })

  describe('when dependencies are missing', function () {
    it('throws an exception', function () {
      fixtureName = 'missing_dependencies'
      var error
      try {
        run()
      } catch(e) {
        error = e
      }
      expect(error).not.to.be.null
      expect(error.name).to.eql('MissingDependenciesError')
      expect(error.message).to.eql(
        'Required dependencies missing: module_a, module_b at path: ' +
        basePath() +
        '/node_modules. Run `npm install`'
      )
    })
  })

  describe('when dependencies are unsaved', function () {
    it('throws an exception', function () {
      fixtureName = 'unsaved_dependencies'
      var error
      try {
        run()
      } catch(e) {
        error = e
      }
      expect(error).not.to.be.null
      expect(error.name).to.eql('UnsavedDependenciesError')
      expect(error.message).to.eql(
        'Unsaved dependencies installed: unsaved_module_a, ' +
        'unsaved_module_b. Run `npm prune` to remove them or add them to ' +
        'your package.json'
      )
    })
  })

  describe('circular dependencies', function () {
    describe('dependencies are valid', function () {
      it('does not throw an exception', function () {
        fixtureName = 'circular_dependencies'
        run()
      })
    })
  })

  describe('when modules have been deduped', function () {
    describe('dependencies are valid', function () {
      it('does not throw an exception', function () {
        fixtureName = 'valid_flattened_dependencies'
        run()
      })
    })

    describe('dependencies are invalid', function () {
      it('throws an exception', function () {
        fixtureName = 'invalid_flattened_dependencies'
        var error
        try {
          run()
        } catch(e) {
          error = e
        }
        expect(error).not.to.be.null
        expect(error.name).to.eql('MissingDependenciesError')
        expect(error.message).to.eql(
          'Required dependency missing: shared_module at path: ' +
          basePath() +
          '/node_modules/module_a/node_modules. Run `npm install`'
        )
      })
    })
  })
})

@@ -542,22 +542,34 @@
 tion run
+CheckModules
  () %7B%0A
-
     retu
@@ -830,24 +830,36 @@
 s'%0A      run
+CheckModules
 ()%0A    %7D)%0A  
@@ -1038,32 +1038,44 @@
 ry %7B%0A        run
+CheckModules
 ()%0A      %7D catch
@@ -1570,32 +1570,44 @@
 ry %7B%0A        run
+CheckModules
 ()%0A      %7D catch
@@ -2164,32 +2164,44 @@
 ies'%0A        run
+CheckModules
 ()%0A      %7D)%0A    
@@ -2431,32 +2431,44 @@
 ies'%0A        run
+CheckModules
 ()%0A      %7D)%0A    
@@ -2651,32 +2651,32 @@
 r%0A        try %7B%0A
-
           run()%0A
@@ -2672,16 +2672,28 @@
      run
+CheckModules
 ()%0A     

Rename run function