ardour
filesystem_test.cc
Go to the documentation of this file.
1 #include "filesystem_test.h"
2 
3 #include <glib.h>
4 #include <glib/gstdio.h>
5 
6 #include <unistd.h>
7 #include <stdlib.h>
8 
9 #include <glibmm/miscutils.h>
10 #include <glibmm/fileutils.h>
11 
12 #include "pbd/file_utils.h"
13 #include "pbd/pathexpand.h"
14 
15 #include "test_common.h"
16 
17 using namespace std;
18 using namespace PBD;
19 
21 
22 namespace {
23 
24 class PwdReset
25 {
26 public:
27 
28  PwdReset(const string& new_pwd)
29  : m_old_pwd(Glib::get_current_dir()) {
30  CPPUNIT_ASSERT (g_chdir (new_pwd.c_str()) == 0);
31  }
32 
33  ~PwdReset()
34  {
35  CPPUNIT_ASSERT (g_chdir (m_old_pwd.c_str()) == 0);
36  }
37 
38 private:
39 
40  string m_old_pwd;
41 
42 };
43 
44 } // anon
45 
46 void
48 {
49 #ifndef PLATFORM_WINDOWS
50  string output_path = test_output_directory ("testPathIsWithin");
51  PwdReset pwd_reset(output_path);
52 
53  CPPUNIT_ASSERT (g_mkdir_with_parents ("foo/bar/baz", 0755) == 0);
54 
55  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/bar/baz"), Glib::build_filename(output_path, "foo/bar/baz")));
56  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/bar"), Glib::build_filename(output_path, "foo/bar/baz")));
57  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo"), Glib::build_filename(output_path, "foo/bar/baz")));
58  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/bar"), Glib::build_filename(output_path, "foo/bar/baz")));
59  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/bar"), Glib::build_filename(output_path, "foo/bar")));
60 
61  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/bar/baz"), Glib::build_filename(output_path, "frobozz")) == false);
62 
63  int const r = symlink ("bar", "foo/jim");
64  CPPUNIT_ASSERT (r == 0);
65 
66  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/jim/baz"), Glib::build_filename(output_path, "foo/bar/baz")));
67  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/jim"), Glib::build_filename(output_path, "foo/bar/baz")));
68  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo"), Glib::build_filename(output_path, "foo/bar/baz")));
69  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/jim"), Glib::build_filename(output_path, "foo/bar/baz")));
70  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/jim"), Glib::build_filename(output_path, "foo/bar")));
71 
72  CPPUNIT_ASSERT (PBD::path_is_within (Glib::build_filename(output_path, "foo/jim/baz"), Glib::build_filename(output_path, "frobozz")) == false);
73 #endif
74 }
75 
76 void
78 {
79  string testdata_path;
80  CPPUNIT_ASSERT (find_file (test_search_path (), "RosegardenPatchFile.xml", testdata_path));
81 
82  string output_path = test_output_directory ("CopyFile");
83 
84  output_path = Glib::build_filename (output_path, "RosegardenPatchFile.xml");
85 
86  cerr << endl;
87  cerr << "CopyFile test output path: " << output_path << endl;
88 
89  CPPUNIT_ASSERT (PBD::copy_file (testdata_path, output_path));
90 }
91 
92 void
94 {
95  vector<string> i18n_files;
96 
97  Searchpath i18n_path(test_search_path());
98  i18n_path.add_subdirectory_to_paths("i18n_test");
99 
100  PBD::find_files_matching_pattern (i18n_files, i18n_path, "*.tst");
101 
102  CPPUNIT_ASSERT (i18n_files.size() == 8);
103 
104  cerr << endl;
105  cerr << "Copying " << i18n_files.size() << " test files from: "
106  << i18n_path.to_string () << endl;
107 
108  for (vector<string>::iterator i = i18n_files.begin(); i != i18n_files.end(); ++i) {
109  string input_path = *i;
110  string output_file = Glib::path_get_basename(*i);
111  string output_path = test_output_directory ("CopyFile");
112  output_path = Glib::build_filename (output_path, output_file);
113 
114  cerr << "Copying test file: " << input_path
115  << " To " << output_path << endl;
116 
117  CPPUNIT_ASSERT (PBD::copy_file (input_path, output_path));
118  }
119 }
120 
121 void
123 {
124  vector<string> patch_files;
125 
126  PBD::find_files_matching_pattern (patch_files, test_search_path (), "*PatchFile*");
127 
128  CPPUNIT_ASSERT(test_search_path ().size() == 1);
129 
130  CPPUNIT_ASSERT(patch_files.size() == 2);
131 }
132 
133 string
134 create_test_directory (std::string test_dir)
135 {
136  vector<string> test_files;
137  vector<string> i18n_files;
138 
139  Searchpath spath(test_search_path());
140  PBD::get_files (test_files, spath);
141 
142  spath.add_subdirectory_to_paths("i18n_test");
143 
144  PBD::get_files (i18n_files, spath);
145 
146  string output_dir = test_output_directory (test_dir);
147 
148  CPPUNIT_ASSERT (test_search_path().size () != 0);
149 
150  string test_dir_path = test_search_path()[0];
151 
152  cerr << endl;
153  cerr << "Copying " << test_files.size() << " test files from: "
154  << test_dir_path << " to " << output_dir << endl;
155 
156  CPPUNIT_ASSERT (test_files.size() != 0);
157 
158  PBD::copy_files (test_dir_path, output_dir);
159 
160  vector<string> copied_files;
161 
162  PBD::get_files (copied_files, output_dir);
163 
164  CPPUNIT_ASSERT (copied_files.size() == test_files.size());
165 
166  string subdir_path = Glib::build_filename (output_dir, "subdir");
167 
168  CPPUNIT_ASSERT (g_mkdir_with_parents (subdir_path.c_str(), 0755) == 0);
169 
170  cerr << endl;
171  cerr << "Copying " << i18n_files.size() << " i18n test files to: "
172  << subdir_path << endl;
173 
174  for (vector<string>::iterator i = i18n_files.begin(); i != i18n_files.end(); ++i) {
175  string input_filepath = *i;
176  string output_filename = Glib::path_get_basename(*i);
177  string output_filepath = Glib::build_filename (subdir_path, output_filename);
178 
179  CPPUNIT_ASSERT (PBD::copy_file (input_filepath, output_filepath));
180  }
181 
182  copied_files.clear();
183  PBD::get_files (copied_files, subdir_path);
184 
185  CPPUNIT_ASSERT (copied_files.size() == i18n_files.size());
186 
187  return output_dir;
188 }
189 
190 void
192 {
193  string output_dir_path = create_test_directory ("ClearDirectory");
194 
195  vector<string> files_in_output_dir;
196 
197  PBD::get_paths (files_in_output_dir, output_dir_path, true, true);
198 
199  size_t removed_file_size = 0;
200  vector<string> removed_files;
201 
202  CPPUNIT_ASSERT (PBD::clear_directory (output_dir_path, &removed_file_size, &removed_files) ==0);
203 
204  cerr << "Removed " << removed_files.size() << " files of total size: "
205  << removed_file_size << endl;
206 
207  CPPUNIT_ASSERT (removed_files.size () == files_in_output_dir.size ());
208 
209  string subdir_path = Glib::build_filename (output_dir_path, "subdir");
210 
211  // make sure the directory structure is still there
212  CPPUNIT_ASSERT (Glib::file_test (subdir_path, Glib::FILE_TEST_IS_DIR));
213 }
214 
215 void
217 {
218  string output_dir_path = create_test_directory ("RemoveDirectory");
219 
220  vector<string> files_in_output_dir;
221 
222  PBD::get_paths (files_in_output_dir, output_dir_path, false, true);
223 
224  CPPUNIT_ASSERT (files_in_output_dir.size () != 0);
225 
226  PBD::remove_directory (output_dir_path);
227 
228  // doesn't actually remove directory though...just contents
229  CPPUNIT_ASSERT (Glib::file_test (output_dir_path, Glib::FILE_TEST_IS_DIR));
230 
231  files_in_output_dir.clear ();
232 
233  PBD::get_paths (files_in_output_dir, output_dir_path, false, true);
234 
235  CPPUNIT_ASSERT (files_in_output_dir.size () == 0);
236 }
237 
238 void
240 {
241 #ifndef PLATFORM_WINDOWS
242  string top_dir = test_output_directory ("testCanonicalPath");
243  PwdReset pwd_reset(top_dir);
244 
245  string pwd = Glib::get_current_dir ();
246 
247  CPPUNIT_ASSERT (!pwd.empty());
248  CPPUNIT_ASSERT (pwd == top_dir);
249 
250  CPPUNIT_ASSERT (g_mkdir ("gtk2_ardour", 0755) == 0);
251  CPPUNIT_ASSERT (g_mkdir_with_parents ("libs/pbd/test", 0755) == 0);
252 
253  const char* relative_path = "./gtk2_ardour/../libs/pbd/test";
254  string canonical_path = PBD::canonical_path (relative_path);
255  // no expansion expected in this case
256  string expanded_path = PBD::path_expand (relative_path);
257  string expected_path = top_dir + string("/libs/pbd/test");
258 
259  CPPUNIT_ASSERT (canonical_path == expected_path);
260  CPPUNIT_ASSERT (expanded_path == expected_path);
261 #endif
262 }
LIBPBD_API std::string path_expand(std::string path)
void remove_directory(const std::string &dir)
Definition: file_utils.cc:465
bool path_is_within(std::string const &haystack, std::string needle)
Definition: file_utils.cc:375
Definition: Beats.hpp:239
bool find_file(const Searchpath &search_path, const string &filename, std::string &result)
Definition: file_utils.cc:187
int clear_directory(const string &dir, size_t *size, vector< string > *paths)
Definition: file_utils.cc:458
LIBPBD_TEMPLATE_MEMBER_API Searchpath & add_subdirectory_to_paths(const std::string &subdir)
Definition: search_path.cc:158
bool copy_file(const std::string &from_path, const std::string &to_path)
Definition: file_utils.cc:282
void find_files_matching_pattern(vector< string > &result, const Searchpath &paths, const Glib::PatternSpec &pattern)
Definition: file_utils.cc:168
void testCopyFileASCIIFilename()
void copy_files(const std::string &from_path, const std::string &to_dir)
Definition: file_utils.cc:319
PBD::Searchpath test_search_path()
Definition: test_util.cc:133
void get_paths(vector< string > &result, const Searchpath &paths, bool files_only, bool recurse)
Definition: file_utils.cc:144
LIBPBD_TEMPLATE_MEMBER_API const std::string to_string() const
Definition: search_path.cc:99
std::string test_output_directory(std::string prefix)
Definition: test_common.cc:47
Definition: debug.h:30
void testFindFilesMatchingPattern()
void get_files(vector< string > &result, const Searchpath &paths)
Definition: file_utils.cc:154
CPPUNIT_TEST_SUITE_REGISTRATION(FilesystemTest)
void testCopyFileUTF8Filename()
void testRemoveDirectory()
LIBPBD_API std::string canonical_path(const std::string &path)
Definition: pathexpand.cc:90
string create_test_directory(std::string test_dir)