]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // libs/filesystem/test/convenience_test.cpp -----------------------------------------// |
2 | ||
3 | // Copyright Beman Dawes, 2002 | |
4 | // Copyright Vladimir Prus, 2002 | |
5 | // Use, modification, and distribution is subject to the Boost Software | |
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
7 | // http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | // See library home page at http://www.boost.org/libs/filesystem | |
10 | ||
11 | #include <boost/config/warning_disable.hpp> | |
12 | ||
13 | // See deprecated_test for tests of deprecated features | |
92f5a8d4 | 14 | #ifndef BOOST_FILESYSTEM_NO_DEPRECATED |
7c673cae FG |
15 | # define BOOST_FILESYSTEM_NO_DEPRECATED |
16 | #endif | |
92f5a8d4 | 17 | #ifndef BOOST_SYSTEM_NO_DEPRECATED |
7c673cae FG |
18 | # define BOOST_SYSTEM_NO_DEPRECATED |
19 | #endif | |
20 | ||
21 | #include <boost/filesystem/convenience.hpp> | |
92f5a8d4 TL |
22 | #include <boost/filesystem/directory.hpp> |
23 | #include <boost/filesystem/exception.hpp> | |
7c673cae FG |
24 | |
25 | #include <boost/config.hpp> | |
26 | # if defined( BOOST_NO_STD_WSTRING ) | |
27 | # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support | |
28 | # endif | |
29 | ||
30 | #include <boost/detail/lightweight_test.hpp> | |
31 | #include <boost/detail/lightweight_main.hpp> | |
32 | #include <boost/bind.hpp> | |
33 | #include <fstream> | |
34 | #include <iostream> | |
35 | ||
36 | namespace fs = boost::filesystem; | |
37 | using fs::path; | |
38 | namespace sys = boost::system; | |
39 | ||
40 | namespace | |
41 | { | |
42 | template< typename F > | |
43 | bool throws_fs_error(F func) | |
44 | { | |
45 | try { func(); } | |
46 | ||
47 | catch (const fs::filesystem_error &) | |
48 | { | |
49 | return true; | |
50 | } | |
51 | return false; | |
52 | } | |
53 | ||
54 | void create_recursive_iterator(const fs::path & ph) | |
55 | { | |
56 | fs::recursive_directory_iterator it(ph); | |
57 | } | |
58 | } | |
59 | ||
60 | // ------------------------------------------------------------------------------------// | |
61 | ||
62 | int cpp_main(int, char*[]) | |
63 | { | |
64 | ||
65 | // create_directories() tests --------------------------------------------------------// | |
66 | ||
67 | BOOST_TEST(!fs::create_directories("/")); // should be harmless | |
68 | ||
69 | path unique_dir = fs::unique_path(); // unique name in case tests running in parallel | |
70 | path unique_yy = unique_dir / "yy"; | |
71 | path unique_yya = unique_dir / "yya"; | |
72 | path unique_yy_zz = unique_dir / "yy" / "zz"; | |
73 | ||
74 | fs::remove_all(unique_dir); // make sure slate is blank | |
75 | BOOST_TEST(!fs::exists(unique_dir)); // reality check | |
76 | ||
77 | BOOST_TEST(fs::create_directories(unique_dir)); | |
78 | BOOST_TEST(fs::exists(unique_dir)); | |
79 | BOOST_TEST(fs::is_directory(unique_dir)); | |
80 | ||
81 | BOOST_TEST(fs::create_directories(unique_yy_zz)); | |
82 | BOOST_TEST(fs::exists(unique_dir)); | |
83 | BOOST_TEST(fs::exists(unique_yy)); | |
84 | BOOST_TEST(fs::exists(unique_yy_zz)); | |
85 | BOOST_TEST(fs::is_directory(unique_dir)); | |
86 | BOOST_TEST(fs::is_directory(unique_yy)); | |
87 | BOOST_TEST(fs::is_directory(unique_yy_zz)); | |
88 | ||
89 | path is_a_file(unique_dir / "uu"); | |
90 | { | |
91 | std::ofstream f(is_a_file.string().c_str()); | |
92 | BOOST_TEST(!!f); | |
93 | } | |
94 | BOOST_TEST(throws_fs_error( | |
95 | boost::bind(fs::create_directories, is_a_file))); | |
96 | BOOST_TEST(throws_fs_error( | |
97 | boost::bind(fs::create_directories, is_a_file / "aa"))); | |
98 | ||
99 | // recursive_directory_iterator tests ----------------------------------------// | |
100 | ||
101 | sys::error_code ec; | |
102 | fs::recursive_directory_iterator it("/no-such-path", ec); | |
103 | BOOST_TEST(ec); | |
104 | ||
105 | BOOST_TEST(throws_fs_error( | |
106 | boost::bind(create_recursive_iterator, "/no-such-path"))); | |
107 | ||
108 | fs::remove(unique_dir / "uu"); | |
109 | ||
110 | #ifdef BOOST_WINDOWS_API | |
111 | // These tests depends on ordering of directory entries, and that's guaranteed | |
112 | // on Windows but not necessarily on other operating systems | |
113 | { | |
114 | std::ofstream f(unique_yya.string().c_str()); | |
115 | BOOST_TEST(!!f); | |
116 | } | |
117 | ||
118 | for (it = fs::recursive_directory_iterator(unique_dir); | |
119 | it != fs::recursive_directory_iterator(); ++it) | |
120 | { std::cout << it->path() << '\n'; } | |
121 | ||
122 | it = fs::recursive_directory_iterator(unique_dir); | |
123 | BOOST_TEST(it->path() == unique_yy); | |
92f5a8d4 | 124 | BOOST_TEST(it.depth() == 0); |
7c673cae FG |
125 | ++it; |
126 | BOOST_TEST(it->path() == unique_yy_zz); | |
92f5a8d4 | 127 | BOOST_TEST(it.depth() == 1); |
7c673cae FG |
128 | it.pop(); |
129 | BOOST_TEST(it->path() == unique_yya); | |
92f5a8d4 | 130 | BOOST_TEST(it.depth() == 0); |
7c673cae FG |
131 | it++; |
132 | BOOST_TEST(it == fs::recursive_directory_iterator()); | |
133 | ||
134 | it = fs::recursive_directory_iterator(unique_dir); | |
135 | BOOST_TEST(it->path() == unique_yy); | |
92f5a8d4 | 136 | it.disable_recursion_pending(); |
7c673cae FG |
137 | ++it; |
138 | BOOST_TEST(it->path() == unique_yya); | |
139 | ++it; | |
140 | BOOST_TEST(it == fs::recursive_directory_iterator()); | |
141 | ||
142 | fs::remove(unique_yya); | |
143 | #endif | |
144 | ||
145 | it = fs::recursive_directory_iterator(unique_yy_zz); | |
146 | BOOST_TEST(it == fs::recursive_directory_iterator()); | |
92f5a8d4 | 147 | |
7c673cae FG |
148 | it = fs::recursive_directory_iterator(unique_dir); |
149 | BOOST_TEST(it->path() == unique_yy); | |
92f5a8d4 | 150 | BOOST_TEST(it.depth() == 0); |
7c673cae FG |
151 | ++it; |
152 | BOOST_TEST(it->path() == unique_yy_zz); | |
92f5a8d4 | 153 | BOOST_TEST(it.depth() == 1); |
7c673cae FG |
154 | it++; |
155 | BOOST_TEST(it == fs::recursive_directory_iterator()); | |
156 | ||
157 | it = fs::recursive_directory_iterator(unique_dir); | |
158 | BOOST_TEST(it->path() == unique_yy); | |
92f5a8d4 | 159 | it.disable_recursion_pending(); |
7c673cae FG |
160 | ++it; |
161 | BOOST_TEST(it == fs::recursive_directory_iterator()); | |
162 | ||
163 | it = fs::recursive_directory_iterator(unique_dir); | |
164 | BOOST_TEST(it->path() == unique_yy); | |
165 | ++it; | |
166 | it.pop(); | |
167 | BOOST_TEST(it == fs::recursive_directory_iterator()); | |
168 | ||
169 | ec.clear(); | |
170 | BOOST_TEST(!ec); | |
92f5a8d4 | 171 | // check that two argument failed constructor creates the end iterator |
7c673cae FG |
172 | BOOST_TEST(fs::recursive_directory_iterator("nosuchdir", ec) |
173 | == fs::recursive_directory_iterator()); | |
174 | BOOST_TEST(ec); | |
175 | ||
176 | fs::remove_all(unique_dir); // clean up behind ourselves | |
177 | ||
178 | return ::boost::report_errors(); | |
179 | } |