Exemplo n.º 1
0
        public async void RemovePkg_UnconditionalRemove_Success()
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                // Generate Package
                var packageX = XPlatTestUtils.CreatePackage();
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, packageX, "net46");

                // Verify that the package reference exists before removing.
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                var itemGroup      = XPlatTestUtils.GetItemGroupForAllFrameworks(projectXmlRoot);

                Assert.NotNull(itemGroup);
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageX.Id, "1.0.0"));

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, projectA);
                var commandRunner = new RemovePackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild).Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                Assert.Equal(0, result);
                Assert.True(XPlatTestUtils.ValidateNoReference(projectXmlRoot, packageX.Id));
            }
        }
Exemplo n.º 2
0
        public async void AddPkg_UnconditionalAddIntoExeProject_Success(string userInputVersion)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, "net46");
                projectA.Properties.Add("OutputType", "exe");
                projectA.Save();

                var packageX = XPlatTestUtils.CreatePackage();

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, userInputVersion, projectA);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result         = commandRunner.ExecuteCommand(packageArgs, MsBuild).Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                var itemGroup      = XPlatTestUtils.GetItemGroupForAllFrameworks(projectXmlRoot);

                // Assert
                Assert.Equal(0, result);
                Assert.NotNull(itemGroup);
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageX.Id, userInputVersion));
                Assert.True(XPlatTestUtils.ValidateAssetsFile(projectA, packageX.Id));
            }
        }
Exemplo n.º 3
0
        public async void AddPkg_UnconditionalAddWithNoRestore_Success(string packageFrameworks,
                                                                       string projectFrameworks,
                                                                       string userInputVersion)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, projectFrameworks);
                var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks);

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, userInputVersion, projectA, noRestore: true);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // If noRestore is set, then we do not perform compatibility check.
                // The added package reference will be unconditional
                var itemGroup = XPlatTestUtils.GetItemGroupForAllFrameworks(projectXmlRoot);

                // Assert
                Assert.Equal(0, result);
                Assert.NotNull(itemGroup);
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageX.Id, userInputVersion));
            }
        }
Exemplo n.º 4
0
        public void RemovePkg_RemoveInvalidPackage_Failure()
        {
            // Arrange

            var unknownPackageId = "package_foo";

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, "net46");

                var packageArgs    = XPlatTestUtils.GetPackageReferenceArgs(unknownPackageId, projectA);
                var commandRunner  = new RemovePackageReferenceCommandRunner();
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                Assert.True(XPlatTestUtils.ValidateNoReference(projectXmlRoot, unknownPackageId));
                var msBuild = MsBuild;

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, msBuild).Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                Assert.Equal(1, result);
                Assert.True(XPlatTestUtils.ValidateNoReference(projectXmlRoot, unknownPackageId));
            }
        }
Exemplo n.º 5
0
        public async void AddPkg_UnconditionalAddWithoutVersion_Success()
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46");
                var packageX = XPlatTestUtils.CreatePackage();

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                // Since user is not inputing a version, it is converted to a "*"
                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, "*", projectA, noVersion: true);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                Assert.Equal(0, result);

                // Since user did not specify a version, the package reference will contain the resolved version
                Assert.True(XPlatTestUtils.ValidateReference(projectXmlRoot, packageX.Id, "1.0.0"));
            }
        }
Exemplo n.º 6
0
        private void VerifyCommand(Action <string, Mock <IListPackageCommandRunner>, CommandLineApplication, Func <LogLevel> > verify)
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectPath = Path.Combine(testDirectory, "project.csproj");
                File.WriteAllText(projectPath, string.Empty);

                var logLevel          = LogLevel.Information;
                var logger            = new TestCommandOutputLogger();
                var testApp           = new CommandLineApplication();
                var mockCommandRunner = new Mock <IListPackageCommandRunner>();
                mockCommandRunner
                .Setup(m => m.ExecuteCommandAsync(It.IsAny <ListPackageArgs>()))
                .Returns(Task.CompletedTask);

                testApp.Name = "dotnet nuget_test";
                ListPackageCommand.Register(testApp,
                                            () => logger,
                                            ll => logLevel = ll,
                                            () => mockCommandRunner.Object);

                // Act & Assert
                try
                {
                    verify(projectPath, mockCommandRunner, testApp, () => logLevel);
                }
                finally
                {
                    XPlatTestUtils.DisposeTemporaryFile(projectPath);
                }
            }
        }
Exemplo n.º 7
0
        public async void AddPkg_FailureIncompatibleFrameworks(string packageFrameworks, string userInputFrameworks)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46; netcoreapp1.0");
                var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks);

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, projectA,
                                                                         frameworks: userInputFrameworks);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                Assert.Equal(1, result);
                Assert.True(XPlatTestUtils.ValidateNoReference(projectXmlRoot, packageX.Id));
            }
        }
