import {findParentFolder, getFilePath, copyHookFiles, verifyHooksFolder} from '../src/fs-utils.js';
import path from 'path';
import fs from 'fs';
import fsp from 'fs-promise';
import rimraf from 'rimraf';

describe('FS-Utils Tests', () => {
  describe('Finding Directory', () => {
    it('Finding Named Directory', () => {
      let gitPath = findParentFolder(__dirname, '.git');
      gitPath.should.equal(path.join(__dirname, '../.git'));
    });

    it('Unable to Find Desired Directory', () => {
      let fn = () => { findParentFolder(path.join(__dirname, '../../')); };
      expect(fn).to.throw(Error);
    });
  });

  describe('Hooks folder verification', () => {
    beforeEach(() => {
      let pathString = path.resolve(path.join('test', 'tmp', '.git', 'hooks'));
      let hooksExists = fsp.existsSync(pathString);
      if(hooksExists) {
        rimraf.sync(pathString);
      }
    });

    it('Confirm hooks folder exists', () => {
      verifyHooksFolder(path.join('test', 'tmp', '.git', 'hooks'));
      assert(fs.existsSync(path.join('test', 'tmp', '.git', 'hooks')));
    });
  });

  describe('Hook Installation', () => {
    before(() => fsp.exists('test/tmp')
      .then(exists => {
        if(!exists) {
          fsp.mkdir('test/tmp')
          .then(() => fsp.mkdir('test/tmp/.git'))
          .then(() => fsp.mkdir('test/tmp/.git/hooks'));
          }
        }
      ));

    beforeEach(() => fsp.exists('test/tmp/.git/hooks/commit-msg')
      .then(exists => {
        if(exists) {
          return fsp.unlink('test/tmp/.git/hooks/commit-msg');
        }
    }));

    it('Hook Creation Test', (done) => {
      copyHookFiles(path.join(__dirname, '/tmp/.git'))
      .then(() => {
         fs.existsSync('test/tmp/.git/hooks/commit-msg').should.equal(true);
         done();
      });
    });

    it('Validate Hook File is Correct', (done) => {
      copyHookFiles(path.join(__dirname, '/tmp/.git'))
      .then(() => {
        let newFile = fs.readFileSync('test/tmp/.git/hooks/commit-msg');
        let oldFile = fs.readFileSync('hooks/commit-msg');
        newFile.should.eql(oldFile);
        done();
      });
    });
  });

  describe('Finding Specified File', () => {
    it('.gitignore is Found from Test', () => {
      let gitPath = getFilePath(__dirname, '.gitignore');
      gitPath.should.equal(path.join(__dirname, '../.gitignore'));
    });

    it('Unable to Find Specified File', () => {
      let fn = () => { getFilePath(path.join(__dirname, '../../')); };
      expect(fn).to.throw(Error);
    });
  });
});

@@ -203,42 +203,434 @@
 ';%0A%0A
-describe('FS-Utils Tests', () =%3E %7B
+const tmpDir = path.join(__dirname, 'tmp');%0Aconst tmpGitDir = path.join(tmpDir, '.git');%0Aconst hooksDir = path.join(tmpGitDir, 'hooks');%0Aconst commitMsgPath = path.join(hooksDir, 'commit-msg');%0A%0Adescribe('FS-Utils Tests', () =%3E %7B%0A  before(() =%3E%0A    fsp.exists(tmpDir)%0A      .then(exists =%3E %7B%0A        if(!exists) %7B%0A          return fsp.mkdir(tmpDir)%0A            .then(() =%3E fsp.mkdir('test/tmp/.git'));%0A        %7D%0A      %7D)%0A  );%0A
 %0A  d
@@ -1122,49 +1122,16 @@
 lve(
-path.join('test', 'tmp', '.git', '
 hooks
-')
+Dir
 );%0A 
@@ -1155,17 +1155,16 @@
 sts = fs
-p
 .existsS
@@ -1328,49 +1328,16 @@
 der(
-path.join('test', 'tmp', '.git', '
 hooks
-')
+Dir
 );%0A 
@@ -1366,49 +1366,16 @@
 ync(
-path.join('test', 'tmp', '.git', '
 hooks
-')
+Dir
 ));%0A
@@ -1441,24 +1441,30 @@
 before(() =%3E
+%0A     
  fsp.exists(
@@ -1463,28 +1463,28 @@
 .exists(
-'test/tmp')%0A
+hooksDir)%0A  
       .t
@@ -1491,32 +1491,34 @@
 hen(exists =%3E %7B%0A
+  
         if(!exis
@@ -1529,24 +1529,33 @@
 %7B%0A          
+  return 
 fsp.mkdir('t
@@ -1556,124 +1556,16 @@
 dir(
-'test/tmp')%0A          .then(() =%3E fsp.mkdir('test/tmp/.git'))%0A          .then(() =%3E fsp.mkdir('test/tmp/.git/
 hooks
-')
+Dir
 );%0A 
@@ -1588,16 +1588,14 @@
    %7D
+)
 %0A
-  
     
-)
 );%0A%0A
@@ -1610,24 +1610,30 @@
 reEach(() =%3E
+%0A     
  fsp.exists(
@@ -1636,42 +1636,25 @@
 sts(
-'test/tmp/.git/hooks/commit-msg')%0A
+commitMsgPath)%0A  
     
@@ -1665,32 +1665,34 @@
 hen(exists =%3E %7B%0A
+  
         if(exist
@@ -1698,32 +1698,34 @@
 ts) %7B%0A          
+  
 return fsp.unlin
@@ -1730,40 +1730,21 @@
 ink(
-'test/tmp/.git/hooks/commit-msg'
+commitMsgPath
 );%0A 
@@ -1750,24 +1750,35 @@
         
+ 
+ 
 %7D%0A
+    
     %7D)
+%0A    
 );%0A%0A    
@@ -1838,41 +1838,17 @@
 les(
-path.join(__dirname, '/tmp/.git')
+tmpGitDir
 )%0A  
@@ -1892,40 +1892,21 @@
 ync(
-'test/tmp/.git/hooks/commit-msg'
+commitMsgPath
 ).sh
@@ -2035,41 +2035,17 @@
 les(
-path.join(__dirname, '/tmp/.git')
+tmpGitDir
 )%0A  
@@ -2104,40 +2104,21 @@
 ync(
-'test/tmp/.git/hooks/commit-msg'
+commitMsgPath
 );%0A 

Fix failing test due to folder creation logic issues