NuGet.Analysis.Rules.MisplacedScriptFileRule.Validate C# (CSharp) Method

Validate() public method

public Validate ( IPackage package ) : IEnumerable
package IPackage
return IEnumerable
        public IEnumerable<PackageIssue> Validate(IPackage package)
        {
            foreach (IPackageFile file in package.GetFiles())
            {
                string path = file.Path;
                if (!path.EndsWith(ScriptExtension, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (!path.StartsWith(Constants.ToolsDirectory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
                {
                    yield return CreatePackageIssueForMisplacedScript(path);
                }
                else
                {
                    string directory = Path.GetDirectoryName(path);
                    string name = Path.GetFileNameWithoutExtension(path);
                    if (!directory.Equals(Constants.ToolsDirectory, StringComparison.OrdinalIgnoreCase) ||
                        !name.Equals("install", StringComparison.OrdinalIgnoreCase) &&
                        !name.Equals("uninstall", StringComparison.OrdinalIgnoreCase) &&
                        !name.Equals("init", StringComparison.OrdinalIgnoreCase))
                    {
                        yield return CreatePackageIssueForUnrecognizedScripts(path);
                    }
                }
            }
        }

Usage Example

        public void ScriptsOutsideToolsFolder()
        {
            // Arrange
            var package = PackageUtility.CreatePackage(
                "A",
                content: new[] { "install.ps1" },
                assemblyReferences: new[] { "init.ps1" }
            );
            var rule = new MisplacedScriptFileRule();

            // Act
            IList<PackageIssue> issues = rule.Validate(package).ToList();

            // Assert
            Assert.Equal(2, issues.Count);

            PackageIssueTestHelper.AssertPackageIssue(
                issues[0],
                "PowerShell file outside tools folder.",
                "The script file 'content\\install.ps1' is outside the 'tools' folder and hence will not be executed during installation of this package.",
                "Move it into the 'tools' folder.");

            PackageIssueTestHelper.AssertPackageIssue(
                issues[1],
                "PowerShell file outside tools folder.",
                "The script file 'init.ps1' is outside the 'tools' folder and hence will not be executed during installation of this package.",
                "Move it into the 'tools' folder.");
        }
All Usage Examples Of NuGet.Analysis.Rules.MisplacedScriptFileRule::Validate