Andrey Ershov
Andrey Ershov

Reputation: 1803

Test NuGet package Installation on C# project in Azure Pipeline

Is it possible, as part of a CI process for NuGet package creation, to install a newly created package to a project, residing in the repository? So that the installation can be tested. It's easy to do using Visual Studio UI, but how to do it on a newly created azure pipeline worker automatically?

Upvotes: 6

Views: 922

Answers (2)

zivkan
zivkan

Reputation: 15071

The NuGet.Client repo has a bunch of tests that install packages into test projects and assert various things. I know of a whole lot of PackageReference tests, but can't remember any packages.config tests. Using the .NET CLI it's easy to script a lot of it, but depending on what you want to do, you might need to write some code to manipulate XML files.

Here's a bunch of useful commands totally written from memory and therefore might not work as is, but it'll get you started:

# create a new .NET Core console app. You'll need to edit the csproj to test different frameworks
dotnet new console

# create nuget.config file
dotnet new nugetconfig

# add a local folder as a package source
nuget sources add -configfile nuget.config -name local -source ..\newPackages

# set the global packages folder to a empty/temporary directory, so the test package
# doesn't pollute the agent's global packages folder
nuget config set -configfile nuget.config globalPackagesFolder gpf

# add the latest version of the package to the project in the current directory.
# use --version to specify a version
dotnet add package MyTestPackage

Since SDK style projects are so short and simple, you may be better off just hardcoding the contents in your code and write them to disk for your tests. It's what we (NuGet.Client) do.

We have plans to eventually move the config options to the dotnet cli so that you won't need to download nuget.exe, but it's really low priority since it's so easy to workaround. nuget.exe works on mono on Linux and Mac, or just hardcode the conents of the config in a string in your test and write it at runtime.

This will only be useful for you if the things you want to test are not impacted by package compatibility issues with PackageReference vs packages.config. However, given the future of .NET is SDK style projects, and SDK style projects don't support packages.config, you can try justifying it by saying it's the future.

Upvotes: 1

Leo Liu
Leo Liu

Reputation: 76870

Install NuGet package on the project in Azure Pipeline

I am afraid it is impossible to install NuGet package on the project in Azure Pipeline.

Because NuGet CLI install command line just Installs a package into the current project but does not modify projects or reference files (packages.config).:

enter image description here

It is like the command line nuget restore, just download the packages not install it.

To install the package to the project, we need modify the project file via access to visual studio objects:

https://github.com/NuGet/Home/issues/1512

So it should be impossible to install NuGet packages out of Visual Studio, check my another thread for some details.

Besides, we also do not recommend to install NuGet package in Azure Pipeline. If we install a newly created package to a project automatically, it will use the scripts to modify our Repos, which is not recommended and safe.

Personally, the correct process is:

  • Create the new package in the Azure pipeline.
  • Publish the new package to the Artifacts or any other nuget feed.
  • Install/Update the new package to the project with Visual Studio and test it.
  • Update the new package version to the Repos.

Hope this helps.

Upvotes: 3

Related Questions