}
backups_ = SetupBackupDirectories();
for (auto& path : backups_)
- BackupPath(path);
+ ASSERT_TRUE(BackupPath(path));
}
void TearDown() override {
ASSERT_TRUE(request_mode_ == ci::RequestMode::GLOBAL ||
kGlobalUserUid != kTestUserId));
UninstallAllSmokeApps(request_mode_);
for (auto& path : backups_)
- RestorePath(path);
+ ASSERT_TRUE(RestorePath(path));
if (request_mode_ == ci::RequestMode::USER)
ASSERT_TRUE(DeleteTestUser(kNormalUserName));
}
return CallBackend(SIZEOFARRAY(argv), argv, type, mode);
}
-void BackupPath(const bf::path& path) {
+namespace {
+
+boost::filesystem::path GetTrashPath(const boost::filesystem::path& path) {
+ return path.string() + ".trash";
+}
+
+}
+
+bool BackupPath(const bf::path& path) {
+ bf::path trash_path = GetTrashPath(path);
+ if (bf::exists(trash_path)) {
+ LOG(ERROR) << trash_path << " exists. Please remove "
+ << trash_path << " manually!";
+ return false;
+ }
bf::path backup_path = path.string() + ".bck";
std::cout << "Backup path: " << path << " to " << backup_path << std::endl;
bs::error_code error;
<< " (" << error.message() << ")";
if (bf::exists(path)) {
bf::rename(path, backup_path, error);
- if (error)
+ if (error) {
LOG(ERROR) << "Failed to setup test environment. Does some previous"
<< " test crashed? Path: "
<< backup_path << " should not exist.";
+ return false;
+ }
assert(!error);
}
+ return true;
}
-void RestorePath(const bf::path& path) {
+bool RestorePath(const bf::path& path) {
bf::path backup_path = path.string() + ".bck";
std::cout << "Restore path: " << path << " from " << backup_path << std::endl;
bs::error_code error;
bf::remove_all(path, error);
- if (error)
- LOG(ERROR) << "Remove failed: " << path
- << " (" << error.message() << ")";
+ if (error) {
+ bf::path trash_path = GetTrashPath(path);
+ LOG(ERROR) << "Remove failed: " << path << " (" << error.message() << ")";
+ std::cout << "Moving " << path << " to " << trash_path << std::endl;
+ bf::rename(path, trash_path, error);
+ if (error)
+ LOG(ERROR) << "Failed to move " << path << " to " << trash_path
+ << " (" << error.message() << ")";
+ else
+ LOG(ERROR) << trash_path << " should be removed manually!";
+ }
if (bf::exists(backup_path)) {
bf::rename(backup_path, path, error);
- if (error)
+ if (error) {
LOG(ERROR) << "Failed to restore backup path: " << backup_path
<< " (" << error.message() << ")";
+ return false;
+ }
}
+ return true;
}
std::vector<bf::path> SetupBackupDirectories() {