Move tempfile to cpp-utils

This commit is contained in:
Sebastian Messmer 2015-04-25 02:40:02 +02:00
parent 0b2984888d
commit 38327de0c8
9 changed files with 278 additions and 4 deletions

View File

@ -5,7 +5,7 @@ ADD_BII_TARGETS()
ACTIVATE_CPP14()
ADD_BOOST(filesystem)
ADD_BOOST(filesystem system)
# You can safely delete lines from here...

View File

@ -4,7 +4,6 @@
cryptopp/cryptopp: 9
google/gtest: 11
messmer/cmake: 3
messmer/tempfile: 4
[parent]
messmer/cpp-utils: 2

20
tempfile/TempDir.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "TempDir.h"
namespace bf = boost::filesystem;
namespace cpputils {
TempDir::TempDir()
: _path(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%")) {
bf::create_directory(_path);
}
TempDir::~TempDir() {
bf::remove_all(_path);
}
const bf::path &TempDir::path() const {
return _path;
}
}

24
tempfile/TempDir.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#ifndef MESSMER_TEMPFILE_TEMPDIR_H_
#define MESSMER_TEMPFILE_TEMPDIR_H_
#include <boost/filesystem.hpp>
#include "../macros.h"
namespace cpputils {
class TempDir {
public:
TempDir();
virtual ~TempDir();
const boost::filesystem::path &path() const;
private:
const boost::filesystem::path _path;
DISALLOW_COPY_AND_ASSIGN(TempDir);
};
}
#endif

31
tempfile/TempFile.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "TempFile.h"
#include <fstream>
namespace bf = boost::filesystem;
using std::ofstream;
namespace cpputils {
TempFile::TempFile(const bf::path &path, bool create)
: _path(path) {
if (create) {
ofstream file(_path.c_str());
}
}
TempFile::TempFile(bool create)
: TempFile(bf::unique_path(bf::temp_directory_path() / "%%%%-%%%%-%%%%-%%%%"), create) {
}
TempFile::~TempFile() {
if (bf::exists(_path)) {
bf::remove(_path);
}
}
const bf::path &TempFile::path() const {
return _path;
}
}

25
tempfile/TempFile.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#ifndef MESSMER_TEMPFILE_TEMPFILE_H_
#define MESSMER_TEMPFILE_TEMPFILE_H_
#include <boost/filesystem.hpp>
#include "../macros.h"
namespace cpputils {
class TempFile {
public:
TempFile(const boost::filesystem::path &path, bool create = true);
TempFile(bool create = true);
virtual ~TempFile();
const boost::filesystem::path &path() const;
private:
const boost::filesystem::path _path;
DISALLOW_COPY_AND_ASSIGN(TempFile);
};
}
#endif

View File

@ -2,7 +2,7 @@
#include "../../data/Data.h"
#include "google/gtest/gtest.h"
#include "messmer/tempfile/src/TempFile.h"
#include "../../tempfile/TempFile.h"
#include <fstream>
@ -10,7 +10,7 @@ using ::testing::Test;
using ::testing::WithParamInterface;
using ::testing::Values;
using tempfile::TempFile;
using cpputils::TempFile;
using std::ifstream;
using std::ofstream;

View File

@ -0,0 +1,58 @@
#include "google/gtest/gtest.h"
#include "../../tempfile/TempDir.h"
#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));
}

View File

@ -0,0 +1,117 @@
#include "google/gtest/gtest.h"
#include "../../tempfile/TempFile.h"
#include "../../tempfile/TempDir.h"
#include <fstream>
using ::testing::Test;
using std::ifstream;
using std::ofstream;
using namespace cpputils;
namespace bf = boost::filesystem;
class TempFileTest: public Test {
public:
TempFileTest(): tempdir(), filepath_sample(tempdir.path() / "myfile") {}
TempDir tempdir;
bf::path filepath_sample;
void CreateFile(const bf::path &path) {
ofstream file(path.c_str());
}
};
TEST_F(TempFileTest, FileIsCreated) {
TempFile file;
EXPECT_TRUE(bf::exists(file.path()));
EXPECT_TRUE(bf::is_regular_file(file.path()));
}
TEST_F(TempFileTest, FileIsReadable) {
TempFile file;
ifstream opened(file.path().c_str());
EXPECT_TRUE(opened.good());
}
TEST_F(TempFileTest, FileIsCreatedEmpty) {
TempFile file;
ifstream opened(file.path().c_str());
opened.get();
EXPECT_TRUE(opened.eof());
}
TEST_F(TempFileTest, FileIsWriteable) {
TempFile file;
ofstream opened(file.path().c_str());
EXPECT_TRUE(opened.good());
}
TEST_F(TempFileTest, FileIsDeletedAfterUse) {
bf::path filepath;
{
TempFile file;
filepath = file.path();
}
EXPECT_FALSE(bf::exists(filepath));
}
TEST_F(TempFileTest, DontCreateFileSpecified_FileIsNotCreated) {
TempFile file(false);
EXPECT_FALSE(bf::exists(file.path()));
}
TEST_F(TempFileTest, DontCreateFileSpecified_FileIsCreatable) {
TempFile file(false);
CreateFile(file.path());
EXPECT_TRUE(bf::exists(file.path()));
}
TEST_F(TempFileTest, DontCreateFileSpecified_FileIsDeletedAfterUse) {
bf::path filepath;
{
TempFile file(false);
CreateFile(file.path());
filepath = file.path();
}
EXPECT_FALSE(bf::exists(filepath));
}
TEST_F(TempFileTest, PathGiven_FileIsCreatedAtGivenPath) {
TempFile file(filepath_sample);
EXPECT_EQ(filepath_sample, file.path());
}
TEST_F(TempFileTest, PathGiven_FileIsCreatedAndAccessible) {
TempFile file(filepath_sample);
EXPECT_TRUE(bf::exists(filepath_sample));
}
TEST_F(TempFileTest, PathGiven_FileIsDeletedAfterUse) {
{
TempFile file(filepath_sample);
}
EXPECT_FALSE(bf::exists(filepath_sample));
}
TEST_F(TempFileTest, PathGiven_DontCreateFileSpecified_FileIsNotCreated) {
TempFile file(filepath_sample, false);
EXPECT_FALSE(bf::exists(filepath_sample));
}
TEST_F(TempFileTest, PathGiven_DontCreateFileSpecified_FileIsCreatable) {
TempFile file(filepath_sample, false);
CreateFile(filepath_sample);
EXPECT_TRUE(bf::exists(filepath_sample));
}
TEST_F(TempFileTest, PathGiven_DontCreateFileSpecified_FileIsDeletedAfterUse) {
{
TempFile file(filepath_sample, false);
CreateFile(filepath_sample);
}
EXPECT_FALSE(bf::exists(filepath_sample));
}