Exemplo n.º 8
0
        public async void AddPkg_ConditionalAddWithUserInputFramework_Success(string packageFrameworks, string projectFrameworks, string userInputFrameworks)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46; netcoreapp1.0");
                var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks);

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, projectA,
                                                                         frameworks: userInputFrameworks);
                var commandRunner   = new AddPackageReferenceCommandRunner();
                var commonFramework = XPlatTestUtils.GetCommonFramework(packageFrameworks, projectFrameworks, userInputFrameworks);

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                var itemGroup      = XPlatTestUtils.GetItemGroupForFramework(projectXmlRoot, commonFramework);

                // Assert
                Assert.Equal(0, result);
                Assert.NotNull(itemGroup);
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageX.Id, packageX.Version));
            }
        }
Exemplo n.º 9
0
        public void RestoreCommand_VerifyMinClientVersionV3Source()
        {
            // Arrange
            using (var packagesDir = TestFileSystemUtility.CreateRandomTestFolder())
                using (var projectDir = TestFileSystemUtility.CreateRandomTestFolder())
                {
                    var specPath = Path.Combine(projectDir, "TestProject", "project.json");
                    var spec     = XPlatTestUtils.BasicConfigNetCoreApp;

                    // This package has a minclientversion of 9999
                    XPlatTestUtils.AddDependency(spec, "TestPackage.MinClientVersion", "1.0.0");
                    XPlatTestUtils.WriteJson(spec, specPath);

                    var lockFilePath = Path.Combine(projectDir, "project.lock.json");
                    var log          = new TestCommandOutputLogger();

                    var args = new string[]
                    {
                        "restore",
                        projectDir,
                        "-s",
                        "https://api.nuget.org/v3/index.json",
                        "--packages",
                        packagesDir
                    };

                    // Act
                    var exitCode = Program.MainInternal(args, log);

                    // Assert
                    Assert.Equal(1, log.Errors);
                    Assert.Contains("'TestPackage.MinClientVersion 1.0.0' package requires NuGet client version '9.9999.0' or above", log.ShowMessages());
                    Assert.False(File.Exists(lockFilePath));
                }
        }
Exemplo n.º 10
0
        public async void AddPkg_FailureUnknownPackage()
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46; netcoreapp1.0");
                var packageX = XPlatTestUtils.CreatePackage();

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs("unknown_package_id", "1.0.0", projectA);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                Assert.Equal(1, result);
                Assert.True(XPlatTestUtils.ValidateNoReference(projectXmlRoot, packageX.Id));
                Assert.True(XPlatTestUtils.ValidateNoReference(projectXmlRoot, "unknown_package_id"));
            }
        }
Exemplo n.º 11
0
        public async Task Restore_FallbackFolderContainsAllPackages()
        {
            using (var sourceDir = TestFileSystemUtility.CreateRandomTestFolder())
                using (var fallbackDir1 = TestFileSystemUtility.CreateRandomTestFolder())
                    using (var fallbackDir2 = TestFileSystemUtility.CreateRandomTestFolder())
                        using (var packagesDir = TestFileSystemUtility.CreateRandomTestFolder())
                            using (var projectDir = TestFileSystemUtility.CreateRandomTestFolder())
                            {
                                var specPath = Path.Combine(projectDir, "XPlatRestoreTests", "project.json");
                                var spec     = XPlatTestUtils.BasicConfigNetCoreApp;

                                XPlatTestUtils.AddDependency(spec, "a", "1.0.0");
                                XPlatTestUtils.AddDependency(spec, "b", "1.0.0");
                                XPlatTestUtils.WriteJson(spec, specPath);

                                var packageA = new SimpleTestPackageContext("a", "1.0.0");
                                var packageB = new SimpleTestPackageContext("b", "1.0.0");

                                var saveMode = PackageSaveMode.Nuspec | PackageSaveMode.Files;

                                await SimpleTestPackageUtility.CreateFolderFeedV3(
                                    fallbackDir2,
                                    saveMode,
                                    packageA,
                                    packageB);

                                var log = new TestCommandOutputLogger();

                                var config = $@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <config>
        <add key=""globalPackagesFolder"" value=""{packagesDir}"" />
    </config>
    <fallbackPackageFolders>
        <add key=""a"" value=""{fallbackDir1}"" />
        <add key=""b"" value=""{fallbackDir2}"" />
    </fallbackPackageFolders>
    <packageSources>
        <add key=""a"" value=""{sourceDir}"" />
    </packageSources>
</configuration>";

                                File.WriteAllText(Path.Combine(projectDir, "NuGet.Config"), config);

                                var args = new string[]
                                {
                                    "restore",
                                    projectDir,
                                };

                                // Act
                                var exitCode = Program.MainInternal(args, log);

                                // Assert
                                Assert.Equal(0, exitCode);
                                Assert.Equal(0, log.Errors);
                                Assert.Equal(0, log.Warnings);
                                Assert.Equal(0, Directory.GetDirectories(packagesDir).Length);
                            }
        }
