From 512ff9658ea20cd113e21660dc81504e1d14e3c9 Mon Sep 17 00:00:00 2001 From: Ivan De Marino Date: Fri, 22 Jul 2011 00:29:25 +0100 Subject: [PATCH] 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 --- src/bootstrap.js | 14 ++++++++++++++ src/filesystem.cpp | 21 +++++++++++++++++++++ src/filesystem.h | 6 ++++-- test/fs-spec-02.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test/run-tests.js | 5 +++-- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 test/fs-spec-02.js diff --git a/src/bootstrap.js b/src/bootstrap.js index cab69551..229457d7 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -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"; +}; diff --git a/src/filesystem.cpp b/src/filesystem.cpp index d3120821..999c3795 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -33,6 +33,7 @@ #include #include #include +#include // 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); diff --git a/src/filesystem.h b/src/filesystem.h index c2eee3f8..fceef079 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -34,6 +34,7 @@ #include #include #include +#include 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() diff --git a/test/fs-spec-02.js b/test/fs-spec-02.js new file mode 100644 index 00000000..58fe8b0f --- /dev/null +++ b/test/fs-spec-02.js @@ -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(); + }); +}); \ No newline at end of file diff --git a/test/run-tests.js b/test/run-tests.js index 2e162a4a..5ea52bdc 100644 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -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();