'use strict';
const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');

const repository = require('../lib/util/repository');
const Env = require('..');

describe('repository', () => {
  before(() => {
    if (fs.existsSync(repository.repositoryPath)) {
      fs.removeSync(repository.repositoryPath);
    }
  });

  afterEach(function () {
    this.timeout(20000);
    if (fs.existsSync(repository.repositoryPath)) {
      fs.removeSync(repository.repositoryPath);
    }
    repository.cleanupPackageCache('yeoman-environment', true);
  });

  it('is exposed on the Environment object', () => {
    assert.equal(Env.repository, repository);
  });

  describe('Environment#installLocalGenerators', () => {
    before(function () {
      this.timeout(100000);
      this.env = Env.createEnv();
      this.env.installLocalGenerators({'generator-dummytest': '0.1.3'});
    });

    after(() => {
      repository.cleanupPackageCache('generator-dummytest', true);
    });

    it('installs and register the generator', function () {
      this.timeout(10000);
      assert(this.env.get('dummytest:app'));
    });
  });

  describe('#repositoryPath', () => {
    it('returns the repository path', () => {
      assert.equal(repository.repositoryPath, path.resolve('.yo-repository'));
    });
  });

  describe('#resolvePackagePath', () => {
    describe('without args', () => {
      it('returns the node_modules path', () => {
        assert.equal(repository.nodeModulesPath, repository.resolvePackagePath());
      });
    });

    describe('with args', () => {
      it('returns the module path', () => {
        assert.equal(path.join(repository.nodeModulesPath, 'package', 'module'), repository.resolvePackagePath('package', 'module'));
      });
    });
  });

  describe('#createEnvWithVersion()', () => {
    describe('with semver valid range', () => {
      it('returns env', function () {
        this.timeout(500000);
        const env = Env.createEnvWithVersion('~2.3.0');
        assert.ok(env);
        assert.ok(!(env instanceof Env));
      });
    });

    describe('with git repository', () => {
      it('returns env', function () {
        this.timeout(500000);
        const env = Env.createEnvWithVersion('yeoman/environment#v2.8.1');
        assert.equal(repository.getPackageVersion('yeoman-environment'), '2.8.1');
        assert.ok(env);
        assert.ok(!(env instanceof Env));
      });
    });
  });

  describe('repository workflow', () => {
    it('run the workflow correctly', function () {
      this.timeout(500000);
      repository.installPackage('yeoman-environment', '2.3.0');
      assert.equal(repository.getPackageVersion('yeoman-environment'), '2.3.0');

      // Force install another version
      repository.installPackage('yeoman-environment', '2.8.1');
      assert.equal(repository.getPackageVersion('yeoman-environment'), '2.8.1');

      // Instantiate the installed version
      assert.ok(repository.requireModule('yeoman-environment'));
      assert.equal(repository.getPackageVersion('yeoman-environment'), '2.8.1');
    });
  });
});

@@ -226,23 +226,54 @@
  before(
-() =%3E %7B
+function () %7B%0A    this.timeout(20000);
 %0A    if 
@@ -809,25 +809,25 @@
 his.timeout(
-1
+2
 00000);%0A    
@@ -1925,34 +1925,42 @@
 %7B%0A      
-it('returns env', 
+let env;%0A      beforeEach(
 function
@@ -1995,38 +1995,32 @@
 00000);%0A        
-const 
 env = Env.create
@@ -2055,64 +2055,223 @@
     
-  assert.ok(env);%0A        assert.ok(!(env instanceof Env
+%7D);%0A%0A      it('returns env and #cleanup without force fails', () =%3E %7B%0A        assert.ok(env);%0A        assert.ok(!(env instanceof Env));%0A        assert.throws(() =%3E repository.cleanupPackageCache('yeoman-environment'
 ));%0A

Improve repository tests