Exemplo n.º 12
0
        public void Restore_WithMissingConfigFile_Fails()
        {
            using (var projectDir = TestFileSystemUtility.CreateRandomTestFolder())
                using (var configDir = TestFileSystemUtility.CreateRandomTestFolder())
                {
                    var specPath = Path.Combine(projectDir, "XPlatRestoreTests", "project.json");
                    var spec     = XPlatTestUtils.BasicConfigNetCoreApp;

                    XPlatTestUtils.AddDependency(spec, "costura.fody", "1.3.3");
                    XPlatTestUtils.AddDependency(spec, "fody", "1.29.4");
                    XPlatTestUtils.WriteJson(spec, specPath);

                    var log = new TestCommandOutputLogger();

                    var args = new string[]
                    {
                        "restore",
                        projectDir,
                        "--configfile",
                        Path.Combine(configDir, "DoesNotExist.config"),
                    };

                    // Act
                    var exitCode = Program.MainInternal(args, log);

                    // Assert
                    Assert.Equal(1, exitCode);
                    Assert.Equal(1, log.Errors);
                    Assert.Contains("DoesNotExist.config", log.ShowErrors()); // file does not exist
                }
        }
Exemplo n.º 13
0
        public async void AddPkg_UnconditionalAddWithDotnetCliToolAndNoRestore_Success(string packageFrameworks,
                                                                                       string projectFrameworks,
                                                                                       string userInputVersion)
        {
            // Arrange
            using (var pathContext = new SimpleTestPathContext())
            {
                // Generate DotNetCliToolReference Package
                var packageDotnetCliToolX = XPlatTestUtils.CreatePackage(packageId: "PackageDotnetCliToolX",
                                                                         packageType: PackageType.DotnetCliTool);

                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageDotnetCliToolX);

                // Generate test package
                var packageY = XPlatTestUtils.CreatePackage(packageId: "PackageY");

                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageY);

                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46");

                projectA.DotnetCLIToolReferences.Add(packageDotnetCliToolX);

                projectA.Save();

                // Verify that the package reference exists before removing.
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                var itemGroup      = XPlatTestUtils.GetItemGroupForAllFrameworks(projectXmlRoot, packageType: PackageType.DotnetCliTool);

                Assert.NotNull(itemGroup);
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageDotnetCliToolX.Id, "1.0.0", PackageType.DotnetCliTool));

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageY.Id, userInputVersion, projectA, noRestore: true);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild).Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                itemGroup      = XPlatTestUtils.GetItemGroupForAllFrameworks(projectXmlRoot);

                // Assert
                Assert.Equal(0, result);
                Assert.NotNull(itemGroup);
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageY.Id, userInputVersion));
            }
        }
Exemplo n.º 14
0
        public async void AddPkg_ConditionalAddTwoPackages_Success(string packageFrameworks, string projectFrameworks, string userInputFrameworks)
        {
            // Arrange
            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(ProjectName, pathContext, projectFrameworks);
                var packageX = XPlatTestUtils.CreatePackage("PkgX", frameworkString: packageFrameworks);
                var packageY = XPlatTestUtils.CreatePackage("PkgY", frameworkString: packageFrameworks);

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageY);

                var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id,
                                                                         packageX.Version,
                                                                         projectA,
                                                                         frameworks: userInputFrameworks);
                var commandRunner   = new AddPackageReferenceCommandRunner();
                var msBuild         = MsBuild;
                var commonFramework = XPlatTestUtils.GetCommonFramework(packageFrameworks,
                                                                        projectFrameworks,
                                                                        userInputFrameworks);

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, msBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageY.Id, packageY.Version, projectA);

                // Act
                result = commandRunner.ExecuteCommand(packageArgs, msBuild)
                         .Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                var itemGroup = XPlatTestUtils.GetItemGroupForFramework(projectXmlRoot, commonFramework);

                // Assert
                Assert.Equal(0, result);
                Assert.True(XPlatTestUtils.ValidateTwoReferences(projectXmlRoot, packageX, packageY));
            }
        }
