|
3 | 3 |
|
4 | 4 | using Coverlet.Core.Abstractions;
|
5 | 5 | using Coverlet.Core.Helpers;
|
| 6 | +using Coverlet.Core.Instrumentation; |
6 | 7 | using Coverlet.Core.Symbols;
|
7 | 8 | using Coverlet.MSbuild.Tasks;
|
8 | 9 | using Microsoft.Build.Framework;
|
@@ -61,5 +62,83 @@ public void Execute_StateUnderTest_Failure()
|
61 | 62 | Assert.NotEmpty(_errors);
|
62 | 63 | Assert.Contains("Module test path 'testPath' not found", _errors[0].Message);
|
63 | 64 | }
|
| 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 | + } |
64 | 143 | }
|
65 | 144 | }
|
0 commit comments