Merge pull request #24343 from mshabunin:fix-test-writes

Fix tests writing to current work dir #24343

Several tests were writing files in the current work directory and did not clean up after test. Moved all temporary files to the `/tmp` dir and added a cleanup code.
This commit is contained in:
Maksim Shabunin 2023-10-03 16:34:25 +03:00 committed by GitHub
parent 9f74982a54
commit 1bccc14e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 40 deletions

View File

@ -435,6 +435,8 @@ protected:
CV_Assert( ov1 == v1 );
CV_Assert( osc1 == sc1 );
CV_Assert( og1 == g1 );
fs.release();
remove(fname.c_str());
}
catch(...)
{
@ -489,6 +491,7 @@ TEST(Core_InputOutput, FileStorage)
char arr[66];
snprintf(arr, sizeof(arr), "snprintf is hell %d", 666);
EXPECT_NO_THROW(f << arr);
remove(file.c_str());
}
TEST(Core_InputOutput, FileStorageKey)
@ -534,6 +537,7 @@ TEST(Core_InputOutput, FileStorageSpaces)
ASSERT_STREQ(values[i].c_str(), valuesReadAppend[i].c_str());
}
g3.release();
EXPECT_EQ(0, remove(fileName.c_str()));
}
struct data_t
@ -585,12 +589,15 @@ struct data_t
static void test_filestorage_basic(int write_flags, const char* suffix_name, bool testReadWrite, bool useMemory = false)
{
const bool generateTestData = false; // enable to regenerate reference in opencv_extra
const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
CV_Assert(test_info);
std::string name = (std::string(test_info->test_case_name()) + "--" + test_info->name() + suffix_name);
std::string name_34 = string(cvtest::TS::ptr()->get_data_path()) + "io/3_4/" + name;
if (!testReadWrite)
if (!testReadWrite || generateTestData)
name = string(cvtest::TS::ptr()->get_data_path()) + "io/" + name;
else
name = cv::tempfile(name.c_str());
{
const size_t rawdata_N = 40;
@ -636,10 +643,7 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo
rawdata.push_back(tmp);
}
}
#ifdef GENERATE_TEST_DATA
#else
if (testReadWrite || useMemory)
#endif
if (testReadWrite || useMemory || generateTestData)
{
cv::FileStorage fs(name, write_flags + (useMemory ? cv::FileStorage::MEMORY : 0));
fs << "normal_2d_mat" << _2d_out;
@ -684,6 +688,7 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo
}
std::cout << "Storage size: " << sz << std::endl;
EXPECT_LE(sz, (size_t)6000);
}
{ /* read */
cv::FileStorage fs(name, cv::FileStorage::READ + (useMemory ? cv::FileStorage::MEMORY : 0));
@ -761,6 +766,10 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo
ASSERT_EQ(_rd_in.dims , _rd_out.dims);
ASSERT_EQ(_rd_in.depth(), _rd_out.depth());
EXPECT_EQ(0, cv::norm(_rd_in, _rd_out, NORM_INF));
if (testReadWrite && !useMemory && !generateTestData)
{
EXPECT_EQ(0, remove(name.c_str()));
}
}
}
@ -807,7 +816,7 @@ TEST(Core_InputOutput, filestorage_heap_overflow)
const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
CV_Assert(test_info);
std::string name = std::string(test_info->test_case_name()) + "--" + test_info->name();
std::string name = cv::tempfile();
const char data[] = {0x00, 0x2f, 0x4a, 0x4a, 0x50, 0x4a, 0x4a };
std::ofstream file;
@ -819,6 +828,7 @@ TEST(Core_InputOutput, filestorage_heap_overflow)
// This just shouldn't segfault, otherwise it's fine
EXPECT_ANY_THROW(FileStorage(name, FileStorage::READ));
EXPECT_EQ(0, remove(name.c_str()));
}
TEST(Core_InputOutput, filestorage_base64_valid_call)
@ -829,18 +839,6 @@ TEST(Core_InputOutput, filestorage_base64_valid_call)
: (std::string(test_info->test_case_name()) + "--" + test_info->name());
char const * filenames[] = {
"core_io_base64_other_test.yml",
"core_io_base64_other_test.xml",
"core_io_base64_other_test.json",
"core_io_base64_other_test.yml?base64",
"core_io_base64_other_test.xml?base64",
"core_io_base64_other_test.json?base64",
0
};
char const * real_name[] = {
"core_io_base64_other_test.yml",
"core_io_base64_other_test.xml",
"core_io_base64_other_test.json",
"core_io_base64_other_test.yml",
"core_io_base64_other_test.xml",
"core_io_base64_other_test.json",
@ -852,14 +850,16 @@ TEST(Core_InputOutput, filestorage_base64_valid_call)
for (int n = 0; n < 6; n++)
{
char const* suffix_name = filenames[n];
SCOPED_TRACE(suffix_name);
std::string name = basename + '_' + suffix_name;
std::string file_name = basename + '_' + real_name[n];
const int idx = n / 2;
const std::string mode_suffix = (n % 2 == 0) ? "" : "?base64";
std::string suffix_name = basename + "_" + filenames[idx];
std::string file_name = cv::tempfile(suffix_name.c_str());
std::string mode_file_name = file_name + mode_suffix;
SCOPED_TRACE(mode_file_name);
EXPECT_NO_THROW(
{
cv::FileStorage fs(name, cv::FileStorage::WRITE_BASE64);
cv::FileStorage fs(mode_file_name, cv::FileStorage::WRITE_BASE64);
fs << "manydata" << "[";
fs << "[:";
@ -887,7 +887,7 @@ TEST(Core_InputOutput, filestorage_base64_valid_call)
EXPECT_NO_THROW(
{
cv::FileStorage fs(name, cv::FileStorage::WRITE);
cv::FileStorage fs(mode_file_name, cv::FileStorage::WRITE);
fs << "manydata" << "[";
fs << str_out;
@ -931,10 +931,10 @@ TEST(Core_InputOutput, filestorage_base64_invalid_call)
0
};
for (char const ** ptr = filenames; *ptr; ptr++)
for (int idx = 0; idx < 3; ++idx)
{
char const * suffix_name = *ptr;
std::string name = basename + '_' + suffix_name;
const string base_suffix = basename + '_' + filenames[idx];
std::string name = cv::tempfile(base_suffix.c_str());
EXPECT_NO_THROW({
cv::FileStorage fs(name, cv::FileStorage::WRITE);
@ -955,7 +955,7 @@ TEST(Core_InputOutput, filestorage_base64_invalid_call)
TEST(Core_InputOutput, filestorage_yml_vec2i)
{
const std::string file_name = "vec2i.yml";
const std::string file_name = cv::tempfile("vec2i.yml");
cv::Vec2i vec(2, 1), ovec;
/* write */
@ -1037,7 +1037,7 @@ TEST(Core_InputOutput, filestorage_vec_vec_io)
}
}
String fileName = "vec_vec_io_test.";
String basename = "vec_vec_io_test.";
std::vector<String> formats;
formats.push_back("xml");
@ -1046,11 +1046,13 @@ TEST(Core_InputOutput, filestorage_vec_vec_io)
for(size_t i = 0; i < formats.size(); i++)
{
FileStorage writer(fileName + formats[i], FileStorage::WRITE);
const String basename_plus(basename + formats[i]);
const String fileName = tempfile(basename_plus.c_str());
FileStorage writer(fileName, FileStorage::WRITE);
writer << "vecVecMat" << outputMats;
writer.release();
FileStorage reader(fileName + formats[i], FileStorage::READ);
FileStorage reader(fileName, FileStorage::READ);
std::vector<std::vector<Mat> > testMats;
reader["vecVecMat"] >> testMats;
@ -1067,7 +1069,7 @@ TEST(Core_InputOutput, filestorage_vec_vec_io)
}
reader.release();
remove((fileName + formats[i]).c_str());
remove(fileName.c_str());
}
}
@ -1658,7 +1660,7 @@ TEST(Core_InputOutput, FileStorage_json_bool)
TEST(Core_InputOutput, FileStorage_free_file_after_exception)
{
const std::string fileName = "FileStorage_free_file_after_exception_test.yml";
const std::string fileName = cv::tempfile("FileStorage_free_file_after_exception_test.yml");
const std::string content = "%YAML:1.0\n cameraMatrix;:: !<tag:yaml.org,2002:opencv-matrix>\n";
std::fstream testFile;
@ -1681,11 +1683,11 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception)
TEST(Core_InputOutput, FileStorage_write_to_sequence)
{
const std::vector<std::string> formatExts = { ".yml", ".json", ".xml" };
const std::string fileName = "FileStorage_write_to_sequence";
for (const auto& ext : formatExts)
{
FileStorage fs(fileName + ext, FileStorage::WRITE);
const std::string name = tempfile(ext.c_str());
FileStorage fs(name, FileStorage::WRITE);
std::vector<int> in = { 23, 42 };
fs.startWriteStruct("some_sequence", cv::FileNode::SEQ);
for (int i : in)
@ -1693,7 +1695,7 @@ TEST(Core_InputOutput, FileStorage_write_to_sequence)
fs.endWriteStruct();
fs.release();
FileStorage fsIn(fileName + ext, FileStorage::READ);
FileStorage fsIn(name, FileStorage::READ);
FileNode seq = fsIn["some_sequence"];
FileNodeIterator it = seq.begin(), it_end = seq.end();
std::vector<int> out;
@ -1701,12 +1703,13 @@ TEST(Core_InputOutput, FileStorage_write_to_sequence)
out.push_back((int)*it);
EXPECT_EQ(in, out);
EXPECT_EQ(0, remove(name.c_str()));
}
}
TEST(Core_InputOutput, FileStorage_YAML_parse_multiple_documents)
{
const std::string filename = "FileStorage_YAML_parse_multiple_documents.yml";
const std::string filename = cv::tempfile("FileStorage_YAML_parse_multiple_documents.yml");
FileStorage fs;
fs.open(filename, FileStorage::WRITE);

View File

@ -474,12 +474,13 @@ TEST(Core_PCA, accuracy)
ASSERT_LE(err, diffBackPrjEps) << "bad accuracy of cvBackProjectPCA() (CV_PCA_DATA_AS_COL)";
#endif
// Test read and write
FileStorage fs( "PCA_store.yml", FileStorage::WRITE );
const std::string filename = cv::tempfile("PCA_store.yml");
FileStorage fs( filename, FileStorage::WRITE );
rPCA.write( fs );
fs.release();
PCA lPCA;
fs.open( "PCA_store.yml", FileStorage::READ );
fs.open( filename, FileStorage::READ );
lPCA.read( fs.root() );
err = cvtest::norm(rPCA.eigenvectors, lPCA.eigenvectors, NORM_L2 | NORM_RELATIVE);
EXPECT_LE(err, 0) << "bad accuracy of write/load functions (YML)";
@ -487,6 +488,7 @@ TEST(Core_PCA, accuracy)
EXPECT_LE(err, 0) << "bad accuracy of write/load functions (YML)";
err = cvtest::norm(rPCA.mean, lPCA.mean, NORM_L2 | NORM_RELATIVE);
EXPECT_LE(err, 0) << "bad accuracy of write/load functions (YML)";
EXPECT_EQ(0, remove(filename.c_str()));
}
class Core_ArrayOpTest : public cvtest::BaseTest

View File

@ -1045,6 +1045,7 @@ TEST(Imgcodecs_Tiff_Modes, write_multipage)
{
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), read_pages[i], pages[i]);
}
EXPECT_EQ(0, remove(tmp_filename.c_str()));
}
//==================================================================================================