|
1 | 1 | namespace Microsoft.ComponentDetection.Detectors.Tests;
|
2 | 2 |
|
| 3 | +using System; |
| 4 | +using System.IO; |
3 | 5 | using System.Linq;
|
4 | 6 | using System.Threading.Tasks;
|
5 | 7 | using FluentAssertions;
|
| 8 | +using Microsoft.Build.Logging.StructuredLogger; |
6 | 9 | using Microsoft.ComponentDetection.Contracts;
|
7 | 10 | using Microsoft.ComponentDetection.Contracts.TypedComponent;
|
8 | 11 | using Microsoft.ComponentDetection.Detectors.NuGet;
|
9 | 12 | using Microsoft.ComponentDetection.TestsUtilities;
|
10 | 13 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
11 | 14 |
|
| 15 | +using MSBuildTask = Microsoft.Build.Logging.StructuredLogger.Task; |
| 16 | +using Task = System.Threading.Tasks.Task; |
| 17 | + |
12 | 18 | [TestClass]
|
13 | 19 | [TestCategory("Governance/All")]
|
14 | 20 | [TestCategory("Governance/ComponentDetection")]
|
15 | 21 | public class NuGetMSBuildBinaryLogComponentDetectorTests : BaseDetectorTest<NuGetMSBuildBinaryLogComponentDetector>
|
16 | 22 | {
|
| 23 | + static NuGetMSBuildBinaryLogComponentDetectorTests() => NuGetMSBuildBinaryLogComponentDetector.EnsureMSBuildIsRegistered(); |
| 24 | + |
17 | 25 | [TestMethod]
|
18 | 26 | public async Task DependenciesAreReportedForEachProjectFile()
|
19 | 27 | {
|
@@ -206,12 +214,224 @@ public async Task PackagesReportedFromSeparateProjectsDoNotOverlap()
|
206 | 214 | solutionComponents.Should().BeEmpty();
|
207 | 215 | }
|
208 | 216 |
|
| 217 | + [TestMethod] |
| 218 | + public async Task PackagesImplicitlyAddedBySdkDuringPublishAreAdded() |
| 219 | + { |
| 220 | + // When a project is published, the SDK will add references to some AppHost specific packages. Doing an actual |
| 221 | + // publish operation here would be too slow, so a mock in-memory binary log is used that has the same shape |
| 222 | + // (although trimmed down) of a real publish log. |
| 223 | + var binlog = new Build() |
| 224 | + { |
| 225 | + Succeeded = true, |
| 226 | + Children = |
| 227 | + { |
| 228 | + new Project() |
| 229 | + { |
| 230 | + ProjectFile = "project.csproj", |
| 231 | + Children = |
| 232 | + { |
| 233 | + new Target() |
| 234 | + { |
| 235 | + Name = "ResolveFrameworkReferences", |
| 236 | + Children = |
| 237 | + { |
| 238 | + // ResolvedAppHostPack |
| 239 | + new MSBuildTask() |
| 240 | + { |
| 241 | + Name = "GetPackageDirectory", |
| 242 | + Children = |
| 243 | + { |
| 244 | + new Folder() |
| 245 | + { |
| 246 | + Name = "OutputItems", |
| 247 | + Children = |
| 248 | + { |
| 249 | + new AddItem() |
| 250 | + { |
| 251 | + Name = "ResolvedAppHostPack", |
| 252 | + Children = |
| 253 | + { |
| 254 | + new Item() |
| 255 | + { |
| 256 | + Name = "AppHost", |
| 257 | + Children = |
| 258 | + { |
| 259 | + new Metadata() |
| 260 | + { |
| 261 | + Name = "NuGetPackageId", |
| 262 | + Value = "Microsoft.NETCore.App.Host.win-x64", |
| 263 | + }, |
| 264 | + new Metadata() |
| 265 | + { |
| 266 | + Name = "NuGetPackageVersion", |
| 267 | + Value = "6.0.33", |
| 268 | + }, |
| 269 | + }, |
| 270 | + }, |
| 271 | + }, |
| 272 | + }, |
| 273 | + }, |
| 274 | + }, |
| 275 | + }, |
| 276 | + }, |
| 277 | + |
| 278 | + // ResolvedSingleFileHostPack |
| 279 | + new MSBuildTask() |
| 280 | + { |
| 281 | + Name = "GetPackageDirectory", |
| 282 | + Children = |
| 283 | + { |
| 284 | + new Folder() |
| 285 | + { |
| 286 | + Name = "OutputItems", |
| 287 | + Children = |
| 288 | + { |
| 289 | + new AddItem() |
| 290 | + { |
| 291 | + Name = "ResolvedSingleFileHostPack", |
| 292 | + Children = |
| 293 | + { |
| 294 | + new Item() |
| 295 | + { |
| 296 | + Name = "SingleFileHost", |
| 297 | + Children = |
| 298 | + { |
| 299 | + new Metadata() |
| 300 | + { |
| 301 | + Name = "NuGetPackageId", |
| 302 | + Value = "Microsoft.NETCore.App.Host.win-x64", |
| 303 | + }, |
| 304 | + new Metadata() |
| 305 | + { |
| 306 | + Name = "NuGetPackageVersion", |
| 307 | + Value = "6.0.33", |
| 308 | + }, |
| 309 | + }, |
| 310 | + }, |
| 311 | + }, |
| 312 | + }, |
| 313 | + }, |
| 314 | + }, |
| 315 | + }, |
| 316 | + }, |
| 317 | + |
| 318 | + // ResolvedComHostPack |
| 319 | + new MSBuildTask() |
| 320 | + { |
| 321 | + Name = "GetPackageDirectory", |
| 322 | + Children = |
| 323 | + { |
| 324 | + new Folder() |
| 325 | + { |
| 326 | + Name = "OutputItems", |
| 327 | + Children = |
| 328 | + { |
| 329 | + new AddItem() |
| 330 | + { |
| 331 | + Name = "ResolvedComHostPack", |
| 332 | + Children = |
| 333 | + { |
| 334 | + new Item() |
| 335 | + { |
| 336 | + Name = "ComHost", |
| 337 | + Children = |
| 338 | + { |
| 339 | + new Metadata() |
| 340 | + { |
| 341 | + Name = "NuGetPackageId", |
| 342 | + Value = "Microsoft.NETCore.App.Host.win-x64", |
| 343 | + }, |
| 344 | + new Metadata() |
| 345 | + { |
| 346 | + Name = "NuGetPackageVersion", |
| 347 | + Value = "6.0.33", |
| 348 | + }, |
| 349 | + }, |
| 350 | + }, |
| 351 | + }, |
| 352 | + }, |
| 353 | + }, |
| 354 | + }, |
| 355 | + }, |
| 356 | + }, |
| 357 | + |
| 358 | + // ResolvedIjwHostPack |
| 359 | + new MSBuildTask() |
| 360 | + { |
| 361 | + Name = "GetPackageDirectory", |
| 362 | + Children = |
| 363 | + { |
| 364 | + new Folder() |
| 365 | + { |
| 366 | + Name = "OutputItems", |
| 367 | + Children = |
| 368 | + { |
| 369 | + new AddItem() |
| 370 | + { |
| 371 | + Name = "ResolvedIjwHostPack", |
| 372 | + Children = |
| 373 | + { |
| 374 | + new Item() |
| 375 | + { |
| 376 | + Name = "IjwHost", |
| 377 | + Children = |
| 378 | + { |
| 379 | + new Metadata() |
| 380 | + { |
| 381 | + Name = "NuGetPackageId", |
| 382 | + Value = "Microsoft.NETCore.App.Host.win-x64", |
| 383 | + }, |
| 384 | + new Metadata() |
| 385 | + { |
| 386 | + Name = "NuGetPackageVersion", |
| 387 | + Value = "6.0.33", |
| 388 | + }, |
| 389 | + }, |
| 390 | + }, |
| 391 | + }, |
| 392 | + }, |
| 393 | + }, |
| 394 | + }, |
| 395 | + }, |
| 396 | + }, |
| 397 | + }, |
| 398 | + }, |
| 399 | + }, |
| 400 | + }, |
| 401 | + }, |
| 402 | + }; |
| 403 | + |
| 404 | + // in-memory logs need to be `.buildlog` |
| 405 | + var tempFile = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():d}.buildlog"); |
| 406 | + try |
| 407 | + { |
| 408 | + Serialization.Write(binlog, tempFile); |
| 409 | + using var binLogStream = File.OpenRead(tempFile); |
| 410 | + |
| 411 | + var (scanResult, componentRecorder) = await this.DetectorTestUtility |
| 412 | + .WithFile(tempFile, binLogStream) |
| 413 | + .ExecuteDetectorAsync(); |
| 414 | + var detectedComponents = componentRecorder.GetDetectedComponents(); |
| 415 | + |
| 416 | + var components = detectedComponents |
| 417 | + .Select(d => d.Component) |
| 418 | + .Cast<NuGetComponent>() |
| 419 | + .OrderBy(c => c.Name) |
| 420 | + .Select(c => $"{c.Name}/{c.Version}"); |
| 421 | + components.Should().Equal("Microsoft.NETCore.App.Host.win-x64/6.0.33"); |
| 422 | + } |
| 423 | + finally |
| 424 | + { |
| 425 | + File.Delete(tempFile); |
| 426 | + } |
| 427 | + } |
| 428 | + |
209 | 429 | private async Task<(IndividualDetectorScanResult ScanResult, IComponentRecorder ComponentRecorder)> ExecuteDetectorAndGetBinLogAsync(
|
210 | 430 | string projectContents,
|
211 | 431 | (string FileName, string Content)[] additionalFiles = null,
|
212 | 432 | (string Name, string Version, string TargetFramework, string DependenciesXml)[] mockedPackages = null)
|
213 | 433 | {
|
214 |
| - using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync("project.csproj", projectContents, additionalFiles, mockedPackages); |
| 434 | + using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync("project.csproj", projectContents, additionalFiles: additionalFiles, mockedPackages: mockedPackages); |
215 | 435 | var (scanResult, componentRecorder) = await this.DetectorTestUtility
|
216 | 436 | .WithFile("msbuild.binlog", binLogStream)
|
217 | 437 | .ExecuteDetectorAsync();
|
|
0 commit comments