Skip to content

Commit 5666085

Browse files
committed
add test Execute_StateUnderTest_Success()
1 parent 59c1678 commit 5666085

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

test/coverlet.msbuild.tasks.tests/InstrumentationTaskTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Coverlet.Core.Abstractions;
55
using Coverlet.Core.Helpers;
6+
using Coverlet.Core.Instrumentation;
67
using Coverlet.Core.Symbols;
78
using Coverlet.MSbuild.Tasks;
89
using Microsoft.Build.Framework;
@@ -61,5 +62,83 @@ public void Execute_StateUnderTest_Failure()
6162
Assert.NotEmpty(_errors);
6263
Assert.Contains("Module test path 'testPath' not found", _errors[0].Message);
6364
}
65+
66+
[Fact]
67+
public void Execute_StateUnderTest_Success()
68+
{
69+
70+
DirectoryInfo directory = Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), nameof(InstrumentationTaskTests)));
71+
string[] files =
72+
[
73+
"System.Private.CoreLib.dll",
74+
"System.Private.CoreLib.pdb"
75+
];
76+
77+
foreach (string file in files)
78+
{
79+
File.Copy(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets", file), Path.Combine(directory.FullName, file), overwrite: true);
80+
}
81+
// Arrange
82+
var partialMockFileSystem = new Mock<FileSystem>();
83+
partialMockFileSystem.CallBase = true;
84+
partialMockFileSystem.Setup(fs => fs.OpenRead(It.IsAny<string>())).Returns((string path) =>
85+
{
86+
if (Path.GetFileName(path.Replace(@"\", @"/")) == files[1])
87+
{
88+
return File.OpenRead(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets"), files[1]));
89+
}
90+
else
91+
{
92+
return File.OpenRead(path);
93+
}
94+
});
95+
partialMockFileSystem.Setup(fs => fs.Exists(It.IsAny<string>())).Returns((string path) =>
96+
{
97+
if (Path.GetFileName(path.Replace(@"\", @"/")) == files[1])
98+
{
99+
return File.Exists(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "TestAssets"), files[1]));
100+
}
101+
else
102+
{
103+
if (path.Contains(@":\git\runtime"))
104+
{
105+
return true;
106+
}
107+
else
108+
{
109+
return File.Exists(path);
110+
}
111+
}
112+
});
113+
114+
var log = new TaskLoggingHelper(_buildEngine.Object, "InstrumentationTask");
115+
Mock<Coverlet.Core.Abstractions.ILogger> _mockLogger = new();
116+
117+
IServiceCollection serviceCollection = new ServiceCollection();
118+
serviceCollection.AddTransient<IFileSystem>(_ => partialMockFileSystem.Object);
119+
serviceCollection.AddTransient<IProcessExitHandler, ProcessExitHandler>();
120+
serviceCollection.AddTransient<IAssemblyAdapter, AssemblyAdapter>();
121+
serviceCollection.AddTransient<Coverlet.Core.Abstractions.ILogger, MSBuildLogger>(_ => new MSBuildLogger(log));
122+
serviceCollection.AddTransient<IRetryHelper, RetryHelper>();
123+
serviceCollection.AddSingleton<IInstrumentationHelper, InstrumentationHelper>();
124+
serviceCollection.AddSingleton<ISourceRootTranslator, SourceRootTranslator>(serviceProvider => new SourceRootTranslator(_mockLogger.Object, new FileSystem()));
125+
serviceCollection.AddSingleton<ICecilSymbolHelper, CecilSymbolHelper>();
126+
127+
ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
128+
BaseTask.ServiceProvider = serviceProvider;
129+
130+
var instrumentationTask = new InstrumentationTask
131+
{
132+
Path = Path.Combine(directory.FullName, files[0]),
133+
BuildEngine = _buildEngine.Object
134+
};
135+
136+
// Act
137+
bool success = instrumentationTask.Execute();
138+
139+
// Assert
140+
Assert.True(success);
141+
Assert.Empty(_errors);
142+
}
64143
}
65144
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)