-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Format * Add exists internal task
- Loading branch information
Showing
17 changed files
with
193 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "exists_task.h" | ||
|
||
#define BOOST_FILESYSTEM_NO_DEPRECATED | ||
#define BOOST_NO_CXX11_SCOPED_ENUMS | ||
#include <boost/filesystem.hpp> | ||
|
||
namespace fs = boost::filesystem; | ||
|
||
|
||
exists_task::exists_task(size_t id, std::shared_ptr<task_metadata> task_meta) : task_base(id, task_meta) | ||
{ | ||
if (task_meta_->cmd_args.size() < 2) { | ||
throw task_exception("At least two arguments required."); | ||
} | ||
} | ||
|
||
|
||
exists_task::~exists_task() | ||
{ | ||
} | ||
|
||
|
||
std::shared_ptr<task_results> exists_task::run() | ||
{ | ||
std::shared_ptr<task_results> result(new task_results()); | ||
|
||
try { | ||
for (size_t i = 1; i < task_meta_->cmd_args.size(); ++i) { | ||
std::string file = task_meta_->cmd_args[i]; | ||
if (!fs::exists(file)) { | ||
result->status = task_status::FAILED; | ||
result->error_message = "File/folder '" + file + "' cannot be found"; | ||
result->output = task_meta_->cmd_args[0]; | ||
break; | ||
} | ||
} | ||
} catch (fs::filesystem_error &e) { | ||
result->status = task_status::FAILED; | ||
result->error_message = std::string("Cannot check file/folder existance. Error: ") + e.what(); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef RECODEX_WORKER_INTERNAL_EXISTS_TASK_H | ||
#define RECODEX_WORKER_INTERNAL_EXISTS_TASK_H | ||
|
||
#include "../task_base.h" | ||
|
||
|
||
/** | ||
* Check if file or folder exists. | ||
*/ | ||
class exists_task : public task_base | ||
{ | ||
public: | ||
/** | ||
* Constructor with initialization. | ||
* @param id Unique identificator of load order of tasks. | ||
* @param task_meta Variable containing further info about task. It's required that | ||
* @a cmd_args entry has at least one argument - names of files/folders which should be checked. | ||
* @throws task_exception when wrong arguments provided. | ||
*/ | ||
exists_task(size_t id, std::shared_ptr<task_metadata> task_meta); | ||
/** | ||
* Destructor. | ||
*/ | ||
virtual ~exists_task(); | ||
/** | ||
* Run the action. | ||
* @return Evaluation results to be pushed back to frontend. | ||
*/ | ||
virtual std::shared_ptr<task_results> run(); | ||
}; | ||
|
||
#endif // RECODEX_WORKER_INTERNAL_EXISTS_TASK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include <boost/filesystem.hpp> | ||
#include <fstream> | ||
#include <memory> | ||
#include "../src/tasks/internal/exists_task.h" | ||
|
||
namespace fs = boost::filesystem; | ||
|
||
class exists_task_test : public ::testing::Test | ||
{ | ||
protected: | ||
fs::path root; | ||
fs::path target; | ||
std::shared_ptr<exists_task> task; | ||
std::shared_ptr<task_metadata> task_meta; | ||
|
||
virtual void SetUp() | ||
{ | ||
root = fs::temp_directory_path() / fs::unique_path(); | ||
fs::create_directory(root); | ||
fs::create_directory(root / "subdir"); | ||
|
||
create_file(root / "file_a"); | ||
create_file(root / "subdir" / "file_b"); | ||
|
||
task_meta = std::make_shared<task_metadata>(); | ||
task_meta->cmd_args = {"failure message", root.string()}; | ||
task = std::make_shared<exists_task>(1, task_meta); | ||
} | ||
|
||
virtual void TearDown() | ||
{ | ||
fs::remove_all(root); | ||
fs::remove(root); | ||
} | ||
|
||
void create_file(const fs::path &path) | ||
{ | ||
std::ofstream f(path.string()); | ||
f << "test file"; | ||
f.close(); | ||
} | ||
}; | ||
|
||
TEST_F(exists_task_test, root_exists) | ||
{ | ||
auto results = task->run(); | ||
ASSERT_EQ(task_status::OK, results->status) << "Failed with: " + results->error_message; | ||
} | ||
|
||
TEST_F(exists_task_test, file_subdir_exists) | ||
{ | ||
task_meta->cmd_args = {"failure message", (root / "subdir").string(), (root / "file_a").string()}; | ||
|
||
auto results = task->run(); | ||
ASSERT_EQ(task_status::OK, results->status) << "Failed with: " + results->error_message; | ||
} | ||
|
||
TEST_F(exists_task_test, file_not_exists) | ||
{ | ||
task_meta->cmd_args = {"failure message", (root / "not_a_file").string()}; | ||
|
||
auto results = task->run(); | ||
ASSERT_EQ(task_status::FAILED, results->status); | ||
ASSERT_TRUE(results->error_message.find("cannot be found") != std::string::npos); | ||
ASSERT_EQ("failure message", results->output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.