Skip to content

Commit

Permalink
Add nfproj to supported project extensions
Browse files Browse the repository at this point in the history
- Add extension to supported projects.
- Add unit tests for install and update commands in CLI.
  • Loading branch information
josesimoes committed Mar 31, 2021
1 parent f24bad0 commit d4551ab
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class ProjectHelper
".jsproj",
".wixproj",
".nuproj",
".nfproj",
};

public static HashSet<string> SupportedProjectExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1677,5 +1677,161 @@ public void UpdateCommand_Self_FailsWithMoreThanOneSource()
result.AllOutput.Contains(NuGetResources.Error_UpdateSelf_Source);
}
}

[Fact]
public async Task UpdateCommand_NF_Project_Success()
{
using (TestDirectory packagesSourceDirectory = TestDirectory.Create())
using (TestDirectory solutionDirectory = TestDirectory.Create())
using (TestDirectory workingPath = TestDirectory.Create())
{
// setup directories
string projectDirectory1 = Path.Combine(solutionDirectory, "proj1");
string projectDirectory2 = Path.Combine(solutionDirectory, "proj2");
string packagesDirectory = Path.Combine(solutionDirectory, "packages");

// create packages IDs
PackageIdentity a1 = new PackageIdentity("A", new NuGetVersion("1.0.0"));
PackageIdentity a2 = new PackageIdentity("A", new NuGetVersion("2.0.0"));

PackageIdentity b1 = new PackageIdentity("B", new NuGetVersion("1.0.0"));
PackageIdentity b2 = new PackageIdentity("B", new NuGetVersion("2.0.0"));

// create packages
var a1Package = new SimpleTestPackageContext()
{
Id = a1.Id,
Version = a1.Version.ToString()
};
a1Package.Files.Clear();
a1Package.AddFile($"lib/{a1.Id}.dll");

var a1File = await a1Package.CreateAsFileAsync(packagesSourceDirectory, a1Package.PackageName);

var a2Package = new SimpleTestPackageContext()
{
Id = a2.Id,
Version = a2.Version.ToString()
};
a2Package.Files.Clear();
a2Package.AddFile($"lib/{a2.Id}.dll");

await a2Package.CreateAsFileAsync(packagesSourceDirectory, a2Package.PackageName);

var b1Package = new SimpleTestPackageContext()
{
Id = b1.Id,
Version = b1.Version.ToString()
};
b1Package.Files.Clear();
b1Package.AddFile($"lib/{b1.Id}.dll");

var b1File = await a1Package.CreateAsFileAsync(packagesSourceDirectory, b1Package.PackageName);

var b2Package = new SimpleTestPackageContext()
{
Id = b2.Id,
Version = b2.Version.ToString()
};
b2Package.Files.Clear();
b2Package.AddFile($"lib/{b2.Id}.dll");

await b2Package.CreateAsFileAsync(packagesSourceDirectory, b2Package.PackageName);

// build list of packages (initial versions on the project files)
var packages = new List<(string, string)>();
packages.Add((a1.Id, a1.Version.ToString()));
packages.Add((b1.Id, b1.Version.ToString()));

// create everything related with project 1
Directory.CreateDirectory(projectDirectory1);

Util.CreateFile(
projectDirectory1,
"proj1.nfproj",
Util.GetNFProjXML(
"proj1",
packages));

Util.CreateFile(
projectDirectory1,
"packages.config",
Util.GetNFPackageConfig(packages));

// create everything related with project 2
Directory.CreateDirectory(projectDirectory2);

Util.CreateFile(
projectDirectory2,
"proj2.nfproj",
Util.GetNFProjXML(
"proj2",
packages));

Util.CreateFile(
projectDirectory1,
"packages.config",
Util.GetNFPackageConfig(packages));

List<string> projectList = new string[] { "proj1", "proj2" }.ToList();

// create solution file
Util.CreateFile(solutionDirectory, "a.sln",
Util.CreateNFSolutionFileContent(projectList));

// get paths for projects and solutions
string projectFile1 = Path.Combine(projectDirectory1, "proj1.nfproj");
string projectFile2 = Path.Combine(projectDirectory2, "proj2.nfproj");
string solutionFile = Path.Combine(solutionDirectory, "a.sln");

var testNuGetProjectContext = new TestNuGetProjectContext();
string msbuildDirectory = MsBuildUtility.GetMsBuildToolset(null, null).Path;
var projectSystem1 = new MSBuildProjectSystem(msbuildDirectory, projectFile1, testNuGetProjectContext);
var projectSystem2 = new MSBuildProjectSystem(msbuildDirectory, projectFile2, testNuGetProjectContext);
var msBuildProject1 = new MSBuildNuGetProject(projectSystem1, packagesDirectory, projectDirectory1);
var msBuildProject2 = new MSBuildNuGetProject(projectSystem2, packagesDirectory, projectDirectory2);

using (FileStream stream = File.OpenRead(a1File.FullName))
{
var downloadResult = new DownloadResourceResult(stream, packagesSourceDirectory);
await msBuildProject1.InstallPackageAsync(
a1,
downloadResult,
testNuGetProjectContext,
CancellationToken.None);
}

using (FileStream stream = File.OpenRead(b1File.FullName))
{
var downloadResult = new DownloadResourceResult(stream, packagesSourceDirectory);
await msBuildProject2.InstallPackageAsync(
b1,
downloadResult,
testNuGetProjectContext,
CancellationToken.None);
}

projectSystem1.Save();
projectSystem2.Save();

var args = new[]
{
"update",
solutionFile,
"-Source",
packagesSourceDirectory
};

CommandRunnerResult r = CommandRunner.Run(
Util.GetNuGetExePath(),
workingPath,
string.Join(" ", args),
waitForExit: true);

Assert.True(r.ExitCode == 0, "Output is " + r.AllOutput + ". Error is " + r.Errors);
Assert.Contains($"Successfully installed '{a2.Id} {a2.Version}'", r.AllOutput);
Assert.Contains($"Successfully installed '{b2.Id} {b2.Version}'", r.AllOutput);
}
}
}
}
150 changes: 150 additions & 0 deletions test/NuGet.Clients.Tests/NuGet.CommandLine.Test/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,5 +1237,155 @@ public static void TestCommandInvalidArguments(string command)
// Verify traits of help message in stdout
Assert.Contains("usage:", result.Item2);
}

