-
Notifications
You must be signed in to change notification settings - Fork 29
(GH-110) Use PSParser.Tokenize #128
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IPackage> package = new Mock<IPackage>(); | ||
protected Mock<IPackageFile> packageFile = new Mock<IPackageFile>(); | ||
|
||
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<IPackageFile>() { 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<IPackageFile>() { 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(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<PSParseError> errors = null; | ||
var tokens = PSParser.Tokenize(contents, out errors); | ||
|
||
var requiredCalls = tokens.Where(p => p.Type != PSTokenType.Comment && | ||
p.Content.to_lower().Contains("createshortcut") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I would call it requiredCalls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ferventcoder I just copied what you had here: #110 (comment) I didn't give it much thought in the first pass 😄 Now that I look at it again, in our case, we are looking for methods that shouldn't be called, so yes, |
||
); | ||
|
||
if (requiredCalls.Any()) valid = false; | ||
} | ||
|
||
return valid; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad hint path here. Reference the shell of SMA in lib.