From 6ce5f87d5b5fa017df019a79f694cc3eb56fabba Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Fri, 17 Nov 2023 22:08:57 +0100 Subject: [PATCH] BLI_fileops rename unittests: add tests for directory rename. Pull Request: https://projects.blender.org/blender/blender/pulls/115073 --- .../blender/blenlib/tests/BLI_fileops_test.cc | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/source/blender/blenlib/tests/BLI_fileops_test.cc b/source/blender/blenlib/tests/BLI_fileops_test.cc index 1dabb4492af..72c927a8c15 100644 --- a/source/blender/blenlib/tests/BLI_fileops_test.cc +++ b/source/blender/blenlib/tests/BLI_fileops_test.cc @@ -116,6 +116,67 @@ TEST_F(FileOpsTest, rename) ASSERT_TRUE(BLI_exists(test_filepath_dst.c_str())); close(fd_dst); + + /* + * Check directory renaming. + */ + + const std::string dir_name_src = "test_dir_src"; + const std::string dir_name_dst = "test_dir_dst"; + + const std::string test_dirpath_src = temp_dir + SEP_STR + dir_name_src; + const std::string test_dirpath_dst = temp_dir + SEP_STR + dir_name_dst; + + BLI_dir_create_recursive(test_dirpath_src.c_str()); + ASSERT_TRUE(BLI_exists(test_dirpath_src.c_str())); + + /* `test_dirpath_dst` does not exist, so regular rename should succeed. */ + ASSERT_EQ(0, BLI_rename(test_dirpath_src.c_str(), test_dirpath_dst.c_str())); + ASSERT_FALSE(BLI_exists(test_dirpath_src.c_str())); + ASSERT_TRUE(BLI_exists(test_dirpath_dst.c_str())); + + BLI_dir_create_recursive(test_dirpath_src.c_str()); + ASSERT_TRUE(BLI_exists(test_dirpath_src.c_str())); + + /* `test_dirpath_dst` now exists, so regular rename should succeed on Unix, but fail on Windows. + */ +#ifdef WIN32 + ASSERT_NE(0, BLI_rename(test_dirpath_src.c_str(), test_dirpath_dst.c_str())); + ASSERT_TRUE(BLI_exists(test_dirpath_src.c_str())); +#else + ASSERT_EQ(0, BLI_rename(test_dirpath_src.c_str(), test_dirpath_dst.c_str())); + ASSERT_FALSE(BLI_exists(test_dirpath_src.c_str())); +#endif + ASSERT_TRUE(BLI_exists(test_dirpath_dst.c_str())); + +#ifndef WIN32 + BLI_dir_create_recursive(test_dirpath_src.c_str()); +#endif + ASSERT_TRUE(BLI_exists(test_dirpath_src.c_str())); + + const std::string test_dir_filepath_src = test_dirpath_src + SEP_STR + file_name_src; + const std::string test_dir_filepath_dst = test_dirpath_dst + SEP_STR + file_name_dst; + + ASSERT_FALSE(BLI_exists(test_dir_filepath_src.c_str())); + ASSERT_FALSE(BLI_exists(test_dir_filepath_dst.c_str())); + BLI_file_touch(test_dir_filepath_src.c_str()); + ASSERT_TRUE(BLI_exists(test_dir_filepath_src.c_str())); + + /* `test_dir_filepath_src` does not exist, so regular rename should succeed. */ + ASSERT_EQ(0, BLI_rename(test_dir_filepath_src.c_str(), test_dir_filepath_dst.c_str())); + ASSERT_FALSE(BLI_exists(test_dir_filepath_src.c_str())); + ASSERT_TRUE(BLI_exists(test_dir_filepath_dst.c_str())); + + /* `test_dirpath_dst` exists and is not empty, so regular rename should fail on all platforms. */ + ASSERT_NE(0, BLI_rename(test_dirpath_src.c_str(), test_dirpath_dst.c_str())); + ASSERT_TRUE(BLI_exists(test_dirpath_src.c_str())); + ASSERT_TRUE(BLI_exists(test_dirpath_dst.c_str())); + + /* `test_dirpath_dst` exists and is not empty, so even overwrite rename should fail on all + * platforms. */ + ASSERT_NE(0, BLI_rename_overwrite(test_dirpath_src.c_str(), test_dirpath_dst.c_str())); + ASSERT_TRUE(BLI_exists(test_dirpath_src.c_str())); + ASSERT_TRUE(BLI_exists(test_dirpath_dst.c_str())); } /*