Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_filesystem.cpp File Reference
#include <boost/test/included/unit_test.hpp>
#include <boost/filesystem/fstream.hpp>
#include <fc/filesystem.hpp>
#include <fc/exception/exception.hpp>
Include dependency graph for test_filesystem.cpp:

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   fc_filesystem
 

Functions

std::string getFileContent (const string &filename)
 
 BOOST_AUTO_TEST_CASE (dir_copy)
 
 FC_LOG_AND_RETHROW ()
 
 BOOST_AUTO_TEST_CASE (file_copy)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   fc_filesystem

Definition at line 1 of file test_filesystem.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( dir_copy )

Definition at line 19 of file test_filesystem.cpp.

19 {
20 // 1. check whether dir can be copied when target dir does not exist,
21 // but not recursively (compatible with 1.73)
22
23 const string src_dir {"/tmp/fc_copy_test_src"};
24 if (!fc::exists(src_dir)) {
25 create_directories(src_dir);
26 }
27
28 BOOST_CHECK_EQUAL(fc::exists(src_dir), true);
29 const string test_file_name = "fc_copy_test_file";
30 if (!fc::exists(src_dir + "/" + test_file_name)) {
31 boost::filesystem::ofstream ofs { src_dir + "/" + test_file_name};
32 ofs << "This the test of fc system copy \n";
33 ofs.close();
34 }
35 BOOST_CHECK_EQUAL(fc::exists(src_dir + "/" + test_file_name), true);
36
37 const string tgt_dir {"/tmp/fc_copy_test_tgt"};
38 if (fc::exists(tgt_dir)) {
39 remove_all(tgt_dir);
40 }
41 BOOST_CHECK_EQUAL(fc::exists(tgt_dir), false);
42
43 fc::copy(src_dir, tgt_dir);
44 BOOST_CHECK_EQUAL(fc::exists(tgt_dir), true);
45 // check not copied recursively
46 BOOST_CHECK_EQUAL(fc::exists(tgt_dir + "/" + test_file_name), false);
47
48 // 2. check whether exception be thrown (no overwritten) when target dir does exist,
49 BOOST_CHECK_EQUAL(fc::exists(tgt_dir), true);
50 BOOST_CHECK_EXCEPTION(fc::copy(src_dir, tgt_dir), fc::exception, [](const fc::exception& e) {
52 });
53
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
int64_t code() const
bool exists(const path &p)
void remove_all(const path &p)
void create_directories(const path &p)
void copy(const path &from, const path &to)
@ unspecified_exception_code
Definition exception.hpp:20
FC_LOG_AND_RETHROW()
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( file_copy )

Definition at line 56 of file test_filesystem.cpp.

56 {
57 // 1. check whether file can be copied when target file does not exist,
58 const string src_dir {"/tmp/fc_copy_test_src"};
59 if (!fc::exists(src_dir)) {
60 create_directories(src_dir);
61 }
62
63 BOOST_CHECK_EQUAL(fc::exists(src_dir), true);
64 const string test_file_name = "fc_copy_test_file";
65 if (!fc::exists(src_dir + "/" + test_file_name)) {
66 boost::filesystem::ofstream ofs { src_dir + "/" + test_file_name};
67 ofs << "This the test of fc system copy \n";
68 ofs.close();
69 }
70 BOOST_CHECK_EQUAL(fc::exists(src_dir + "/" + test_file_name), true);
71
72 const string tgt_dir {"/tmp/fc_copy_test_tgt"};
73 if (!fc::exists(tgt_dir)) {
74 fc::copy(src_dir, tgt_dir);
75 }
76 BOOST_CHECK_EQUAL(fc::exists(tgt_dir), true);
77 BOOST_CHECK_EQUAL(fc::exists(tgt_dir + "/" + test_file_name), false);
78 fc::copy(src_dir + "/" + test_file_name, tgt_dir + "/" + test_file_name);
79 BOOST_CHECK_EQUAL(fc::exists(tgt_dir + "/" + test_file_name), true);
80 const string src_file_content = getFileContent(src_dir + "/" + test_file_name);
81 BOOST_CHECK_EQUAL(src_file_content.empty(), false);
82 const string tgt_file_content = getFileContent(tgt_dir + "/" + test_file_name);
83 BOOST_CHECK_EQUAL(src_file_content, tgt_file_content);
84
85 // 2. check whether exception be thrown (no overwritten) when target file does exist,
86 BOOST_CHECK_EQUAL(fc::exists(tgt_dir + "/" + test_file_name), true);
87 BOOST_CHECK_EXCEPTION(fc::copy(src_dir + "/" + test_file_name, tgt_dir + "/" + test_file_name),
89 [](const fc::exception& e) {
91 });
92
std::string getFileContent(const string &filename)
Here is the call graph for this function:

◆ FC_LOG_AND_RETHROW()

FC_LOG_AND_RETHROW ( )

◆ getFileContent()

std::string getFileContent ( const string & filename)

Definition at line 9 of file test_filesystem.cpp.

9 {
10 string ret;
11 boost::filesystem::ifstream ifs { filename };
12 ifs >> ret;
13 ifs.close();
14 return ret;
15}
CK_RV ret
Here is the caller graph for this function: