Added "copy(source, dest)" to the set of FS API.

* Also, added test case accordingly.
1.3
Ivan De Marino 2011-09-01 00:01:40 +01:00
parent cffd688efd
commit f381f61c64
4 changed files with 52 additions and 18 deletions

View File

@ -189,11 +189,6 @@ bool FileSystem::makeTree(const QString &path) const
return QDir().mkpath(path);
}
bool FileSystem::_remove(const QString &path) const
{
return QFile::remove(path);
}
bool FileSystem::_removeDirectory(const QString &path) const
{
return QDir().rmdir(path);
@ -303,3 +298,12 @@ QObject *FileSystem::_open(const QString &path, const QString &mode) const
qDebug() << "FileSystem::open - " << "Couldn't be opened:" << path;
return NULL;
}
bool FileSystem::_remove(const QString &path) const
{
return QFile::remove(path);
}
bool FileSystem::_copy(const QString &source, const QString &destination) const {
return QFile(source).copy(destination);
}

View File

@ -76,12 +76,6 @@ public slots:
int _size(const QString &path) const;
QVariant lastModified(const QString &path) const;
// Files / Directories
// - copy()
// - move()
// - touch(path, date)
// - rename()
// Directory
// - copyTree(source, target) //< copies files from a source path to a target path,
// copying the files of the source tree to the corresponding locations
@ -99,9 +93,13 @@ public slots:
QObject *_open(const QString &path, const QString &mode) const;
// 'read(path)' implemented in "fs-shim.js"
// 'write(path, mode)' implemented in the "fs-shim.js"
// 'remove(path)' implemented in "fs-shim.js" using '_remove(path)'
bool _remove(const QString &path) const;
// 'copy(source, destination)' implemented in "fs-shim.js" using '_copy(source, destination)'
bool _copy(const QString &source, const QString &destination) const;
// - move()
// - touch(path, date)
// - rename()
// Listing
QStringList list(const QString &path) const;

View File

@ -79,7 +79,7 @@ window.fs.write = function (path, content, mode) {
/** Return the size of a file, in bytes.
* It will throw an exception if it fails.
*
* @param path Path fo the file to read the size of
* @param path Path of the file to read the size of
* @return File size in bytes
*/
window.fs.size = function (path) {
@ -90,10 +90,22 @@ window.fs.size = function (path) {
throw "Unable to read file '" + path + "' size";
};
/** Copy a file.
* It will throw an exception if it fails.
*
* @param source Path of the source file
* @param destination Path of the destination file
*/
window.fs.copy = function (source, destination) {
if (!fs._copy(source, destination)) {
throw "Unable to copy file '" + source + "' at '" + destination + "'";
}
};
/** Removes a file.
* It will throw an exception if it fails.
*
* @param path Path fo the file to remove
* @param path Path of the file to remove
*/
window.fs.remove = function (path) {
if (!fs._remove(path)) {
@ -104,7 +116,7 @@ window.fs.remove = function (path) {
/** Removes a directory.
* It will throw an exception if it fails.
*
* @param path Path fo the directory to remove
* @param path Path of the directory to remove
*/
window.fs.removeDirectory = function (path) {
if (!fs._removeDirectory(path)) {
@ -115,7 +127,7 @@ window.fs.removeDirectory = function (path) {
/** Removes a directory tree.
* It will throw an exception if it fails.
*
* @param path Path fo the directory tree to remove
* @param path Path of the directory tree to remove
*/
window.fs.removeTree = function (path) {
if (!fs._removeTree(path)) {

View File

@ -1,5 +1,6 @@
describe("Basic Files API (read, write, remove, ...)", function() {
var FILENAME = "temp-01.test",
FILENAME_COPY = FILENAME + ".copy",
ABSENT = "absent-01.test";
it("should be able to create and write a file", function() {
@ -25,15 +26,34 @@ describe("Basic Files API (read, write, remove, ...)", function() {
expect(content).toEqual("hello\nworld\n");
});
it("should copy a file", function() {
expect(fs.exists(FILENAME_COPY)).toBeFalsy();
fs.copy(FILENAME, FILENAME_COPY);
expect(fs.exists(FILENAME_COPY)).toBeTruthy();
expect(fs.read(FILENAME)).toEqual(fs.read(FILENAME_COPY));
});
it("should be able to remove a file", function() {
expect(fs.exists(FILENAME)).toBeTruthy();
fs.remove(FILENAME);
expect(fs.exists(FILENAME)).toBeTruthy();
fs.remove(FILENAME);
expect(fs.exists(FILENAME)).toBeFalsy();
});
it("should be able to remove a (copied) file", function() {
expect(fs.exists(FILENAME_COPY)).toBeTruthy();
fs.remove(FILENAME_COPY);
expect(fs.exists(FILENAME_COPY)).toBeFalsy();
});
it("should throw an exception when trying to open for read a non existing file", function(){
expect(function(){
fs.open(ABSENT, "r");
}).toThrow("Unable to open file '"+ ABSENT +"'");
});
it("should throw an exception when trying to copy a non existing file", function() {
expect(function(){
fs.copy(ABSENT, FILENAME_COPY);
}).toThrow("Unable to copy file '" + ABSENT + "' at '" + FILENAME_COPY + "'");
});
});