Exemplo n.º 15
0
        public async void AddPkg_UnconditionalAddAsUpdate_Succcess(string userInputVersionOld, string userInputVersionNew, bool noVersion)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA      = XPlatTestUtils.CreateProject(ProjectName, pathContext, "net46; netcoreapp1.0");
                var latestVersion = "1.0.0";
                var packages      = new SimpleTestPackageContext[] { XPlatTestUtils.CreatePackage(packageVersion: latestVersion),
                                                                     XPlatTestUtils.CreatePackage(packageVersion: "0.0.5"), XPlatTestUtils.CreatePackage(packageVersion: "0.0.9") };


                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3Async(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packages);

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packages[0].Id, userInputVersionOld, projectA);
                var commandRunner = new AddPackageReferenceCommandRunner();
                var msBuild       = MsBuild;

                // Create a package ref with the old version
                var result = commandRunner.ExecuteCommand(packageArgs, msBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                //Preconditions
                Assert.True(XPlatTestUtils.ValidateReference(projectXmlRoot, packages[0].Id, userInputVersionOld));

                //The model fom which the args are generated needs updated as well
                projectA.AddPackageToAllFrameworks(new SimpleTestPackageContext(packages[0].Id, userInputVersionOld));

                packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packages[0].Id, userInputVersionNew, projectA, noVersion: noVersion);
                commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                // Create a package ref with the new version
                result = commandRunner.ExecuteCommand(packageArgs, msBuild)
                         .Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                // Verify that the only package reference is with the new version
                Assert.Equal(0, result);
                Assert.True(XPlatTestUtils.ValidateReference(projectXmlRoot, packages[0].Id, noVersion ? latestVersion : userInputVersionNew));
            }
        }
Exemplo n.º 16
0
        public async Task PushToServerWhichRejectsDuplicates_SkipDuplicate_Succeeds(PackageSource packageSource)
        {
            // Arrange
            using (var packageDir = TestDirectory.Create())
                using (TestFileSystemUtility.SetCurrentDirectory(packageDir))
                {
                    var packageId      = "XPlatPushTests.PushToServerSucceeds";
                    var packageVersion = "1.0.0";
                    var packageFile    = await TestPackagesCore.GetRuntimePackageAsync(packageDir, packageId, packageVersion);

                    var configFile    = XPlatTestUtils.CopyFuncTestConfig(packageDir);
                    var logFirstPush  = new TestCommandOutputLogger();
                    var logSecondPush = new TestCommandOutputLogger();

                    var apiKey = XPlatTestUtils.ReadApiKey(packageSource.Name);
                    Assert.False(string.IsNullOrEmpty(apiKey));
                    var pushArgs = new List <string>
                    {
                        "push",
                        packageFile.FullName,
                        "--source",
                        packageSource.Source,
                        "--api-key",
                        apiKey,
                        "--skip-duplicate"
                    };

                    // Act
                    var exitCodeFirstPush  = NuGet.CommandLine.XPlat.Program.MainInternal(pushArgs.ToArray(), logFirstPush);
                    var exitCodeSecondPush = NuGet.CommandLine.XPlat.Program.MainInternal(pushArgs.ToArray(), logSecondPush);

                    // Assert First Push - it should happen without error.
                    var outputMessagesFirstPush = logFirstPush.ShowMessages();
                    Assert.Equal(string.Empty, logFirstPush.ShowErrors());
                    Assert.Equal(0, exitCodeFirstPush);

                    // Assert Second Push - it should happen without error, even though a duplicate is present.
                    var outputMessagesSecondPush = logSecondPush.ShowMessages();

                    Assert.Equal(string.Empty, logSecondPush.ShowErrors());
                    Assert.Equal(0, exitCodeSecondPush);
                    Assert.Contains($"PUT {packageSource.Source}", outputMessagesSecondPush);
                    Assert.DoesNotContain("already exists at feed", outputMessagesSecondPush);
                    Assert.Contains("Your package was pushed.", outputMessagesSecondPush);
                }
        }
Exemplo n.º 17
0
        public void RestoreCommand_VerifyMinClientVersionLocalFolder()
        {
            // Arrange
            using (var sourceDir = TestFileSystemUtility.CreateRandomTestFolder())
                using (var packagesDir = TestFileSystemUtility.CreateRandomTestFolder())
                    using (var projectDir = TestFileSystemUtility.CreateRandomTestFolder())
                    {
                        var packageContext = new SimpleTestPackageContext()
                        {
                            Id               = "packageA",
                            Version          = "1.0.0",
                            MinClientVersion = "9.9.9"
                        };

                        SimpleTestPackageUtility.CreatePackages(sourceDir, packageContext);

                        var specPath = Path.Combine(projectDir, "TestProject", "project.json");
                        var spec     = XPlatTestUtils.BasicConfigNetCoreApp;

                        XPlatTestUtils.AddDependency(spec, "packageA", "1.0.0");
                        XPlatTestUtils.WriteJson(spec, specPath);

                        var lockFilePath = Path.Combine(projectDir, "project.lock.json");
                        var log          = new TestCommandOutputLogger();

                        var args = new string[]
                        {
                            "restore",
                            projectDir,
                            "-s",
                            sourceDir,
                            "--packages",
                            packagesDir
                        };

                        // Act
                        var exitCode = Program.MainInternal(args, log);

                        // Assert
                        Assert.Equal(1, log.Errors);
                        Assert.Contains("'packageA 1.0.0' package requires NuGet client version '9.9.9' or above", log.ShowMessages());
                        Assert.False(File.Exists(lockFilePath));
                    }
        }
