diff --git a/CMakeLists.txt b/CMakeLists.txt index cbc03a26..93f505d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ ADD_BII_TARGETS() ACTIVATE_CPP14() -ADD_BOOST(filesystem) +ADD_BOOST(filesystem system) # You can safely delete lines from here... diff --git a/biicode.conf b/biicode.conf index 77033b7d..9a769a4e 100644 --- a/biicode.conf +++ b/biicode.conf @@ -4,7 +4,6 @@ cryptopp/cryptopp: 9 google/gtest: 11 messmer/cmake: 3 - messmer/tempfile: 4 [parent] messmer/cpp-utils: 2 diff --git a/tempfile/TempDir.cpp b/tempfile/TempDir.cpp new file mode 100644 index 00000000..8daa399f --- /dev/null +++ b/tempfile/TempDir.cpp @@ -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; +} + +} diff --git a/tempfile/TempDir.h b/tempfile/TempDir.h new file mode 100644 index 00000000..4e8c3e59 --- /dev/null +++ b/tempfile/TempDir.h @@ -0,0 +1,24 @@ +#pragma once +#ifndef MESSMER_TEMPFILE_TEMPDIR_H_ +#define MESSMER_TEMPFILE_TEMPDIR_H_ + +#include +#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 diff --git a/tempfile/TempFile.cpp b/tempfile/TempFile.cpp new file mode 100644 index 00000000..bea2d5aa --- /dev/null +++ b/tempfile/TempFile.cpp @@ -0,0 +1,31 @@ +#include "TempFile.h" + +#include + +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; +} + +} diff --git a/tempfile/TempFile.h b/tempfile/TempFile.h new file mode 100644 index 00000000..e5a93380 --- /dev/null +++ b/tempfile/TempFile.h @@ -0,0 +1,25 @@ +#pragma once +#ifndef MESSMER_TEMPFILE_TEMPFILE_H_ +#define MESSMER_TEMPFILE_TEMPFILE_H_ + +#include +#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 diff --git a/test/data/DataTest.cpp b/test/data/DataTest.cpp index 98e4565d..74d01fe6 100644 --- a/test/data/DataTest.cpp +++ b/test/data/DataTest.cpp @@ -2,7 +2,7 @@ #include "../../data/Data.h" #include "google/gtest/gtest.h" -#include "messmer/tempfile/src/TempFile.h" +#include "../../tempfile/TempFile.h" #include @@ -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; diff --git a/test/tempfile/TempDirTest.cpp b/test/tempfile/TempDirTest.cpp new file mode 100644 index 00000000..afd8315d --- /dev/null +++ b/test/tempfile/TempDirTest.cpp @@ -0,0 +1,58 @@ +#include "google/gtest/gtest.h" + +#include "../../tempfile/TempDir.h" + +#include + +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)); +} diff --git a/test/tempfile/TempFileTest.cpp b/test/tempfile/TempFileTest.cpp new file mode 100644 index 00000000..b20a9aaf --- /dev/null +++ b/test/tempfile/TempFileTest.cpp @@ -0,0 +1,117 @@ +#include "google/gtest/gtest.h" + +#include "../../tempfile/TempFile.h" +#include "../../tempfile/TempDir.h" + +#include + +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)); +}