Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix issue with verification script in katas #328

Merged
merged 2 commits into from
Nov 24, 2019
Merged
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
25 changes: 21 additions & 4 deletions PSKoans/Koans/Katas/ProcessingStrings.Koans.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using module PSKoans
using namespace System.Management.Automation.Language
using namespace System.Collections.Generic
[Koan(Position = 150)]
param()
<#
@@ -51,10 +53,25 @@ Describe "The Stock Challenge" {
#>

$Verification = {
[string[]]$AllowedCommands = @()
[string[]]$AllowedVariables = @("*")
$Function = Get-Item -Path 'Function:Get-GreatestVarianceDate'
$Function.ScriptBlock.CheckRestrictedLanguage($AllowedCommands, $AllowedVariables, $false)
$Functions = [Hashset[string]]::new([StringComparer]::OrdinalIgnoreCase)
$Ast = (Get-Command 'Get-GreatestVarianceDate' -CommandType Function).ScriptBlock.Ast
$Ast.FindAll(
{
param($node)
if ($node -is [CommandAst] -and ($name = $node.GetCommandName()) -and !$Functions.Contains($name)) {
throw 'Usage of external cmdlets and functions is not permitted.'
}

if ($node -is [FunctionDefinitionAst]) {
$Functions.Add($node.Name) > $null
return
}

if ($node.Left -is [VariableExpressionAst] -and $node.Left.VariablePath.DriveName -eq 'Function') {
$Functions.Add($node.Left.VariablePath.UserPath -replace '^function:') > $null
}
}, $true
)
}

function Get-GreatestVarianceDate {
23 changes: 19 additions & 4 deletions PSKoans/Koans/Katas/SortingCharacters.Koans.ps1
Original file line number Diff line number Diff line change
@@ -16,10 +16,25 @@ param()
Describe 'Kata - Sorting Characters' {
BeforeAll {
$Verification = {
[string[]]$AllowedCommands = @()
[string[]]$AllowedVariables = @("*")
$Function = Get-Item -Path 'Function:Get-SortedString'
$Function.ScriptBlock.CheckRestrictedLanguage($AllowedCommands, $AllowedVariables, $false)
$Functions = [Hashset[string]]::new([StringComparer]::OrdinalIgnoreCase)
$Ast = (Get-Command 'Get-GreatestVarianceDate' -CommandType Function).ScriptBlock.Ast
$Ast.FindAll(
{
param($node)
if ($node -is [CommandAst] -and ($name = $node.GetCommandName()) -and !$Functions.Contains($name)) {
throw 'Usage of external cmdlets and functions is not permitted.'
}

if ($node -is [FunctionDefinitionAst]) {
$Functions.Add($node.Name) > $null
return
}

if ($node.Left -is [VariableExpressionAst] -and $node.Left.VariablePath.DriveName -eq 'Function') {
$Functions.Add($node.Left.VariablePath.UserPath -replace '^function:') > $null
}
}, $true
)
}

function Get-SortedString {