Filesystem API: added the "Attributes" API group from CommonJS/Filesystem definition

* Tests for this new API are also provided
* The 'fs.size()' method is implemented with a shim in 'bootstrap.js' to cover the exception throwing behaviour
1.3
Ivan De Marino 2011-07-22 00:29:25 +01:00
parent 5adc92aec0
commit 512ff9658e
5 changed files with 88 additions and 4 deletions

View File

@ -133,3 +133,17 @@ window.fs.write = function(path, content, mode) {
f.write(content);
f.close();
};
/** 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
* @return File size in bytes
*/
window.fs.size = function(path) {
var size = fs._size(path);
if (size !== -1) {
return size;
}
throw "Unable to read file '"+ path +"' size";
};

View File

@ -33,6 +33,7 @@
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QDateTime>
// File
// public:
@ -121,6 +122,26 @@ FileSystem::FileSystem(QObject *parent) :
{ }
// public slots:
// Attributes
int FileSystem::_size(const QString &path) const
{
QFileInfo fi(path);
if (fi.exists()) {
return fi.size();
}
return -1;
}
QVariant FileSystem::lastModified(const QString &path) const
{
QFileInfo fi(path);
if (fi.exists()) {
return QVariant(fi.lastModified());
}
return QVariant(QDateTime());
}
bool FileSystem::exists(const QString &path) const
{
return QFile::exists(path);

View File

@ -34,6 +34,7 @@
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QVariant>
class File : public QObject
{
@ -71,8 +72,9 @@ public:
public slots:
// Attributes
// - int size(const QString &path) const; //< TODO - in bytes, or throw excep if doesn't exists
// - QDateTime lastModified(const QString &path) const; //< TODO - returns the time that a file was last modified as a Date object.
// 'size(path)' implemented in "bootstrap.js" JavaScript shim, using '_size(path)'
int _size(const QString &path) const;
QVariant lastModified(const QString &path) const;
// Files / Directories
// - copy()

46
test/fs-spec-02.js Normal file
View File

@ -0,0 +1,46 @@
describe("Attributes Files API", function() {
var FILENAME = "temp-02.test",
CONTENT = "This is a test for PhantomJS, an awesome headless browser to do all sort of stuff :) ",
CONTENT_MULTIPLIER = 1024,
ABSENT = "absent-02.test";
it("should throw an exception when trying to read the size of a non existing file", function(){
expect(function(){
fs.size(ABSENT, "r");
}).toThrow("Unable to read file '"+ ABSENT +"' size");
});
it("should return a null Date object when trying to read the last modified date of a non existing file", function(){
expect(fs.lastModified(ABSENT)).toBeNull();
});
it("should create temporary file '"+ FILENAME +"' and writes some content in it", function(){
try{
var f = fs.open(FILENAME, "w");
expect(f).toBeDefined();
for (var i = 1; i <= CONTENT_MULTIPLIER; ++i) {
f.write(CONTENT);
}
f.close();
} catch (e) { }
});
it("should be able to read the size of a temporary file '"+ FILENAME +"'", function() {
expect(fs.size(FILENAME)).toEqual(CONTENT.length * CONTENT_MULTIPLIER);
});
it("should be able to read the Date on which a temporary file '"+ FILENAME +"' was last modified", function() {
var flm = fs.lastModified(FILENAME),
now = new Date();
expect(now.getDay()).toEqual(flm.getDay());
expect(now.getMonth()).toEqual(flm.getMonth());
expect(now.getFullYear()).toEqual(flm.getFullYear());
expect(now.getMilliseconds()).toNotEqual(flm.getMilliseconds());
});
it("should remove temporary file '"+ FILENAME +"'", function(){
expect(fs.remove(FILENAME)).toBeTruthy();
});
});

View File

@ -3,7 +3,8 @@ phantom.injectJs("./lib/jasmine.js");
phantom.injectJs("./lib/jasmine-console.js");
// Load specs
phantom.injectJs("./fs-spec-01.js"); //< Filesystem Specs 01
phantom.injectJs("./fs-spec-01.js"); //< Filesystem Specs 01 (Basic)
phantom.injectJs("./fs-spec-02.js"); //< Filesystem Specs 02 (Attributes)
// Launch tests
var jasmineEnv = jasmine.getEnv();
@ -11,7 +12,7 @@ var jasmineEnv = jasmine.getEnv();
// Add a ConsoleReporter to 1) print with colors on the console 2) exit when finished
jasmineEnv.addReporter(new jasmine.ConsoleReporter(function(msg){
// Print messages straight to the console
console.log(msg);
console.log(msg.replace('\n', ''));
}, function(reporter){
// On complete
phantom.exit();