Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

(GH-110) Use PSParser.Tokenize #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<Compile Include="infrastructure.app\DependencyWithNoVersionGuidelineSpecs.cs" />
<Compile Include="infrastructure.app\IncludesChocolateyDependencyNoteSpecs.cs" />
<Compile Include="infrastructure.app\PackageIdUsesDotsNoteSpecs.cs" />
<Compile Include="infrastructure.app\InstallScriptsShouldntUseCreateShortcutNoteSpecs.cs" />
<Compile Include="infrastructure.app\PackageIdUsesUnderscoresNoteSpecs.cs" />
<Compile Include="infrastructure.app\PrereleaseVersionAsPartOfPackageIdRequirementSpecs.cs" />
<Compile Include="infrastructure.app\PackageIdTooLongWithNoDashesNoteSpecs.cs" />
Expand Down
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
Expand Up @@ -83,6 +83,11 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll</HintPath>
Copy link
Member

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.

</Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.Reactive.Core">
<HintPath>..\packages\Rx-Core.2.2.5\lib\net40\System.Reactive.Core.dll</HintPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I would call it requiredCalls.

Copy link
Member Author

Choose a reason for hiding this comment

The 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, requiredCalls doesn't make sense. Perhaps prohibitedCalls. I have no imagination when it comes to naming 😄

);

if (requiredCalls.Any()) valid = false;
}

return valid;
Expand Down