Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_filesystem.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE fc_filesystem
2#include <boost/test/included/unit_test.hpp>
3#include <boost/filesystem/fstream.hpp>
4#include <fc/filesystem.hpp>
6
7using namespace fc;
8
9std::string getFileContent(const string& filename) {
10 string ret;
11 boost::filesystem::ifstream ifs { filename };
12 ifs >> ret;
13 ifs.close();
14 return ret;
15}
16
17BOOST_AUTO_TEST_SUITE(fc_filesystem)
18
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
55
56BOOST_AUTO_TEST_CASE(file_copy) try {
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
94
95BOOST_AUTO_TEST_SUITE_END()
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
int64_t code() const
Defines exception's used by fc.
namespace sysio::chain
Definition authority.cpp:3
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
std::string getFileContent(const string &filename)
BOOST_AUTO_TEST_CASE(dir_copy)
FC_LOG_AND_RETHROW()
CK_RV ret