Add simple test for PBD::find_files_matching_pattern
[ardour.git] / libs / pbd / test / filesystem_test.cc
1 #include "filesystem_test.h"
2
3 #include <unistd.h>
4 #include <stdlib.h>
5
6 #include <glibmm/miscutils.h>
7
8 #include "pbd/file_utils.h"
9
10 #include "test_common.h"
11
12 using namespace std;
13 using namespace PBD;
14
15 CPPUNIT_TEST_SUITE_REGISTRATION (FilesystemTest);
16
17 void
18 FilesystemTest::testPathIsWithin ()
19 {
20 #ifndef PLATFORM_WINDOWS
21         system ("rm -r foo");
22         CPPUNIT_ASSERT (g_mkdir_with_parents ("foo/bar/baz", 0755) == 0);
23
24         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar/baz", "foo/bar/baz"));
25         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz"));
26         CPPUNIT_ASSERT (PBD::path_is_within ("foo", "foo/bar/baz"));
27         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz"));
28         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar"));
29
30         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar/baz", "frobozz") == false);
31
32         int const r = symlink ("bar", "foo/jim");
33         CPPUNIT_ASSERT (r == 0);
34
35         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar/baz", "foo/bar/baz"));
36         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz"));
37         CPPUNIT_ASSERT (PBD::path_is_within ("foo", "foo/bar/baz"));
38         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar/baz"));
39         CPPUNIT_ASSERT (PBD::path_is_within ("foo/bar", "foo/bar"));
40
41         CPPUNIT_ASSERT (PBD::path_is_within ("foo/jim/baz", "frobozz") == false);
42 #endif
43 }
44
45 void
46 FilesystemTest::testCopyFileASCIIFilename ()
47 {
48         string testdata_path;
49         CPPUNIT_ASSERT (find_file (test_search_path (), "RosegardenPatchFile.xml", testdata_path));
50
51         string output_path = test_output_directory ("CopyFile");
52
53         output_path = Glib::build_filename (output_path, "RosegardenPatchFile.xml");
54
55         cerr << endl;
56         cerr << "CopyFile test output path: " << output_path << endl;
57
58         CPPUNIT_ASSERT (PBD::copy_file (testdata_path, output_path));
59 }
60
61 void
62 FilesystemTest::testCopyFileUTF8Filename ()
63 {
64         vector<string> i18n_files;
65
66         Searchpath i18n_path(test_search_path());
67         i18n_path.add_subdirectory_to_paths("i18n_test");
68
69         PBD::find_files_matching_pattern (i18n_files, i18n_path, "*.tst");
70
71         cerr << endl;
72         cerr << "Copying " << i18n_files.size() << " test files from: "
73              << i18n_path.to_string () << endl;
74
75         for (vector<string>::iterator i = i18n_files.begin(); i != i18n_files.end(); ++i) {
76                 string input_path = *i;
77                 string output_file = Glib::path_get_basename(*i);
78                 string output_path = test_output_directory ("CopyFile");
79                 output_path = Glib::build_filename (output_path, output_file);
80
81                 cerr << "Copying test file: " << input_path
82                      << " To " << output_path << endl;
83
84                 CPPUNIT_ASSERT (PBD::copy_file (input_path, output_path));
85         }
86 }
87
88 void
89 FilesystemTest::testFindFilesMatchingPattern ()
90 {
91         vector<string> patch_files;
92
93         PBD::find_files_matching_pattern (patch_files, test_search_path (), "*PatchFile*");
94
95         CPPUNIT_ASSERT(test_search_path ().size() == 1);
96
97         CPPUNIT_ASSERT(patch_files.size() == 2);
98 }