Skip to content

Commit

Permalink
feat: Add FileVersionInfo support (#1177)
Browse files Browse the repository at this point in the history
Added a wrapper for the immutable `FileVersionInfo`.

For testing, the `IFileVersionInfo` can be stored in the `MockFileData` as a property.
Since testing it is rarely neccessary, I didn't add a constructor for it but it's a mutable property, so it can be set anytime after initialization.
A `MockFileVersionInfo` has also been added which is `mutable`.

In a normal scenario, the `FileVersionInfo` can be accessed through the `FileVersionInfo` property of `IFileInfo`.

The `MockFileSystem`'s `AddFile` method will initialize this version if it's null, and the values will be the default (Only it's FileName will be set).

Since the `System.IO.FileInfo` does not contain a `FileVersionInfo` property, I excluded it from the `ApiParityTests`

PS.: Sorry, seems like were some auto formats on my side which removed some extra spaces and the Visual Studio's Diff viewer did not show these before commit. I'm not really familiar with github, thats why I didn't try to revert them after commit.
  • Loading branch information
t4m45 authored Dec 28, 2024
1 parent 7bcc722 commit ce67de2
Show file tree
Hide file tree
Showing 26 changed files with 1,063 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public MockFileData(MockFileData template)
/// </summary>
public byte[] Contents { get; set; }

/// <summary>
/// Gets or sets the file version info of the <see cref="MockFileData"/>
/// </summary>
public IFileVersionInfo FileVersionInfo { get; set; }

/// <summary>
/// Gets or sets the string contents of the <see cref="MockFileData"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public override void Encrypt()
var mockFileData = GetMockFileDataForWrite();
mockFileData.Attributes |= FileAttributes.Encrypted;
}

/// <inheritdoc />
public override void MoveTo(string destFileName)
{
Expand Down Expand Up @@ -323,7 +323,7 @@ public override IFileInfo Replace(string destinationFileName, string destination
mockFile.Replace(path, destinationFileName, destinationBackupFileName, ignoreMetadataErrors);
return mockFileSystem.FileInfo.New(destinationFileName);
}

/// <inheritdoc />
public override IDirectoryInfo Directory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public MockFileSystem(IDictionary<string, MockFileData> files, MockFileSystemOpt
File = new MockFile(this);
Directory = new MockDirectory(this, currentDirectory);
FileInfo = new MockFileInfoFactory(this);
FileVersionInfo = new MockFileVersionInfoFactory(this);
FileStream = new MockFileStreamFactory(this);
DirectoryInfo = new MockDirectoryInfoFactory(this);
DriveInfo = new MockDriveInfoFactory(this);
Expand Down Expand Up @@ -98,6 +99,8 @@ public MockFileSystem(IDictionary<string, MockFileData> files, MockFileSystemOpt
/// <inheritdoc />
public override IFileInfoFactory FileInfo { get; }
/// <inheritdoc />
public override IFileVersionInfoFactory FileVersionInfo { get; }
/// <inheritdoc />
public override IFileStreamFactory FileStream { get; }
/// <inheritdoc />
public override IPath Path { get; }
Expand Down Expand Up @@ -252,6 +255,8 @@ public void AddFile(string path, MockFileData mockFile)
AddDirectory(directoryPath);
}

mockFile.FileVersionInfo ??= new MockFileVersionInfo(fixedPath);

SetEntry(fixedPath, mockFile);
}

Expand Down Expand Up @@ -568,7 +573,7 @@ private bool FileIsReadOnly(string path)
}

#if FEATURE_SERIALIZABLE
[Serializable]
[Serializable]
#endif
private class FileSystemEntry
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using System.Diagnostics;
using System.Text;

namespace System.IO.Abstractions.TestingHelpers
{
/// <inheritdoc />
#if FEATURE_SERIALIZABLE
[Serializable]
#endif
public class MockFileVersionInfo : FileVersionInfoBase
{
/// <inheritdoc />
public MockFileVersionInfo(
string fileName,
string fileVersion = null,
string productVersion = null,
string fileDescription = null,
string productName = null,
string companyName = null,
string comments = null,
string internalName = null,
bool isDebug = false,
bool isPatched = false,
bool isPrivateBuild = false,
bool isPreRelease = false,
bool isSpecialBuild = false,
string language = null,
string legalCopyright = null,
string legalTrademarks = null,
string originalFilename = null,
string privateBuild = null,
string specialBuild = null)
{
FileName = fileName;
FileVersion = fileVersion;
ProductVersion = productVersion;
FileDescription = fileDescription;
ProductName = productName;
CompanyName = companyName;
Comments = comments;
InternalName = internalName;
IsDebug = isDebug;
IsPatched = isPatched;
IsPrivateBuild = isPrivateBuild;
IsPreRelease = isPreRelease;
IsSpecialBuild = isSpecialBuild;
Language = language;
LegalCopyright = legalCopyright;
LegalTrademarks = legalTrademarks;
OriginalFilename = originalFilename;
PrivateBuild = privateBuild;
SpecialBuild = specialBuild;

if (Version.TryParse(fileVersion, out Version version))
{
FileMajorPart = version.Major;
FileMinorPart = version.Minor;
FileBuildPart = version.Build;
FilePrivatePart = version.Revision;
}

var parsedProductVersion = ProductVersionParser.Parse(productVersion);

ProductMajorPart = parsedProductVersion.Major;
ProductMinorPart = parsedProductVersion.Minor;
ProductBuildPart = parsedProductVersion.Build;
ProductPrivatePart = parsedProductVersion.PrivatePart;
}

/// <inheritdoc/>
public override string FileName { get; }

/// <inheritdoc/>
public override string FileVersion { get; }

/// <inheritdoc/>
public override string ProductVersion { get; }

/// <inheritdoc/>
public override string FileDescription { get; }

/// <inheritdoc/>
public override string ProductName { get; }

/// <inheritdoc/>
public override string CompanyName { get; }

/// <inheritdoc/>
public override string Comments { get; }

/// <inheritdoc/>
public override string InternalName { get; }

/// <inheritdoc/>
public override bool IsDebug { get; }

/// <inheritdoc/>
public override bool IsPatched { get; }

/// <inheritdoc/>
public override bool IsPrivateBuild { get; }

/// <inheritdoc/>
public override bool IsPreRelease { get; }

/// <inheritdoc/>
public override bool IsSpecialBuild { get; }

/// <inheritdoc/>
public override string Language { get; }

/// <inheritdoc/>
public override string LegalCopyright { get; }

/// <inheritdoc/>
public override string LegalTrademarks { get; }

/// <inheritdoc/>
public override string OriginalFilename { get; }

/// <inheritdoc/>
public override string PrivateBuild { get; }

/// <inheritdoc/>
public override string SpecialBuild { get; }

/// <inheritdoc/>
public override int FileMajorPart { get; }

/// <inheritdoc/>
public override int FileMinorPart { get; }

/// <inheritdoc/>
public override int FileBuildPart { get; }

/// <inheritdoc/>
public override int FilePrivatePart { get; }

/// <inheritdoc/>
public override int ProductMajorPart { get; }

/// <inheritdoc/>
public override int ProductMinorPart { get; }

/// <inheritdoc/>
public override int ProductBuildPart { get; }

/// <inheritdoc/>
public override int ProductPrivatePart { get; }

/// <inheritdoc cref="FileVersionInfo.ToString" />
public override string ToString()
{
// An initial capacity of 512 was chosen because it is large enough to cover
// the size of the static strings with enough capacity left over to cover
// average length property values.
var sb = new StringBuilder(512);
sb.Append("File: ").AppendLine(FileName);
sb.Append("InternalName: ").AppendLine(InternalName);
sb.Append("OriginalFilename: ").AppendLine(OriginalFilename);
sb.Append("FileVersion: ").AppendLine(FileVersion);
sb.Append("FileDescription: ").AppendLine(FileDescription);
sb.Append("Product: ").AppendLine(ProductName);
sb.Append("ProductVersion: ").AppendLine(ProductVersion);
sb.Append("Debug: ").AppendLine(IsDebug.ToString());
sb.Append("Patched: ").AppendLine(IsPatched.ToString());
sb.Append("PreRelease: ").AppendLine(IsPreRelease.ToString());
sb.Append("PrivateBuild: ").AppendLine(IsPrivateBuild.ToString());
sb.Append("SpecialBuild: ").AppendLine(IsSpecialBuild.ToString());
sb.Append("Language: ").AppendLine(Language);
return sb.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace System.IO.Abstractions.TestingHelpers
{
/// <inheritdoc />
#if FEATURE_SERIALIZABLE
[Serializable]
#endif
public class MockFileVersionInfoFactory : IFileVersionInfoFactory
{
private readonly IMockFileDataAccessor mockFileSystem;

/// <inheritdoc />
public MockFileVersionInfoFactory(IMockFileDataAccessor mockFileSystem)
{
this.mockFileSystem = mockFileSystem ?? throw new ArgumentNullException(nameof(mockFileSystem));
}

/// <inheritdoc />
public IFileSystem FileSystem => mockFileSystem;

/// <inheritdoc />
public IFileVersionInfo GetVersionInfo(string fileName)
{
MockFileData mockFileData = mockFileSystem.GetFile(fileName);

if (mockFileData != null)
{
return mockFileData.FileVersionInfo;
}

throw CommonExceptions.FileNotFound(fileName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System.Reflection;
using System.Text.RegularExpressions;

namespace System.IO.Abstractions.TestingHelpers
{
/// <summary>
/// Provides functionality to parse a product version string into its major, minor, build, and private parts.
/// </summary>
internal static class ProductVersionParser
{
/// <summary>
/// Parses a product version string and extracts the numeric values for the major, minor, build, and private parts,
/// mimicking the behavior of the <see cref="AssemblyInformationalVersionAttribute"/> attribute.
/// </summary>
/// <param name="productVersion">The product version string to parse.</param>
/// <returns>
/// A <see cref="ProductVersion"/> object containing the parsed major, minor, build, and private parts.
/// If the input is invalid, returns a <see cref="ProductVersion"/> with all parts set to 0.
/// </returns>
/// <remarks>
/// The method splits the input string into segments separated by dots ('.') and attempts to extract
/// the leading numeric value from each segment. A maximum of 4 segments are processed; if more than
/// 4 segments are present, all segments are ignored. Additionally, if a segment does not contain
/// a valid numeric part at its start or it contains more than just a number, the rest of the segments are ignored.
/// </remarks>
public static ProductVersion Parse(string productVersion)
{
if (string.IsNullOrWhiteSpace(productVersion))
{
return new();
}

var segments = productVersion.Split('.');
if (segments.Length > 4)
{
// if more than 4 segments are present, all segments are ignored
return new();
}

var regex = new Regex(@"^\d+");

int[] parts = new int[4];

for (int i = 0; i < segments.Length; i++)
{
var match = regex.Match(segments[i]);
if (match.Success && int.TryParse(match.Value, out int number))
{
parts[i] = number;

if (match.Value != segments[i])
{
// when a segment contains more than a number, the rest of the segments are ignored
break;
}
}
else
{
// when a segment is not valid, the rest of the segments are ignored
break;
}
}

return new()
{
Major = parts[0],
Minor = parts[1],
Build = parts[2],
PrivatePart = parts[3]
};
}

/// <summary>
/// Represents a product version with numeric parts for major, minor, build, and private versions.
/// </summary>
public class ProductVersion
{
/// <summary>
/// Gets the major part of the version number
/// </summary>
public int Major { get; init; }

/// <summary>
/// Gets the minor part of the version number
/// </summary>
public int Minor { get; init; }

/// <summary>
/// Gets the build part of the version number
/// </summary>
public int Build { get; init; }

/// <summary>
/// Gets the private part of the version number
/// </summary>
public int PrivatePart { get; init; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal FileInfoBase() { }

/// <inheritdoc cref="IFileInfo.Encrypt"/>
public abstract void Encrypt();

/// <inheritdoc cref="IFileInfo.MoveTo(string)"/>
public abstract void MoveTo(string destFileName);

Expand Down Expand Up @@ -73,7 +73,7 @@ internal FileInfoBase() { }

/// <inheritdoc cref="IFileInfo.Replace(string,string,bool)"/>
public abstract IFileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);

/// <inheritdoc cref="IFileInfo.Directory"/>
public abstract IDirectoryInfo Directory { get; }

Expand Down
Loading

0 comments on commit ce67de2

Please sign in to comment.