forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst-fs.hh
79 lines (65 loc) · 1.83 KB
/
tst-fs.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (C) 2013 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#ifndef _TST_FS_HH
#define _TST_FS_HH
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <boost/filesystem.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/format.hpp>
namespace fs = boost::filesystem;
typedef boost::format fmt;
class TempDir : public fs::path
{
public:
TempDir() : fs::path(tmpnam(NULL)) {
fs::create_directories(*this);
}
virtual ~TempDir() {
fs::remove_all(*this);
}
};
const fs::path& mkfile(const fs::path& path)
{
fs::ofstream fstream(path);
return path;
}
void assert_exists(const fs::path path)
{
struct stat buf;
int error = stat(path.c_str(), &buf);
BOOST_REQUIRE_MESSAGE(error == 0, fmt("Path %s should exist, errorno=%d") % path.string() % errno);
}
void assert_stat_error(const fs::path path, int expected_errno)
{
struct stat buf;
int error = stat(path.c_str(), &buf);
BOOST_REQUIRE_MESSAGE(error == -1, fmt("Stat on %s should fail") % path.string());
BOOST_REQUIRE_EQUAL(errno, expected_errno);
}
void assert_stat_ok(const fs::path path)
{
struct stat buf;
int error = stat(path.c_str(), &buf);
BOOST_REQUIRE_MESSAGE(error == 0, fmt("Stat on %s should succeed") % path.string());
}
void assert_lstat_error(const fs::path path, int expected_errno)
{
struct stat buf;
int error = lstat(path.c_str(), &buf);
BOOST_REQUIRE_MESSAGE(error == -1, fmt("lstat on %s should fail") % path.string());
BOOST_REQUIRE_EQUAL(errno, expected_errno);
}
void assert_lstat_ok(const fs::path path)
{
struct stat buf;
int error = lstat(path.c_str(), &buf);
BOOST_REQUIRE_MESSAGE(error == 0, fmt("lstat on %s should succeed") % path.string());
}
#endif