示例#1
0
        private string SetupNuGetPackage(bool multiTarget, [CallerMemberName] string callingMethod = "")
        {
            TestAsset helloWorldAsset = _testAssetsManager
                                        .CopyTestAsset("PortableTool", callingMethod + multiTarget)
                                        .WithSource()
                                        .WithProjectChanges(project =>
            {
                XNamespace ns          = project.Root.Name.Namespace;
                XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();

                if (multiTarget)
                {
                    propertyGroup.Element(ns + "TargetFramework").Remove();
                    propertyGroup.Add(new XElement(ns + "TargetFrameworks", "netcoreapp1.1;netcoreapp2.0"));
                }
            })
                                        .Restore(Log);

            _testRoot = helloWorldAsset.TestRoot;

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute();

            return(packCommand.GetNuGetPackage());
        }
        public void It_contains_shim_with_no_build(bool multiTarget, string targetFramework)
        {
            var testAsset = CreateTestAsset(multiTarget, nameof(It_contains_shim_with_no_build) + multiTarget + targetFramework, targetFramework);

            var buildCommand = new BuildCommand(testAsset);

            buildCommand.Execute().Should().Pass();

            var packCommand = new PackCommand(Log, testAsset.TestRoot);

            packCommand.Execute("/p:NoBuild=true").Should().Pass();
            var nugetPackage = packCommand.GetNuGetPackage();

            using (var nupkgReader = new PackageArchiveReader(nugetPackage))
            {
                IEnumerable <NuGetFramework> supportedFrameworks = nupkgReader.GetSupportedFrameworks();
                supportedFrameworks.Should().NotBeEmpty();

                foreach (NuGetFramework framework in supportedFrameworks)
                {
                    var allItems = nupkgReader.GetToolItems().SelectMany(i => i.Items).ToList();
                    allItems.Should().Contain($"tools/{framework.GetShortFolderName()}/any/shims/win-x64/{_customToolCommandName}.exe",
                                              "Name should be the same as the command name even customized");
                    allItems.Should().Contain($"tools/{framework.GetShortFolderName()}/any/shims/osx.10.12-x64/{_customToolCommandName}",
                                              "RID should be the exact match of the RID in the property, even Apphost only has version of win, osx and linux");
                }
            }
        }
示例#3
0
        public void CompatibleFrameworksInPackage()
        {
            TestProject testProject = new()
            {
                Name             = "TestPackage",
                TargetFrameworks = "netstandard2.0;net5.0",
            };

            string sourceCode = @"
namespace PackageValidationTests
{
    public class First
    {
        public void test() { }
#if NETSTANDARD2_0
        public void test(string test) { }
#endif
    }
}";

            testProject.SourceFiles.Add("Hello.cs", sourceCode);
            TestAsset   asset       = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
            PackCommand packCommand = new PackCommand(Log, Path.Combine(asset.TestRoot, testProject.Name));
            var         result      = packCommand.Execute();

            Assert.Equal(string.Empty, result.StdErr);
            Package package = NupkgParser.CreatePackage(packCommand.GetNuGetPackage(), null);

            new CompatibleFrameworkInPackageValidator(string.Empty, null, _log).Validate(package);
            Assert.NotEmpty(_log.errors);
            // TODO: add asserts for assembly and header metadata.
            Assert.Contains("CP0002 Member 'PackageValidationTests.First.test(string)' exists on the left but not on the right", _log.errors);
        }
示例#4
0
        public void Given_nuget_alias_It_finds_the_entry_point_dll_and_command_name_and_put_in_setting_file()
        {
            TestAsset helloWorldAsset = _testAssetsManager
                                        .CopyTestAsset("PortableTool")
                                        .WithSource()
                                        .WithProjectChanges(project =>
            {
                XNamespace ns          = project.Root.Name.Namespace;
                XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
                propertyGroup.Elements("TargetFramework").First().SetValue("targetframeworkAlias");
                XElement conditionPropertyGroup = new XElement("PropertyGroup");
                project.Root.Add(conditionPropertyGroup);
                conditionPropertyGroup.SetAttributeValue("Condition", "'$(TargetFramework)' == 'targetframeworkAlias'");
                conditionPropertyGroup.SetElementValue("TargetFrameworkIdentifier", ".NETCoreApp");
                conditionPropertyGroup.SetElementValue("TargetFrameworkVersion", "v3.1");
                conditionPropertyGroup.SetElementValue("TargetFrameworkMoniker", ".NETCoreApp,Version=v3.1");
            });

            _testRoot = helloWorldAsset.TestRoot;

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            var result = packCommand.Execute();

            result.Should().Pass();

            var nugetPackage = packCommand.GetNuGetPackage();

            AssertFiles(nugetPackage);
        }
