|
| 1 | +// Copyright (c) Toni Solarin-Sodara |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using Coverlet.Core.Abstractions; |
| 5 | +using Coverlet.Core.Helpers; |
| 6 | +using Coverlet.Core.Symbols; |
| 7 | +using Coverlet.MSbuild.Tasks; |
| 8 | +using Microsoft.Build.Framework; |
| 9 | +using Microsoft.Build.Utilities; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using Moq; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace coverlet.msbuild.tasks.tests |
| 15 | +{ |
| 16 | + public class InstrumentationTaskTests |
| 17 | + { |
| 18 | + private readonly Mock<IBuildEngine> _buildEngine; |
| 19 | + private readonly List<BuildErrorEventArgs> _errors; |
| 20 | + |
| 21 | + public InstrumentationTaskTests() |
| 22 | + { |
| 23 | + _buildEngine = new Mock<IBuildEngine>(); |
| 24 | + _errors = new List<BuildErrorEventArgs>(); |
| 25 | + _buildEngine.Setup(x => x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())).Callback<BuildErrorEventArgs>(e => _errors.Add(e)); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void Execute_StateUnderTest_Failure() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var mockFileSystem = new Mock<IFileSystem>(); |
| 33 | + mockFileSystem.Setup(x => x.Exists(It.IsAny<string>())).Returns(false); |
| 34 | + |
| 35 | + var log = new TaskLoggingHelper(_buildEngine.Object, "InstrumentationTask"); |
| 36 | + |
| 37 | + IServiceCollection serviceCollection = new ServiceCollection(); |
| 38 | + serviceCollection.AddTransient<IFileSystem>(_ => mockFileSystem.Object); |
| 39 | + serviceCollection.AddTransient<IProcessExitHandler, ProcessExitHandler>(); |
| 40 | + serviceCollection.AddTransient<IAssemblyAdapter, AssemblyAdapter>(); |
| 41 | + serviceCollection.AddTransient<Coverlet.Core.Abstractions.ILogger, MSBuildLogger>(_ => new MSBuildLogger(log)); |
| 42 | + serviceCollection.AddTransient<IRetryHelper, RetryHelper>(); |
| 43 | + serviceCollection.AddSingleton<IInstrumentationHelper, InstrumentationHelper>(); |
| 44 | + serviceCollection.AddSingleton<ISourceRootTranslator, SourceRootTranslator>(serviceProvider => new SourceRootTranslator("testPath", serviceProvider.GetRequiredService<Coverlet.Core.Abstractions.ILogger>(), mockFileSystem.Object, new Mock<IAssemblyAdapter>().Object)); |
| 45 | + serviceCollection.AddSingleton<ICecilSymbolHelper, CecilSymbolHelper>(); |
| 46 | + |
| 47 | + ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); |
| 48 | + BaseTask.ServiceProvider = serviceProvider; |
| 49 | + |
| 50 | + var instrumentationTask = new InstrumentationTask |
| 51 | + { |
| 52 | + Path = "testPath", |
| 53 | + BuildEngine = _buildEngine.Object |
| 54 | + }; |
| 55 | + |
| 56 | + // Act |
| 57 | + bool success = instrumentationTask.Execute(); |
| 58 | + |
| 59 | + // Assert |
| 60 | + Assert.False(success); |
| 61 | + Assert.NotEmpty(_errors); |
| 62 | + Assert.Contains("Module test path 'testPath' not found", _errors[0].Message); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments