// 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 System.IO; using Microsoft.DotNet.Cli.Utils; using Microsoft.Extensions.ProjectModel.Tests; using NuGet.Frameworks; using Xunit; using Xunit.Abstractions; namespace Microsoft.Extensions.ProjectModel.MsBuild { public class MsBuildProjectDependencyProviderTests : IClassFixture { private const string SkipReason = "CI doesn't yet have a new enough version of .NET Core SDK"; 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 = SkipReason)] 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 lib1Dll = Assert.Single(context.CompilationAssemblies, a => a.Name.Equals("Library1", StringComparison.OrdinalIgnoreCase)); Assert.False(File.Exists(lib1Dll.ResolvedPath), $"Design time build. Shouldn't produce a file to {lib1Dll.ResolvedPath}"); // This reference doesn't resolve so should not be available here. Assert.DoesNotContain("xyz", context.CompilationAssemblies.Select(a => a.Name), StringComparer.OrdinalIgnoreCase); var packageDependencies = context.PackageDependencies; var mvcPackage = Assert.Single(context.PackageDependencies, p => p.Name.Equals("Microsoft.AspNetCore.Mvc", StringComparison.OrdinalIgnoreCase)); Assert.Contains("Microsoft.Extensions.DependencyInjection", mvcPackage.Dependencies.Select(d => d.Name), StringComparer.OrdinalIgnoreCase); Assert.Equal(Path.Combine(fileProvider.Root, "Root", "..", "Library1", "Library1.csproj"), context.ProjectReferences.First()); } } } }