wibble  0.1.28
fs.test.h
Go to the documentation of this file.
1 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2  (c) 2007 Enrico Zini <enrico@enricozini.org> */
3 #include <wibble/sys/fs.h>
4 #include <cstdlib>
5 #include <set>
6 #include <cstdlib>
7 #include <unistd.h>
8 
9 #include <wibble/test.h>
10 
11 using namespace std;
12 using namespace wibble::sys::fs;
13 
14 struct TestFs {
15 
16 // Test directory iteration
18  Directory dir("/");
19 
20  set<string> files;
21  for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
22  files.insert(*i);
23 
24  assert(files.size() > 0);
25  assert(files.find(".") != files.end());
26  assert(files.find("..") != files.end());
27  assert(files.find("etc") != files.end());
28  assert(files.find("usr") != files.end());
29  assert(files.find("tmp") != files.end());
30  }
31 
32  // Ensure that nonexisting directories and files are reported as not valid
34  Directory dir1("/antaniblindalasupercazzola123456");
35  assert(!dir1.valid());
36 
37  Directory dir2("/etc/passwd");
38  assert(!dir2.valid());
39  }
40 
42  // Mkpath should succeed on existing directory
43  mkpath(".");
44 
45  // Mkpath should succeed on existing directory
46  mkpath("./.");
47 
48  // Mkpath should succeed on existing directory
49  mkpath("/");
50  }
51 
53  // Try creating a path with mkpath
54  system("rm -rf test-mkpath");
55  mkpath("test-mkpath/test-mkpath");
56  assert(wibble::sys::fs::access("test-mkpath", F_OK));
57  assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK));
58  system("rm -rf test-mkpath");
59  }
60 
62  // Try creating a path with mkFilePath
63  system("rm -rf test-mkpath");
64  mkFilePath("test-mkpath/test-mkpath/file");
65  assert(wibble::sys::fs::access("test-mkpath", F_OK));
66  assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK));
67  assert(!wibble::sys::fs::access("test-mkpath/test-mkpath/file", F_OK));
68  system("rm -rf test-mkpath");
69  }
70 
72  system("rm -f does-not-exist");
73  assert(!deleteIfExists("does-not-exist"));
74  system("touch does-exist");
75  assert(deleteIfExists("does-exist"));
76  }
77 
79  system("rm -rf testdir");
80  assert(!isDirectory("testdir"));
81  system("touch testdir");
82  assert(!isDirectory("testdir"));
83  system("rm testdir; mkdir testdir");
84  assert(isDirectory("testdir"));
85  }
86 };
87 
88 // vim:set ts=4 sw=4: