var rewire  = require('rewire'),
    sinon = require('sinon'),
    expect = require('chai').expect,
    docker = rewire('../../lib/docker');

var DEFAULT_REPOSITORY = docker.__get__('DEFAULT_REPOSITORY'),
    DEFAULT_TAG        = docker.__get__('DEFAULT_TAG');

describe('Docker', function() {

    describe('.getClient', function() {

        // This test ensure we can speak with the Docker host
        // set in the environment. If another test fails to speak
        // with the targeted Docker host, run this test suite first.
        it('returns a valid docker client', function(done) {
            var client = docker.getClient();

            client.ping(function(err) {
                done(err);
            });
        });

        context('when repository is present in environment', function() {
            var repository = 'test', env;

            setEnv('REPOSITORY', repository);

            var client = docker.getClient();

            revertEnv();

            it('returns a docker client using env repository', function() {
                expect(client.repository).to.equal(repository);
            });
        });

        context('when repository is not present in environment', function() {
            var env;

            setEnv('REPOSITORY', '');

            var client = docker.getClient();

            revertEnv();

            it('returns a docker client using default repository', function() {
                expect(client.repository).to.equal(DEFAULT_REPOSITORY);
            });
        });

        context('when tag is present in environment', function() {
            var tag = '1.0.0', env;

            setEnv('TAG', tag);

            var client = docker.getClient();
            
            revertEnv();

            it('returns a docker client using env tag', function() {
                expect(client.tag).to.equal(tag);
            });
        });

        context('when tag is not present in environment', function() {
            var env;

            setEnv('TAG', '');

            var client = docker.getClient();

            revertEnv();

            it('returns a docker client using default tag', function() {
                expect(client.tag).to.equal(DEFAULT_TAG);
            });
        });

        function setEnv(key, value) {
            env = process.env;

            process.env[key] = value;
        }

        function revertEnv() {
            process.env = env;
        }
    });
});

@@ -839,23 +839,71 @@
  'test',
+ client,
  env;%0A%0A
+            beforeEach(function() %7B%0A    
         
@@ -945,35 +945,35 @@
 );%0A%0A            
-var
+   
  client = docker
@@ -986,39 +986,110 @@
 ient();%0A
-%0A            revertEnv(
+            %7D);%0A%0A            afterEach(function() %7B%0A                revertEnv();%0A            %7D
 );%0A%0A    
@@ -1338,39 +1338,87 @@
 %0A            var
+ client,
  env;%0A%0A
+            beforeEach(function() %7B%0A    
             setE
@@ -1444,35 +1444,35 @@
 );%0A%0A            
-var
+   
  client = docker
@@ -1485,39 +1485,110 @@
 ient();%0A
-%0A            revertEnv(
+            %7D);%0A%0A            afterEach(function() %7B%0A                revertEnv();%0A            %7D
 );%0A%0A    
@@ -1861,23 +1861,71 @@
 '1.0.0',
+ client,
  env;%0A%0A
+            beforeEach(function() %7B%0A    
         
@@ -1953,35 +1953,35 @@
 );%0A%0A            
-var
+   
  client = docker
@@ -2006,17 +2006,60 @@
         
-%0A
+%7D);%0A%0A            afterEach(function() %7B%0A    
         
@@ -2066,32 +2066,48 @@
     revertEnv();
+%0A            %7D);
 %0A%0A            it
@@ -2327,18 +2327,67 @@
     
+ 
 var
+ client,
  env;%0A%0A
+            beforeEach(function() %7B%0A    
     
@@ -2418,35 +2418,35 @@
 );%0A%0A            
-var
+   
  client = docker
@@ -2463,31 +2463,102 @@
 ();%0A
-%0A            revertEnv(
+            %7D);%0A%0A            afterEach(function() %7B%0A                revertEnv();%0A            %7D
 );%0A%0A

use proper before each hook for docker tests