// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Linq; using Microsoft.Extensions.ProjectModel.Tests; using Xunit; using Xunit.Abstractions; using System.IO; using Microsoft.DotNet.Cli.Utils; using NuGet.Frameworks; namespace Microsoft.Extensions.ProjectModel.MsBuild { public class MsBuildProjectDependencyProviderTests: IClassFixture { private const string NugetConfigTxt = @" "; private const string RootProjectTxt = @" Microsoft.TestProject TestProject Library netcoreapp1.0 bin\$(Configuration) 1.0.0-* 1.0.0-* 1.0.1 "; private const string LibraryProjectTxt = @" Microsoft.Library Library1 Library netcoreapp1.0 bin\$(Configuration) 1.0.0-* 1.0.1 "; private readonly MsBuildFixture _fixture; private readonly ITestOutputHelper _output; public MsBuildProjectDependencyProviderTests(MsBuildFixture fixture, ITestOutputHelper output) { _fixture = fixture; _output = output; } [Fact(Skip = "CI doesn't yet have a new enough version of .NET Core SDK")] public void BuildDependenciesForProject() { using (var fileProvider = new TemporaryFileProvider()) { Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Root")); Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Library1")); // TODO remove when SDK becomes available on other feeds fileProvider.Add("NuGet.config", NugetConfigTxt); // Add Root Project fileProvider.Add($"Root/test.csproj", RootProjectTxt); fileProvider.Add($"Root/One.cs", "public class Abc {}"); fileProvider.Add($"Root/Two.cs", "public class Abc2 {}"); fileProvider.Add($"Root/Excluded.cs", "public class Abc {}"); // Add Class Library project fileProvider.Add($"Library1/Library1.csproj", LibraryProjectTxt); fileProvider.Add($"Library1/Three.cs", "public class Abc3 {}"); var testContext = _fixture.GetMsBuildContext(); var muxer = Path.Combine(testContext.ExtensionsPath, "../..", "dotnet.exe"); var result = Command .Create(muxer, new[] { "restore3", Path.Combine(fileProvider.Root, "Library1","Library1.csproj") }) .OnErrorLine(l => _output.WriteLine(l)) .OnOutputLine(l => _output.WriteLine(l)) .Execute(); Assert.Equal(0, result.ExitCode); result = Command .Create(muxer, new[] { "restore3", Path.Combine(fileProvider.Root, "Root", "test.csproj") }) .OnErrorLine(l => _output.WriteLine(l)) .OnOutputLine(l => _output.WriteLine(l)) .Execute(); Assert.Equal(0, result.ExitCode); var builder = new MsBuildProjectContextBuilder() .AsDesignTimeBuild() .UseMsBuild(testContext) .WithTargetFramework(FrameworkConstants.CommonFrameworks.NetCoreApp10) .WithConfiguration("Debug") .WithProjectFile(fileProvider.GetFileInfo(Path.Combine("Root", "test.csproj"))); var context = builder.Build(); var compilationAssemblies = context.CompilationAssemblies; var lib1Dll = compilationAssemblies .Where(assembly => assembly.Name.Equals("Library1", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); Assert.NotNull(lib1Dll); Assert.True(File.Exists(lib1Dll.ResolvedPath)); // This reference doesn't resolve so should not be available here. Assert.False(compilationAssemblies.Any(assembly => assembly.Name.Equals("xyz", StringComparison.OrdinalIgnoreCase))); var packageDependencies = context.PackageDependencies; var mvcPackage = packageDependencies .Where(p => p.Name.Equals("Microsoft.AspNetCore.Mvc", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); Assert.NotNull(mvcPackage); Assert.True(mvcPackage.Dependencies.Any(dependency => dependency.Name.Equals("Microsoft.Extensions.DependencyInjection", StringComparison.OrdinalIgnoreCase))); Assert.True(context.ProjectReferences.First().Equals(Path.Combine(fileProvider.Root, "Library1", "Library1.csproj"))); } } } }