diff --git a/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj b/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj index a31459e..3cda193 100644 --- a/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj +++ b/src/chocolatey.package.validator.tests/chocolatey.package.validator.tests.csproj @@ -87,6 +87,7 @@ + diff --git a/src/chocolatey.package.validator.tests/infrastructure.app/InstallScriptsShouldntUseCreateShortcutNoteSpecs.cs b/src/chocolatey.package.validator.tests/infrastructure.app/InstallScriptsShouldntUseCreateShortcutNoteSpecs.cs new file mode 100644 index 0000000..1d2de30 --- /dev/null +++ b/src/chocolatey.package.validator.tests/infrastructure.app/InstallScriptsShouldntUseCreateShortcutNoteSpecs.cs @@ -0,0 +1,103 @@ +// Copyright © 2015 - Present RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.package.validator.tests.infrastructure.app +{ + using System.Collections.Generic; + using chocolatey.package.validator.infrastructure.app.rules; + using chocolatey.package.validator.infrastructure.rules; + using Moq; + using NuGet; + using Should; + + public abstract class InstallScriptsShouldntUseCreateShortcutNoteSpecsBase : TinySpec + { + protected InstallScriptsShouldntUseCreateShortcutNote note; + protected Mock package = new Mock(); + protected Mock packageFile = new Mock(); + + public override void Context() + { + note = new InstallScriptsShouldntUseCreateShortcutNote(); + } + + public class when_inspecting_package_with_installation_script_with_createshortcut : InstallScriptsShouldntUseCreateShortcutNoteSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + packageFile.Setup(f => f.GetStream()).Returns(@"$WshShell = New-Object -comObject WScript.Shell +$Shortcut = $WshShell.CreateShortcut('$Home/Desktop/ColorPix.lnk') +$Shortcut.TargetPath = 'C:/Program Files (x86)/ColorPix/ColorPix.exe' +$Shortcut.Save()".to_stream()); + packageFile.Setup(f => f.Path).Returns("test.ps1"); + + package.Setup(p => p.GetFiles()).Returns(new List() { packageFile.Object }); + } + + public override void Because() + { + result = this.note.is_valid(package.Object); + } + + [Fact] + public void should_not_be_valid() + { + result.Validated.ShouldBeFalse(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + + public class when_inspecting_package_with_installation_script_without_createshortcut : InstallScriptsShouldntUseCreateShortcutNoteSpecsBase + { + private PackageValidationOutput result; + + public override void Context() + { + base.Context(); + + packageFile.Setup(f => f.GetStream()).Returns("Write-Output Test".to_stream()); + packageFile.Setup(f => f.Path).Returns("test.ps1"); + + package.Setup(p => p.GetFiles()).Returns(new List() { packageFile.Object }); + } + + public override void Because() + { + result = this.note.is_valid(package.Object); + } + + [Fact] + public void should_be_valid() + { + result.Validated.ShouldBeTrue(); + } + + [Fact] + public void should_not_override_the_base_message() + { + result.ValidationFailureMessageOverride.ShouldBeNull(); + } + } + } +} diff --git a/src/chocolatey.package.validator/chocolatey.package.validator.csproj b/src/chocolatey.package.validator/chocolatey.package.validator.csproj index 3490ac5..21c9bd2 100644 --- a/src/chocolatey.package.validator/chocolatey.package.validator.csproj +++ b/src/chocolatey.package.validator/chocolatey.package.validator.csproj @@ -83,6 +83,11 @@ + + + False + ..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll + ..\packages\Rx-Core.2.2.5\lib\net40\System.Reactive.Core.dll diff --git a/src/chocolatey.package.validator/infrastructure.app/rules/InstallScriptsShouldntUseCreateShortcutNote.cs b/src/chocolatey.package.validator/infrastructure.app/rules/InstallScriptsShouldntUseCreateShortcutNote.cs index 91ad490..dc43120 100644 --- a/src/chocolatey.package.validator/infrastructure.app/rules/InstallScriptsShouldntUseCreateShortcutNote.cs +++ b/src/chocolatey.package.validator/infrastructure.app/rules/InstallScriptsShouldntUseCreateShortcutNote.cs @@ -15,13 +15,16 @@ namespace chocolatey.package.validator.infrastructure.app.rules { + using System.Collections.ObjectModel; using System.IO; + using System.Linq; + using System.Management.Automation; using infrastructure.rules; using NuGet; public class InstallScriptsShouldntUseCreateShortcutNote : BasePackageRule { - public override string ValidationFailureMessage { get { return + public override string ValidationFailureMessage { get { return @"Installation Scripts are using .CreateShortcut. The reviewer will ensure that there is a valid reason for not using a built-in Chocolatey Helper for creating shortcuts. [More...](https://github.com/chocolatey/package-validator/wiki/UsageOfCreateShortcut)"; } } public override PackageValidationOutput is_valid(IPackage package) @@ -35,7 +38,14 @@ public override PackageValidationOutput is_valid(IPackage package) var contents = file.GetStream().ReadToEnd().to_lower(); - if (contents.Contains(".createshortcut")) valid = false; + Collection errors = null; + var tokens = PSParser.Tokenize(contents, out errors); + + var requiredCalls = tokens.Where(p => p.Type != PSTokenType.Comment && + p.Content.to_lower().Contains("createshortcut") + ); + + if (requiredCalls.Any()) valid = false; } return valid;