Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
#69 Implement UserScript::import and UserScript::load
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Sep 23, 2017
1 parent 95e5089 commit 20c87aa
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace MellowPlayer::Application

virtual void setName(const QString& name) = 0;

virtual void import(const QString& path) = 0;
virtual void load(const QString& path) = 0;
virtual bool import(const QString& path) = 0;
virtual bool load(const QString& path) = 0;
};
}
4 changes: 4 additions & 0 deletions lib/MellowPlayer/Infrastructure/Helpers/FileHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ QString FileHelper::createLogDirectory()
qDir.mkpath(qDir.path());
return directory;
}

QString FileHelper::userScriptsDirectory() {
return appDataDirectory() + "UserScripts" + QDir::separator();
}
1 change: 1 addition & 0 deletions lib/MellowPlayer/Infrastructure/Helpers/FileHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace MellowPlayer::Infrastructure
public:
static QString createLogDirectory();
static QString appDataDirectory();
static QString userScriptsDirectory();
static QString logDirectory();
};
}
44 changes: 41 additions & 3 deletions lib/MellowPlayer/Infrastructure/StreamingServices/UserScript.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
#include <QtCore/QDir>
#include <MellowPlayer/Infrastructure/Helpers/FileHelper.hpp>
#include <QtCore/QUuid>
#include "UserScript.hpp"

using namespace MellowPlayer::Application;
using namespace MellowPlayer::Infrastructure;

void UserScript::import(const QString& path)
bool UserScriptDirectory::create() const
{
qDebug() << "importing" << path;
QString path = FileHelper::userScriptsDirectory();
QDir qDir(path);
if (!qDir.exists())
return qDir.mkpath(path);
return true;
}

void UserScript::load(const QString& path)
QString UserScriptDirectory::generateFileName() const {
return path() + QDir::separator() + QUuid::createUuid().toString() + ".js";
}

QString UserScriptDirectory::path() const {
return FileHelper::userScriptsDirectory();
}

bool UserScript::import(const QString& sourcePath)
{
UserScriptDirectory directory;
if (directory.create() && QFile::exists(sourcePath))
{
QString destinationPath = directory.generateFileName();
path_ = destinationPath;
qDebug() << "importing" << sourcePath << " to " << destinationPath;
if (QFile::copy(sourcePath, destinationPath))
return load(destinationPath);
return true;
}
return false;
}

bool UserScript::load(const QString& path)
{
qDebug() << "loading" << path;
path_ = path;

QFile file(path);
if (file.open(QFile::ReadOnly)) {
auto content = file.readAll();
code_ = QString::fromUtf8(content);
return true;
}
return false;
}
13 changes: 11 additions & 2 deletions lib/MellowPlayer/Infrastructure/StreamingServices/UserScript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

namespace MellowPlayer::Infrastructure
{

class UserScriptDirectory
{
public:
QString path() const;
bool create() const;
QString generateFileName() const;
};

class UserScript : public Application::UserScriptBase
{
public:
void import(const QString& path) override;
void load(const QString& path) override;
bool import(const QString& sourcePath) override;
bool load(const QString& path) override;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <catch.hpp>
#include <MellowPlayer/Infrastructure/StreamingServices/UserScript.hpp>
#include <QtCore/QDir>
#include <MellowPlayer/Infrastructure/Helpers/FileHelper.hpp>

using namespace MellowPlayer::Infrastructure;

SCENARIO("UserScriptDirectoryTests")
{
GIVEN("a UserScriptDirectory instance")
{
UserScriptDirectory directory;
QDir dir(FileHelper::userScriptsDirectory());
dir.removeRecursively();
REQUIRE(!dir.exists());

WHEN("get path")
{
auto path = directory.path();

THEN("path is not empty")
{
REQUIRE(!path.isEmpty());
}
}

WHEN("create directory")
{
directory.create();

THEN("directory exists")
{
REQUIRE(QDir().exists(directory.path()));
}
}

WHEN("generating two file name")
{
auto fileName1 = directory.generateFileName();
auto fileName2 = directory.generateFileName();

THEN("they are different")
{
REQUIRE(fileName1 != fileName2);
}

}
}
}

SCENARIO("UserScriptTests")
{
GIVEN("A UserScript instance")
{
UserScript userScript;
QDir dir(FileHelper::userScriptsDirectory());
dir.removeRecursively();
REQUIRE(!dir.exists());

WHEN("importing a file that does not exists")
{
bool retVal = userScript.import("/an/invalid/path");
THEN("import return value is false")
{
REQUIRE(!retVal);
}
}

WHEN("importing a file that exists")
{
QByteArray fileContent("alert('custom user script')");
QString sourceFilePath = FileHelper::appDataDirectory() + "fakeUserScript.js";
QFile file(sourceFilePath);
file.open(QFile::WriteOnly);
file.write(fileContent);
file.close();

bool retVal = userScript.import(sourceFilePath);

THEN("import return value is true")
{
REQUIRE(retVal);

AND_THEN("the file exists")
{
REQUIRE(QFile::exists(userScript.path()));
}

AND_THEN("the file content is loaded and available via the code property")
{
REQUIRE(userScript.code().toStdString() == fileContent.toStdString());
}
}
}
}
}

0 comments on commit 20c87aa

Please sign in to comment.