Exemplo n.º 18
0
        public async Task PushToServerSkipDuplicateSucceeds(PackageSource packageSource)
        {
            // Arrange
            using (var packageDir = TestDirectory.Create())
                using (TestFileSystemUtility.SetCurrentDirectory(packageDir))
                {
                    var packageId      = "XPlatPushTests.PushToServerSucceeds";
                    var packageVersion = "1.0.0";
                    var packageFile    = await TestPackagesCore.GetRuntimePackageAsync(packageDir, packageId, packageVersion);

                    var configFile = XPlatTestUtils.CopyFuncTestConfig(packageDir);
                    var log        = new TestCommandOutputLogger();

                    var apiKey = XPlatTestUtils.ReadApiKey(packageSource.Name);
                    Assert.False(string.IsNullOrEmpty(apiKey));

                    var pushArgs = new List <string>
                    {
                        "push",
                        packageFile.FullName,
                        "--source",
                        packageSource.Source,
                        "--api-key",
                        apiKey,
                        "--skip-duplicate"
                    };

                    // Act
                    var exitCode = NuGet.CommandLine.XPlat.Program.MainInternal(pushArgs.ToArray(), log);

                    // Assert
                    var outputMessages = log.ShowMessages();

                    Assert.Equal(string.Empty, log.ShowErrors());
                    Assert.Equal(0, exitCode);
                    Assert.Contains($"PUT {packageSource.Source}", outputMessages);


                    //info: PUT http://localhost:5000/api/v2/package/
                    //info: Conflict http://localhost:5000/api/v2/package/ 127ms
                    Assert.Contains("already exists at feed", outputMessages);
                    Assert.Contains("Your package was pushed.", outputMessages);
                }
        }
        public void AddPkg_RemoveParsing(string packageOption, string package,
                                         string projectOption, string project)
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectPath = Path.Combine(testDirectory, project);
                File.Create(projectPath).Dispose();
                var argList = new List <string>()
                {
                    "remove",
                    packageOption,
                    package,
                    projectOption,
                    projectPath
                };

                var logger            = new TestCommandOutputLogger();
                var testApp           = new CommandLineApplication();
                var mockCommandRunner = new Mock <IPackageReferenceCommandRunner>();
                mockCommandRunner
                .Setup(m => m.ExecuteCommand(It.IsAny <PackageReferenceArgs>(), It.IsAny <MSBuildAPIUtility>()))
                .ReturnsAsync(0);

                testApp.Name = "dotnet nuget_test";
                RemovePackageReferenceCommand.Register(testApp,
                                                       () => logger,
                                                       () => mockCommandRunner.Object);

                // Act
                var result = testApp.Execute(argList.ToArray());

                XPlatTestUtils.DisposeTemporaryFile(projectPath);

                // Assert
                mockCommandRunner.Verify(m => m.ExecuteCommand(It.Is <PackageReferenceArgs>(p =>
                                                                                            p.PackageId == package &&
                                                                                            p.ProjectPath == projectPath),
                                                               It.IsAny <MSBuildAPIUtility>()));

                Assert.Equal(0, result);
            }
        }
Exemplo n.º 20
0
        // [InlineData(TestServers.Klondike, nameof(TestServers.Klondike), false)] // 500 Internal Server Error pushing
        // [InlineData(TestServers.NuGetServer, nameof(TestServers.NuGetServer), false)] // 500 - missing manifest?
        public async Task PushToServerSucceeds(string sourceUri, string feedName, bool mustDeleteFirst)
        {
            // Arrange
            using (var packageDir = TestDirectory.Create())
                using (TestFileSystemUtility.SetCurrentDirectory(packageDir))
                {
                    var packageId      = "XPlatPushTests.PushToServerSucceeds";
                    var packageVersion = "1.0.0";
                    var packageFile    = await TestPackagesCore.GetRuntimePackageAsync(packageDir, packageId, packageVersion);

                    var configFile = XPlatTestUtils.CopyFuncTestConfig(packageDir);
                    var log        = new TestCommandOutputLogger();

                    var apiKey = XPlatTestUtils.ReadApiKey(feedName);
                    Assert.False(string.IsNullOrEmpty(apiKey));

                    if (mustDeleteFirst)
                    {
                        DeletePackageBeforePush(packageId, packageVersion, sourceUri, apiKey);
                    }

                    var pushArgs = new List <string>()
                    {
                        "push",
                        packageFile.FullName,
                        "--source",
                        sourceUri,
                        "--api-key",
                        apiKey
                    };

                    // Act
                    int exitCode = NuGet.CommandLine.XPlat.Program.MainInternal(pushArgs.ToArray(), log);

                    // Assert
                    Assert.Equal(string.Empty, log.ShowErrors());
                    Assert.Equal(0, exitCode);
                    Assert.Contains($"PUT {sourceUri}", log.ShowMessages());
                    Assert.Contains("Your package was pushed.", log.ShowMessages());
                }
        }
Exemplo n.º 21
0
        public async void AddPkg_ConditionalAddAsUpdate_Succcess(string packageFrameworks, string projectFrameworks,
                                                                 string userInputFrameworks, string userInputVersionOld, string userInputVersionNew)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, projectFrameworks);
                var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks);

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, userInputVersionOld, projectA);
                var commandRunner = new AddPackageReferenceCommandRunner();
                var msBuild       = MsBuild;

                // Create a package ref with old version
                var result = commandRunner.ExecuteCommand(packageArgs, msBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, userInputVersionNew, projectA);
                commandRunner = new AddPackageReferenceCommandRunner();
                var commonFramework = XPlatTestUtils.GetCommonFramework(packageFrameworks, projectFrameworks, userInputFrameworks);

                // Act
                // Create a package ref with new version
                result = commandRunner.ExecuteCommand(packageArgs, msBuild)
                         .Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                // Verify that the only package reference is with the new version
                Assert.Equal(0, result);
                Assert.True(XPlatTestUtils.ValidateReference(projectXmlRoot, packageX.Id, userInputVersionNew));
            }
        }
Exemplo n.º 22
0
        public async void AddPkg_UnconditionalAddTwoPackages_Success()
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46");
                var packageX = XPlatTestUtils.CreatePackage("PkgX");
                var packageY = XPlatTestUtils.CreatePackage("PkgY");

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageY);

                var packageArgs   = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, packageX.Version, projectA);
                var commandRunner = new AddPackageReferenceCommandRunner();

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageY.Id, packageY.Version, projectA);

                // Act
                result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                         .Result;
                projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;

                // Assert
                Assert.Equal(0, result);
                Assert.True(XPlatTestUtils.ValidateTwoReferences(projectXmlRoot, packageX, packageY));
            }
        }
Exemplo n.º 23
0
        public void Restore_WithConfigFileInDifferentDirectory_Succeeds(string sourceUri)
        {
            using (var packagesDir = TestDirectory.Create())
                using (var projectDir = TestDirectory.Create())
                    using (var configDir = TestDirectory.Create())
                    {
                        var configFile = XPlatTestUtils.CopyFuncTestConfig(configDir);

                        var specPath = Path.Combine(projectDir, "XPlatRestoreTests", "project.json");
                        var spec     = XPlatTestUtils.BasicConfigNetCoreApp;

                        XPlatTestUtils.AddDependency(spec, "costura.fody", "1.3.3");
                        XPlatTestUtils.AddDependency(spec, "fody", "1.29.4");
                        XPlatTestUtils.WriteJson(spec, specPath);

                        var log = new TestCommandOutputLogger();

                        var args = new List <string>()
                        {
                            "restore",
                            projectDir,
                            "--packages",
                            packagesDir,
                            "--source",
                            sourceUri,
                            "--no-cache",
                            "--configfile",
                            configFile
                        };

                        // Act
                        int exitCode = NuGet.CommandLine.XPlat.Program.MainInternal(args.ToArray(), log);

                        Assert.Contains($@"OK {sourceUri}/FindPackagesById()?id='fody'", log.ShowMessages());
                        Assert.Equal(string.Empty, log.ShowErrors());
                        Assert.Equal(0, exitCode);

                        var lockFilePath = Path.Combine(projectDir, "XPlatRestoreTests", "project.lock.json");
                        Assert.True(File.Exists(lockFilePath));
                    }
        }
