diff options
author | Alex Vallée <avallee@chromium.org> | 2015-05-06 16:26:00 -0400 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2015-07-30 15:08:53 -0700 |
commit | 47d67c96ec991ef1690b4c07188335cbc4bfa2aa (patch) | |
tree | 172e26db49ad9f4d57b47fc17f31622e47b56558 /base | |
parent | eb0b151369371993a70c2079b8253f6dd24814b7 (diff) |
Write mkdirs in more idiomatic C++ style.
~ Rewrote mkdirs to be in C++ style.
~ Replaced adb_dir{start,stop} with std::string params and (r)find.
+ Added test for mkdirs.
Also make base/test_utils.h public and support temporary directories
as well as files.
Change-Id: I6fcbdc5e0099f3359d3aac6b00c436f250ca1329
Diffstat (limited to 'base')
-rw-r--r-- | base/Android.mk | 2 | ||||
-rw-r--r-- | base/file_test.cpp | 14 | ||||
-rw-r--r-- | base/include/base/test_utils.h (renamed from base/test_utils.h) | 25 | ||||
-rw-r--r-- | base/logging_test.cpp | 2 | ||||
-rw-r--r-- | base/test_utils.cpp | 43 |
5 files changed, 64 insertions, 22 deletions
diff --git a/base/Android.mk b/base/Android.mk index 7bd317b77..4e135f653 100644 --- a/base/Android.mk +++ b/base/Android.mk @@ -21,6 +21,7 @@ libbase_src_files := \ logging.cpp \ stringprintf.cpp \ strings.cpp \ + test_utils.cpp \ libbase_test_src_files := \ file_test.cpp \ @@ -28,7 +29,6 @@ libbase_test_src_files := \ stringprintf_test.cpp \ strings_test.cpp \ test_main.cpp \ - test_utils.cpp \ libbase_cppflags := \ -Wall \ diff --git a/base/file_test.cpp b/base/file_test.cpp index b138094e3..4056684ef 100644 --- a/base/file_test.cpp +++ b/base/file_test.cpp @@ -24,7 +24,7 @@ #include <string> -#include "test_utils.h" +#include "base/test_utils.h" TEST(file, ReadFileToString_ENOENT) { std::string s("hello"); @@ -47,10 +47,10 @@ TEST(file, ReadFileToString_success) { TEST(file, WriteStringToFile) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); - ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename)) + ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path)) << strerror(errno); std::string s; - ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) + ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) << strerror(errno); EXPECT_EQ("abc", s); } @@ -61,16 +61,16 @@ TEST(file, WriteStringToFile) { TEST(file, WriteStringToFile2) { TemporaryFile tf; ASSERT_TRUE(tf.fd != -1); - ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.filename, 0660, + ASSERT_TRUE(android::base::WriteStringToFile("abc", tf.path, 0660, getuid(), getgid())) << strerror(errno); struct stat sb; - ASSERT_EQ(0, stat(tf.filename, &sb)); + ASSERT_EQ(0, stat(tf.path, &sb)); ASSERT_EQ(0660U, static_cast<unsigned int>(sb.st_mode & ~S_IFMT)); ASSERT_EQ(getuid(), sb.st_uid); ASSERT_EQ(getgid(), sb.st_gid); std::string s; - ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) + ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) << strerror(errno); EXPECT_EQ("abc", s); } @@ -109,7 +109,7 @@ TEST(file, WriteFully) { ASSERT_TRUE(tf.fd != -1); ASSERT_TRUE(android::base::WriteFully(tf.fd, "abc", 3)); std::string s; - ASSERT_TRUE(android::base::ReadFileToString(tf.filename, &s)) + ASSERT_TRUE(android::base::ReadFileToString(tf.path, &s)) << strerror(errno); EXPECT_EQ("abc", s); } diff --git a/base/test_utils.h b/base/include/base/test_utils.h index 132d3a767..83f0f1ceb 100644 --- a/base/test_utils.h +++ b/base/include/base/test_utils.h @@ -17,16 +17,37 @@ #ifndef TEST_UTILS_H #define TEST_UTILS_H +#include <string> + +#include <base/macros.h> + class TemporaryFile { public: TemporaryFile(); ~TemporaryFile(); int fd; - char filename[1024]; + char path[1024]; + + private: + void init(const std::string& tmp_dir); + + DISALLOW_COPY_AND_ASSIGN(TemporaryFile); +}; + +#if !defined(_WIN32) +class TemporaryDir { + public: + TemporaryDir(); + ~TemporaryDir(); + + char path[1024]; private: - void init(const char* tmp_dir); + bool init(const std::string& tmp_dir); + + DISALLOW_COPY_AND_ASSIGN(TemporaryDir); }; +#endif #endif // TEST_UTILS_H diff --git a/base/logging_test.cpp b/base/logging_test.cpp index c91857a0a..1a92c9451 100644 --- a/base/logging_test.cpp +++ b/base/logging_test.cpp @@ -21,7 +21,7 @@ #include "base/file.h" #include "base/stringprintf.h" -#include "test_utils.h" +#include "base/test_utils.h" #include <gtest/gtest.h> diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 0517bc713..dceb8b788 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "test_utils.h" +#include "base/test_utils.h" #include <fcntl.h> #include <stdio.h> @@ -26,34 +26,55 @@ #include <windows.h> #endif -TemporaryFile::TemporaryFile() { +#include <string> + +static std::string GetSystemTempDir() { #if defined(__ANDROID__) - init("/data/local/tmp"); + return "/data/local/tmp"; #elif defined(_WIN32) char wd[MAX_PATH] = {}; _getcwd(wd, sizeof(wd)); - init(wd); + return wd; #else - init("/tmp"); + return "/tmp"; #endif } +TemporaryFile::TemporaryFile() { + init(GetSystemTempDir()); +} + TemporaryFile::~TemporaryFile() { close(fd); - unlink(filename); + unlink(path); } -void TemporaryFile::init(const char* tmp_dir) { - snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); +void TemporaryFile::init(const std::string& tmp_dir) { + snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir.c_str()); #if !defined(_WIN32) - fd = mkstemp(filename); + fd = mkstemp(path); #else // Windows doesn't have mkstemp, and tmpfile creates the file in the root // directory, requiring root (?!) permissions. We have to settle for mktemp. - if (mktemp(filename) == nullptr) { + if (mktemp(path) == nullptr) { abort(); } - fd = open(filename, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); + fd = open(path, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); #endif } + +#if !defined(_WIN32) +TemporaryDir::TemporaryDir() { + init(GetSystemTempDir()); +} + +TemporaryDir::~TemporaryDir() { + rmdir(path); +} + +bool TemporaryDir::init(const std::string& tmp_dir) { + snprintf(path, sizeof(path), "%s/TemporaryDir-XXXXXX", tmp_dir.c_str()); + return (mkdtemp(path) != nullptr); +} +#endif |