using NUnit.Framework; using System.IO; using System.Xml; using PortingAssistant.Client.NuGet.InternalNuGet; using Microsoft.Extensions.Logging.Abstractions; using System.Collections.Generic; using NuGet.Protocol.Core.Types; using NuGet.Configuration; using System.Linq; using System; using PortingAssistant.Client.Model; using Settings = NuGet.Configuration.Settings; namespace PortingAssistant.Client.Tests { public class PortingAssistantInternalNuGetCompatibilityHandlerTest { private IPortingAssistantInternalNuGetCompatibilityHandler _internalNuGetCompatibilityHandler; private string _tmpSolutionPath; private IEnumerable _sourceRepositories; private IEnumerable GetInternalRepository(string pathToSolution) { var setting = Settings.LoadDefaultSettings(pathToSolution); var sourceRepositoryProvider = new SourceRepositoryProvider( new PackageSourceProvider(setting), Repository.Provider.GetCoreV3()); var repositories = sourceRepositoryProvider .GetRepositories() .Where(r => !string.Equals(r.PackageSource.Name, "nuget.org", StringComparison.OrdinalIgnoreCase)); return repositories; } [SetUp] public void Setup() { _internalNuGetCompatibilityHandler = new PortingAssistantInternalNuGetCompatibilityHandler(NullLogger.Instance); _tmpSolutionPath = CreateFakeSolutionFile(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())); _sourceRepositories = GetInternalRepository(_tmpSolutionPath); } [TearDown] public void Cleanup() { Directory.Delete(_tmpSolutionPath, true); } [Test] public void CheckCompatibilityOfCompatiblePackageReturnsTrue() { var resultTask1 = _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "12.0.3", "netcoreapp3.1", _sourceRepositories); var resultTask2 = _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "12.0.3", "netstandard2.0", _sourceRepositories); resultTask1.Wait(); resultTask2.Wait(); Assert.True(resultTask1.Result.IsCompatible); Assert.True(resultTask2.Result.IsCompatible); } [Test] public void CheckCompatibilityOfIncompatiblePackageReturnsFalse() { var resultTask1 = _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "6.0.1", "netcoreapp3.1", _sourceRepositories); var resultTask2 = _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "6.0.1", "netstandard2.0", _sourceRepositories); resultTask1.Wait(); resultTask2.Wait(); Assert.False(resultTask1.Result.IsCompatible); Assert.False(resultTask2.Result.IsCompatible); } [Test] public void CheckCompatibilityOfNonexistentPackageThrowsException() { Assert.ThrowsAsync(async () => await _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("ThisPackageDoesNotExist", "1.0.0", "netcoreapp3.1", _sourceRepositories)); } [Test] public void CheckCompatibilityWithEmptyInternalRepositoriesThrowsException() { Assert.ThrowsAsync(async () => await _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "12.0.3", "netcoreapp3.1", new List())); } [Test] public void CheckCompatibilityWithNullInternalRepositoriesThrowsArgumentException() { Assert.ThrowsAsync(async () => await _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "12.0.3", "netcoreapp3.1", null)); } [Test] public void CheckCompatibilityWithNullPackageNameThrowsArgumentException() { Assert.ThrowsAsync(async () => await _internalNuGetCompatibilityHandler.CheckCompatibilityAsync(null, "12.0.3", "netcoreapp3.1", _sourceRepositories)); } [Test] public void CheckCompatibilityWithNullTargetFrameworkThrowsArgumentException() { Assert.ThrowsAsync(async () => await _internalNuGetCompatibilityHandler.CheckCompatibilityAsync("Newtonsoft.Json", "12.0.3", null, _sourceRepositories)); } private string CreateFakeSolutionFile(string tmpDir) { Directory.CreateDirectory(tmpDir); var configFilePath = Path.Combine(tmpDir, "nuget.config"); XmlDocument doc = new XmlDocument(); XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(docNode); var testSource = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestXml", "TestNugetRepository"); //create a local test nuget.config file. extra space in url should be trimmed string xml = "" + "" + "" + "" + "" + "" + "" + $" " + "" + "" + ""; doc.LoadXml(xml); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create(configFilePath, settings); doc.Save(writer); writer.Close(); return tmpDir; } } }