Skip to content

Commit

Permalink
[skip ci] add basic unit test for msbuild api
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartheek Penagamuri authored and Pragnya Pandrate committed Jul 5, 2022
1 parent 991539a commit 1211852
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private void AddPackageVersionIntoItemGroupCPM(Project project, LibraryDependenc

// Get the ItemGroup to add a PackageVersion to or create a new one.
var propsItemGroup = GetItemGroup(directoryBuildPropsRootElement.ItemGroups, PACKAGE_VERSION_TYPE_TAG) ?? directoryBuildPropsRootElement.AddItemGroup();
AddPackageVersionIntoPropsItemGroup(project, propsItemGroup, libraryDependency);
AddPackageVersionIntoPropsItemGroup(propsItemGroup, libraryDependency);

// Save the updated props file.
directoryBuildPropsRootElement.Save();
Expand All @@ -245,7 +245,7 @@ private void AddPackageVersionIntoItemGroupCPM(Project project, LibraryDependenc
/// </summary>
/// <param name="project">Project that needs to be modified.</param>
/// <returns>The directory build props root element.</returns>
private ProjectRootElement GetDirectoryBuildPropsRootElement(Project project)
internal ProjectRootElement GetDirectoryBuildPropsRootElement(Project project)
{
// Get the Directory.Packages.props path.
string directoryPackagesPropsPath = project.GetPropertyValue(DirectoryPackagesPropsPathPropertyName);
Expand All @@ -256,10 +256,9 @@ private ProjectRootElement GetDirectoryBuildPropsRootElement(Project project)
/// <summary>
/// Add package name and version into the props file.
/// </summary>
/// <param name="project">Project that is being modified.</param>
/// <param name="itemGroup">Item group that needs to be modified in the props file.</param>
/// <param name="libraryDependency">Package Dependency of the package to be added.</param>
private void AddPackageVersionIntoPropsItemGroup(Project project, ProjectItemGroupElement itemGroup,
private void AddPackageVersionIntoPropsItemGroup(ProjectItemGroupElement itemGroup,
LibraryDependency libraryDependency)
{
// Add both package reference information and version metadata using the PACKAGE_VERSION_TYPE_TAG.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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.IO;
using Microsoft.Build.Definition;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
using NuGet.CommandLine.XPlat;
using NuGet.Test.Utility;
using Xunit;
using Project = Microsoft.Build.Evaluation.Project;

namespace NuGet.CommandLine.Xplat.Tests
{
public class MSBuildAPIUtilityTests
{
static MSBuildAPIUtilityTests()
{
MSBuildLocator.RegisterDefaults();
}

[Fact]
public void MSBuilldTest()
{
var testDirectory = TestDirectory.Create();

var projectCollection = new ProjectCollection(
globalProperties: null,
remoteLoggers: null,
loggers: null,
toolsetDefinitionLocations: ToolsetDefinitionLocations.Default,
// Having more than 1 node spins up multiple msbuild.exe instances to run builds in parallel
// However, these targets complete so quickly that the added overhead makes it take longer
maxNodeCount: 1,
onlyLogCriticalEvents: false,
loadProjectsReadOnly: false);

var projectOptions = new ProjectOptions
{
LoadSettings = ProjectLoadSettings.DoNotEvaluateElementsWithFalseCondition,
ProjectCollection = projectCollection
};

var propsFile =
@$"<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>";
File.WriteAllText(Path.Combine(testDirectory, "Directory.Packages.props"), propsFile);

string projectContent =
@$"<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>";
File.WriteAllText(Path.Combine(testDirectory, "projectA.csproj"), projectContent);

var project = Project.FromFile(Path.Combine(testDirectory, "projectA.csproj"), projectOptions);

var yes = new MSBuildAPIUtility(logger: new TestLogger()).GetDirectoryBuildPropsRootElement(project);

Assert.Equal(Path.Combine(testDirectory, "Directory.Packages.props"), yes.FullPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<Reference Include="System.IO.Compression" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Locator" />
</ItemGroup>

<Import Project="$(BuildCommonDirectory)common.targets" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

0 comments on commit 1211852

Please sign in to comment.