Skip to content

Commit

Permalink
feat: enable mocking the IFileVersionInfo (#704)
Browse files Browse the repository at this point in the history
Add a method to the `MockFileSystem` that allows setting the `IFileVersionInfo`:

```csharp
MockFileSystem fileSystem = new();
fileSystem.WithFileVersionInfo("foo.dll", b => b.SetFileVersion("2.3.4"));
fileSystem.Initialize().WithFile("foo.dll");

IFileVersionInfo fileVersionInfo = fileSystem.FileVersionInfo.GetVersionInfo("foo.dll");
fileVersionInfo.FileVersion.Should().Be("2.3.4");
```
  • Loading branch information
vbreuss authored Jan 10, 2025
1 parent 2f6e114 commit 9406d1f
Show file tree
Hide file tree
Showing 23 changed files with 1,260 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
unit-tests:
name: "Unit tests"
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
runs-on: ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
unit-tests:
name: "Unit tests"
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, windows-latest, macos-latest ]
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageVersion Include="SharpCompress" Version="0.38.0"/>
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Testably.Abstractions.Testing.Helpers;
using Testably.Abstractions.Testing.Storage;

namespace Testably.Abstractions.Testing.FileSystem;

Expand All @@ -24,7 +26,15 @@ public IFileVersionInfo GetVersionInfo(string fileName)
using IDisposable registration = _fileSystem.StatisticsRegistration
.FileVersionInfo.RegisterMethod(nameof(GetVersionInfo), fileName);

return FileVersionInfoMock.New(_fileSystem.Storage.GetLocation(fileName), _fileSystem);
fileName.EnsureValidFormat(_fileSystem, nameof(fileName));
IStorageLocation location = _fileSystem.Storage.GetLocation(fileName);
location.ThrowExceptionIfNotFound(_fileSystem);

FileVersionInfoContainer container = _fileSystem.Storage.GetVersionInfo(location)
?? FileVersionInfoContainer.None;

return FileVersionInfoMock.New(_fileSystem.Storage.GetLocation(fileName), container,
_fileSystem);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using Testably.Abstractions.Testing.Statistics;
using Testably.Abstractions.Testing.Storage;

Expand All @@ -13,11 +14,16 @@ internal sealed class FileVersionInfoMock : IFileVersionInfo
public IFileSystem FileSystem
=> _fileSystem;

private readonly FileVersionInfoContainer _container;
private readonly MockFileSystem _fileSystem;
private readonly string _path;

private FileVersionInfoMock(IStorageLocation location, MockFileSystem fileSystem)
private FileVersionInfoMock(
IStorageLocation location,
FileVersionInfoContainer container,
MockFileSystem fileSystem)
{
_container = container;
_fileSystem = fileSystem;
_path = location.FullPath;
}
Expand All @@ -33,7 +39,7 @@ public string? Comments
.FileVersionInfo.RegisterPathProperty(_path,
nameof(Comments), PropertyAccess.Get);

return "";
return _container.Comments;
}
}

Expand All @@ -46,7 +52,7 @@ public string? CompanyName
.FileVersionInfo.RegisterPathProperty(_path,
nameof(CompanyName), PropertyAccess.Get);

return "";
return _container.CompanyName;
}
}

Expand All @@ -59,7 +65,7 @@ public int FileBuildPart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FileBuildPart), PropertyAccess.Get);

return 0;
return _container.FileBuildPart;
}
}

Expand All @@ -72,7 +78,7 @@ public string? FileDescription
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FileDescription), PropertyAccess.Get);

return "";
return _container.FileDescription;
}
}

Expand All @@ -85,7 +91,7 @@ public int FileMajorPart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FileMajorPart), PropertyAccess.Get);

return 0;
return _container.FileMajorPart;
}
}

Expand All @@ -98,7 +104,7 @@ public int FileMinorPart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FileMinorPart), PropertyAccess.Get);

return 0;
return _container.FileMinorPart;
}
}

Expand All @@ -111,7 +117,7 @@ public string FileName
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FileName), PropertyAccess.Get);

return "";
return _path;
}
}

Expand All @@ -124,7 +130,7 @@ public int FilePrivatePart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FilePrivatePart), PropertyAccess.Get);

return 0;
return _container.FilePrivatePart;
}
}

Expand All @@ -137,7 +143,7 @@ public string? FileVersion
.FileVersionInfo.RegisterPathProperty(_path,
nameof(FileVersion), PropertyAccess.Get);

return "";
return _container.FileVersion;
}
}

Expand All @@ -150,7 +156,7 @@ public string? InternalName
.FileVersionInfo.RegisterPathProperty(_path,
nameof(InternalName), PropertyAccess.Get);

