pytorch/c10/test/util/tempfile_test.cpp
cyy 7b91f762b6 Use std::filesystem in c10 tempfile and tempdir (#106656)
This PR simplifies c10::TempFile and c10::TempDir. It also deletes Windows temp files in c10::~TempFile, this behavior is absent on the current version.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/106656
Approved by: https://github.com/ezyang
2023-09-03 13:03:10 +00:00

24 lines
733 B
C++

#include <c10/util/tempfile.h>
#include <gtest/gtest.h>
#include <optional>
TEST(TempFileTest, MatchesExpectedPattern) {
c10::TempFile pattern = c10::make_tempfile("test-pattern-");
ASSERT_TRUE(stdfs::is_regular_file(pattern.name));
#if !defined(_WIN32)
ASSERT_NE(pattern.name.find("test-pattern-"), std::string::npos);
#endif // !defined(_WIN32)
}
TEST(TempDirTest, tryMakeTempdir) {
std::optional<c10::TempDir> tempdir = c10::make_tempdir("test-dir-");
auto tempdir_name = tempdir->name;
// directory should exist while tempdir is alive
ASSERT_TRUE(stdfs::is_directory(tempdir_name));
// directory should not exist after tempdir destroyed
tempdir.reset();
ASSERT_FALSE(stdfs::is_directory(tempdir_name));
}