Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override bool Exists
.DirectoryInfo.RegisterPathProperty(Location.FullPath,
nameof(Exists), PropertyAccess.Get);

return base.Exists && FileSystemType == FileSystemTypes.Directory;
return base.Exists && State.Type == FileSystemTypes.Directory;
}
}

Expand Down Expand Up @@ -158,7 +158,7 @@ public IEnumerable<IDirectoryInfo> EnumerateDirectories(
FullName,
searchPattern,
EnumerationOptionsHelper.FromSearchOption(searchOption))
.Select(location => New(location, _fileSystem));
.Select(location => WithInitializedState(New(location, _fileSystem)));
}

#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS
Expand All @@ -175,7 +175,7 @@ public IEnumerable<IDirectoryInfo> EnumerateDirectories(
FullName,
searchPattern,
enumerationOptions)
.Select(location => New(location, _fileSystem));
.Select(location => WithInitializedState(New(location, _fileSystem)));
}
#endif

Expand Down Expand Up @@ -212,7 +212,7 @@ public IEnumerable<IFileInfo> EnumerateFiles(
FullName,
searchPattern,
EnumerationOptionsHelper.FromSearchOption(searchOption))
.Select(location => FileInfoMock.New(location, _fileSystem));
.Select(location => WithInitializedState(FileInfoMock.New(location, _fileSystem)));
}

#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS
Expand All @@ -228,7 +228,7 @@ public IEnumerable<IFileInfo> EnumerateFiles(
FullName,
searchPattern,
enumerationOptions)
.Select(location => FileInfoMock.New(location, _fileSystem));
.Select(location => WithInitializedState(FileInfoMock.New(location, _fileSystem)));
}
#endif

Expand Down Expand Up @@ -266,7 +266,7 @@ public IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(
FullName,
searchPattern,
EnumerationOptionsHelper.FromSearchOption(searchOption))
.Select(location => FileSystemInfoMock.New(location, _fileSystem));
.Select(location => WithInitializedState(FileSystemInfoMock.New(location, _fileSystem)));
}

#if FEATURE_FILESYSTEM_ENUMERATION_OPTIONS
Expand All @@ -283,7 +283,7 @@ public IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(
FullName,
searchPattern,
enumerationOptions)
.Select(location => FileSystemInfoMock.New(location, _fileSystem));
.Select(location => WithInitializedState(FileSystemInfoMock.New(location, _fileSystem)));
}
#endif

Expand Down Expand Up @@ -431,6 +431,7 @@ public void MoveTo(string destDirName)
.EnsureValidFormat(_fileSystem, nameof(destDirName))),
recursive: true)
?? throw ExceptionFactory.DirectoryNotFound(FullName);
ResetCache(true);
}

#endregion
Expand All @@ -447,6 +448,13 @@ public void MoveTo(string destDirName)
return new DirectoryInfoMock(location, fileSystem);
}

private static T WithInitializedState<T>(T fileSystemInfo)
where T : FileSystemInfoMock
{
fileSystemInfo.InitializeState();
return fileSystemInfo;
}

private IEnumerable<IStorageLocation> EnumerateInternal(
FileSystemTypes fileSystemTypes,
string path,
Expand Down
26 changes: 14 additions & 12 deletions Source/Testably.Abstractions.Testing/FileSystem/FileInfoMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override bool Exists
.FileInfo.RegisterPathProperty(Location.FullPath,
nameof(Exists), PropertyAccess.Get);

return base.Exists && FileSystemType == FileSystemTypes.File;
return base.Exists && State.Type == FileSystemTypes.File;
}
}

Expand Down Expand Up @@ -101,16 +101,15 @@ public long Length
.FileInfo.RegisterPathProperty(Location.FullPath,
nameof(Length), PropertyAccess.Get);

if (Container is NullContainer ||
Container.Type != FileSystemTypes.File)
if (!State.Exists || State.Type != FileSystemTypes.File)
{
throw ExceptionFactory.FileNotFound(
_fileSystem.Execute.IsNetFramework
? Location.FriendlyName
: Location.FullPath);
}

return Container.GetBytes().Length;
return State.Length;
}
}

Expand Down Expand Up @@ -138,7 +137,11 @@ public StreamWriter AppendText()
using IDisposable registration = _fileSystem.StatisticsRegistration
.FileInfo.RegisterPathMethod(Location.FullPath, nameof(AppendText));

return new StreamWriter(Open(FileMode.Append, FileAccess.Write));
StreamWriter streamWriter = new(Open(FileMode.Append, FileAccess.Write));
#if NET8_0_OR_GREATER
ResetCache(true);
#endif
return streamWriter;
}

/// <inheritdoc cref="IFileInfo.CopyTo(string)" />
Expand Down Expand Up @@ -179,12 +182,9 @@ public FileSystemStream Create()
using IDisposable registration = _fileSystem.StatisticsRegistration
.FileInfo.RegisterPathMethod(Location.FullPath, nameof(Create));

if (!_fileSystem.Execute.IsNetFramework)
{
Refresh();
}

return _fileSystem.File.Create(FullName);
FileSystemStream stream = _fileSystem.File.Create(FullName);
ResetCache(!_fileSystem.Execute.IsNetFramework);
return stream;
}

/// <inheritdoc cref="IFileInfo.CreateText()" />
Expand All @@ -195,7 +195,7 @@ public StreamWriter CreateText()

StreamWriter streamWriter = new(_fileSystem.File.Create(FullName));
#if NET8_0_OR_GREATER
Refresh();
ResetCache(true);
#endif
return streamWriter;
}
Expand Down Expand Up @@ -232,6 +232,7 @@ public void MoveTo(string destFileName)
_fileSystem.Storage.GetLocation(destFileName
.EnsureValidArgument(_fileSystem, nameof(destFileName))))
?? throw ExceptionFactory.FileNotFound(FullName);
ResetCache(true);
}

#if FEATURE_FILE_MOVETO_OVERWRITE
Expand All @@ -248,6 +249,7 @@ public void MoveTo(string destFileName, bool overwrite)
.EnsureValidArgument(_fileSystem, nameof(destFileName))),
overwrite)
?? throw ExceptionFactory.FileNotFound(FullName);
ResetCache(true);
}
#endif

Expand Down
Loading
Loading