Skip to content

Commit

Permalink
implementing FileSystemService with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
varunreddy committed Nov 19, 2015
1 parent 5337c08 commit 7d64903
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"development"
],
"dependencies": {
"fs-extra": "^0.26.2",
"minimist": "^1.2.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions src/lib/service_system/managers/ProjectManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/**
* Created by varunreddy on 11/16/15.
*/

4 changes: 1 addition & 3 deletions src/lib/service_system/managers/UserManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/**
* Created by varunreddy on 11/16/15.
*/

60 changes: 60 additions & 0 deletions src/lib/service_system/services/FileSystemService.ts
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);
})
}
}
2 changes: 2 additions & 0 deletions src/resources/basic_template/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log("project is ready");
185 changes: 185 additions & 0 deletions test/service_system/services/FileSystemService.spec.ts
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);
})
});

});



});




3 changes: 3 additions & 0 deletions tsd.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
},
"minimatch/minimatch.d.ts": {
"commit": "001ca36ba58cef903c4c063555afb07bbc36bb58"
},
"fs-extra/fs-extra.d.ts": {
"commit": "fc341765ebbb04b7109981a25dced01f488f4d94"
}
}
}

0 comments on commit 7d64903

Please sign in to comment.