From 232fcd438ce095b37fea35cccb14b7c3504323c2 Mon Sep 17 00:00:00 2001 From: puritys Date: Wed, 26 Nov 2014 13:23:26 +0800 Subject: [PATCH] fix bug of unlink method and modify unit test --- .travis.yml | 1 + nodejs/file.js | 8 +++++--- tests/file.js | 30 +++++++++++++++++++++++++++++- tests/global.js | 11 +++++++++-- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index bfdf423..15f7c72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ before_script: - "npm install -g mocha" - "npm install istanbul" - "npm install coveralls" + - "npm install phplike" - "npm build ./" diff --git a/nodejs/file.js b/nodejs/file.js index 2bdc68f..452188f 100644 --- a/nodejs/file.js +++ b/nodejs/file.js @@ -47,6 +47,10 @@ function rmdir(dir, isForce) {//{{{ } };//}}} +function unlink(filename) { + return fs.unlinkSync(filename); +} + /* * @param filename @@ -100,9 +104,6 @@ exports.mkdir = function(dirName) {/*{{{*/ } };/*}}}*/ -exports.unlink = function (filename) { - return fs.unlinkSync(filename); -} @@ -114,3 +115,4 @@ exports.readdir = readdir; exports.is_dir = is_dir; exports.is_file = is_file; exports.rmdir = rmdir; +exports.unlink = unlink; diff --git a/tests/file.js b/tests/file.js index 935c8e8..c563074 100644 --- a/tests/file.js +++ b/tests/file.js @@ -23,7 +23,13 @@ describe('Test function: is_dir, file.js', function () { it('Test directory is exist.', function() { var isDir = phplikeMod.is_dir("tests"); assert.equal(true, isDir); - }) + }); + + it('File is not a dir.', function() { + var file = 'tests/global.js'; + var res = phplikeMod.is_dir(file); + assert.equal(false, res); + }); }); @@ -40,12 +46,25 @@ describe('File Handle', function () { }); + it('read a not exist file', function () { + var content = phplikeMod.file_get_contents("./tests/file.jsxx"); + assert.equal("", content); + + }); + it('write file', function () { phplikeMod.file_put_contents("./tests/tmp", "test"); var content = phplikeMod.file_get_contents("./tests/tmp"); assert.equal("test", content); }); + it('write a binary content into file', function () { + phplikeMod.file_put_contents("./tests/tmp2", "test", "binary"); + var content = phplikeMod.file_get_contents("./tests/tmp2"); + assert.equal("test", content); + phplikeMod.unlink("./tests/tmp2"); + }); + it('delete file', function () { phplikeMod.file_put_contents("./tests/tmp", "test"); var content = phplikeMod.file_get_contents("./tests/tmp"); @@ -66,8 +85,17 @@ describe('dir handle', function () { assert.equal(true, isDir); }); + it('create single level dir', function () { + phplikeMod.mkdir("test_dir"); + var isDir = phplikeMod.is_dir("test_dir"); + assert.equal(true, isDir); + phplikeMod.rmdir("test_dir"); + }); + it('delete dir - force', function() { var isForce = true; + phplikeMod.mkdir(dir); + phplikeMod.file_put_contents(dir + "/tmp", "test"); phplikeMod.rmdir("./tests/test", isForce); isDir = phplikeMod.is_dir(dir); assert.equal(false, isDir); diff --git a/tests/global.js b/tests/global.js index 003794c..c38574b 100644 --- a/tests/global.js +++ b/tests/global.js @@ -1,7 +1,7 @@ -// global function test +// global function test and test the phplike module which has already installed. global.UNIT_TEST = true; -require('./../nodejs/index.js'); +require('phplike'); var assert = require("assert"); @@ -29,7 +29,14 @@ describe('Test global function: core function', function() { assert.equal("dGVzdA==", res); }); +}); +describe('Test global function: is_dir', function() { + it('File is not a dir.', function() { + var file = 'tests/global.js'; + var res = is_dir(file); + assert.equal(false, res); + }); });