Exemplo n.º 24
0
        public async void AddPkg_ConditionalAddWithoutVersion_Success(string packageFrameworks,
                                                                      string projectFrameworks,
                                                                      string userInputFrameworks)
        {
            // Arrange

            using (var pathContext = new SimpleTestPathContext())
            {
                var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, projectFrameworks);
                var packageX = XPlatTestUtils.CreatePackage(frameworkString: packageFrameworks);

                // Generate Package
                await SimpleTestPackageUtility.CreateFolderFeedV3(
                    pathContext.PackageSource,
                    PackageSaveMode.Defaultv3,
                    packageX);

                // Since user is not inputing a version, it is converted to a "*" in the command
                var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id, "*",
                                                                         projectA,
                                                                         frameworks: userInputFrameworks,
                                                                         noVersion: true);

                var commandRunner   = new AddPackageReferenceCommandRunner();
                var commonFramework = XPlatTestUtils.GetCommonFramework(packageFrameworks, projectFrameworks, userInputFrameworks);

                // Act
                var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                             .Result;
                var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                var itemGroup      = XPlatTestUtils.GetItemGroupForFramework(projectXmlRoot, commonFramework);

                // Assert
                Assert.Equal(0, result);
                Assert.NotNull(itemGroup);

                // Since user did not specify a version, the package reference will contain the resolved version
                Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageX.Id, "1.0.0"));
            }
        }
Exemplo n.º 25
0
        public void BasicListPackageParsing_Interactive()
        {
            // Arrange
            using (var testDirectory = TestDirectory.Create())
            {
                var projectPath = Path.Combine(testDirectory, "project.csproj");
                File.WriteAllText(projectPath, string.Empty);

                var argList = new List <string>()
                {
                    "list",
                    "--interactive",
                    projectPath
                };

                var logger            = new TestCommandOutputLogger();
                var testApp           = new CommandLineApplication();
                var mockCommandRunner = new Mock <IListPackageCommandRunner>();
                mockCommandRunner
                .Setup(m => m.ExecuteCommandAsync(It.IsAny <ListPackageArgs>()))
                .Returns(Task.CompletedTask);

                testApp.Name = "dotnet nuget_test";
                ListPackageCommand.Register(testApp,
                                            () => logger,
                                            () => mockCommandRunner.Object);

                // Act
                var result = testApp.Execute(argList.ToArray());

                XPlatTestUtils.DisposeTemporaryFile(projectPath);

                // Assert
                mockCommandRunner.Verify();
                Assert.NotNull(HttpHandlerResourceV3.CredentialService);
                Assert.Equal(0, result);
            }
        }
Exemplo n.º 26
0
        public async void AddPkg_UnconditionalAddWithPackageDirectory_Success()
        {
            // Arrange

            using (var tempGlobalPackagesDirectory = TestDirectory.Create())
                using (var pathContext = new SimpleTestPathContext())
                {
                    var projectA = XPlatTestUtils.CreateProject(projectName, pathContext, "net46");
                    var packageX = XPlatTestUtils.CreatePackage();

                    // Generate Package
                    await SimpleTestPackageUtility.CreateFolderFeedV3(
                        pathContext.PackageSource,
                        PackageSaveMode.Defaultv3,
                        packageX);

                    var packageArgs = XPlatTestUtils.GetPackageReferenceArgs(packageX.Id,
                                                                             packageX.Version,
                                                                             projectA,
                                                                             packageDirectory: tempGlobalPackagesDirectory.Path);
                    var commandRunner = new AddPackageReferenceCommandRunner();

                    // Act
                    var result = commandRunner.ExecuteCommand(packageArgs, MsBuild)
                                 .Result;
                    var projectXmlRoot = XPlatTestUtils.LoadCSProj(projectA.ProjectPath).Root;
                    var itemGroup      = XPlatTestUtils.GetItemGroupForAllFrameworks(projectXmlRoot);

                    // Assert
                    Assert.Equal(0, result);
                    Assert.NotNull(itemGroup);
                    Assert.True(XPlatTestUtils.ValidateReference(itemGroup, packageX.Id, packageX.Version));

                    // Since user provided packge directory, assert if package is present
                    Assert.True(XPlatTestUtils.ValidatePackageDownload(tempGlobalPackagesDirectory.Path, packageX));
                }
        }
