public void BuildPackage_WithDefaultExcludes_ExcludesDefaultExcludes()
        {
            using (var test = DefaultExclusionsTest.Create())
            {
                var args = new PackArgs()
                {
                    CurrentDirectory = test.CurrentDirectory.FullName,
                    Exclude          = Enumerable.Empty <string>(),
                    Logger           = NullLogger.Instance,
                    Path             = test.NuspecFile.FullName
                };
                var runner = new PackCommandRunner(args, createProjectFactory: null);

                Assert.True(runner.RunPackageBuild());

                using (FileStream stream = test.NupkgFile.OpenRead())
                    using (var package = new ZipArchive(stream, ZipArchiveMode.Read))
                    {
                        foreach (string unexpectedEntryName in test.UnexpectedEntryNames)
                        {
                            Assert.Equal(0, package.Entries.Count(entry => entry.Name == unexpectedEntryName));
                        }

                        foreach (string expectedEntryName in test.ExpectedEntryNames)
                        {
                            Assert.Equal(1, package.Entries.Count(entry => entry.Name == expectedEntryName));
                        }
                    }
            }
        }
Пример #2
0
        public void Validate_AssemblyWithValidTFMInPath_NoWarning()
        {
            // Arrange
            var nuspecContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                "<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">" +
                                "   <metadata>" +
                                "        <id>test</id>" +
                                "        <version>1.0.0</version>" +
                                "        <authors>Unit Test</authors>" +
                                "        <description>Sample Description</description>" +
                                "        <language>en-US</language>" +
                                "    <dependencies>" +
                                "      <dependency id=\"System.Collections.Immutable\" version=\"4.3.0\" />" +
                                "    </dependencies>" +
                                "    </metadata>" +
                                "</package>";

            using (var testDirectory = TestDirectory.Create())
            {
                var nuspecPath = Path.Combine(testDirectory, "test.nuspec");
                File.AppendAllText(nuspecPath, nuspecContent);

                // create a random_tfm directory in a lib directory
                Directory.CreateDirectory(Path.Combine(testDirectory, "lib", "net46"));

                // place a dll inside the folder
                var stream = File.Create(Path.Combine(testDirectory, "lib", "net46", "test.dll"));
                stream.Dispose();

                var builder = new PackageBuilder();
                var runner  = new PackCommandRunner(
                    new PackArgs
                {
                    CurrentDirectory = testDirectory,
                    OutputDirectory  = testDirectory,
                    Path             = nuspecPath,
                    Exclude          = Array.Empty <string>(),
                    Symbols          = true,
                    Logger           = NullLogger.Instance
                },
                    MSBuildProjectFactory.ProjectCreator,
                    builder);

                Assert.True(runner.RunPackageBuild());

                var ruleSet   = RuleSet.PackageCreationRuleSet;
                var nupkgPath = Path.Combine(testDirectory, "test.1.0.0.nupkg");

                using (var reader = new PackageArchiveReader(nupkgPath))
                {
                    var issues = new List <PackagingLogMessage>();
                    foreach (var rule in ruleSet)
                    {
                        issues.AddRange(rule.Validate(reader).OrderBy(p => p.Code.ToString(), StringComparer.CurrentCulture));
                    }

                    Assert.False(issues.Any(p => p.Code == NuGetLogCode.NU5103 && p.Message.Contains("'lib' is not recognized as a valid framework name")));
                }
            }
        }
Пример #3
0
        public void Validate_FileWithEmptyDirectorySymbolWithNoOtherFiles_NoWarning()
        {
            // Arrange
            var nuspecContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                "<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">" +
                                "   <metadata>" +
                                "        <id>test</id>" +
                                "        <version>1.0.0</version>" +
                                "        <authors>Unit Test</authors>" +
                                "        <description>Sample Description</description>" +
                                "        <language>en-US</language>" +
                                "    </metadata>" +
                                "</package>";

            using (var testDirectory = TestDirectory.Create())
            {
                var nuspecPath = Path.Combine(testDirectory, "test.nuspec");
                File.AppendAllText(nuspecPath, nuspecContent);

                // create a directory that contains a _._ file but no other file
                var toolsPath = Path.Combine(testDirectory, "tools");
                Directory.CreateDirectory(toolsPath);

                var emptyDirFilePath = Path.Combine(toolsPath, "_._");
                var stream           = File.CreateText(emptyDirFilePath);
                stream.Dispose();

                var builder = new PackageBuilder();
                var runner  = new PackCommandRunner(
                    new PackArgs
                {
                    CurrentDirectory = testDirectory,
                    OutputDirectory  = testDirectory,
                    Path             = nuspecPath,
                    Exclude          = Array.Empty <string>(),
                    Symbols          = false,
                    Logger           = NullLogger.Instance
                },
                    MSBuildProjectFactory.ProjectCreator,
                    builder);

                Assert.True(runner.RunPackageBuild());

                var ruleSet   = RuleSet.PackageCreationRuleSet;
                var nupkgPath = Path.Combine(testDirectory, "test.1.0.0.nupkg");

                using (var reader = new PackageArchiveReader(nupkgPath))
                {
                    var issues = new List <PackagingLogMessage>();
                    foreach (var rule in ruleSet)
                    {
                        issues.AddRange(rule.Validate(reader).OrderBy(p => p.Code.ToString(), StringComparer.CurrentCulture));
                    }

                    Assert.False(issues.Any(p => p.Code == NuGetLogCode.NU5109 && p.Message.Contains(@"The file at 'tools/_._' uses the symbol for empty directory '_._'")));
                }
            }
        }
Пример #4
0
        public void Validate_NuSpecFileWithNonDefaultProjectUrl_NoWarning()
        {
            // Arrange
            var nuspecContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                "<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">" +
                                "   <metadata>" +
                                "        <id>test</id>" +
                                "        <version>1.0.0</version>" +
                                "        <authors>Unit Test</authors>" +
                                "        <description>Sample Description</description>" +
                                "        <language>en-US</language>" +
                                "        <projectUrl>http://unit.test</projectUrl>" +
                                "        <licenseUrl>http://unit.test</licenseUrl>" +
                                "        <iconUrl>http://unit.test</iconUrl>" +
                                "    <dependencies>" +
                                "      <dependency id=\"System.Collections.Immutable\" version=\"4.3.0\" />" +
                                "    </dependencies>" +
                                "    </metadata>" +
                                "</package>";

            using (var testDirectory = TestDirectory.Create())
            {
                var nuspecPath = Path.Combine(testDirectory, "test.nuspec");
                File.AppendAllText(nuspecPath, nuspecContent);

                var builder = new PackageBuilder();
                var runner  = new PackCommandRunner(
                    new PackArgs
                {
                    CurrentDirectory = testDirectory,
                    OutputDirectory  = testDirectory,
                    Path             = nuspecPath,
                    Exclude          = Array.Empty <string>(),
                    Symbols          = true,
                    Logger           = NullLogger.Instance
                },
                    MSBuildProjectFactory.ProjectCreator,
                    builder);

                Assert.True(runner.RunPackageBuild());

                var ruleSet   = RuleSet.PackageCreationRuleSet;
                var nupkgPath = Path.Combine(testDirectory, "test.1.0.0.nupkg");

                using (var reader = new PackageArchiveReader(nupkgPath))
                {
                    var issues = new List <PackagingLogMessage>();
                    foreach (var rule in ruleSet)
                    {
                        issues.AddRange(rule.Validate(reader).OrderBy(p => p.Code.ToString(), StringComparer.CurrentCulture));
                    }

                    Assert.False(issues.Any(p => p.Code == NuGetLogCode.NU5102));
                }
            }
        }
Пример #5
0
        public void PackTask_Dispose()
        {
            using (var directory = TestDirectory.Create())
            {
                var nuspecPath = Path.Combine(directory, "test.nuspec");
                File.WriteAllText(nuspecPath, @"
<package xmlns=""http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"">
  <metadata>
    <id>Test</id>
    <summary>Summary</summary>
    <description>Description</description>
    <version>1.0.0</version>
    <authors>Microsoft</authors>
    <dependencies>
      <dependency id=""System.Collections.Immutable"" version=""4.3.0"" />
    </dependencies>
  </metadata>
</package>
");

                var builder = new PackageBuilder();

                var runner = new PackCommandRunner(
                    new PackArgs
                {
                    CurrentDirectory = directory,
                    OutputDirectory  = directory,
                    Path             = nuspecPath,
                    Exclude          = Array.Empty <string>(),
                    Symbols          = true,
                    Logger           = NullLogger.Instance
                },
                    MSBuildProjectFactory.ProjectCreator,
                    builder);

                runner.RunPackageBuild();
            }
        }
        public void RunPackageBuild_WithGenerateNugetPackageFalse_ReturnsTrue()
        {
            using (var testDirectory = TestDirectory.Create())
            {
                var args = new PackArgs()
                {
                    CurrentDirectory = testDirectory.Path,
                    Logger           = NullLogger.Instance,
                    PackTargetArgs   = new MSBuildPackTargetArgs()
                    {
                        NuspecOutputPath = Path.Combine(testDirectory.Path, "obj", "Debug"),
                        ContentFiles     = new Dictionary <string, IEnumerable <ContentMetadata> >()
                    },
                    Path    = string.Empty,
                    Exclude = Array.Empty <string>()
                };
                var packageBuilder = new PackageBuilder()
                {
                    Id          = "test",
                    Version     = new NuGetVersion(1, 0, 0),
                    Description = "Testing PackCommandRunner.GenerateNugetPackage = false"
                };
                packageBuilder.Authors.Add("tester");

                var runner = new PackCommandRunner(args, MSBuildProjectFactory.ProjectCreator, packageBuilder);
                runner.GenerateNugetPackage = false;

                // Act
                var actual = runner.RunPackageBuild();

                // Assert
                Assert.True(actual, "PackCommandRunner.RunPackageBuild was not successful");
                var expectedNuspecPath = Path.Combine(args.PackTargetArgs.NuspecOutputPath, "test.1.0.0.nuspec");
                Assert.True(File.Exists(expectedNuspecPath), "nuspec file does not exist");
            }
        }
Пример #7
0
 public bool BuildPackage(PackCommandRunner runner)
 {
     return(runner.RunPackageBuild());
 }
Пример #8
0
        public override void ExecuteCommand()
        {
            var packArgs = new PackArgs();

            packArgs.Logger           = Console;
            packArgs.Arguments        = Arguments;
            packArgs.OutputDirectory  = OutputDirectory;
            packArgs.BasePath         = BasePath;
            packArgs.MsBuildDirectory = new Lazy <string>(() => MsBuildUtility.GetMsBuildDirectoryFromMsBuildPath(MSBuildPath, MSBuildVersion, Console).Value.Path);

            if (!string.IsNullOrEmpty(PackagesDirectory))
            {
                packArgs.PackagesDirectory = Path.GetFullPath(PackagesDirectory);
            }

            if (!string.IsNullOrEmpty(SolutionDirectory))
            {
                packArgs.SolutionDirectory = Path.GetFullPath(SolutionDirectory);
            }

            // Get the input file
            packArgs.Path = PackCommandRunner.GetInputFile(packArgs);

            // Set the current directory if the files being packed are in a different directory
            PackCommandRunner.SetupCurrentDirectory(packArgs);

            Console.WriteLine(LocalizedResourceManager.GetString("PackageCommandAttemptingToBuildPackage"), Path.GetFileName(packArgs.Path));

            if (!string.IsNullOrEmpty(MinClientVersion))
            {
                if (!System.Version.TryParse(MinClientVersion, out _minClientVersionValue))
                {
                    throw new CommandException(LocalizedResourceManager.GetString("PackageCommandInvalidMinClientVersion"));
                }
            }

            if (!string.IsNullOrEmpty(SymbolPackageFormat))
            {
                packArgs.SymbolPackageFormat = PackArgs.GetSymbolPackageFormat(SymbolPackageFormat);
            }
            packArgs.Build   = Build;
            packArgs.Exclude = Exclude;
            packArgs.ExcludeEmptyDirectories   = ExcludeEmptyDirectories;
            packArgs.IncludeReferencedProjects = IncludeReferencedProjects;
            switch (Verbosity)
            {
            case Verbosity.Detailed:
            {
                packArgs.LogLevel = LogLevel.Verbose;
                break;
            }

            case Verbosity.Normal:
            {
                packArgs.LogLevel = LogLevel.Information;
                break;
            }

            case Verbosity.Quiet:
            {
                packArgs.LogLevel = LogLevel.Minimal;
                break;
            }
            }
            packArgs.MinClientVersion  = _minClientVersionValue;
            packArgs.NoDefaultExcludes = NoDefaultExcludes;
            packArgs.NoPackageAnalysis = NoPackageAnalysis;
            if (Properties.Any())
            {
                packArgs.Properties.AddRange(Properties);
            }
            packArgs.Suffix  = Suffix;
            packArgs.Symbols = Symbols;
            packArgs.Tool    = Tool;
            packArgs.InstallPackageToOutputPath    = InstallPackageToOutputPath;
            packArgs.OutputFileNamesWithoutVersion = OutputFileNamesWithoutVersion;

            if (!string.IsNullOrEmpty(Version))
            {
                NuGetVersion version;
                if (!NuGetVersion.TryParse(Version, out version))
                {
                    throw new PackagingException(NuGetLogCode.NU5010, string.Format(CultureInfo.CurrentCulture, NuGetResources.InstallCommandPackageReferenceInvalidVersion, Version));
                }
                packArgs.Version = version.ToFullString();
            }

            var packCommandRunner = new PackCommandRunner(packArgs, ProjectFactory.ProjectCreator);

            if (!packCommandRunner.RunPackageBuild())
            {
                throw new ExitCodeException(1);
            }
        }