return "";
return _container.InternalName;
}
}

Expand All @@ -163,7 +169,7 @@ public bool IsDebug
.FileVersionInfo.RegisterPathProperty(_path,
nameof(IsDebug), PropertyAccess.Get);

return false;
return _container.IsDebug;
}
}

Expand All @@ -176,7 +182,7 @@ public bool IsPatched
.FileVersionInfo.RegisterPathProperty(_path,
nameof(IsPatched), PropertyAccess.Get);

return false;
return _container.IsPatched;
}
}

Expand All @@ -189,7 +195,7 @@ public bool IsPreRelease
.FileVersionInfo.RegisterPathProperty(_path,
nameof(IsPreRelease), PropertyAccess.Get);

return false;
return _container.IsPreRelease;
}
}

Expand All @@ -202,7 +208,7 @@ public bool IsPrivateBuild
.FileVersionInfo.RegisterPathProperty(_path,
nameof(IsPrivateBuild), PropertyAccess.Get);

return false;
return _container.IsPrivateBuild;
}
}

Expand All @@ -215,7 +221,7 @@ public bool IsSpecialBuild
.FileVersionInfo.RegisterPathProperty(_path,
nameof(IsSpecialBuild), PropertyAccess.Get);

return false;
return _container.IsSpecialBuild;
}
}

Expand All @@ -228,7 +234,7 @@ public string? Language
.FileVersionInfo.RegisterPathProperty(_path,
nameof(Language), PropertyAccess.Get);

return "";
return _container.Language;
}
}

Expand All @@ -241,7 +247,7 @@ public string? LegalCopyright
.FileVersionInfo.RegisterPathProperty(_path,
nameof(LegalCopyright), PropertyAccess.Get);

return "";
return _container.LegalCopyright;
}
}

Expand All @@ -254,7 +260,7 @@ public string? LegalTrademarks
.FileVersionInfo.RegisterPathProperty(_path,
nameof(LegalTrademarks), PropertyAccess.Get);

return "";
return _container.LegalTrademarks;
}
}

Expand All @@ -267,7 +273,7 @@ public string? OriginalFilename
.FileVersionInfo.RegisterPathProperty(_path,
nameof(OriginalFilename), PropertyAccess.Get);

return "";
return _container.OriginalFilename;
}
}

Expand All @@ -280,7 +286,7 @@ public string? PrivateBuild
.FileVersionInfo.RegisterPathProperty(_path,
nameof(PrivateBuild), PropertyAccess.Get);

return "";
return _container.PrivateBuild;
}
}

Expand All @@ -293,7 +299,7 @@ public int ProductBuildPart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(ProductBuildPart), PropertyAccess.Get);

return 0;
return _container.ProductBuildPart;
}
}

Expand All @@ -306,7 +312,7 @@ public int ProductMajorPart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(ProductMajorPart), PropertyAccess.Get);

return 0;
return _container.ProductMajorPart;
}
}

Expand All @@ -319,7 +325,7 @@ public int ProductMinorPart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(ProductMinorPart), PropertyAccess.Get);

return 0;
return _container.ProductMinorPart;
}
}

Expand All @@ -332,7 +338,7 @@ public string? ProductName
.FileVersionInfo.RegisterPathProperty(_path,
nameof(ProductName), PropertyAccess.Get);

return "";
return _container.ProductName;
}
}

Expand All @@ -345,7 +351,7 @@ public int ProductPrivatePart
.FileVersionInfo.RegisterPathProperty(_path,
nameof(ProductPrivatePart), PropertyAccess.Get);

return 0;
return _container.ProductPrivatePart;
}
}

Expand All @@ -358,7 +364,7 @@ public string? ProductVersion
.FileVersionInfo.RegisterPathProperty(_path,
nameof(ProductVersion), PropertyAccess.Get);

return "";
return _container.ProductVersion;
}
}

Expand All @@ -371,12 +377,38 @@ public string? SpecialBuild
.FileVersionInfo.RegisterPathProperty(_path,
nameof(SpecialBuild), PropertyAccess.Get);

return "";
return _container.SpecialBuild;
}
}

#endregion

internal static FileVersionInfoMock New(IStorageLocation location, MockFileSystem fileSystem)
=> new(location, fileSystem);
/// <inheritdoc cref="object.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.
StringBuilder sb = new(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();
}

internal static FileVersionInfoMock New(
IStorageLocation location,
FileVersionInfoContainer container,
MockFileSystem fileSystem)
=> new(location, container, fileSystem);
}
Loading

0 comments on commit 9406d1f

Please sign in to comment.