29 #include <boost/filesystem.hpp> 31 #include <std_msgs/String.h> 33 #include <gmock/gmock.h> 34 #include <gtest/gtest.h> 42 using ::testing::UnorderedElementsAre;
50 boost::filesystem::remove_all(test_dir);
52 old_home = getenv(
"HOME");
58 boost::filesystem::remove_all(test_dir);
60 if (old_home ==
nullptr) {
63 setenv(
"HOME", old_home,
true);
68 static constexpr
char test_dir[] =
"./temp_file_utils_test_dir";
69 static constexpr
char test_dir_str[] =
"~/temp_file_utils_test_dir";
83 wordexp_t wordexp_result;
84 wordexp(
"~/.ros/file_utils_test_dir/", &wordexp_result, 0);
85 write_directory = *(wordexp_result.we_wordv);
86 path = boost::filesystem::path(write_directory);
93 boost::filesystem::remove_all(path);
94 boost::filesystem::create_directories(path);
100 boost::filesystem::remove_all(path);
104 for (std::string bag_filename : bag_filenames) {
107 std_msgs::String str_msg;
108 str_msg.data = std::string(
"foo");
118 for (std::string bag_file_in_write_directory : bag_files_in_write_directory) {
119 EXPECT_EQ(boost::filesystem::extension(bag_file_in_write_directory),
".bag");
121 return bag_files_in_write_directory;
131 std::string expanded_dir;
132 setenv(
"HOME",
".",
true);
134 ASSERT_EQ(test_dir, expanded_dir);
135 ASSERT_TRUE(success);
136 ASSERT_TRUE(boost::filesystem::exists(expanded_dir));
142 boost::filesystem::create_directories(test_dir);
145 std::string expanded_dir;
146 setenv(
"HOME",
".",
true);
148 ASSERT_EQ(test_dir, expanded_dir);
149 ASSERT_TRUE(success);
150 ASSERT_TRUE(boost::filesystem::exists(expanded_dir));
155 using namespace boost::filesystem;
158 create_directories(test_dir);
159 permissions(test_dir, owner_read | group_read | others_read);
162 std::string expanded_dir;
163 setenv(
"HOME",
".",
true);
165 ASSERT_EQ(test_dir, expanded_dir);
166 ASSERT_FALSE(success);
172 std::ofstream temp_file(test_dir);
176 std::string expanded_dir;
177 setenv(
"HOME",
".",
true);
179 ASSERT_EQ(test_dir, expanded_dir);
180 ASSERT_FALSE(success);
183 TEST(DeleteFileTest, TestFileRemovalSucceeds)
185 std::string path =
"./TestRosbagRemovalSuccessfulCase.bag";
186 std::ofstream file(path);
191 TEST(DeleteFileTest, TestFileRemovalFailsFileDoesntExist)
196 TEST(DeleteFileTest, TestRemoveDirectoryFails)
198 char dir_template[] =
"/tmp/DeleteFileTestXXXXXX";
199 mkdtemp(dir_template);
204 TEST(GetRosBagStartTimeTest, SucceedsOnProperlyFormattedInputs)
219 TEST(GetRosBagStartTimeTest, ReturnsZeroOnInvalidInput)
229 createRosbagInWriteDirectory(std::vector<std::string>({
"test1.bag",
"nonbagfile",
"test2.bag"}));
233 std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(10));
235 createRosbagInWriteDirectory(std::vector<std::string>({
"test_late1.bag",
"nonbagfile2",
"test_late2.bag"}));
237 EXPECT_THAT(checkGetRosbagsToUploadFileExtensions(goal_received_time),
238 UnorderedElementsAre(write_directory +
"test1.bag", write_directory +
"test2.bag"));
244 std::vector<std::string> bag_files_to_create{
"test1.bag.test",
"test1.bag.random",
"test1.bag.how.about.this.Bag",
"text_file.txt"};
245 createRosbagInWriteDirectory(std::move(bag_files_to_create));
246 EXPECT_EQ(checkGetRosbagsToUploadFileExtensions(time_of_function_called),
247 std::vector<std::string>({}));
boost::filesystem::path path
~ExpandAndCreateDirTests()
static constexpr char test_dir_str[]
void open(std::string const &filename, uint32_t mode=bagmode::Read)
TEST(DeleteFileTest, TestFileRemovalSucceeds)
bool ExpandAndCreateDir(const std::string &dir, std::string &expanded_dir)
TEST_F(ExpandAndCreateDirTests, TestForNonexistingDirectory)
std::vector< std::string > checkGetRosbagsToUploadFileExtensions(ros::Time time_of_goal_received)
ros::Time GetRosBagStartTime(const std::string &file_path)
Get the time a rosbag started.
std::vector< std::string > GetRosbagsToUpload(const std::string &write_directory, const std::function< bool(rosbag::View &)> &select_file)
static constexpr char test_dir[]
Aws::Rosbag::RecorderErrorCode DeleteFile(const std::string &file_path)
delete a file
std::string write_directory
void createRosbagInWriteDirectory(std::vector< std::string > bag_filenames)
ExpandAndCreateDirTests()
void write(std::string const &topic, ros::MessageEvent< T > const &event)