Public/Tests/New-TemporaryDirectory.Tests.ps1
|
BeforeAll { $TestPath = Split-Path -Parent -Path $PSScriptRoot $FunctionFileName = (Split-Path -Leaf $PSCommandPath ) -replace '\.Tests\.', '.' # You can use this Variable to call your function via it's name or ignore/remove as required $FunctionName = $FunctionFileName.Replace('.ps1', '') . $(Join-Path -Path $TestPath -ChildPath $FunctionFileName) } Describe -Name "Performing basic validation test on function $FunctionFileName" { BeforeAll { $script:createdDirs = @() } It "Should create a directory" { $result = New-TemporaryDirectory $script:createdDirs += $result.FullName Test-Path $result.FullName | Should -BeTrue } It "Should use Prefix in directory name" { $result = New-TemporaryDirectory -Prefix "testprefix" $script:createdDirs += $result.FullName $result.Name | Should -BeLike "testprefix_*" } It "Should return DirectoryInfo object" { $result = New-TemporaryDirectory $script:createdDirs += $result.FullName $result | Should -BeOfType [System.IO.DirectoryInfo] } AfterAll { foreach ($dir in $script:createdDirs) { if (Test-Path $dir) { Remove-Item $dir -Recurse -Force } } } } Describe -Tags 'PSSA' -Name 'Testing against PSScriptAnalyzer rules' { BeforeAll { $ScriptAnalyzerSettings = Get-Content -Path (Join-Path -Path (Get-Location) -ChildPath 'PSScriptAnalyzerSettings.psd1') | Out-String | Invoke-Expression $AnalyzerIssues = Invoke-ScriptAnalyzer -Path "$TestPath\$FunctionFileName" -Settings $ScriptAnalyzerSettings $ScriptAnalyzerRuleNames = Get-ScriptAnalyzerRule | Select-Object -ExpandProperty RuleName } foreach ($Rule in $ScriptAnalyzerRuleNames) { if ($ScriptAnalyzerSettings.excluderules -notcontains $Rule) { It "Function $FunctionFileName should pass $Rule" { $Failures = $AnalyzerIssues | Where-Object -Property RuleName -EQ -Value $rule ($Failures | Measure-Object).Count | Should -Be 0 } } else { # We still want it in the tests, but since it doesn't actually get tested we will skip It "Function $FunctionFileName should pass $Rule" -Skip { $Failures = $AnalyzerIssues | Where-Object -Property RuleName -EQ -Value $rule ($Failures | Measure-Object).Count | Should -Be 0 } } } } |