From 773e7db14d9b9b97ed59e82092cc67ccf0c16d64 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 26 Jan 2016 10:33:53 -0800 Subject: [PATCH] Add a helper class to generate temp paths for tests --- tests/CMakeLists.txt | 12 +++++++++--- tests/util/test_file.cpp | 31 +++++++++++++++++++++++++++++++ tests/util/test_file.hpp | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 tests/util/test_file.cpp create mode 100644 tests/util/test_file.hpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 269301c9..a0986977 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,11 +1,17 @@ -include_directories(../external/catch/single_include) +include_directories(../external/catch/single_include .) + +set(HEADERS + util/test_file.hpp +) set(SOURCES index_set.cpp main.cpp - parser.cpp) + parser.cpp + util/test_file.cpp +) -add_executable(tests ${SOURCES}) +add_executable(tests ${SOURCES} ${HEADERS}) target_link_libraries(tests realm-object-store) add_custom_target(run-tests USES_TERMINAL DEPENDS tests COMMAND ./tests) diff --git a/tests/util/test_file.cpp b/tests/util/test_file.cpp new file mode 100644 index 00000000..932218c5 --- /dev/null +++ b/tests/util/test_file.cpp @@ -0,0 +1,31 @@ +#include "util/test_file.hpp" + +#include + +#include +#include + +TestFile::TestFile() +{ + static std::string tmpdir = [] { + realm::disable_sync_to_disk(); + + const char* dir = getenv("TMPDIR"); + if (dir && *dir) + return dir; + return "/tmp"; + }(); + path = tmpdir + "/realm.XXXXXX"; + mktemp(&path[0]); + unlink(path.c_str()); +} + +TestFile::~TestFile() +{ + unlink(path.c_str()); +} + +InMemoryTestFile::InMemoryTestFile() +{ + in_memory = true; +} diff --git a/tests/util/test_file.hpp b/tests/util/test_file.hpp new file mode 100644 index 00000000..bd20d671 --- /dev/null +++ b/tests/util/test_file.hpp @@ -0,0 +1,15 @@ +#ifndef REALM_TEST_UTIL_TEST_FILE_HPP +#define REALM_TEST_UTIL_TEST_FILE_HPP + +#include "shared_realm.hpp" + +struct TestFile : realm::Realm::Config { + TestFile(); + ~TestFile(); +}; + +struct InMemoryTestFile : TestFile { + InMemoryTestFile(); +}; + +#endif