You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to mock what would happen if I get a file from disk, store a FileInfoBase representation of it, delete the file from disk, try to move the representation, and see if the file exists in the new location. But I seem to be missing something in my understanding with the following code, as I would have expected that both fileExists1 and fileExists2 would operate the same and both return false.
varfilePath=@"C:\folder\file.txt";varnewFolder=@"C:\newFolder";varfileSystem=newMockFileSystem(newDictionary<string,MockFileData>{{filePath,newMockFileData(newbyte[5])},});fileSystem.AddDirectory(newFolder);varfileInfo=fileSystem.FileInfo.FromFileName(filePath);varnewDir=fileSystem.DirectoryInfo.FromDirectoryName(newFolder);// file is removedfileSystem.File.Delete(filePath);// move file wrapper to new locationvarnewPath=Path.Combine(newDir.FullName,fileInfo.Name);fileInfo.MoveTo(newPath);// Check that the file still exists on disk before trying to do anything with itvarfileExists1=fileSystem.FileExists(@"C:\newFolder\file.txt");varfileExists2=fileSystem.File.Exists(@"C:\newFolder\file.txt");
But what happens is that fileExists1 returns true, and fileExists2 throws a Null Reference Exception. Why does this happen, and what is the correct way of checking if the file exists?
The problem is actually with the fileInfo.MoveTo(newPath) line. Since the underlying file was deleted, the MoveTo should have failed with a FileNotFoundException. No exceptions were thrown and the underlying tracking code in MockFileSystem became corrupt.
I've submitted PR #236 to fix this so that a FileNotFoundException is thrown. In your case you will still need to change your code, but the real error will be clear.
Thanks for reopening. Let's leave it open until merged, since I've done the PR against this issue. I had a similar problem as you (unexpected NullReferenceException) and found this ticket while invetigating. My PR fixes both your issue and mine.
@ausarb@robertlarkins this was resolved with #299. We actually had two PRs opened for this, and both were quite stale so I just did a rebase to fix the conflicts and get this into the latest release.
Thanks for your PR and bringing this to our attention
Activity
ausarb commentedon Aug 7, 2017
The problem is actually with the fileInfo.MoveTo(newPath) line. Since the underlying file was deleted, the MoveTo should have failed with a FileNotFoundException. No exceptions were thrown and the underlying tracking code in MockFileSystem became corrupt.
I've submitted PR #236 to fix this so that a FileNotFoundException is thrown. In your case you will still need to change your code, but the real error will be clear.
robertlarkins commentedon Aug 9, 2017
Ah, that would explain it, thanks. Hopefully a merge will come soon.
robertlarkins commentedon Aug 9, 2017
On a side not, should the issue be closed now, or once the merge has occurred?
ausarb commentedon Aug 9, 2017
Thanks for reopening. Let's leave it open until merged, since I've done the PR against this issue. I had a similar problem as you (unexpected NullReferenceException) and found this ticket while invetigating. My PR fixes both your issue and mine.
jpreese commentedon Jul 7, 2018
@ausarb @robertlarkins this was resolved with #299. We actually had two PRs opened for this, and both were quite stale so I just did a rebase to fix the conflicts and get this into the latest release.
Thanks for your PR and bringing this to our attention