Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for default values on stream creation #860

Closed
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 @@ -77,6 +77,11 @@ public interface IMockFileDataAccessor : IFileSystem
/// </summary>
PathVerifier PathVerifier { get; }

/// <summary>
/// Get if attributes for a file created via stream should be set to default or not
/// </summary>
bool SetAttributesOnStreamCreation { get; }

/// <summary>
/// Gets a reference to the underlying file system.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion src/System.IO.Abstractions.TestingHelpers/MockFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ private Stream CreateInternal(string path, FileAccess access, FileOptions option
mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path));
VerifyDirectoryExists(path);

var mockFileData = new MockFileData(new byte[0]);
var mockFileData = mockFileDataAccessor.SetAttributesOnStreamCreation
? MockFileData.NewObject
: new MockFileData(Array.Empty<byte>());

mockFileDataAccessor.AddFile(path, mockFileData);
return OpenInternal(path, FileMode.Open, access, options);
}
Expand Down
8 changes: 8 additions & 0 deletions src/System.IO.Abstractions.TestingHelpers/MockFileData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public class MockFileData
Attributes = (FileAttributes)(-1),
};

internal static readonly MockFileData NewObject = new MockFileData(string.Empty)
{
CreationTime = DateTime.Now,
LastWriteTime = DateTime.Now,
LastAccessTime = DateTime.Now,
Attributes = FileAttributes.Normal,
};

/// <summary>
/// Gets the default date time offset.
/// E.g. for not existing files.
Expand Down
6 changes: 5 additions & 1 deletion src/System.IO.Abstractions.TestingHelpers/MockFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public MockFileStream(
throw CommonExceptions.FileNotFound(path);
}

mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
var mfd = mockFileDataAccessor.SetAttributesOnStreamCreation
? MockFileData.NewObject
: new MockFileData(Array.Empty<byte>());

mockFileDataAccessor.AddFile(path, mfd);
}

this.access = access;
Expand Down
2 changes: 2 additions & 0 deletions src/System.IO.Abstractions.TestingHelpers/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public MockFileSystem(IDictionary<string, MockFileData> files, string currentDir
public IFileSystem FileSystem => this;
/// <inheritdoc />
public PathVerifier PathVerifier => pathVerifier;
/// <inheritdoc/>
public bool SetAttributesOnStreamCreation { get; set; }

private string FixPath(string path, bool checkCaps = false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,28 @@ public void MockFileInfo_CreateText_ShouldUpdateCachedDataAndReturnTrueForExists
Assert.IsTrue(fileInfo.Exists);
}

[Test]
public void MockFileInfo_Create_ShouldSetProperDefaultAttributes()
{
// Arrange
var fileSystem = new MockFileSystem();
fileSystem.SetAttributesOnStreamCreation = true;
var file = XFS.Path(@"C:\test\test.txt");
var fi = fileSystem.FileInfo.FromFileName(file);
if (!fi.Directory.Exists)
{
fi.Directory.Create();
}

// Act
using (var stream = fi.Create())
{
// Assert
Assert.AreEqual(FileAttributes.Normal, fi.Attributes);
Assert.LessOrEqual(DateTime.Now - fi.CreationTime, TimeSpan.FromSeconds(1));
}
}

[Test]
public void MockFileInfo_Delete_ShouldUpdateCachedDataAndReturnFalseForExists()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,25 @@ public void MockFileStream_Flush_ShouldNotChangePosition()
Assert.AreEqual(200, stream.Position);
}
}

[Test]
public void MockFileStream_Create_ShouldSetProperDefaultAttributes()
{
// Arrange
var fileSystem = new MockFileSystem();
fileSystem.SetAttributesOnStreamCreation = true;
var dir = XFS.Path(@"C:\test\");
var file = XFS.Path(@"C:\test\test.txt");
fileSystem.Directory.CreateDirectory(dir);

// Act
using (var stream = fileSystem.FileStream.Create(file, FileMode.Create))
{
var fi = fileSystem.FileInfo.FromFileName(file);
// Assert
Assert.AreEqual(FileAttributes.Normal, fi.Attributes);
Assert.LessOrEqual(DateTime.Now - fi.CreationTime, TimeSpan.FromSeconds(1));
}
}
}
}