@@ -29,7 +29,10 @@ namespace
29
29
{
30
30
using urbg_t = std::mt19937_64;
31
31
32
- std::string get_random_filename (urbg_t & urbg) { return Strings::b32_encode (urbg ()); }
32
+ std::string get_random_filename (urbg_t & urbg, StringLiteral tag)
33
+ {
34
+ return Strings::b32_encode (urbg ()).append (tag.data (), tag.size ());
35
+ }
33
36
34
37
bool is_valid_symlink_failure (const std::error_code& ec) noexcept
35
38
{
@@ -131,7 +134,7 @@ namespace
131
134
CHECK_EC_ON_FILE (base, ec);
132
135
for (unsigned int i = 0 ; i < 5 ; ++i)
133
136
{
134
- create_directory_tree (urbg, fs, base / get_random_filename (urbg), remaining_depth - 1 );
137
+ create_directory_tree (urbg, fs, base / get_random_filename (urbg, " _tree " ), remaining_depth - 1 );
135
138
}
136
139
137
140
#if !defined(_WIN32)
@@ -177,7 +180,7 @@ namespace
177
180
178
181
auto & fs = setup ();
179
182
180
- auto temp_dir = base_temporary_directory () / get_random_filename (urbg);
183
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _enum " );
181
184
INFO (" temp dir is: " << temp_dir.native ());
182
185
183
186
const auto target_root = temp_dir / " target" ;
@@ -619,7 +622,7 @@ TEST_CASE ("remove readonly", "[files]")
619
622
620
623
auto & fs = setup ();
621
624
622
- auto temp_dir = base_temporary_directory () / get_random_filename (urbg);
625
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _remove_readonly " );
623
626
INFO (" temp dir is: " << temp_dir.native ());
624
627
625
628
fs.create_directory (temp_dir, VCPKG_LINE_INFO);
@@ -672,7 +675,7 @@ TEST_CASE ("remove all", "[files]")
672
675
673
676
auto & fs = setup ();
674
677
675
- auto temp_dir = base_temporary_directory () / get_random_filename (urbg);
678
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _remove_all " );
676
679
INFO (" temp dir is: " << temp_dir.native ());
677
680
678
681
create_directory_tree (urbg, fs, temp_dir);
@@ -692,7 +695,7 @@ TEST_CASE ("remove all symlinks", "[files]")
692
695
693
696
auto & fs = setup ();
694
697
695
- auto temp_dir = base_temporary_directory () / get_random_filename (urbg);
698
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _remove_all_symlinks " );
696
699
INFO (" temp dir is: " << temp_dir.native ());
697
700
698
701
const auto target_root = temp_dir / " target" ;
@@ -840,7 +843,7 @@ TEST_CASE ("copy_file", "[files]")
840
843
841
844
auto & fs = setup ();
842
845
843
- auto temp_dir = base_temporary_directory () / get_random_filename (urbg);
846
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _copy_file " );
844
847
INFO (" temp dir is: " << temp_dir.native ());
845
848
846
849
fs.create_directory (temp_dir, VCPKG_LINE_INFO);
@@ -914,13 +917,107 @@ TEST_CASE ("copy_file", "[files]")
914
917
CHECK_EC_ON_FILE (temp_dir, ec);
915
918
}
916
919
920
+ TEST_CASE (" rename" , " [files]" )
921
+ {
922
+ urbg_t urbg;
923
+
924
+ auto & fs = setup ();
925
+
926
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _rename" );
927
+ INFO (" temp dir is: " << temp_dir.native ());
928
+
929
+ static constexpr StringLiteral FileTxt = " file.txt" ;
930
+ fs.remove_all (temp_dir, VCPKG_LINE_INFO);
931
+ fs.create_directory (temp_dir, VCPKG_LINE_INFO);
932
+ auto temp_dir_a = temp_dir / " a" ;
933
+ fs.create_directory (temp_dir_a, VCPKG_LINE_INFO);
934
+ auto temp_dir_a_file = temp_dir_a / FileTxt;
935
+ auto temp_dir_b = temp_dir / " b" ;
936
+ auto temp_dir_b_file = temp_dir_b / FileTxt;
937
+
938
+ static constexpr StringLiteral text_file_contents = " hello there" ;
939
+ fs.write_contents (temp_dir_a_file, text_file_contents, VCPKG_LINE_INFO);
940
+
941
+ // try rename_with_retry
942
+ {
943
+ fs.rename_with_retry (temp_dir_a, temp_dir_b, VCPKG_LINE_INFO);
944
+ REQUIRE (!fs.exists (temp_dir_a, VCPKG_LINE_INFO));
945
+ REQUIRE (fs.read_contents (temp_dir_b_file, VCPKG_LINE_INFO) == text_file_contents);
946
+
947
+ // put things back
948
+ fs.rename (temp_dir_b, temp_dir_a, VCPKG_LINE_INFO);
949
+ REQUIRE (fs.read_contents (temp_dir_a_file, VCPKG_LINE_INFO) == text_file_contents);
950
+ REQUIRE (!fs.exists (temp_dir_b, VCPKG_LINE_INFO));
951
+ }
952
+
953
+ // try rename_or_delete directory, target does not exist
954
+ {
955
+ REQUIRE (fs.rename_or_delete (temp_dir_a, temp_dir_b, VCPKG_LINE_INFO));
956
+ REQUIRE (!fs.exists (temp_dir_a, VCPKG_LINE_INFO));
957
+ REQUIRE (fs.read_contents (temp_dir_b_file, VCPKG_LINE_INFO) == text_file_contents);
958
+
959
+ // put things back
960
+ fs.rename (temp_dir_b, temp_dir_a, VCPKG_LINE_INFO);
961
+ REQUIRE (fs.read_contents (temp_dir_a_file, VCPKG_LINE_INFO) == text_file_contents);
962
+ REQUIRE (!fs.exists (temp_dir_b, VCPKG_LINE_INFO));
963
+ }
964
+
965
+ // try rename_or_delete directory, target exists
966
+ {
967
+ fs.create_directory (temp_dir_b, VCPKG_LINE_INFO);
968
+ fs.write_contents (temp_dir_b_file, text_file_contents, VCPKG_LINE_INFO);
969
+
970
+ // Note that the VCPKG_LINE_INFO overload implicitly tests that ec got cleared
971
+ REQUIRE (!fs.rename_or_delete (temp_dir_a, temp_dir_b, VCPKG_LINE_INFO));
972
+ REQUIRE (!fs.exists (temp_dir_a, VCPKG_LINE_INFO));
973
+ REQUIRE (fs.read_contents (temp_dir_b_file, VCPKG_LINE_INFO) == text_file_contents);
974
+
975
+ // put things back
976
+ fs.rename (temp_dir_b, temp_dir_a, VCPKG_LINE_INFO);
977
+ REQUIRE (fs.read_contents (temp_dir_a_file, VCPKG_LINE_INFO) == text_file_contents);
978
+ REQUIRE (!fs.exists (temp_dir_b, VCPKG_LINE_INFO));
979
+ }
980
+
981
+ // try rename_or_delete file, target does not exist
982
+ {
983
+ fs.create_directory (temp_dir_b, VCPKG_LINE_INFO);
984
+ REQUIRE (fs.rename_or_delete (temp_dir_a_file, temp_dir_b_file, VCPKG_LINE_INFO));
985
+ REQUIRE (!fs.exists (temp_dir_a_file, VCPKG_LINE_INFO));
986
+ REQUIRE (fs.read_contents (temp_dir_b_file, VCPKG_LINE_INFO) == text_file_contents);
987
+
988
+ // put things back
989
+ fs.rename (temp_dir_b_file, temp_dir_a_file, VCPKG_LINE_INFO);
990
+ REQUIRE (fs.read_contents (temp_dir_a_file, VCPKG_LINE_INFO) == text_file_contents);
991
+ REQUIRE (!fs.exists (temp_dir_b_file, VCPKG_LINE_INFO));
992
+ fs.remove (temp_dir_b, VCPKG_LINE_INFO);
993
+ }
994
+
995
+ // try rename_or_delete file, target exists
996
+ {
997
+ fs.create_directory (temp_dir_b, VCPKG_LINE_INFO);
998
+ fs.write_contents (temp_dir_b_file, text_file_contents, VCPKG_LINE_INFO);
999
+ // Note that the VCPKG_LINE_INFO overload implicitly tests that ec got cleared
1000
+ // Also note that POSIX rename() will just delete the target like we want by itself so
1001
+ // this returns true.
1002
+ REQUIRE (fs.rename_or_delete (temp_dir_a_file, temp_dir_b_file, VCPKG_LINE_INFO));
1003
+ REQUIRE (!fs.exists (temp_dir_a_file, VCPKG_LINE_INFO));
1004
+ REQUIRE (fs.read_contents (temp_dir_b_file, VCPKG_LINE_INFO) == text_file_contents);
1005
+
1006
+ // put things back
1007
+ fs.rename (temp_dir_b_file, temp_dir_a_file, VCPKG_LINE_INFO);
1008
+ REQUIRE (fs.read_contents (temp_dir_a_file, VCPKG_LINE_INFO) == text_file_contents);
1009
+ REQUIRE (!fs.exists (temp_dir_b_file, VCPKG_LINE_INFO));
1010
+ fs.remove (temp_dir_b, VCPKG_LINE_INFO);
1011
+ }
1012
+ }
1013
+
917
1014
TEST_CASE (" copy_symlink" , " [files]" )
918
1015
{
919
1016
urbg_t urbg;
920
1017
921
1018
auto & fs = setup ();
922
1019
923
- auto temp_dir = base_temporary_directory () / get_random_filename (urbg);
1020
+ auto temp_dir = base_temporary_directory () / get_random_filename (urbg, " _copy_symlink " );
924
1021
INFO (" temp dir is: " << temp_dir.native ());
925
1022
926
1023
fs.create_directory (temp_dir, VCPKG_LINE_INFO);
@@ -1080,7 +1177,7 @@ TEST_CASE ("remove all -- benchmarks", "[files][!benchmark]")
1080
1177
temp_dirs.resize (meter.runs ());
1081
1178
1082
1179
std::generate (begin (temp_dirs), end (temp_dirs), [&] {
1083
- Path temp_dir = base_temporary_directory () / get_random_filename (urbg);
1180
+ Path temp_dir = base_temporary_directory () / get_random_filename (urbg, " _remove_all_bench " );
1084
1181
create_directory_tree (urbg, fs, temp_dir, max_depth);
1085
1182
return temp_dir;
1086
1183
});
0 commit comments