Exemplo n.º 27
0
        public void AddPkg_ArgParsing(string packageOption, string package, string versionOption, string version, string dgFileOption,
                                      string dgFilePath, string projectOption, string project, string frameworkOption, string frameworkString, string sourceOption,
                                      string sourceString, string packageDirectoryOption, string packageDirectory, string noRestoreSwitch)
        {
            // Arrange
            var projectPath = Path.Combine(Path.GetTempPath(), project);

            File.Create(projectPath).Dispose();

            var argList = new List <string>()
            {
                "add",
                packageOption,
                package,
                versionOption,
                version,
                dgFileOption,
                dgFilePath,
                projectOption,
                projectPath
            };

            if (!string.IsNullOrEmpty(frameworkOption))
            {
                argList.Add(frameworkOption);
                argList.Add(frameworkString);
            }
            if (!string.IsNullOrEmpty(sourceOption))
            {
                argList.Add(sourceOption);
                argList.Add(sourceString);
            }
            if (!string.IsNullOrEmpty(packageDirectoryOption))
            {
                argList.Add(packageDirectoryOption);
                argList.Add(packageDirectory);
            }
            if (!string.IsNullOrEmpty(noRestoreSwitch))
            {
                argList.Add(noRestoreSwitch);
            }

            var logger            = new TestCommandOutputLogger();
            var testApp           = new CommandLineApplication();
            var mockCommandRunner = new Mock <IPackageReferenceCommandRunner>();

            mockCommandRunner
            .Setup(m => m.ExecuteCommand(It.IsAny <PackageReferenceArgs>(), It.IsAny <MSBuildAPIUtility>()))
            .ReturnsAsync(0);

            testApp.Name = "dotnet nuget_test";
            AddPackageReferenceCommand.Register(testApp,
                                                () => logger,
                                                () => mockCommandRunner.Object);

            // Act
            var result = testApp.Execute(argList.ToArray());

            XPlatTestUtils.DisposeTemporaryFile(projectPath);

            // Assert
            mockCommandRunner.Verify(m => m.ExecuteCommand(It.Is <PackageReferenceArgs>(p => p.PackageDependency.Id == package &&
                                                                                        p.PackageDependency.VersionRange.OriginalString == version &&
                                                                                        p.ProjectPath == projectPath &&
                                                                                        p.DgFilePath == dgFilePath &&
                                                                                        p.NoRestore == !string.IsNullOrEmpty(noRestoreSwitch) &&
                                                                                        (string.IsNullOrEmpty(frameworkOption) || !string.IsNullOrEmpty(frameworkOption) && p.Frameworks.SequenceEqual(MSBuildStringUtility.Split(frameworkString))) &&
                                                                                        (string.IsNullOrEmpty(sourceOption) || !string.IsNullOrEmpty(sourceOption) && p.Sources.SequenceEqual(MSBuildStringUtility.Split(sourceString))) &&
                                                                                        (string.IsNullOrEmpty(packageDirectoryOption) || !string.IsNullOrEmpty(packageDirectoryOption) && p.PackageDirectory == packageDirectory)),
                                                           It.IsAny <MSBuildAPIUtility>()));

            Assert.Equal(0, result);
        }
Exemplo n.º 28
0
        public async Task RestoreCommand_VerifyMinClientVersionAlreadyInstalled()
        {
            // Arrange
            using (var emptyDir = TestFileSystemUtility.CreateRandomTestFolder())
                using (var workingDir = TestFileSystemUtility.CreateRandomTestFolder())
                    using (var packagesDir = TestFileSystemUtility.CreateRandomTestFolder())
                        using (var projectDir = TestFileSystemUtility.CreateRandomTestFolder())
                        {
                            var logger = new TestLogger();

                            var packageContext = new SimpleTestPackageContext()
                            {
                                Id               = "packageA",
                                Version          = "1.0.0",
                                MinClientVersion = "9.9.9"
                            };

                            var packagePath = Path.Combine(workingDir, "packageA.1.0.0.nupkg");

                            SimpleTestPackageUtility.CreatePackages(workingDir, packageContext);

                            // install the package
                            using (var fileStream = File.OpenRead(packagePath))
                            {
                                await PackageExtractor.InstallFromSourceAsync((stream) =>
                                                                              fileStream.CopyToAsync(stream, 4096, CancellationToken.None),
                                                                              new VersionFolderPathContext(new PackageIdentity("packageA", NuGetVersion.Parse("1.0.0")),
                                                                                                           packagesDir,
                                                                                                           logger,
                                                                                                           PackageSaveMode.Defaultv3,
                                                                                                           XmlDocFileSaveMode.None),
                                                                              CancellationToken.None);
                            }

                            var specPath = Path.Combine(projectDir, "TestProject", "project.json");
                            var spec     = XPlatTestUtils.BasicConfigNetCoreApp;

                            XPlatTestUtils.AddDependency(spec, "packageA", "1.0.0");
                            XPlatTestUtils.WriteJson(spec, specPath);

                            var lockFilePath = Path.Combine(projectDir, "project.lock.json");
                            var log          = new TestCommandOutputLogger();

                            var args = new string[]
                            {
                                "restore",
                                projectDir,
                                "-s",
                                emptyDir,
                                "--packages",
                                packagesDir
                            };

                            // Act
                            var exitCode = Program.MainInternal(args, log);

                            // Assert
                            Assert.Equal(1, log.Errors);
                            Assert.Contains("'packageA 1.0.0' package requires NuGet client version '9.9.9' or above", log.ShowMessages());
                            Assert.False(File.Exists(lockFilePath));
                        }
        }