Skip to content
Merged
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
35 changes: 27 additions & 8 deletions FeatureFlags.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,41 @@ Write-Verbose "Found JSON.Net assembly at $jsonLibPath"
try {
$jsonType = Add-Type -Path $jsonLibPath -PassThru
$jsonSchemaType = Add-Type -Path $schemaLibPath -PassThru
Write-Verbose "JSON.Net type: $jsonType"
Write-Verbose "NjsonSchema type: $jsonSchemaType"
#Write-Verbose "JSON.Net type: $jsonType"
#Write-Verbose "NjsonSchema type: $jsonSchemaType"
} catch {
Write-Error "Error loading JSON libraries ($jsonLibPath, $schemaLibPath): $($_.Exception.Message)"
throw
}

$script:schema = $null
try {
Write-Verbose "Reading JSON schema..."
$script:schemaPath = Get-Content $PSScriptRoot\featureflags.schema.json
$script:schema = [NJsonSchema.JSonSchema4]::FromJsonAsync($script:schemaPath).GetAwaiter().GetResult()
Write-Debug "Loaded JSON schema from featureflags.schema.json."
} catch {
Write-Error "Error loading JSON schema"
Write-Host $_.Exception.Message
Write-Error "Error reading JSON schema: $($_.Exception.Message)"
throw
}

try {
Write-Verbose "Loading JSON schema..."
$script:schema = [NJsonSchema.JSonSchema]::FromJsonAsync($script:schemaPath).GetAwaiter().GetResult()
} catch {
$firstException = $_.Exception
# As a fallback, try reading using the JsonSchema4 object. The JSON schema library
# exposes that object to .NET Framework instead of JsonSchema for some reason.
try {
Write-Verbose "Loading JSON schema (fallback)..."
$script:schema = [NJsonSchema.JSonSchema4]::FromJsonAsync($script:schemaPath).GetAwaiter().GetResult()
} catch {
Write-Error "Error loading JSON schema: $($_.Exception.Message). First error: $($firstException.Message)."
Write-Host $_.Exception.Message
throw
}
}
Write-Verbose "Loaded JSON schema from featureflags.schema.json."
Write-Verbose $script:schema

<#
.SYNOPSIS
Validates feature flag configuration.
Expand Down Expand Up @@ -225,9 +244,9 @@ function Test-FeatureConditions
$probability = $condition.probability
$random = (Get-Random) % 100 / 100.0
Write-Verbose "random: ${random}. Checking against ${probability}"
if($random -gt $condition.probability)
if($random -ge $condition.probability)
{
Write-Verbose "Probability condition not met: ${random} > ${probability}"
Write-Verbose "Probability condition not met: ${random} ${probability}"
return $false
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
linux:
imageName: 'ubuntu-16.04'
mac:
imageName: 'macos-10.13'
imageName: 'macos-10.15'
windows:
imageName: 'vs2017-win2016'

Expand Down
12 changes: 10 additions & 2 deletions test/FeatureFlags.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
https://github.com/pester/Pester/wiki/Installation-and-Update.
After updating, run the Invoke-Pester cmdlet from the project directory.
#>

$ModuleName = "FeatureFlags"
Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force
Get-Module $ModuleName | Remove-Module -Force
Import-Module $PSScriptRoot\test-functions.psm1

$VerbosePreference = "Continue"
$Module = Import-Module $PSScriptRoot\..\${ModuleName}.psd1 -Force -PassThru
if ($null -eq $Module) {
Write-Error "Could not import $ModuleName"
exit 1
}
Write-Host "Done."
Write-Host $Module.ExportedCommands.Values.Name

Describe 'Confirm-FeatureFlagConfig' {
Context 'Validation of invalid configuration' {
It 'Fails on empty or null configuration' {
Expand Down
9 changes: 8 additions & 1 deletion tools/run-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
$parentDir = Split-Path -Parent (Split-Path -Parent $PSCommandPath)
$testDir = Join-Path $parentDir -ChildPath "test"

# Debug info.
$PSVersionTable | Out-String

Write-Host "Checking for Pester > 4.0.0..."
$pester = Get-Module -ListAvailable | Where-Object {$_.Name -eq "Pester" -and $_.Version -gt '4.0.0'}
if ($pester.Count -eq 0) {
Write-Host "Cannot find the Pester module. Installing it."
Install-Module Pester -Force -Scope CurrentUser
Install-Module Pester -Force -Scope CurrentUser -RequiredVersion 4.10.1
} else {
Write-Host "Found Pester version $($pester.Version)."
}

$FailedTests = Invoke-Pester $testDir -EnableExit -OutputFile "test/results.xml" -OutputFormat "NUnitXML" -CodeCoverage "$parentDir/FeatureFlags.psm1"
if ($FailedTests -gt 0) {
Write-Error "Error: $FailedTests Pester tests failed."
Expand Down