fs.test.h
Go to the documentation of this file.
/* -*- C++ -*- (c) 2007--2011 Petr Rockai <me@mornfall.net>
(c) 2007--2013 Enrico Zini <enrico@enricozini.org> */
#include "wibble/sys/fs.h"
#include <wibble/exception.h>
#include <cstdlib>
#include <set>
#include <cstdlib>
#include <unistd.h>
#include <wibble/test.h>
using namespace std;
using namespace wibble::sys::fs;
struct TestFs {
// Test directory iteration
Test directoryIterate() {
#ifdef POSIX
Directory dir("/");
set<string> files;
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
files.insert(*i);
assert(files.size() > 0);
assert(files.find(".") != files.end());
assert(files.find("..") != files.end());
assert(files.find("etc") != files.end());
assert(files.find("bin") != files.end());
assert(files.find("tmp") != files.end());
files.clear();
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
files.insert(*i);
assert(files.size() > 0);
assert(files.find(".") != files.end());
assert(files.find("..") != files.end());
assert(files.find("etc") != files.end());
assert(files.find("bin") != files.end());
assert(files.find("tmp") != files.end());
#endif
}
Test directoryIsdir()
{
{
Directory dir("/");
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
if (*i == "etc")
{
assert(i.isdir());
assert(!i.isreg());
}
}
{
Directory dir("/etc");
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
if (*i == "passwd")
{
assert(i.isreg());
assert(!i.isdir());
}
}
{
Directory dir("/dev");
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
{
if (*i == "null")
{
assert(i.ischr());
assert(!i.isblk());
}
else if (*i == "sda")
{
assert(i.isblk());
assert(!i.ischr());
}
}
}
}
// Ensure that nonexisting directories and files are reported as not valid
Test invalidDirectories() {
#ifdef POSIX
Directory dir1("/antaniblindalasupercazzola123456");
assert(!dir1.exists());
try {
Directory::const_iterator i = dir1.begin();
assert(false);
} catch (wibble::exception::System& e) {
}
Directory dir2("/etc/passwd");
assert(!dir2.exists());
try {
Directory::const_iterator i = dir2.begin();
assert(false);
} catch (wibble::exception::System& e) {
}
#endif
}
Test _mkPath() {
#ifdef POSIX
// Mkpath should succeed on existing directory
mkpath(".");
// Mkpath should succeed on existing directory
mkpath("./.");
// Mkpath should succeed on existing directory
mkpath("/");
#endif
}
Test _mkPath2() {
#ifdef POSIX
// Try creating a path with mkpath
system("rm -rf test-mkpath");
mkpath("test-mkpath/test-mkpath");
assert(wibble::sys::fs::access("test-mkpath", F_OK));
assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK));
system("rm -rf test-mkpath");
#endif
}
Test _mkFilePath() {
#ifdef POSIX
// Try creating a path with mkFilePath
system("rm -rf test-mkpath");
mkFilePath("test-mkpath/test-mkpath/file");
assert(wibble::sys::fs::access("test-mkpath", F_OK));
assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK));
assert(!wibble::sys::fs::access("test-mkpath/test-mkpath/file", F_OK));
system("rm -rf test-mkpath");
#endif
}
Test _mkdirIfMissing() {
// Creating works and is idempotent
{
system("rm -rf test-mkpath");
assert(!wibble::sys::fs::access("test-mkpath", F_OK));
wibble::sys::fs::mkdirIfMissing("test-mkpath");
assert(wibble::sys::fs::access("test-mkpath", F_OK));
wibble::sys::fs::mkdirIfMissing("test-mkpath");
}
// Creating fails if it exists and it is a file
{
system("rm -rf test-mkpath; touch test-mkpath");
try {
wibble::sys::fs::mkdirIfMissing("test-mkpath");
assert(false);
} catch (wibble::exception::Consistency& e) {
assert(string(e.what()).find("exists but it is not a directory") != string::npos);
}
}
// Deal with dangling symlinks
{
system("rm -rf test-mkpath; ln -s ./tmp/tmp/tmp/DOESNOTEXISTS test-mkpath");
try {
wibble::sys::fs::mkdirIfMissing("test-mkpath");
assert(false);
} catch (wibble::exception::Consistency& e) {
assert(string(e.what()).find("looks like a dangling symlink") != string::npos);
}
}
}
Test _deleteIfExists() {
#ifdef POSIX
system("rm -f does-not-exist");
assert(!deleteIfExists("does-not-exist"));
system("touch does-exist");
assert(deleteIfExists("does-exist"));
#endif
}
Test _isdir() {
#ifdef POSIX
system("rm -rf testdir");
assert(!isdir("testdir"));
system("touch testdir");
assert(!isdir("testdir"));
system("rm testdir; mkdir testdir");
assert(isdir("testdir"));
#endif
}
Test writeFileAtomically() {
using namespace wibble::sys;
string test("ciao");
fs::writeFileAtomically("testfile", test);
string test1 = readFile("testfile");
assert_eq(test1, test);
}
Test timestamp() {
#ifdef POSIX
using namespace wibble::sys;
system("rm -f testfile");
assert_eq(fs::timestamp("testfile", 0), 0);
writeFile("testfile", "");
assert(fs::timestamp("testfile") != 0);
assert(fs::timestamp("testfile", 0) != 0);
unlink("testfile");
assert_eq(fs::timestamp("testfile", 0), 0);
#endif
}
};
// vim:set ts=4 sw=4:
wibble::exception::Consistency
Exception thrown when some consistency check fails.
Definition: exception.h:255
wibble::exception::Generic::what
virtual const char * what() const
Definition: exception.h:213
wibble::exception::System
Base class for system exceptions.
Definition: exception.h:397
wibble::sys::fs::Directory::const_iterator
Definition: fs.h:161
wibble::sys::fs::Directory
Nicely wrap access to directories.
Definition: fs.h:154
wibble::sys::fs::Directory::exists
bool exists() const
Check if the directory exists.
wibble::sys::fs::Directory::begin
const_iterator begin() const
Begin iterator.
wibble::sys::fs::Directory::end
const_iterator end() const
End iterator.
exception.h
fs.h
wibble::sys::fs
Definition: fs.cpp:24
wibble::sys::fs::mkdirIfMissing
void mkdirIfMissing(const std::string &dir, mode_t mode=0777)
Create the given directory, if it does not already exists.
wibble::sys::fs::deleteIfExists
bool deleteIfExists(const std::string &file)
Delete a file if it exists.
Definition: fs.cpp:244
wibble::sys::fs::mkpath
void mkpath(const std::string &dir)
Create all the component of the given directory, including the directory itself.
wibble::sys::fs::access
bool access(const std::string &s, int m)
access() a filename
wibble::sys::fs::writeFile
void writeFile(const std::string &file, const std::string &data)
Write data to file, replacing existing contents if it already exists.
Definition: fs.cpp:198
wibble::sys::fs::writeFileAtomically
void writeFileAtomically(const std::string &file, const std::string &data)
Write data to file, replacing existing contents if it already exists.
Definition: fs.cpp:206
wibble::sys::fs::timestamp
time_t timestamp(const std::string &file)
File mtime.
Definition: fs.cpp:274
wibble::sys::fs::readFile
std::string readFile(const std::string &file)
Read whole file into memory. Throws exceptions on failure.
Definition: fs.cpp:164
wibble::sys::fs::unlink
void unlink(const std::string &fname)
Delete the file.
Definition: fs.cpp:262
wibble::sys::fs::isdir
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
wibble::sys::fs::mkFilePath
void mkFilePath(const std::string &file)
Ensure that the path to the given file exists, creating it if it does not.
wibble::sys
Definition: buffer.cpp:28
TestFs
Definition: fs.test.h:15
TestFs::writeFileAtomically
Test writeFileAtomically()
Definition: fs.test.h:194
TestFs::_isdir
Test _isdir()
Definition: fs.test.h:183
TestFs::invalidDirectories
Test invalidDirectories()
Definition: fs.test.h:85
TestFs::directoryIsdir
Test directoryIsdir()
Definition: fs.test.h:46
TestFs::_mkPath
Test _mkPath()
Definition: fs.test.h:105
TestFs::_mkdirIfMissing
Test _mkdirIfMissing()
Definition: fs.test.h:141
TestFs::directoryIterate
Test directoryIterate()
Definition: fs.test.h:18
TestFs::_mkPath2
Test _mkPath2()
Definition: fs.test.h:118
TestFs::timestamp
Test timestamp()
Definition: fs.test.h:202
TestFs::_mkFilePath
Test _mkFilePath()
Definition: fs.test.h:129
TestFs::_deleteIfExists
Test _deleteIfExists()
Definition: fs.test.h:174
test.h
Test
void Test
Definition: test.h:178
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
assert
#define assert(x)
Definition: test.h:30