1
0
Fork 0

commented

This commit is contained in:
Jan Steemann 2013-04-17 14:26:44 +02:00
parent 79330daa90
commit b4d115fd4d
1 changed files with 112 additions and 26 deletions

View File

@ -49,9 +49,14 @@ function FileSystemSuite () {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
setUp : function () { setUp : function () {
// create a name for a temporary directory we will run all tests in
// as we are creating a new name, this shouldn't collide with any existing
// directories, and we can also remove our directory when the tests are
// over
tempDir = fs.join(fs.getTempFile('', false)); tempDir = fs.join(fs.getTempFile('', false));
try { try {
// create this directory before any tests
fs.makeDirectoryRecursive(tempDir); fs.makeDirectoryRecursive(tempDir);
} }
catch (err) { catch (err) {
@ -63,38 +68,49 @@ function FileSystemSuite () {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
tearDown : function () { tearDown : function () {
// some sanity check as we don't want to unintentionally remove "." or "/"
if (tempDir.length > 5) { if (tempDir.length > 5) {
// remove our temporary directory with all its subdirectories
// we created it, so we don't care what's in it
fs.removeDirectoryRecursive(tempDir); fs.removeDirectoryRecursive(tempDir);
} }
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief exists() /// @brief exists() - test if a file / directory exists
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testExists : function () { testExists : function () {
var tempName; var tempName;
// existing directory // create the name for a new directory
tempName = fs.join(tempDir, 'foo'); tempName = fs.join(tempDir, 'foo');
try { try {
// remove it if it exists
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
} }
catch (err) { catch (err) {
} }
// create the directory with all paths to it
fs.makeDirectoryRecursive(tempName); fs.makeDirectoryRecursive(tempName);
// now the directory should exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// remove it again
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
// non-existing directory/file // non-existing directory/file
tempName = fs.join(tempDir, 'meow'); tempName = fs.join(tempDir, 'meow');
// this file should not exist
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// file // create a new file
tempName = fs.getTempFile('', true); tempName = fs.getTempFile('', true);
// the file should now exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// clean it up
fs.remove(tempName); fs.remove(tempName);
}, },
@ -105,30 +121,40 @@ function FileSystemSuite () {
testGetTempFile : function () { testGetTempFile : function () {
var tempName; var tempName;
// creating a new file // creating a new empty file with a temporary name
tempName = fs.getTempFile('', true); tempName = fs.getTempFile('', true);
assertTrue(tempName !== ''); assertTrue(tempName !== '');
// the file should be located inside the tempPath
assertEqual(fs.getTempPath(), tempName.substr(0, fs.getTempPath().length)); assertEqual(fs.getTempPath(), tempName.substr(0, fs.getTempPath().length));
// the file should exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// clean up
fs.remove(tempName); fs.remove(tempName);
// without creating // create a filename only, without creating the file itself
tempName = fs.getTempFile('', false); tempName = fs.getTempFile('', false);
assertTrue(tempName !== ''); assertTrue(tempName !== '');
// filename should be located underneath tempPath
assertEqual(fs.getTempPath(), tempName.substr(0, fs.getTempPath().length)); assertEqual(fs.getTempPath(), tempName.substr(0, fs.getTempPath().length));
// the file should not exist
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// in a subdirectory // create a temporary filename for a file in a subdirectory, without creating the file
tempName = fs.getTempFile('tests', false); tempName = fs.getTempFile('tests', false);
assertTrue(tempName !== ''); assertTrue(tempName !== '');
// filename should be located underneath tempPath
assertEqual(fs.join(fs.getTempPath(), 'test'), tempName.substr(0, fs.join(fs.getTempPath(), 'test').length)); assertEqual(fs.join(fs.getTempPath(), 'test'), tempName.substr(0, fs.join(fs.getTempPath(), 'test').length));
// file should not yet exist
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// clean up
fs.removeDirectory(fs.join(fs.getTempPath(), 'tests')); fs.removeDirectory(fs.join(fs.getTempPath(), 'tests'));
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief getTempPath() /// @brief getTempPath() - test the system's temp path
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testGetTempPath : function () { testGetTempPath : function () {
@ -137,76 +163,102 @@ function FileSystemSuite () {
tempName = fs.getTempPath(); tempName = fs.getTempPath();
assertTrue(tempName !== ''); assertTrue(tempName !== '');
// the temp path should also be an existing directory
assertTrue(fs.isDirectory(tempName)); assertTrue(fs.isDirectory(tempName));
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief isDirectory() /// @brief isDirectory() - test if a directory exists
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testIsDirectory : function () { testIsDirectory : function () {
var tempName; var tempName;
// existing directory // create a new directory name
tempName = fs.join(tempDir, 'foo'); tempName = fs.join(tempDir, 'foo');
try { try {
// remove any existing directory first
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
} }
catch (err) { catch (err) {
} }
// create the directory with the full path to it
fs.makeDirectoryRecursive(tempName); fs.makeDirectoryRecursive(tempName);
// check if it does exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// should also be a directory
assertTrue(fs.isDirectory(tempName)); assertTrue(fs.isDirectory(tempName));
// remove the directory
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
// non-existing directory/file // non-existing directory/file
tempName = fs.join(tempDir, 'meow'); tempName = fs.join(tempDir, 'meow');
// this file shouldn't exist
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// and thus shouldn't be a directory
assertFalse(fs.isDirectory(tempName)); assertFalse(fs.isDirectory(tempName));
// file // create a new file
tempName = fs.getTempFile('', true); tempName = fs.getTempFile('', true);
// should exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// but shouldn't be a directory
assertFalse(fs.isDirectory(tempName)); assertFalse(fs.isDirectory(tempName));
// clean up
fs.remove(tempName); fs.remove(tempName);
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief isFile() /// @brief isFile() - test if a file exists
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testIsFile : function () { testIsFile : function () {
var tempName; var tempName;
// existing directory // create a new directory name
tempName = fs.join(tempDir, 'foo'); tempName = fs.join(tempDir, 'foo');
try { try {
// remove any existing directory if it exists
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
} }
catch (err) { catch (err) {
} }
try { try {
// remove any existing file if it exists
fs.remove(tempName); fs.remove(tempName);
} }
catch (err) { catch (err) {
} }
// now create the whole directory with all paths to it
fs.makeDirectoryRecursive(tempName); fs.makeDirectoryRecursive(tempName);
// directory should now exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// but should not be a file
assertFalse(fs.isFile(tempName)); assertFalse(fs.isFile(tempName));
// remove it
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
// non-existing directory/file // non-existing directory/file
tempName = fs.join(tempDir, 'meow'); tempName = fs.join(tempDir, 'meow');
// this file shouldn't exist
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// and shouldn't be a file
assertFalse(fs.isFile(tempName)); assertFalse(fs.isFile(tempName));
// file // now create a new file
tempName = fs.getTempFile('', true); tempName = fs.getTempFile('', true);
// should exist
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// and should be a file
assertTrue(fs.isFile(tempName)); assertTrue(fs.isFile(tempName));
// clean up and remove the file
fs.remove(tempName); fs.remove(tempName);
}, },
@ -218,37 +270,53 @@ function FileSystemSuite () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief makeDirectory() /// @brief makeDirectory() - create a directory
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testMakeDirectory : function () { testMakeDirectory : function () {
var tempName; var tempName;
// create the name for a subdirectory
tempName = fs.join(tempDir, 'bar'); tempName = fs.join(tempDir, 'bar');
try { try {
// remove an existing directory if it already exists
fs.removeDirectoryRecursive(tempName); fs.removeDirectoryRecursive(tempName);
} }
catch (err) { catch (err) {
} }
// the directory shouldn't exist. we just deleted it if it was there
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// now create the directory
fs.makeDirectory(tempName); fs.makeDirectory(tempName);
// directory should be there after creation
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// now remove it
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
// create the name for another subdirectory
tempName = fs.join(tempDir, 'baz'); tempName = fs.join(tempDir, 'baz');
try { try {
// remove it if it does exist
fs.removeDirectoryRecursive(tempName); fs.removeDirectoryRecursive(tempName);
} }
catch (err) { catch (err) {
} }
// make with an existing directory // the directory shouldn't be there...
assertFalse(fs.exists(tempName)); assertFalse(fs.exists(tempName));
// now create it
fs.makeDirectory(tempName); fs.makeDirectory(tempName);
// should have succeeded
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
try { try {
// try to create the same directory again. this should fail
fs.makeDirectory(tempName); fs.makeDirectory(tempName);
fail(); fail();
} }
@ -256,16 +324,21 @@ function FileSystemSuite () {
assertEqual(ERRORS.ERROR_SYS_ERROR.code, err.errorNum); assertEqual(ERRORS.ERROR_SYS_ERROR.code, err.errorNum);
} }
// make subdirectory // create a subdirectory in the directory we created
tempName = fs.join(tempDir, 'baz', 'foo'); tempName = fs.join(tempDir, 'baz', 'foo');
fs.makeDirectory(tempName); fs.makeDirectory(tempName);
// the subdirectory should now be there
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
// create a file in the subdirecory
tempName = fs.join(tempDir, 'baz', 'foo', 'test'); tempName = fs.join(tempDir, 'baz', 'foo', 'test');
// write something to the file
fs.write(tempName, "this is a test"); fs.write(tempName, "this is a test");
// the file should exist after writing to it
assertTrue(fs.exists(tempName)); assertTrue(fs.exists(tempName));
try { try {
// create a directory with the name of an already existing file. this should fail.
fs.makeDirectory(tempName); fs.makeDirectory(tempName);
fail(); fail();
} }
@ -273,73 +346,83 @@ function FileSystemSuite () {
assertEqual(ERRORS.ERROR_SYS_ERROR.code, err.errorNum); assertEqual(ERRORS.ERROR_SYS_ERROR.code, err.errorNum);
} }
// remove all stuff we created for testing
fs.removeDirectoryRecursive(fs.join(tempDir, 'baz')); fs.removeDirectoryRecursive(fs.join(tempDir, 'baz'));
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief makeDirectoryRecursive() /// @brief makeDirectoryRecursive() - create a directory will all paths to it
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testMakeDirectoryRecursive : function () { testMakeDirectoryRecursive : function () {
var tempName; var tempName;
// create the name for a new subdirectory
tempName = fs.join(tempDir, 'bar'); tempName = fs.join(tempDir, 'bar');
try { try {
// make sure it does not yet exist
fs.removeDirectoryRecursive(tempName); fs.removeDirectoryRecursive(tempName);
} }
catch (err) { catch (err) {
} }
// create // create the subdirectory
fs.makeDirectoryRecursive(tempName); fs.makeDirectoryRecursive(tempName);
// check if it is there
assertTrue(fs.isDirectory(tempName)); assertTrue(fs.isDirectory(tempName));
// create again // create the subdirectory again. this should not fail
fs.makeDirectoryRecursive(tempName); fs.makeDirectoryRecursive(tempName);
// should still be a directory
assertTrue(fs.isDirectory(tempName)); assertTrue(fs.isDirectory(tempName));
// create subdirectories // create subdirectory in subdirectory of subdirectory
tempName = fs.join(tempDir, 'bar', 'baz', 'test'); tempName = fs.join(tempDir, 'bar', 'baz', 'test');
fs.makeDirectoryRecursive(tempName); fs.makeDirectoryRecursive(tempName);
assertTrue(fs.isDirectory(tempName)); assertTrue(fs.isDirectory(tempName));
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief move() /// @brief move() - TODO
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testMove : function () { testMove : function () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief read() /// @brief read() - TODO
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testRead : function () { testRead : function () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief remove() /// @brief remove() - TODO
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testRemove : function () { testRemove : function () {
}, },
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// @brief size() /// @brief test size() - the filesize of a file
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
testSize : function () { testSize : function () {
var tempName; var tempName;
// existing file // create a new file with a specific content
tempName = fs.join(tempDir, 'foo'); tempName = fs.join(tempDir, 'foo');
fs.write(tempName, "this is a test file"); fs.write(tempName, "this is a test file");
// test the size of the new file
assertEqual(19, fs.size(tempName)); assertEqual(19, fs.size(tempName));
// remove the new file
fs.remove(tempName); fs.remove(tempName);
// non-existing file // now the file does not exist
try { try {
// try to read filesize. this should fail
fs.size(tempName); fs.size(tempName);
fail(); fail();
} }
@ -350,12 +433,15 @@ function FileSystemSuite () {
// directory // directory
fs.makeDirectory(tempName); fs.makeDirectory(tempName);
try { try {
// try to read the filesize of a directory. this should fail
fs.size(tempName); fs.size(tempName);
fail(); fail();
} }
catch (err) { catch (err) {
assertEqual(ERRORS.ERROR_FILE_NOT_FOUND.code, err.errorNum); assertEqual(ERRORS.ERROR_FILE_NOT_FOUND.code, err.errorNum);
} }
// remove the directory
fs.removeDirectory(tempName); fs.removeDirectory(tempName);
} }