示例#5
0
        private string SetupNuGetPackage(bool multiTarget, [CallerMemberName] string callingMethod = "")
        {
            TestAsset helloWorldAsset = _testAssetsManager
                                        .CopyTestAsset("PortableTool", callingMethod + multiTarget)
                                        .WithSource()
                                        .WithProjectChanges(project =>
            {
                XNamespace ns          = project.Root.Name.Namespace;
                XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
                propertyGroup.Add(new XElement(ns + "PackAsToolShimRuntimeIdentifiers", "win-x64;osx.10.12-x64"));
                propertyGroup.Add(new XElement(ns + "ToolCommandName", _customToolCommandName));

                if (multiTarget)
                {
                    propertyGroup.Element(ns + "TargetFramework").Remove();
                    propertyGroup.Add(new XElement(ns + "TargetFrameworks", "netcoreapp2.1"));
                }
            })
                                        .Restore(Log);

            _testRoot = helloWorldAsset.TestRoot;

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute();
            _packageId = Path.GetFileNameWithoutExtension(packCommand.ProjectFile);

            return(packCommand.GetNuGetPackage());
        }
        public void It_finds_commandName_and_put_in_setting_file()
        {
            const string explicitCommandName = "explicit_command_name";
            TestAsset    helloWorldAsset     = _testAssetsManager
                                               .CopyTestAsset("PortableTool", "PackPortableToolToolCommandName")
                                               .WithSource()
                                               .WithProjectChanges(project =>
            {
                XNamespace ns          = project.Root.Name.Namespace;
                XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
                propertyGroup.Add(new XElement("ToolCommandName", explicitCommandName));
            })
                                               .Restore(Log);
            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute();

            var nugetPackage = packCommand.GetNuGetPackage();

            using (var nupkgReader = new PackageArchiveReader(nugetPackage))
            {
                var    anyTfm      = nupkgReader.GetSupportedFrameworks().First().GetShortFolderName();
                var    tmpfilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                string copiedFile  = nupkgReader.ExtractFile($"tools/{anyTfm}/any/DotnetToolSettings.xml", tmpfilePath, null);

                XDocument.Load(copiedFile)
                .Element("DotNetCliTool")
                .Element("Commands")
                .Element("Command")
                .Attribute("Name")
                .Value
                .Should().Be(explicitCommandName);
            }
        }
        public void It_produces_valid_shims_when_the_first_build_is_wrong(bool multiTarget, string targetFramework)
        {
            // The first build use wrong package id and should embed wrong string to shims. However, the pack should produce correct shim
            // since it includes build target. And the incremental build should consider the shim to be invalid and recreate that.

            if (!Environment.Is64BitOperatingSystem)
            {
                // only sample test on win-x64 since shims are RID specific
                return;
            }

            TestAsset helloWorldAsset = CreateTestAsset(multiTarget,
                                                        "It_produces_valid_shims2" + multiTarget + targetFramework,
                                                        targetFramework: targetFramework);

            var testRoot = helloWorldAsset.TestRoot;

            var buildCommand = new BuildCommand(helloWorldAsset);

            buildCommand.Execute("/p:PackageId=wrongpackagefirstbuild");

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute().Should().Pass();
            var nugetPackage = packCommand.GetNuGetPackage();

            _packageId = Path.GetFileNameWithoutExtension(packCommand.ProjectFile);

            AssertValidShim(testRoot, nugetPackage);
        }
        private TestPackageReference GetPackageReference(TestProject project)
        {
            var asset = _testAssetsManager.CreateTestProject(project, project.Name);
            var pack  = new PackCommand(Log, Path.Combine(asset.TestRoot, project.Name));

            pack.Execute().Should().Pass();

            return(new TestPackageReference(project.Name, "1.0.0", pack.GetNuGetPackage(project.Name)));
        }
示例#9
0
        private string GetPackagePath(string targetFramework, string packageName, string version)
        {
            var project     = GetProject(targetFramework, packageName, version);
            var packCommand = new PackCommand(Log, _testAssetsManager.CreateTestProject(project).TestRoot, packageName);

            packCommand
            .Execute()
            .Should()
            .Pass();
            return(packCommand.GetNuGetPackage(packageName, packageVersion: version));
        }
示例#10
0
        private TestPackageReference GetPackageReference(string targetFramework, string packageName, string projectFileContent)
        {
            var project     = GetProject(targetFramework, packageName, projectFileContent);
            var packCommand = new PackCommand(Log, _testAssetsManager.CreateTestProject(project).TestRoot, packageName);

            packCommand
            .Execute()
            .Should()
            .Pass();
            return(new TestPackageReference(packageName, "1.0.0", packCommand.GetNuGetPackage(packageName)));
        }
示例#11
0
        private TestPackageReference GetPackageReference(string targetFramework, string packageName, string projectFileContent, [CallerMemberName] string callingMethod = "", string identifier = null)
        {
            var project     = GetProject(targetFramework, packageName, projectFileContent);
            var packCommand = new PackCommand(Log, _testAssetsManager.CreateTestProject(project, callingMethod: callingMethod, identifier: identifier).TestRoot, packageName);

            packCommand
            .Execute()
            .Should()
            .Pass();
            return(new TestPackageReference(packageName, "1.0.0", packCommand.GetNuGetPackage(packageName)));
        }
示例#12
0
        private string GetPackagePath(string targetFramework, string packageName, string version, [CallerMemberName] string callingMethod = "", string identifier = null)
        {
            var project     = GetProject(targetFramework, packageName, version);
            var packCommand = new PackCommand(Log, _testAssetsManager.CreateTestProject(project, callingMethod: callingMethod, identifier: identifier).TestRoot, packageName);

            packCommand
            .Execute()
            .Should()
            .Pass();
            return(packCommand.GetNuGetPackage(packageName, packageVersion: version));
        }
        private string SetupNuGetPackage(bool multiTarget, [CallerMemberName] string callingMethod = "")
        {
            TestAsset helloWorldAsset = CreateTestAsset(multiTarget, callingMethod);

            _testRoot = helloWorldAsset.TestRoot;

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute();
            _packageId = Path.GetFileNameWithoutExtension(packCommand.ProjectFile);

            return(packCommand.GetNuGetPackage());
        }
示例#14
0
        private string SetupNuGetPackage([CallerMemberName] string callingMethod = "")
        {
            TestAsset testAsset = _testAssetsManager
                                  .CopyTestAsset("PortableToolWithP2P", callingMethod)
                                  .WithSource();

            var appProjectDirectory = Path.Combine(testAsset.TestRoot, "App");
            var packCommand         = new PackCommand(Log, appProjectDirectory);

            packCommand.Execute();

            return(packCommand.GetNuGetPackage());
        }
        public void It_has_native_and_transitive_dependencies_dll(bool multiTarget)
        {
            TestAsset helloWorldAsset = _testAssetsManager
                                        .CopyTestAsset("PortableTool")
                                        .WithSource()
                                        .WithProjectChanges(project =>
            {
                ChangeToPackageThatDependesOnOtherPackage(project);

                XNamespace ns          = project.Root.Name.Namespace;
                XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();

                if (multiTarget)
                {
                    propertyGroup.Element(ns + "TargetFramework").Remove();
                    propertyGroup.Add(new XElement(ns + "TargetFrameworks", "netcoreapp2.1"));
                }
            });

            helloWorldAsset.Restore(Log);

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute();
            var nugetPackage = packCommand.GetNuGetPackage();

            using (var nupkgReader = new PackageArchiveReader(nugetPackage))
            {
                IEnumerable <NuGet.Frameworks.NuGetFramework> supportedFrameworks = nupkgReader.GetSupportedFrameworks();
                supportedFrameworks.Should().NotBeEmpty();

                var transitiveDependency = "runtimes/unix/lib/netstandard1.3/System.Data.SqlClient.dll";
                var nativeDependency     = "runtimes/win7-x86/native/sni.dll";

                foreach (var dependency in new string[] { transitiveDependency, nativeDependency })
                {
                    foreach (NuGet.Frameworks.NuGetFramework framework in supportedFrameworks)
                    {
                        var allItems = nupkgReader.GetToolItems().SelectMany(i => i.Items).ToList();
                        allItems.Should().Contain($"tools/{framework.GetShortFolderName()}/any/{dependency}");
                    }
                }

                nupkgReader
                .GetPackageDependencies().First().Packages
                .Should().BeEmpty();
            }
        }
        private string SetupNuGetPackage(
            bool multiTarget,
            [CallerMemberName] string callingMethod        = "",
            Dictionary <string, string> additionalProperty = null)
        {
            TestAsset helloWorldAsset = CreateTestAsset(multiTarget, callingMethod, additionalProperty);

            _testRoot = helloWorldAsset.TestRoot;

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            packCommand.Execute().Should().Pass();
            _packageId = Path.GetFileNameWithoutExtension(packCommand.ProjectFile);

            return(packCommand.GetNuGetPackage(packageVersion: _packageVersion));
        }
示例#17
0
        public void MultipleCompatibleFrameworksInPackage()
        {
            string      name        = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
            TestProject testProject = new()
            {
                Name             = name,
                TargetFrameworks = $"netstandard2.0;netcoreapp3.1;{ToolsetInfo.CurrentTargetFramework}",
            };

            string sourceCode = @"
namespace PackageValidationTests
{
    public class First
    {
        public void test() { }
#if NETSTANDARD2_0
        public void test(string test) { }
#endif
#if NETCOREAPP3_1
        public void test(bool test) { }
#endif
    }
}";

            testProject.SourceFiles.Add("Hello.cs", sourceCode);
            TestAsset   asset       = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
            PackCommand packCommand = new PackCommand(Log, Path.Combine(asset.TestRoot, testProject.Name));
            var         result      = packCommand.Execute();

            Assert.Equal(string.Empty, result.StdErr);
            Package package = Package.Create(packCommand.GetNuGetPackage(), null);

            new CompatibleFrameworkInPackageValidator(_log).Validate(new PackageValidatorOption(package));
            Assert.NotEmpty(_log.errors);

            string assemblyName = $"{asset.TestProject.Name}.dll";

            // TODO: add asserts for assembly and header metadata.
            Assert.Contains($"CP0002 Member 'PackageValidationTests.First.test(string)' exists on lib/netstandard2.0/{assemblyName} but not on lib/netcoreapp3.1/{assemblyName}", _log.errors);
            Assert.Contains($"CP0002 Member 'PackageValidationTests.First.test(bool)' exists on lib/netcoreapp3.1/{assemblyName} but not on lib/{ToolsetInfo.CurrentTargetFramework}/{assemblyName}", _log.errors);
        }
示例#18
0
        public void ValidatePackageWithReferences()
        {
            TestLogger log = new TestLogger();

            string testDependencySource = @"namespace PackageValidationTests { public class ItermediateBaseClass
#if NETSTANDARD2_0
: IBaseInterface 
#endif
{ } }";

            TestProject testSubDependency = CreateTestProject(@"namespace PackageValidationTests { public interface IBaseInterface { } }", "netstandard2.0");
            TestProject testDependency    = CreateTestProject(
                testDependencySource,
                "netstandard2.0;net5.0",
                new[] { testSubDependency });
            TestProject testProject = CreateTestProject(@"namespace PackageValidationTests { public class First : ItermediateBaseClass { } }", "netstandard2.0;net5.0", new[] { testDependency });

            TestAsset   asset       = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
            PackCommand packCommand = new PackCommand(Log, Path.Combine(asset.TestRoot, testProject.Name));
            var         result      = packCommand.Execute();

            Assert.Equal(string.Empty, result.StdErr);
            Package package = NupkgParser.CreatePackage(packCommand.GetNuGetPackage(), null);

            // First we run without references. Without references, ApiCompat should not be able to see that class First
            // removed an interface due to it's base class removing that implementation. We validate that APICompat doesn't
            // log errors when not using references.
            new CompatibleFrameworkInPackageValidator("CP1003", null, false, log, null).Validate(package);
            Assert.Empty(log.errors);

            // Now we do pass in references. With references, ApiCompat should now detect that an interface was removed in a
            // dependent assembly, causing one of our types to stop implementing that assembly. We validate that a CP0008 is logged.
            Dictionary <string, HashSet <string> > references = new()
            {
                { "netstandard2.0", new HashSet <string> {
                      Path.Combine(asset.TestRoot, asset.TestProject.Name, "bin", "Debug", "netstandard2.0")
                  } },
                { "net5.0", new HashSet <string> {
                      Path.Combine(asset.TestRoot, asset.TestProject.Name, "bin", "Debug", "net5.0")
                  } }
            };
        public void Given_wpf_project_It_contains_shim_with_WindowsGraphicalUserInterfaceBit()
        {
            ushort windowsGUISubsystem = 0x2;
            var    testProject         = new TestProject()
            {
                Name             = "wpfTool",
                TargetFrameworks = "netcoreapp3.0",
                IsSdkProject     = true,
                ProjectSdk       = "Microsoft.NET.Sdk.WindowsDesktop",
                IsWinExe         = true,
            };

            testProject.AdditionalProperties.Add("UseWPF", "true");
            testProject.AdditionalProperties.Add("PackAsToolShimRuntimeIdentifiers", "win-x64;osx.10.12-x64");
            testProject.AdditionalProperties.Add("ToolCommandName", _customToolCommandName);
            testProject.AdditionalProperties.Add("PackAsTool", "true");

            TestAsset asset       = _testAssetsManager.CreateTestProject(testProject);
            var       packCommand = new PackCommand(Log, Path.Combine(asset.Path, testProject.Name));

            packCommand
            .Execute()
            .Should()
            .Pass();

            var nugetPackage = packCommand.GetNuGetPackage(packageVersion: _packageVersion);

            using (var nupkgReader = new PackageArchiveReader(nugetPackage))
            {
                IEnumerable <NuGetFramework> supportedFrameworks = nupkgReader.GetSupportedFrameworks();
                supportedFrameworks.Should().NotBeEmpty();

                var    tmpfilePath = Path.Combine(asset.TestRoot, "temp", Path.GetRandomFileName());
                string copiedFile  = nupkgReader.ExtractFile(
                    $"tools/netcoreapp3.0/any/shims/win-x64/{_customToolCommandName}.exe",
                    tmpfilePath,
                    null);
                HostModel.AppHost.PEUtils.GetWindowsGraphicalUserInterfaceBit(copiedFile).Should().Be(windowsGUISubsystem);
            }
        }
示例#20
0
        private TestPackageReference GetPackageReference()
        {
            var referencedPackage = new TestProject()
            {
                Name             = "CopyPPFilesToOutput",
                TargetFrameworks = "netcoreapp3.0",
            };

            var packageAsset = _testAssetsManager.CreateTestProject(referencedPackage);

            WriteFile(Path.Combine(packageAsset.TestRoot, referencedPackage.Name, "Nontransformed.ps1"), "Content file");
            WriteFile(Path.Combine(packageAsset.TestRoot, referencedPackage.Name, "Test.ps1.pp"), "Content file");
            packageAsset = packageAsset
                           .WithProjectChanges(project => AddContent(project));

            var packCommand = new PackCommand(Log, packageAsset.TestRoot, referencedPackage.Name);

            packCommand.Execute()
            .Should()
            .Pass();
            return(new TestPackageReference(referencedPackage.Name, "1.0.0", packCommand.GetNuGetPackage(referencedPackage.Name)));
        }
示例#21
0
        private TestPackageReference GeneratePackageToGoMissing()
        {
            var project = new TestProject
            {
                Name             = "packagethatwillgomissing",
                TargetFrameworks = "netstandard1.3",
            };

            var asset = _testAssetsManager
                        .CreateTestProject(project, project.Name);

            var pack = new PackCommand(
                Log,
                Path.Combine(asset.TestRoot, project.Name));

            pack.Execute().Should().Pass();

            return(new TestPackageReference(
                       project.Name,
                       "1.0.0",
                       pack.GetNuGetPackage(project.Name)));
        }
示例#22
0
        private string SetupNuGetPackage(bool multiTarget, [CallerMemberName] string callingMethod = "")
        {
            TestAsset helloWorldAsset = _testAssetsManager
                                        .CopyTestAsset("PortableTool", callingMethod + multiTarget)
                                        .WithSource()
                                        .WithProjectChanges(project =>
            {
                XNamespace ns          = project.Root.Name.Namespace;
                XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
            })
                                        .WithTargetFrameworkOrFrameworks(_targetFrameworkOrFrameworks, multiTarget);

            _testRoot = helloWorldAsset.TestRoot;

            var packCommand = new PackCommand(Log, helloWorldAsset.TestRoot);

            var result = packCommand.Execute();

            result.Should().Pass();

            return(packCommand.GetNuGetPackage());
        }
        public void It_builds_and_result_contains_dependencies_dll()
        {
            TestAsset testAsset           = SetupAndRestoreTestAsset();
            var       appProjectDirectory = Path.Combine(testAsset.TestRoot, "App");
            var       buildCommand        = new BuildCommand(testAsset, "App");

            buildCommand.Execute();

            var packCommand = new PackCommand(Log, appProjectDirectory);
            // Do not run pack, just use it to get nupkg since it should be run by build.
            var nugetPackage = packCommand.GetNuGetPackage();

            using (var nupkgReader = new PackageArchiveReader(nugetPackage))
            {
                IEnumerable <NuGet.Frameworks.NuGetFramework> supportedFrameworks = nupkgReader.GetSupportedFrameworks();
                supportedFrameworks.Should().NotBeEmpty();

                foreach (NuGet.Frameworks.NuGetFramework framework in supportedFrameworks)
                {
                    var allItems = nupkgReader.GetToolItems().SelectMany(i => i.Items).ToList();
                    allItems.Should().Contain($"tools/{framework.GetShortFolderName()}/any/Library.dll");
                }
            }
        }