Skip to content

Commit

Permalink
Make code follow PSScriptAnalyzer rules (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmeister authored and nohwnd committed Dec 14, 2018
1 parent a05748b commit 08f7271
Show file tree
Hide file tree
Showing 76 changed files with 523 additions and 523 deletions.
10 changes: 5 additions & 5 deletions Assert.psm1
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Import-Module $PSScriptRoot/TypeClass/src/TypeClass.psm1 -DisableNameChecking
Import-Module $PSScriptRoot/TypeClass/src/TypeClass.psm1 -DisableNameChecking
Import-Module $PSScriptRoot/Format/src/Format.psm1 -DisableNameChecking

. $PSScriptRoot/Compatibility/src/Compatibility.ps1

Get-ChildItem -Path $PSScriptRoot/src/ -Recurse -Filter *.ps1 |
foreach { . $_.FullName }
Get-ChildItem -Path $PSScriptRoot/src/ -Recurse -Filter *.ps1 |
ForEach-Object { . $_.FullName }

Export-ModuleMember -Function @(
'Get-EquivalencyOption'
'Assert-Equivalent'
'Assert-Equivalent'
'Assert-Equal' # ?= ' ?eq
'Assert-NotEqual' # ?!= ?ne
'Assert-Same' # ?=== ?ref ?same
'Assert-Same' # ?=== ?ref ?same
'Assert-NotSame' # ?!=== ?notref ?notsame
'Assert-Null' # ?0 ?null
'Assert-NotNull' # ?!0 ?notnull
Expand Down
4 changes: 2 additions & 2 deletions Axiom/src/Verify-AssertionFailed.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-AssertionFailed {
function Verify-AssertionFailed {
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ScriptBlock]$ScriptBlock
Expand All @@ -13,7 +13,7 @@ function Verify-AssertionFailed {
$assertionExceptionThrown = $true
$_
}

if (-not $assertionExceptionThrown) {
throw [Exception]"An exception of type Assertions.AssertionException was expected but no exception was thrown!"
}
Expand Down
6 changes: 3 additions & 3 deletions Axiom/src/Verify-Equal.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-Equal {
function Verify-Equal {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,
Expand All @@ -9,12 +9,12 @@ function Verify-Equal {
if ($Expected -ne $Actual) {
$message = "Expected and actual values differ!`n"+
"Expected: '$Expected'`n"+
"Actual : '$Actual'"
"Actual : '$Actual'"
if ($Expected -is [string] -and $Actual -is [string]) {
$message += "`nExpected length: $($Expected.Length)`nActual length: $($Actual.Length)"
}
throw [Exception]$message
}

$Actual
}
6 changes: 3 additions & 3 deletions Axiom/src/Verify-Like.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-Like {
function Verify-Like {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,
Expand All @@ -9,10 +9,10 @@ function Verify-Like {
if ($Actual -notlike $Expected) {
$message = "Expected is not present in Actual!`n"+
"Expected: '$Expected'`n"+
"Actual : '$Actual'"
"Actual : '$Actual'"

throw [Exception]$message
}

$Actual
}
4 changes: 2 additions & 2 deletions Axiom/src/Verify-NotNull.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-NotNull {
function Verify-NotNull {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual
Expand All @@ -7,6 +7,6 @@ function Verify-NotNull {
if ($null -eq $Actual) {
throw [Exception]"Expected not `$null but got `$null."
}

$Actual
}
4 changes: 2 additions & 2 deletions Axiom/src/Verify-NotSame.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-NotSame {
function Verify-NotSame {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,
Expand All @@ -9,6 +9,6 @@ function Verify-NotSame {
if ([object]::ReferenceEquals($Expected, $Actual)) {
throw [Exception]"Expected the objects to be different instance but they were the same instance."
}

$Actual
}
4 changes: 2 additions & 2 deletions Axiom/src/Verify-Null.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-Null {
function Verify-Null {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual
Expand All @@ -7,6 +7,6 @@ function Verify-Null {
if ($null -ne $Actual) {
throw [Exception]"Expected `$null but got '$Actual'."
}

$Actual
}
4 changes: 2 additions & 2 deletions Axiom/src/Verify-Same.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-Same {
function Verify-Same {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,
Expand All @@ -9,6 +9,6 @@ function Verify-Same {
if (-not [object]::ReferenceEquals($Expected, $Actual)) {
throw [Exception]"Expected the objects to be the same instance but they were not."
}

$Actual
}
4 changes: 2 additions & 2 deletions Axiom/src/Verify-Throw.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-Throw
function Verify-Throw
{
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
Expand All @@ -14,7 +14,7 @@ function Verify-Throw
$exceptionThrown = $true
$_
}

if (-not $exceptionThrown) {
throw [Exception]"An exception was expected, but no exception was thrown!"
}
Expand Down
6 changes: 3 additions & 3 deletions Axiom/src/Verify-Type.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Verify-Type {
function Verify-Type {
param (
[Parameter(ValueFromPipeline=$true)]
$Actual,
Expand All @@ -9,9 +9,9 @@ function Verify-Type {
if ($Actual -isnot $Expected) {
$message = "Expected value to be of type $($Expected.FullName)`n"+
"Expected: '$($Expected.FullName)'`n"+
"Actual : '$($Actual.GetType().FullName)'"
"Actual : '$($Actual.GetType().FullName)'"
throw [Exception]$message
}

$Actual
}
24 changes: 12 additions & 12 deletions Compatibility/src/Compatibility.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
function New-PSObject ([hashtable]$Property) {
function New-PSObject ([hashtable]$Property) {
New-Object -Type PSObject -Property $Property
}

function Invoke-WithContext {
param(
[Parameter(Mandatory = $true )]
[ScriptBlock] $ScriptBlock,
[ScriptBlock] $ScriptBlock,
[Parameter(Mandatory = $true)]
[hashtable] $Variables)
# this functions is a psv2 compatible version of
# ScriptBlock InvokeWithContext that is not available

# this functions is a psv2 compatible version of
# ScriptBlock InvokeWithContext that is not available
# in that version of PowerShell

# this is what the code below does
# which in effect sets the context without detaching the
# scriptblock from the original scope
# & {
# # context
# $a = 10
# # context
# $a = 10
# $b = 20
# # invoking our original scriptblock
# & $sb
# }

# a similar solution was $SessionState.PSVariable.Set('a', 10)
# but that sets the variable for all "scopes" in the current
# but that sets the variable for all "scopes" in the current
# scope so the value persist after the original has run which
# is not correct,
# is not correct,

$scriptBlockWithContext = {
param($context)

foreach ($pair in $context.Variables.GetEnumerator()) {
foreach ($pair in $context.Variables.GetEnumerator()) {
New-Variable -Name $pair.Key -Value $pair.Value
}

# this cleans up the variable from the session
# the subexpression outputs the value of the variable
# and then deletes the variable, so the value is still passed
Expand Down Expand Up @@ -64,7 +64,7 @@ function Get-Type ($InputObject) {
try {
$ErrorActionPreference = 'Stop'
# normally this would not ever throw
# but in psv2 when datatable is deserialized then
# but in psv2 when datatable is deserialized then
# [Deserialized.System.Data.DataTable] does not contain
# .GetType()
$InputObject.GetType()
Expand Down
34 changes: 17 additions & 17 deletions Compatibility/tst/Compatibility.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
$here = $MyInvocation.MyCommand.Path | Split-Path
$here = $MyInvocation.MyCommand.Path | Split-Path
Import-Module -Force $here/../../Axiom/src/Axiom.psm1 -DisableNameChecking
. $here/../src/Compatibility.ps1

Describe "New-PSObject" {
Describe "New-PSObject" {
It "Creates a new object of type PSCustomObject" {
$hashtable = @{
$hashtable = @{
Name = 'Jakub'
}

$object = New-PSObject $hashtable
$object = New-PSObject $hashtable
$object | Verify-Type ([PSCustomObject])
}

It "Creates a new PSObject with the properties populated" {
$hashtable = @{
$hashtable = @{
Name = 'Jakub'
}

$object = New-PSObject $hashtable
$object = New-PSObject $hashtable
$object.Name | Verify-Equal $hashtable.Name
}
}
Expand Down Expand Up @@ -47,50 +47,50 @@ Describe "Test-NullOrWhiteSpace" {
}

Describe "Invoke-WithContext" {
BeforeAll {
Get-Module "Test-Module" | Remove-Module
BeforeAll {
Get-Module "Test-Module" | Remove-Module
$body = {
$a = "in test module"
$context = "context"

# all of these are functions returning scriptblocks
# so we can test that they remain bounded to the state of
# so we can test that they remain bounded to the state of
# this module
function sb1 { { "-$a-" } }
function sb2 { { "-$a- -$b-" } }
function sb3 { { "-$context- -$a-" } }
}
New-Module -Name "Test-Module" -ScriptBlock $body | Import-Module
New-Module -Name "Test-Module" -ScriptBlock $body | Import-Module
}

AfterAll {
Get-Module "Test-Module" | Remove-Module
AfterAll {
Get-Module "Test-Module" | Remove-Module
}

It "Keeps the scriptblock attached to the original scope" {
# we define variable $a here and in the module, and we must
# we define variable $a here and in the module, and we must
# resolve $a to the value in the module, not to the local value
# or null
$a = 100
Invoke-WithContext -ScriptBlock (sb1) -Variables @{} |
Invoke-WithContext -ScriptBlock (sb1) -Variables @{} |
Verify-Equal "-in test module-"
}

It "Injects variable `$b into the scope while keeping `$a attached to the module scope" {
Invoke-WithContext -ScriptBlock (sb2) -Variables @{ b = 'injected' } |
Invoke-WithContext -ScriptBlock (sb2) -Variables @{ b = 'injected' } |
Verify-Equal "-in test module- -injected-"
}

It "Does not conflict with `$Context variable that is used internally" {
# internally we wrap the call in something like
# & {
# param($context)
# & $context.ScriptBlock
# & $context.ScriptBlock
# }
# and we need to make sure that the `$context variable
# will not be seen by the original scriptblock to avoid
# naming conflicts
Invoke-WithContext -ScriptBlock (sb3) -Variables @{} |
Invoke-WithContext -ScriptBlock (sb3) -Variables @{} |
Verify-Equal "-context- -in test module-"
}
}
Loading

0 comments on commit 08f7271

Please sign in to comment.