Skip to content

Commit

Permalink
fixes sinjection
Browse files Browse the repository at this point in the history
Adds a workaround to test mono cecil
  • Loading branch information
codingadventures committed Nov 25, 2018
1 parent 4d7813c commit f60d507
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
8 changes: 2 additions & 6 deletions Src/BridgeVs.Build/SInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ internal class SInjection : IDisposable
{
#region [ Private Properties ]
private readonly string _assemblyLocation;
private readonly Stream _assemblyStream;
private readonly ModuleDefinition _moduleDefinition;

private static readonly Func<string, bool> IsSystemAssembly = name => name.Contains("Microsoft") || name.Contains("System") || name.Contains("mscorlib");
Expand Down Expand Up @@ -92,10 +91,7 @@ public SInjection(string assemblyLocation, string snkCertificatePath = null)
if (!FS.FileSystem.File.Exists(assemblyLocation))
throw new Exception($"Assembly at location {assemblyLocation} doesn't exist");

_assemblyStream =
FS.FileSystem.File.Open(assemblyLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

_moduleDefinition = ModuleDefinition.ReadModule(_assemblyStream, GetReaderParameters());
_moduleDefinition = ModuleDefinition.ReadModule(assemblyLocation, GetReaderParameters());

}
#endregion
Expand Down Expand Up @@ -315,7 +311,7 @@ private bool WriteAssembly()
{
_moduleDefinition.Write(file, GetWriterParameters());
}
_assemblyStream?.Dispose();
//_assemblyStream?.Dispose();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<UsingTask TaskName="MapperBuildTask" AssemblyFile="$([MSBuild]::Unescape('$(InstallFolderPath)'))\BridgeVs.Build.dll" />
<UsingTask TaskName="CleanBuildTask" AssemblyFile="$([MSBuild]::Unescape('$(InstallFolderPath)'))\BridgeVs.Build.dll" />

<Target Name="LINQBridgeAfterBuild" DependsOnTargets="CoreCompile" Condition="'True' == 'False'" >
<Target Name="LINQBridgeAfterBuild" DependsOnTargets="CoreCompile" >
<SInjectionBuildTask Assembly="@(MainAssembly->'%(FullPath)')" SolutionName="$(SolutionName)" VisualStudioVer="$(VisualStudioVersion)" Snk="$(MSBuildProjectDirectory)\$(AssemblyOriginatorKeyFile)" />
<MapperBuildTask Assembly="@(MainAssembly->'%(FullPath)')" ProjectPath="$(MSBuildProjectFullPath)" SolutionName="$(SolutionName)" VisualStudioVer="$(VisualStudioVersion)" />
</Target>

<Target Name="LINQBridgeAfterClean" DependsOnTargets="AfterClean" Condition="'True' == 'False'">
<Target Name="LINQBridgeAfterClean" DependsOnTargets="AfterClean">
<CleanBuildTask Assembly="$(AssemblyName)" SolutionName="$(SolutionName)" VisualStudioVer="$(VisualStudioVersion)"/>
</Target>

Expand Down
15 changes: 14 additions & 1 deletion Test/BridgeVs.UnitTest/Build/Tasks/SInjectionBuildTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using BridgeVs.Shared.Common;
using BridgeVs.UnitTest.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mono.Cecil;
using TypeMock.ArrangeActAssert;
using FS = BridgeVs.Shared.FileSystem.FileSystemFactory;

Expand All @@ -17,7 +18,7 @@ public class SInjectionBuildTest
private static string AssemblyToInjectLocation => typeof(CustomType1).Assembly.Location;
private static byte[] AssemblyToInjectBytes => File.ReadAllBytes(AssemblyToInjectLocation);


private static readonly MockFileSystem MockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ AssemblyToInjectLocation, new MockFileData(AssemblyToInjectBytes) }
Expand All @@ -29,6 +30,18 @@ public void Init()
{
Isolate.WhenCalled(() => FS.FileSystem).WillReturn(MockFileSystem);

object[] args =
{
AssemblyToInjectLocation, FileMode.Open, FileAccess.ReadWrite,
FileShare.Read
};
Stream access = FS.FileSystem.File.Open(AssemblyToInjectLocation, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);

Isolate.NonPublic.WhenCalled(typeof(ModuleDefinition), "GetFileStream")
.WithExactArguments(args)
.WillReturn(access);

Isolate.WhenCalled(() => CommonRegistryConfigurations.IsSolutionEnabled("", "")).WillReturn(true);
Isolate.WhenCalled(() => CommonRegistryConfigurations.Map3RdPartyAssembly("", "")).WillReturn(false);
Isolate.WhenCalled(() => CommonRegistryConfigurations.IsErrorTrackingEnabled("")).WillReturn(false);
Expand Down

0 comments on commit f60d507

Please sign in to comment.