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) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const int actual = CountEntries(path);
|
2015-04-25 02:40:02 +02:00
|
|
|
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) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const ofstream file(path.string().c_str());
|
2015-04-25 02:40:02 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsCreated) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const TempDir dir;
|
2015-04-25 02:40:02 +02:00
|
|
|
EXPECT_TRUE(bf::exists(dir.path()));
|
|
|
|
EXPECT_TRUE(bf::is_directory(dir.path()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsCreatedEmpty) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const TempDir dir;
|
2015-04-25 02:40:02 +02:00
|
|
|
EXPECT_ENTRY_COUNT(0, dir.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsWriteable) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const TempDir dir;
|
2015-04-25 02:40:02 +02:00
|
|
|
CreateFile(dir.path() / "myfile");
|
|
|
|
EXPECT_ENTRY_COUNT(1, dir.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TempDirTest, DirIsDeletedAfterUse) {
|
|
|
|
bf::path dirpath;
|
|
|
|
{
|
2023-07-08 23:48:59 +02:00
|
|
|
const TempDir dir;
|
2015-04-25 02:40:02 +02:00
|
|
|
dirpath = dir.path();
|
|
|
|
}
|
|
|
|
EXPECT_FALSE(bf::exists(dirpath));
|
|
|
|
}
|