#region helper methods for .NET nanoFramework tests

/// <summary>
/// Create a basic nfproj file for .NET nanoFramework.
/// </summary>
public static string GetNFProjXML(
string projectName,
List<(string, string)> packages)
{
var projContent = new StringBuilder();

projContent.AppendLine(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""Current"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<PropertyGroup Label=""Globals"">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project=""$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props"" Condition=""Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')"" />
<PropertyGroup>
<Configuration Condition="" '$(Configuration)' == '' "">Debug</Configuration>
<Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>9e332e06-5faf-4861-8318-a806978b677d</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>$NAME$</RootNamespace>
<AssemblyName>$NAME$</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
</PropertyGroup>
<Import Project=""$(NanoFrameworkProjectSystemPath)NFProjectSystem.props"" Condition=""Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')"" />
<ItemGroup>");

foreach ((string, string) package in packages)
{
projContent.AppendLine(
$@" <Reference Include=""{package.Item1}, Version={package.Item2}, Culture=neutral"">
<HintPath>..\packages\{package.Item1}.{package.Item2}\lib\{package.Item1}.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>");
}

projContent.AppendLine(
@" </ItemGroup>
<ItemGroup>
<None Include=""packages.config"" />
</ItemGroup>
<Import Project=""$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets"" Condition=""Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')"" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
</Project>".Replace("$NAME$", projectName));

return projContent.ToString();
}

/// <summary>
/// Create a Solution file with .NET nanoFramework projects in it.
/// </summary>
/// <param name="projectList">List of .NET nanoFramework projects to add to the solution</param>
/// <returns>Content of the Solution file</returns>
public static string CreateNFSolutionFileContent(List<string> projectList)
{
var slnContent = new StringBuilder();

slnContent.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
slnContent.AppendLine("# Visual Studio Version 16");
slnContent.AppendLine("VisualStudioVersion = 16.0.31005.135");

foreach (string project in projectList)
{
slnContent.AppendLine($"Project(\"{{11A8DD76-328B-46DF-9F39-F559912D0360}}\") = \"{project}\", \"{project}\\{project}.nfproj\", \"{{{Guid.NewGuid().ToString()}}}");
slnContent.AppendLine("EndProject");
}

return slnContent.ToString();
}

/// <summary>
/// Creates a test package for .NET nanoFramework.
/// </summary>
/// <param name="packageId">Package ID</param>
/// <param name="version">Package version</param>
/// <param name="path">Path where the package will be saved</param>
/// <param name="contentFiles">Content file(s) to be added to the package</param>
/// <returns></returns>
public static string CreateNFTestPackage(
string packageId,
string version,
string path,
params string[] contentFiles)
{
var packageBuilder = new PackageBuilder
{
Id = packageId,
Version = new SemanticVersion(version)
};

packageBuilder.Description = $"desc of {packageId} {version}";

var libPath = $"lib/{packageId}.dll";

packageBuilder.Files.Add(CreatePackageFile(libPath));

foreach (string contentFile in contentFiles)
{
string packageFilePath = Path.Combine("content", contentFile);
IPackageFile packageFile = CreatePackageFile(packageFilePath);
packageBuilder.Files.Add(packageFile);
}

packageBuilder.Authors.Add("test author");

string packageFileName = string.Format("{0}.{1}.nupkg", packageId, version);
string packageFileFullPath = Path.Combine(path, packageFileName);

using (FileStream fileStream = File.Create(packageFileFullPath))
{
packageBuilder.Save(fileStream);
}

return packageFileFullPath;
}

/// <summary>
/// Build packages.config from list of NuGet packages for .NET nanoFramework.
/// </summary>
/// <param name="packages">List of NuGet packages for .NET nanoFramework</param>
/// <returns></returns>
internal static string GetNFPackageConfig(List<(string, string)> packages)
{
var configContent = new StringBuilder();

configContent.AppendLine("<packages>");

foreach ((string, string) package in packages)
{
configContent.AppendLine($@" <package id=""{package.Item1}"" version=""{package.Item2}"" targetFramework=""netnanoframework10"" />");
}

configContent.AppendLine("</packages>");

return configContent.ToString();
}

#endregion
}
}

0 comments on commit d4551ab

Please sign in to comment.