getting through tests on mac
[feisty_meow.git] / nucleus / library / tests_filesystem / test_directory_tree.cpp
1 /*
2 *  Name   : test_directory_tree
3 *  Author : Chris Koeritz
4 *  Purpose:
5 *    Tests the directory_tree object on some well-known directories.
6 **
7 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 */
14
15 #include <application/hoople_main.h>
16 #include <basis/functions.h>
17 #include <basis/guards.h>
18 #include <filesystem/directory_tree.h>
19 #include <filesystem/filename.h>
20 #include <filesystem/filename_list.h>
21 #include <loggers/critical_events.h>
22 #include <loggers/program_wide_logger.h>
23 #include <processes/launch_process.h>
24 #include <structures/static_memory_gremlin.h>
25 #include <structures/string_array.h>
26 #include <textual/string_manipulation.h>
27 #include <unit_test/unit_base.h>
28
29 using namespace application;
30 using namespace basis;
31 using namespace mathematics;
32 using namespace filesystem;
33 using namespace loggers;
34 using namespace processes;
35 using namespace structures;
36 using namespace textual;
37 using namespace timely;
38 using namespace unit_test;
39
40 const bool JUST_SIZES = false;
41   // determines if we'll only compare file size and time.
42
43 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
44
45 class test_directory_tree : public virtual unit_base, virtual public application_shell
46 {
47 public:
48   test_directory_tree() : application_shell() {}
49   DEFINE_CLASS_NAME("test_directory_tree");
50   int execute();
51 };
52
53 int test_directory_tree::execute()
54 {
55   FUNCDEF("execute");
56
57   astring path = "/usr/lib";
58 #ifdef __WIN32__
59   // default path for windoze uses an area that should always exist.
60   path = environment::get("COMMONPROGRAMFILES");
61 #endif
62
63   // process the command line parameters, which are optionally a directory name and
64   // a pattern to use when scanning.
65   if (_global_argc >= 2)
66     path = _global_argv[1];
67
68   astring pattern = "*";
69   if (_global_argc >= 3)
70     pattern = _global_argv[2];
71
72   {
73 //    log(astring("Scanning directory tree at \"") + path + "\"");
74 //    log(astring("Using pattern-match \"") + pattern + "\"");
75
76     directory_tree dir(path, pattern.s());
77     ASSERT_TRUE(dir.good(), "directory_tree construction should succeed and be readable.");
78
79     dir_tree_iterator *ted = dir.start(directory_tree::prefix);
80       // create our iterator to do a prefix traversal.
81
82     int depth;  // current depth in tree.
83     filename curr;  // the current path the iterator is at.
84     string_array files;  // the filenames held at the iterator.
85
86     while (directory_tree::current(*ted, curr, files)) {
87       // we have a good directory to show.
88       directory_tree::depth(*ted, depth);
89 //      log(string_manipulation::indentation(depth * 2) + astring("[")
90 //          + curr.raw() + "]");
91       astring names;
92       for (int i = 0; i < files.length(); i++) names += files[i] + " ";
93       if (names.length()) {
94         astring split;
95         string_manipulation::split_lines(names, split, depth * 2 + 2);
96 //        log(split);
97       }
98
99       // go to the next place.
100       directory_tree::next(*ted);
101     }
102
103     directory_tree::throw_out(ted);
104   }
105
106   {
107     // second test group.  seek operation.
108 //scan the directory, create some temporary directories and junk filenames
109 //therein, then seek to that location.
110
111   }
112
113   {
114     // third test group.  tree comparison operation.
115 //    log(astring("Self-comparing directory tree at \"") + path + "\"");
116 //    log(astring("Using pattern-match \"") + pattern + "\"");
117
118 //    LOG("reading tree 1.");
119     directory_tree dir(path, pattern.s());
120     ASSERT_TRUE(dir.good(), "the directory should be readable for self-compare");
121
122     // now read a copy of the tree also.
123 //    LOG("reading tree 2.");
124     directory_tree dir2(path, pattern.s());
125     ASSERT_TRUE(dir2.good(), "the directory should read the second time fine too");
126
127     LOG("comparing the two trees.");
128     filename_list diffs;
129     directory_tree::compare_trees(dir, dir2, diffs, file_info::EQUAL_CHECKSUM_TIMESTAMP_FILESIZE);
130 LOG(diffs.text_form());
131
132     ASSERT_FALSE(diffs.elements(), "there should be no differences comparing identical dirs");
133   }
134
135   {
136     // fourth test: see if the calculate function works.
137 //    log(astring("Calculating sums for tree at \"") + path + "\"");
138 //    log(astring("Using pattern-match \"") + pattern + "\"");
139
140 //    LOG("reading tree 1.");
141     directory_tree dir(path, pattern.s());
142     ASSERT_TRUE(dir.good(), "the directory should be readable for checksums");
143
144     // now read a copy of the tree also.
145 //    LOG("reading tree 2.");
146     directory_tree dir2(path, pattern.s());
147     ASSERT_TRUE(dir2.good(), "checksummer should be able to read second time also");
148
149 //    LOG("calculating checksums for tree 1.");
150     ASSERT_TRUE(dir.calculate(JUST_SIZES), "the first checksummer tree can be calculated");
151
152 //    LOG("calculating checksums for tree 2.");
153     ASSERT_TRUE(dir2.calculate(JUST_SIZES), "the second checksummer tree can be calculated");
154
155 //    LOG("comparing the two trees.");
156     filename_list diffs;
157     directory_tree::compare_trees(dir, dir2, diffs, file_info::EQUAL_CHECKSUM_TIMESTAMP_FILESIZE);
158 //LOG(diffs.text_form());
159
160     ASSERT_FALSE(diffs.elements(), "no checksummer differences should be seen for identical directories");
161   }
162
163   {
164     // fifth test: see if the packing works.
165 //    log(astring("Reading tree for packing at \"") + path + "\"");
166 //    log(astring("Using pattern-match \"") + pattern + "\"");
167
168 //    LOG("reading tree.");
169     directory_tree dir(path, pattern.s());
170     ASSERT_TRUE(dir.good(), "packer directory should be read");
171
172 //    LOG("calculating checksums for tree.");
173     ASSERT_TRUE(dir.calculate(JUST_SIZES), "the first packer tree can be calculated");
174
175     byte_array packed_form;
176     int size_packed = dir.packed_size();
177     dir.pack(packed_form);
178 //LOG(a_sprintf("tree became %d abyte array", packed_form.length()));
179     ASSERT_EQUAL(size_packed, packed_form.length(), "packed size should be right");
180
181     directory_tree dir2;
182     ASSERT_TRUE(dir2.unpack(packed_form), "second tree can be unpacked from the first");
183
184 //    LOG("comparing the two trees.");
185     filename_list diffs;
186     directory_tree::compare_trees(dir, dir2, diffs, file_info::EQUAL_CHECKSUM_TIMESTAMP_FILESIZE);
187 //LOG(diffs.text_form());
188
189     ASSERT_FALSE(diffs.elements(), "identical directories should stay same after packing");
190
191     directory_tree::compare_trees(dir2, dir, diffs, file_info::EQUAL_CHECKSUM_TIMESTAMP_FILESIZE);
192     ASSERT_FALSE(diffs.elements(), "no differences for reverse compare identical dirs");
193   }
194
195   {
196     // sixth test: see if the make_directories function works.
197 LOG("reading tree to recreate");
198     directory_tree dir(path, pattern.s());
199     ASSERT_TRUE(dir.good(), "makedirs test directory reading");
200     filename tmpdir(environment::get("FEISTY_MEOW_GENERATED_STORE") + "/zz_balfazzaral");
201     LOG(astring("will write to tmp in ") + tmpdir);
202     basis::outcome result = dir.make_directories(tmpdir.raw());
203     ASSERT_EQUAL(result.value(), common::OKAY, "makedirs should succeed");
204     
205
206 LOG("what happened with that?  did it work?");
207
208 //hmmm: compare the directories with what we expect to be made;
209 //      do a dirtree iterator on the path, and make sure each of those exists in the target place.
210
211
212     // clean up the output directory.
213 //this won't do it; it's a directory!
214 //    bool worked = tmpdir.recursive_unlink();    
215 //    ASSERT_TRUE(worked, "removing temporary files after test");
216
217 //hmmm: plug in real recursive delete here instead.
218 basis::un_int kid;
219 launch_process::run("/bin/rm", astring("-rf ") + tmpdir.raw(), launch_process::AWAIT_APP_EXIT, kid);
220 ASSERT_FALSE(kid, "removing temporary files after test");
221
222   }
223
224
225 // nth test:
226 // combine the results of the second test with a comparison like in the
227 // third test.  delete all of those temporary files that were added.
228 // rescan tree. make sure that a tree containing the temporaries
229 // when compared with the current post-deletion tree produces a list
230 // that contains all the temporary files and directories.
231
232
233 //hmmm: more tests!
234
235   return final_report();
236 }
237
238 HOOPLE_MAIN(test_directory_tree, )
239