2016-02-10 00:58:03 +01:00
|
|
|
#include <gtest/gtest.h>
|
2015-04-25 02:40:02 +02:00
|
|
|
|
2016-02-11 12:53:42 +01:00
|
|
|
#include "cpp-utils/tempfile/TempDir.h"
|
2015-04-25 02:40:02 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using ::testing::Test;
|
|
|
|
using std::ofstream;
|
|
|
|
|
|
|
|
using namespace cpputils;
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
class TempDirTest: public Test {
|
|
|
|
public:
|
|
|
|
void EXPECT_ENTRY_COUNT(int expected, const bf::path &path) {
|
|
|
|
int actual = CountEntries(path);
|
|
|
|
EXPECT_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CountEntries(const bf::path &path) {
|
|
|
|
int count = 0;
|
|
|
|
for (bf::directory_iterator iter(path); iter != bf::directory_iterator(); ++iter) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateFile(const bf::path &path) {
|
|
|
|
ofstream file(path.c_str());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsCreated) {
|
|
|
|
TempDir dir;
|
|
|
|
EXPECT_TRUE(bf::exists(dir.path()));
|
|
|
|
EXPECT_TRUE(bf::is_directory(dir.path()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsCreatedEmpty) {
|
|
|
|
TempDir dir;
|
|
|
|
EXPECT_ENTRY_COUNT(0, dir.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsWriteable) {
|
|
|
|
TempDir dir;
|
|
|
|
CreateFile(dir.path() / "myfile");
|
|
|
|
EXPECT_ENTRY_COUNT(1, dir.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsDeletedAfterUse) {
|
|
|
|
bf::path dirpath;
|
|
|
|
{
|
|
|
|
TempDir dir;
|
|
|
|
dirpath = dir.path();
|
|
|
|
}
|
|
|
|
EXPECT_FALSE(bf::exists(dirpath));
|
|
|
|
}
|