pytorch/c10/test/util/tempfile_test.cpp
Scott Wolchok 44cc873fba [PyTorch] Autoformat c10 (#56830)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56830

Opt into formatting on GitHub and format everything. This is a trial run before turning on formatting for more and eventually all of the codebase.

Test Plan: CI

Reviewed By: zertosh

Differential Revision: D27979080

fbshipit-source-id: a80f0c48691c08ae8ca0af06377b87e6a2351151
2021-04-30 21:23:28 -07:00

30 lines
921 B
C++

#include <c10/util/tempfile.h>
#include <gtest/gtest.h>
#include <sys/stat.h>
#include <sys/types.h>
#if !defined(_WIN32)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TEST(TempFileTest, MatchesExpectedPattern) {
c10::TempFile pattern = c10::make_tempfile("test-pattern-");
ASSERT_NE(pattern.name.find("test-pattern-"), std::string::npos);
}
#endif // !defined(_WIN32)
static bool directory_exists(const char* path) {
struct stat st;
return (stat(path, &st) == 0 && (st.st_mode & S_IFDIR));
}
TEST(TempDirTest, tryMakeTempdir) {
c10::optional<c10::TempDir> tempdir = c10::make_tempdir("test-dir-");
std::string tempdir_name = tempdir->name;
// directory should exist while tempdir is alive
ASSERT_TRUE(directory_exists(tempdir_name.c_str()));
// directory should not exist after tempdir destroyed
tempdir.reset();
ASSERT_FALSE(directory_exists(tempdir_name.c_str()));
}