-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing FileSystemService with tests
- Loading branch information
varunreddy
committed
Nov 19, 2015
1 parent
5337c08
commit 7d64903
Showing
7 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
/** | ||
* Created by varunreddy on 11/16/15. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
/** | ||
* Created by varunreddy on 11/16/15. | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
/// <reference path="../../../typings/tsd.d.ts" /> | ||
|
||
|
||
export class FileSystemService | ||
{ | ||
protected fs = require("fs-extra"); | ||
|
||
|
||
copyDirectory(directoryPath:string, destDir:string):Promise<boolean>{ | ||
|
||
return new Promise((resolve:Function)=>{ | ||
|
||
this.fs.copy(directoryPath,destDir,(error):void=>{ | ||
if (error) | ||
{ | ||
throw error; | ||
} | ||
|
||
resolve(true); | ||
}) | ||
|
||
}); | ||
} | ||
|
||
createDirectories(names:string[], parentDirectoryPath:string):Promise<boolean>{ | ||
|
||
return new Promise((resolve:Function)=>{ | ||
|
||
if(names) | ||
{ | ||
names.forEach((name:string)=>{ | ||
|
||
this.fs.mkdirsSync(parentDirectoryPath+"/"+name); | ||
|
||
}) | ||
} | ||
|
||
resolve(true); | ||
}) | ||
} | ||
|
||
|
||
deleteFiles(filePaths:String[]):Promise<boolean>{ | ||
|
||
return new Promise((resolve:Function)=>{ | ||
|
||
if(filePaths) | ||
{ | ||
filePaths.forEach((filePath:string)=>{ | ||
|
||
this.fs.removeSync(filePath); | ||
|
||
}) | ||
} | ||
|
||
resolve(true); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
console.log("project is ready"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
|
||
|
||
/// <reference path="../../../src/typings/tsd.d.ts" /> | ||
|
||
import chai = require('chai'); | ||
import {FileSystemService} from "../../../src/lib/service_system/services/FileSystemService"; | ||
|
||
var fs = require("fs-extra"); | ||
var path = require('path'); | ||
|
||
var testDir:string = path.resolve("./test"); | ||
var tempDirectoryPath = testDir + "/tempDir"; | ||
|
||
describe('FileSystemService Test cases', () => { | ||
|
||
var fsService:FileSystemService = new FileSystemService(); | ||
var expect = chai.expect; | ||
var assert = chai.assert; | ||
|
||
before((done)=>{ | ||
|
||
fs.mkdirs(tempDirectoryPath, function (err) { | ||
|
||
done(); | ||
}) | ||
|
||
|
||
}); | ||
|
||
after(()=>{ | ||
|
||
fs.removeSync(tempDirectoryPath); | ||
|
||
}); | ||
|
||
describe('createDirectory', () => { | ||
|
||
it('should successfully create the dir', function(done) { | ||
|
||
var promise:Promise<boolean> = fsService.createDirectories(["test","src"],tempDirectoryPath); | ||
|
||
if(!(promise instanceof Promise)) | ||
{ | ||
done("should return a promise") | ||
} | ||
promise.then((result:boolean)=>{ | ||
|
||
var srcExists:boolean = false; | ||
var testExists:boolean = false; | ||
try { | ||
// Query the entry | ||
var stats = fs.lstatSync(tempDirectoryPath+"/test"); | ||
|
||
// Is it a directory? | ||
if (stats.isDirectory()) { | ||
testExists = true; | ||
} | ||
} | ||
catch (e) { | ||
|
||
done("'test' directory should have been created\n"+e); | ||
} | ||
|
||
try { | ||
// Query the entry | ||
var stats = fs.lstatSync(tempDirectoryPath+"/src"); | ||
|
||
// Is it a directory? | ||
if (stats.isDirectory()) { | ||
srcExists = true; | ||
} | ||
} | ||
catch (e) { | ||
done("'src' directory should have been created\n"+e); | ||
} | ||
|
||
expect(srcExists).to.equal(true); | ||
expect(testExists).to.equal(true); | ||
|
||
done(); | ||
}, (error)=>{ | ||
done("should not have failed:\n" + error); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('copyDirectory', () => { | ||
|
||
it('should successfully copy the dir', function(done) { | ||
|
||
fs.mkdirsSync(tempDirectoryPath+"/testCopyDir"); | ||
fs.writeFileSync(tempDirectoryPath+"/testCopyDir/test.txt"); | ||
|
||
var promise:Promise<boolean> = fsService.copyDirectory(tempDirectoryPath+"/testCopyDir",tempDirectoryPath+"/temp"); | ||
|
||
if(!(promise instanceof Promise)) | ||
{ | ||
done("should return a promise") | ||
} | ||
|
||
promise.then(()=>{ | ||
|
||
try { | ||
var stats = fs.lstatSync(tempDirectoryPath+"/temp/test.txt"); | ||
|
||
// Is it a directory? | ||
if (stats.isFile()) { | ||
done(); | ||
} | ||
} | ||
catch (e) { | ||
|
||
done("File Should Have existed\n"+ e); | ||
} | ||
|
||
},(error)=>{ | ||
done("should not have failed\n" + error); | ||
}) | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
describe('deleteFiles', () => { | ||
|
||
it('should successfully delete the file', function(done) { | ||
|
||
fs.writeFileSync(tempDirectoryPath+"/testFile1.js"); | ||
fs.writeFileSync(tempDirectoryPath+"/testFile2.js"); | ||
|
||
var promise:Promise<boolean> = fsService.deleteFiles([tempDirectoryPath+"/testFile1.js",tempDirectoryPath+"/testFile2.js"]); | ||
|
||
if(!(promise instanceof Promise)) | ||
{ | ||
done("should return a promise") | ||
} | ||
|
||
promise.then(()=>{ | ||
|
||
try { | ||
var stats = fs.lstatSync(tempDirectoryPath+"/testFile1.js"); | ||
|
||
// Is it a directory? | ||
if (stats.isFile()) { | ||
done("'testFile1.js' Should not Have existed"); | ||
} | ||
} | ||
catch (e) { | ||
|
||
} | ||
|
||
try { | ||
var stats = fs.lstatSync(tempDirectoryPath+"/testFile2.js"); | ||
|
||
// Is it a directory? | ||
if (stats.isFile()) { | ||
done("'testFile2.js' Should not Have existed"); | ||
} | ||
} | ||
catch (e) { | ||
|
||
|
||
} | ||
|
||
|
||
done(); | ||
|
||
},(error)=>{ | ||
done("should not have failed\n" + error); | ||
}) | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
||
}); | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters