Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
37a244c
Adding .Link
StartAutomating Jul 1, 2020
464d77f
Adding [OutputType()]
StartAutomating Jul 1, 2020
c818c27
Adding OutputType
StartAutomating Jul 1, 2020
bd638d2
Adding OutputType, re-encoding as UTF8.
StartAutomating Jul 1, 2020
d078150
ScriptCop Fixes and Re-encodings.
StartAutomating Jul 1, 2020
1fc3129
Updating Rules and re-encoding
StartAutomating Jul 1, 2020
b3f085e
Updating Azure Pipeline
StartAutomating Jul 1, 2020
c27f869
New Rule: Test for Write-Progress without an -ID.
StartAutomating Jul 1, 2020
587dbbb
Adding Enable/Disable-CommandCoverage
StartAutomating Jul 1, 2020
7d0a925
Re-encoding and adding support for CommandCoverage.
StartAutomating Jul 1, 2020
adafbe8
Re-encoding and adding support for [Diagnostics.CodeAnalysis.Suppress…
StartAutomating Jul 1, 2020
dbe0839
Re-encoding, removing Show-ScriptCop, adding Enable/Disable-CommandCo…
StartAutomating Jul 1, 2020
a20315c
Re-encoding.
StartAutomating Jul 1, 2020
f792b9d
Adding Get-CommandCoverage.
StartAutomating Jul 1, 2020
d6a6e19
Cleaning up Whitespace
StartAutomating Jul 1, 2020
5e5d3b2
Removing Save-Function.
StartAutomating Jul 1, 2020
ab372f6
Removing Save-Function.
StartAutomating Jul 1, 2020
9ee7cfd
Removing Get-ScriptToken
StartAutomating Jul 1, 2020
2e5e123
Reencoding and trimming whitespace.
StartAutomating Jul 1, 2020
ef2bd8d
Crossplatform fixes. Removing vestigial code.
StartAutomating Jul 1, 2020
877d3d3
Removing trailing whitespace, Win8 and Pubcenter sections.
StartAutomating Jul 1, 2020
19549e7
Fixing Pathing
StartAutomating Jul 1, 2020
f8cacbc
Re-encoding
StartAutomating Jul 1, 2020
1e742c0
Re-encoding and trimming whitespace.
StartAutomating Jul 1, 2020
b0b216a
ScriptAnalyzer Fixes
StartAutomating Jul 1, 2020
71173d9
ScriptAnalyzer Fixes
StartAutomating Jul 1, 2020
9693744
Adding LICENSE.
StartAutomating Jul 1, 2020
4152d9a
Trimming Whitespace
StartAutomating Jul 1, 2020
3c4cef0
ScriptAnalyzer Fixes
StartAutomating Jul 1, 2020
5e27c84
Adding Pester Tests (which call ScriptCop tests)
StartAutomating Jul 1, 2020
51678b9
ScriptAnalyzer fixes.
StartAutomating Jul 1, 2020
0b6caf2
Fixing module reload.
StartAutomating Jul 1, 2020
3a5da52
ScriptAnalyzer Fixes
StartAutomating Jul 1, 2020
4a4a1f8
ScriptAnalyzer Fixes
StartAutomating Jul 1, 2020
13208bb
Fixing module load. ScriptAnalyzer fixes.
StartAutomating Jul 1, 2020
7b16099
ScriptAnalyzer fixes
StartAutomating Jul 1, 2020
409ae27
Skipping Reload
StartAutomating Jul 1, 2020
be16d5d
Crossplatform fix in finding .ScriptCop.psd1
StartAutomating Jul 1, 2020
8cda933
Reloading before running Test-Module.
StartAutomating Jul 1, 2020
5a7a25e
Dotting into Values instead of using Select-Object (this will not wor…
StartAutomating Jul 1, 2020
63dd604
Explicitly using Script Scope
StartAutomating Jul 1, 2020
75939c2
Cleaning up Variables (for debugging)
StartAutomating Jul 1, 2020
ceed8c1
Updating Module Version [1.6]. ScriptAnalyzer fixes.
StartAutomating Jul 1, 2020
784dc0f
Merge branch 'master' into ScriptCopEnhancements
StartAutomating Jul 1, 2020
d02aee7
Adding files squashed by merge.
StartAutomating Jul 1, 2020
ea80ebd
Adding files squashed by merge.
StartAutomating Jul 1, 2020
8af88c1
Adding ScriptCop exclusion for Disable-CommandCoverage.
StartAutomating Jul 1, 2020
5ba1266
Updating formatting source
StartAutomating Jul 1, 2020
a16aa0c
Updating format file
StartAutomating Jul 1, 2020
8d8afad
Adding -GetCommandCoverage.
StartAutomating Jul 1, 2020
e7e2f56
Fixing divide by zero when a command has no parameters.
StartAutomating Jul 1, 2020
7d90182
Adding Space in Test Output
StartAutomating Jul 1, 2020
e6cfcba
Adding Space in Test Output
StartAutomating Jul 1, 2020
8544f96
Updating formatting (avoiding using $_)
StartAutomating Jul 2, 2020
98250dc
Removing unused Variable Rule exclusion.
StartAutomating Jul 2, 2020
d2e13e9
Not flagging variables that are global: or script: scoped.
StartAutomating Jul 2, 2020
0a51ae4
Adding Sample Output. Updating About Topic and README. Re-ordering …
StartAutomating Jul 2, 2020
28ec894
Fixing Extension casing.
StartAutomating Jul 2, 2020
ae024c7
Adding Gallery Publish Stage.
StartAutomating Jul 2, 2020
2b35e5b
Updating module description.
StartAutomating Jul 2, 2020
1b45275
Suppressing ScriptAnalyzer False Positives
StartAutomating Jul 2, 2020
c64cfb4
Updating rationale for skipping Test-ForSlowScript.
StartAutomating Jul 2, 2020
4936c14
Fixing collection enumeration/modification issue.
StartAutomating Jul 2, 2020
280ef1b
Adding tests for Unregister-
StartAutomating Jul 2, 2020
a2fa2f8
Fixing issue when no files exist to compare.
StartAutomating Jul 2, 2020
f5da706
Adding tests for Repair-Command.
StartAutomating Jul 2, 2020
dfd9e08
Adding Module Tags, ProjectURI, and LicenseURI.
StartAutomating Jul 2, 2020
92e4115
Adding tests for -ExcludeRule and -Patrol.
StartAutomating Jul 2, 2020
6d29473
Updating Tests for Test-Command -Patrol.
StartAutomating Jul 2, 2020
cbc1669
Adding fault tolerance tests for Register-ScriptCopRule and Register-…
StartAutomating Jul 2, 2020
919d864
Additional Module Tags.
StartAutomating Jul 2, 2020
4bd8214
Additional Module Tags.
StartAutomating Jul 2, 2020
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Assets/ScriptCop_SampleOutput.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Disable-CommandCoverage.ps1
Binary file not shown.
Binary file modified Enable-CommandCoverage.ps1
Binary file not shown.
57 changes: 57 additions & 0 deletions Formatting/ScriptCop.Test.Output.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[OutputType('ScriptCop.Test.Output')]
param()
$testOutput = $_
$in = $_
$writeColor =
if ($testOutput.Passed) {
if (-not $host.UI.SupportsVirtualTerminal) { "DarkGreen" } else { "Success" }
} else {
if (-not $host.UI.SupportsVirtualTerminal) { "Red" } else { "Error" }
}
$testStatus =
if ($testOutput.Passed) {
"+"
} else {
""
}

@(if (-not ($Request -and $response)) {

$msg = " [$testStatus] $($testOutput.TestCase)"
if (-not $host.UI.SupportsVirtualTerminal) {
Write-Host $msg -ForegroundColor $writeColor

if ($test.Errors) {
Write-Host "$($testOutput.Errors |Out-String)" -ForegroundColor $writeColor
}
''

} else {
. $SetOutputStyle -foregroundColor $writeColor
$msg
#[Environment]::NewLine
if ($testOutput.Errors) {

$testOutput.Errors | Out-String -Width $host.UI.RawUI.BufferSize.Width
}
. $clearOutputStyle
}
} else {
"
<div style='float:left;width:80%;font-size:1.11em'>
<h4>
$($testOutput.TestCase)
</h4>
$(if ($testOutput.Errors) {
"<pre>
$($testOutput.Errors | Out-String)
</pre>"
})
</div>
<div style='float:right;width:20%;font-size:1.11em;text-align:right'>
<span style='font-size:1.11em'>$testStatus</span>
</div>
<br style='clear:both' />
"
}) -join ''

53 changes: 53 additions & 0 deletions Formatting/ScriptCop.Test.Pass.Output.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[OutputType('ScriptCop.Test.Pass.Output')]
param()

$testPassOutput = $_

$writeColor =
if ($testPassOutput.Passed) {
if (-not $host.UI.SupportsVirtualTerminal) { "DarkGreen" } else { "Success" }
} else {
if (-not $host.UI.SupportsVirtualTerminal) { "Red" } else { "Error" }
}
$testStatus =
if ($testPassOutput.Passed) {
"+"
} else {
"-"
}



@(if (-not ($request -and $response)) {
$msg = " [$testStatus] $($testPassOutput.TestPass)"
if (-not $host.UI.SupportsVirtualTerminal) {
Write-Host " "
Write-Host "$Msg" -ForegroundColor $writeColor -NoNewline
Write-Host " "
$null= ($testPassOutput.Results | Out-String)
''
} else {
[Environment]::NewLine
. $setOutputStyle -foregroundColor $writeColor
"$Msg"
. $clearOutputStyle
[Environment]::NewLine
$($testPassOutput.Results | Out-String -Width $host.UI.RawUI.BufferSize.Width).Trim()
}
} else {
"<div style='background-color:$writeColor;color:#ffffff'>
<div style='float:left;width:20%;font-size:1.22em'>
<h3>
$($_.TestPass)
</h3>
</div>
<div style='float:right;width:20%;text-align:right;'>
<span style='color:#ffffff;font-size:1.22em'>$testStatus</span>
</div>
<br style='clear:both'/>
<div style='width:80%;margin-left:20%'>
$($_.Results | Out-html)
</div>
</div>
"
}) -join ''
Binary file modified Get-CommandCoverage.ps1
Binary file not shown.
53 changes: 28 additions & 25 deletions Get-FunctionFromScript.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function Get-FunctionFromScript {
<#
.Synopsis
.Synopsis
Gets the functions declared within a script block or a file
.Description
Gets the functions exactly as they are written within a script or file
Expand All @@ -13,57 +13,60 @@
"bar"
}
}
.Link
Test-Command
#>
[CmdletBinding(DefaultParameterSetName='File')]
[OutputType([ScriptBlock])]
param(
# The script block containing functions
[Parameter(Mandatory=$true,
ParameterSetName="ScriptBlock",
ValueFromPipelineByPropertyName=$true)]
ValueFromPipelineByPropertyName=$true)]
[ScriptBlock]
$ScriptBlock,

# A file containing functions
[Parameter(Mandatory=$true,
ParameterSetName="File",
ValueFromPipelineByPropertyName=$true)]
ValueFromPipelineByPropertyName=$true)]
[Alias('FullName')]
[String]
$File,

# If set, outputs the command metadatas
[switch]
$OutputMetaData
)

process {
if ($psCmdlet.ParameterSetName -eq "File") {
$realFile = Get-Item $File
if (-not $realFile) {
$realFile = Get-Item -LiteralPath $File -ErrorAction SilentlyContinue
if (-not $realFile) {
if (-not $realFile) {
return
}
}
$text = [IO.File]::ReadAllText($realFile.Fullname)
$scriptBlock = [ScriptBlock]::Create($text)
if ($scriptBlock) {
$functionsInScript =
Get-FunctionFromScript -ScriptBlock $scriptBlock -OutputMetaData:$OutputMetaData
if ($OutputMetaData)
$functionsInScript =
Get-FunctionFromScript -ScriptBlock $scriptBlock -OutputMetaData:$OutputMetaData
if ($OutputMetaData)
{
$functionsInScript |
$functionsInScript |
Add-Member NoteProperty File $realFile.FullName -PassThru
}
}
} elseif ($psCmdlet.ParameterSetName -eq "ScriptBlock") {
}
} elseif ($psCmdlet.ParameterSetName -eq "ScriptBlock") {
$text = $scriptBlock.ToString()
$tokens = [Management.Automation.PSParser]::Tokenize($scriptBlock, [ref]$null)
$tokens = [Management.Automation.PSParser]::Tokenize($scriptBlock, [ref]$null)
for ($i = 0; $i -lt $tokens.Count; $i++) {
if ($tokens[$i].Content -eq "function" -and
$tokens[$i].Type -eq "Keyword") {




$groupDepth = 0
Expand All @@ -76,39 +79,39 @@

$startI++


$ii = $i
$done = $false
while (-not $done) {
while ($tokens[$ii] -and $tokens[$ii].Type -ne 'GroupStart') { $ii++ }
$groupDepth++
while ($groupDepth -and $tokens[$ii]) {
$ii++
if ($tokens[$ii].Type -eq 'GroupStart') { $groupDepth++ }
if ($tokens[$ii].Type -eq 'GroupStart') { $groupDepth++ }
if ($tokens[$ii].Type -eq 'GroupEnd') { $groupDepth-- }
}
if (-not $tokens[$ii]) { break }
if ($tokens[$ii].Content -eq "}") {
if (-not $tokens[$ii]) { break }
if ($tokens[$ii].Content -eq "}") {
$done = $true
}
}
if (-not $tokens[$ii] -or
if (-not $tokens[$ii] -or
($tokens[$ii].Start + $tokens[$ii].Length) -ge $Text.Length) {
$chunk = $text.Substring($tokens[$startI].Start)
} else {
$chunk = $text.Substring($tokens[$startI].Start,
$chunk = $text.Substring($tokens[$startI].Start,
$tokens[$ii].Start + $tokens[$ii].Length - $tokens[$startI].Start)
}
}
if ($OutputMetaData) {
New-Object PSObject -Property @{
Name = $functionName
Definition = [ScriptBlock]::Create($chunk)
}
}
} else {
[ScriptBlock]::Create($chunk)
}
}
}
}
}
}
}
}
25 changes: 13 additions & 12 deletions Get-ScriptCopFixer.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Get-ScriptCopFixer
function Get-ScriptCopFixer
{
<#
.Synopsis
Expand All @@ -10,37 +10,38 @@ function Get-ScriptCopFixer
.Link
Repair-Command
#>
[OutputType('ScriptCopRule')]
param()

begin {
# Declare the cache if fixers, if it doesn't exist yet.
if (-not $script:ScriptCopFixers) {
$script:ScriptCopFixers = New-Object Collections.ArrayList
}
}

process {
#region Convert Fixers from memory represention to psuedo object
foreach ($fixer in @($script:ScriptCopFixers)) {
foreach ($fixer in @($script:ScriptCopFixers)) {
if ($fixer) {
# Use Select-Object to turn it into the property bafe we want, and then add a typename
# Use Select-Object to turn it into the property bafe we want, and then add a typename
$fixer |
Select-Object Name, @{
Label='File'
Expression={
if ($_.Path){ $_.Path.Replace("$psScriptRoot\", "") } else { $_.ScriptBlock.File.Replace("$psScriptRoot\", "") }
if ($_.Path){ $_.Path.Replace("$psScriptRoot\", "") } else { $_.ScriptBlock.File.Replace("$psScriptRoot\", "") }

}
} |
} |
ForEach-Object {
$_.psObject.typenames.clear()
$null = $_.psObject.typenames.Add('ScriptcopRule')
$_
}
}
}
}
}
#endregion
}
}
}


Binary file modified Get-ScriptCopPatrol.ps1
Binary file not shown.
Binary file modified Get-ScriptCopRule.ps1
Binary file not shown.
Binary file removed Get-ScriptToken.ps1
Binary file not shown.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 James Brundage

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

ScriptCop [1.6]
===============

A PowerShell Static Analysis Tool to help your scripts follow the rules.
---------------

### Commands
-----------------------------
| Verb|Noun |
|---------:|:---------------|
| Disable|-CommandCoverage|
| Enable|-CommandCoverage|
| Get|-CommandCoverage|
| |-ScriptCopFixer |
| |-ScriptCopPatrol|
| |-ScriptCopRule |
| Register|-ScriptCopFixer |
| |-ScriptCopPatrol|
| |-ScriptCopRule |
| Repair|-Command |
| Show|-ScriptCoverage |
| Test|-Command |
| |-Module |
|Unregister|-ScriptCopFixer |
| |-ScriptCopPatrol|
| |-ScriptCopRule |
-----------------------------
ScriptCop is a static analysis and testing tool for PowerShell. It helps make sure your scripts follow the rules.


### Using ScriptCop for Static Analysis

You can run pipe in a command or a module to perform static analysis
~~~Powershell
function foo() {

}

Get-Command foo | Test-Command
~~~

![What's Wrong with Foo](Assets/ScriptCop_SampleOutput.png)



When using ScriptCop analyzes your scripts, it it doesn't run your code, but it examines your code for a set of well-known problems.
You can write custom rules to flag bad scripting style, or you can use the dozens of rules built into ScriptCop.

### Testing with ScriptCop

You can also use ScriptCop to test your modules.
By default, every module command's examples and any demos become test cases for your module.
You can also write your own tests (.test.ps1) files to make sure different parts are working.

You can declare a .ScriptCop.psd1 manifest to group your tests and control what runs by default.

For instance, here is the ScriptCop.ScriptCop.psd1:

~~~Powershell
@{
Demo = "A Quick Intro to ScriptCop"
Coverage = 'ScriptCoverage'
ExampleTests = 'Show-ScriptCoverage:1'
RuleGetters = 'Get-ScriptCopRule:1', 'Get-ScriptCopPatrol:1', 'Get-ScriptCopFixer:1'
ScriptCop = '*'
}
~~~

Each key is the name of a test group.

The group "ScriptCop" is a special group, and it describes the name of static analysis rules you would like to run on each command.




Loading