C
C#2mo ago
!Rushaan

Logical flaw in code

Basically the child paths of the folder is always missing the folder name of the folder being moved in child's new path's part even though the new path is only syntactically valid and it should not be missing it, also the unit test case passes even though it shouldn't. https://pastebin.com/G8rEt4xb
7 Replies
!Rushaan
!RushaanOP2mo ago
Also apart from that its doing what its supposed to do and the updated path of the folder being moved is correct Also if the updated child folder's path does not miss the folder being moved's name at its appropriate place in the path then the updated child folder's path would be entirely correct which is also the goal of fixing this issue js realized i need to sleep rn but id appreciate any help:) help pls my brain feels slightly cooked
glhays
glhays2mo ago
In the MoveFolder method, the folder.FolderPath is updated to newFolderPathOfFolder, but this should be passed to UpdateChildPaths instead of newFolderPath. In the UpdateChildPaths method, the replacement of oldp with newp might not include the folder name correctly. Ensure newp includes the folder name.
public async Task<FolderResponse> MoveFolder(Guid folderId, string newFolderPath)
{
Folder? folder = await _foldersRepository.GetFolderByFolderId(folderId);
if (folder == null)
{
throw new ArgumentException();
}
if (Directory.Exists(newFolderPath) == false)
{
throw new DirectoryNotFoundException();
}

string previousFolderPath = folder.FolderPath;
string newFolderPathOfFolder = Path.Combine(newFolderPath, folder.FolderName);

if (Directory.Exists(newFolderPathOfFolder))
{
throw new DuplicateFolderException();
}

Folder? newParent = await _foldersRepository.GetFolderByFolderPath(newFolderPath);
folder.FolderPath = newFolderPathOfFolder;
folder.ParentFolder = newParent!;

Folder? finalMainFolder = await _foldersRepository.UpdateFolder(folder, true, true, false, false, false, false);
await UpdateChildPaths(folder, previousFolderPath, newFolderPathOfFolder); // Pass newFolderPathOfFolder
Directory.Move(previousFolderPath, newFolderPathOfFolder);

return finalMainFolder!.ToFolderResponse();
}

private async Task UpdateChildPaths(Folder source, string oldp, string newp)
{
Queue<Folder> tempTraversal = new Queue<Folder>();
tempTraversal.Enqueue(source);
while (tempTraversal.Count > 0)
{
Folder temp = tempTraversal.Dequeue();
if (temp.FolderId != source.FolderId)
{
string folderPathBeforeAfter = temp.FolderPath;
folderPathBeforeAfter = folderPathBeforeAfter.Replace(oldp, newp);
temp.FolderPath = folderPathBeforeAfter;
await _foldersRepository.UpdateFolder(temp, true, false, false, false, false, false);
}
if (temp.SubFolders.Count > 0)
{
foreach (Folder temp2 in temp.SubFolders)
{
tempTraversal.Enqueue(temp2);
}
}
foreach (Domain.Entities.File temp2 in temp.Files)
{
string filePathBeforeAfter = temp2.FilePath;
filePathBeforeAfter = filePathBeforeAfter.Replace(oldp, newp);
temp2.FilePath = filePathBeforeAfter;
await _filesRepository.UpdateFile(temp2, true, false, false, false);
}
}
}
public async Task<FolderResponse> MoveFolder(Guid folderId, string newFolderPath)
{
Folder? folder = await _foldersRepository.GetFolderByFolderId(folderId);
if (folder == null)
{
throw new ArgumentException();
}
if (Directory.Exists(newFolderPath) == false)
{
throw new DirectoryNotFoundException();
}

string previousFolderPath = folder.FolderPath;
string newFolderPathOfFolder = Path.Combine(newFolderPath, folder.FolderName);

if (Directory.Exists(newFolderPathOfFolder))
{
throw new DuplicateFolderException();
}

Folder? newParent = await _foldersRepository.GetFolderByFolderPath(newFolderPath);
folder.FolderPath = newFolderPathOfFolder;
folder.ParentFolder = newParent!;

Folder? finalMainFolder = await _foldersRepository.UpdateFolder(folder, true, true, false, false, false, false);
await UpdateChildPaths(folder, previousFolderPath, newFolderPathOfFolder); // Pass newFolderPathOfFolder
Directory.Move(previousFolderPath, newFolderPathOfFolder);

return finalMainFolder!.ToFolderResponse();
}

private async Task UpdateChildPaths(Folder source, string oldp, string newp)
{
Queue<Folder> tempTraversal = new Queue<Folder>();
tempTraversal.Enqueue(source);
while (tempTraversal.Count > 0)
{
Folder temp = tempTraversal.Dequeue();
if (temp.FolderId != source.FolderId)
{
string folderPathBeforeAfter = temp.FolderPath;
folderPathBeforeAfter = folderPathBeforeAfter.Replace(oldp, newp);
temp.FolderPath = folderPathBeforeAfter;
await _foldersRepository.UpdateFolder(temp, true, false, false, false, false, false);
}
if (temp.SubFolders.Count > 0)
{
foreach (Folder temp2 in temp.SubFolders)
{
tempTraversal.Enqueue(temp2);
}
}
foreach (Domain.Entities.File temp2 in temp.Files)
{
string filePathBeforeAfter = temp2.FilePath;
filePathBeforeAfter = filePathBeforeAfter.Replace(oldp, newp);
temp2.FilePath = filePathBeforeAfter;
await _filesRepository.UpdateFile(temp2, true, false, false, false);
}
}
}
!Rushaan
!RushaanOP2mo ago
ohhhhhhhhhhhhhh so await UpdateChildPaths(folder, previousFolderPath, newFolderPathOfFolder); should be enough to fix the issue right tysm I think it will fix it. PS: That means theres very similar fault in unit test of that function too
glhays
glhays2mo ago
I did not focus to much on your test code.
!Rushaan
!RushaanOP2mo ago
its alr I think it has pretty much same issue thats why it was passing but first imma test out the actual function actually if I remember correctly in the assertion part of unit test I did not assert the updated child paths so its possible unit test has no issue
glhays
glhays2mo ago
I was thinking that as well.
!Rushaan
!RushaanOP2mo ago
oh btw I forgot to confirm but its working as expected now and unit tests are still passing

Did you find this page helpful?