1#include <c10/util/tempfile.h>
2#include <gtest/gtest.h>
3#include <sys/stat.h>
4#include <sys/types.h>
5
6#if !defined(_WIN32)
7TEST(TempFileTest, MatchesExpectedPattern) {
8 c10::TempFile pattern = c10::make_tempfile("test-pattern-");
9 ASSERT_NE(pattern.name.find("test-pattern-"), std::string::npos);
10}
11#endif // !defined(_WIN32)
12
13static bool directory_exists(const char* path) {
14 struct stat st;
15 return (stat(path, &st) == 0 && (st.st_mode & S_IFDIR));
16}
17
18TEST(TempDirTest, tryMakeTempdir) {
19 c10::optional<c10::TempDir> tempdir = c10::make_tempdir("test-dir-");
20 std::string tempdir_name = tempdir->name;
21
22 // directory should exist while tempdir is alive
23 ASSERT_TRUE(directory_exists(tempdir_name.c_str()));
24
25 // directory should not exist after tempdir destroyed
26 tempdir.reset();
27 ASSERT_FALSE(directory_exists(tempdir_name.c_str()));
28}
29