Allow overriding existing files in addFile(). Add tests for addFile().

This commit is contained in:
Mikaël Capelle
2020-05-30 14:55:18 +02:00
parent 5c0de6b86a
commit ede01b46ed
3 changed files with 52 additions and 6 deletions
+8 -2
View File
@@ -152,11 +152,12 @@ namespace MOBase {
/**
*
*/
std::shared_ptr<FileTreeEntry> IFileTree::addFile(QString path) {
std::shared_ptr<FileTreeEntry> IFileTree::addFile(QString path, bool replaceIfExists) {
QStringList parts = splitPath(path);
// Check if the file already exists:
if (fetchEntry(parts, IFileTree::FILE_OR_DIRECTORY) != nullptr) {
auto existingEntry = fetchEntry(parts, IFileTree::FILE_OR_DIRECTORY);
if (!replaceIfExists && existingEntry != nullptr) {
return nullptr;
}
@@ -186,6 +187,11 @@ namespace MOBase {
return nullptr;
}
// Remove the existing files if there was one:
if (existingEntry) {
existingEntry->detach();
}
// Insert in the tree:
tree->entries().insert(
std::upper_bound(tree->begin(), tree->end(), entry, FileEntryComparator{}),
+7 -4
View File
@@ -638,16 +638,19 @@ namespace MOBase {
/**
* @brief Create a new file directly under this tree.
*
* This method will return a null pointer if the file already exists. This method
* invalidates iterators to this tree and all the subtrees present in the
* given path.
* This method will return a null pointer if the file already exists and if
* replaceIfExists is false. This method invalidates iterators to this tree and
* all the subtrees present in the given path.
*
* @param name Name of the file.
* @param replaceIfExists If true and an entry already exists at the given path,
* it will be replaced by a new entry. This will replace both files and
* directories.
*
* @return the entry corresponding to the create file, or a null
* pointer if the file was not created.
*/
virtual std::shared_ptr<FileTreeEntry> addFile(QString path);
virtual std::shared_ptr<FileTreeEntry> addFile(QString path, bool replaceIfExists = false);
/**
* @brief Create a new directory tree under this tree.
+37
View File
@@ -498,6 +498,43 @@ TEST(IFileTreeTest, IterOperations) {
});
}
TEST(IFileTreeTest, AddOperations) {
{
auto fileTree = FileListTree::makeTree({
{"a", true},
{"c.x", false},
{"e/q/c.t", false},
{"e/q/p", true}
});
auto map = createMapping(fileTree);
EXPECT_EQ(fileTree->addFile("a"), nullptr);
EXPECT_EQ(fileTree->addFile("c.x"), nullptr);
EXPECT_EQ(fileTree->addFile("e"), nullptr);
EXPECT_EQ(fileTree->addFile("e/q"), nullptr);
EXPECT_EQ(fileTree->addFile("e/q/c.t"), nullptr);
EXPECT_EQ(fileTree->addFile("e/q/p"), nullptr);
auto a_p = fileTree->addFile("a/p");
EXPECT_NE(a_p, nullptr);
EXPECT_EQ(a_p->parent(), map["a"]);
auto e_q_ct = fileTree->addFile("e/q/c.t", true);
EXPECT_NE(e_q_ct, nullptr);
EXPECT_EQ(e_q_ct->parent(), map["e/q"]);
EXPECT_EQ(map["e/q/c.t"]->parent(), nullptr);
EXPECT_EQ(map["e/q"]->astree()->size(), std::size_t{ 2 });
// Directory are replaced with addFile():
auto e_q = fileTree->addFile("e/q", true);
EXPECT_NE(e_q, nullptr);
EXPECT_EQ(e_q->parent(), map["e"]);
EXPECT_EQ(map["e/q"]->parent(), nullptr);
EXPECT_EQ(map["e"]->astree()->size(), std::size_t{ 1 });
}
}
TEST(IFileTreeTest, TreeInsertOperations) {
// Test failure: