gtest: Add a testcase wrapper for handling tmpdirs

This makes it trivial to have a pristine temp directory on every test
case execution. Just derive from the provided class.

Change-Id: I5355914cdedc482eddd0749a9ccc10fc93de6571
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/gtest/tmp.cpp b/src/gtest/tmp.cpp
new file mode 100644
index 0000000..07dfb6d
--- /dev/null
+++ b/src/gtest/tmp.cpp
@@ -0,0 +1,48 @@
+#include <fmt/format.h>
+
+#include <filesystem>
+#include <stdplus/gtest/tmp.hpp>
+
+namespace stdplus
+{
+namespace gtest
+{
+
+TestWithTmp::TestWithTmp() :
+    casedir(fmt::format(
+        "{}/{}", SuiteTmpDir(),
+        ::testing::UnitTest::GetInstance()->current_test_info()->name()))
+{
+    std::filesystem::create_directory(CaseTmpDir());
+}
+
+TestWithTmp::~TestWithTmp()
+{
+    std::filesystem::remove_all(CaseTmpDir());
+}
+
+void TestWithTmp::SetUpTestSuite()
+{
+    std::filesystem::create_directory(SuiteTmpDir());
+}
+
+void TestWithTmp::TearDownTestSuite()
+{
+    std::filesystem::remove_all(SuiteTmpDir());
+}
+
+std::string TestWithTmp::SuiteTmpDir()
+{
+    const char* dir = getenv("TMPDIR");
+    if (dir == nullptr)
+    {
+        dir = "/tmp";
+    }
+    return fmt::format(
+        "{}/{}-{}", dir,
+        ::testing::UnitTest::GetInstance()->current_test_suite()->name(),
+        getpid());
+}
+
+} // namespace gtest
+} // namespace stdplus
diff --git a/src/meson.build b/src/meson.build
index 3f1021e..6ff400d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -112,3 +112,39 @@
     version: meson.project_version(),
     requires: stdplus_reqs)
 endif
+
+if has_gtest
+  stdplus_gtest_deps = [
+    fmt_dep,
+    gtest_dep,
+  ]
+
+  stdplus_gtest_pre = declare_dependency(
+    include_directories: stdplus_gtest_headers,
+    dependencies: stdplus_gtest_deps)
+
+  stdplus_gtest_lib = library(
+    'stdplus-gtest',
+    'gtest/tmp.cpp',
+    dependencies: stdplus_gtest_pre,
+    implicit_include_directories: false,
+    version: meson.project_version(),
+    install: true)
+
+  stdplus_gtest_dep = declare_dependency(
+    dependencies: stdplus_gtest_pre,
+    link_with: stdplus_gtest_lib)
+
+  stdplus_gtest_reqs = []
+  foreach dep : stdplus_gtest_deps
+    if dep.type_name() == 'pkgconfig'
+      stdplus_gtest_reqs += dep
+    endif
+  endforeach
+
+  import('pkgconfig').generate(
+    stdplus_gtest_lib,
+    description: 'C++ helper utilities',
+    version: meson.project_version(),
+    requires: stdplus_gtest_reqs)
+endif