Skip to content

Commit daad7ae

Browse files
author
Ewan Noble
authored
Merge pull request #11 from SkillsFundingAgency/fix-missing-values-2
Fix missing values
2 parents 4aa3acd + 99b4467 commit daad7ae

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

tasks/EnvironmentConfiguration/task/ps_modules/Handlers/private/schema/Get-SchemaProperty.ps1

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,47 @@ function Get-SchemaProperty {
2424
if ($PropertyObject.ExtensionData.ContainsKey("environmentVariable")) {
2525

2626
$VariableName = $PropertyObject.ExtensionData.Item("environmentVariable").Value
27+
$TaskVariable = Get-VstsTaskVariable -Name $VariableName
28+
if (![string]::IsNullOrEmpty($TaskVariable)) {
29+
switch ($PSCmdlet.ParameterSetName) {
2730

28-
switch ($PSCmdlet.ParameterSetName) {
31+
'Standard' {
32+
$TaskVariable = "$($TaskVariable)"
33+
break
34+
}
2935

30-
'Standard' {
31-
$TaskVariable = Get-VstsTaskVariable -Name $VariableName
32-
break
33-
}
34-
35-
'AsInt' {
36-
$TaskVariable = Get-VstsTaskVariable -Name $VariableName -AsInt
37-
break
38-
}
36+
'AsInt' {
37+
$TaskVariable = [int]$TaskVariable
38+
break
39+
}
3940

40-
'AsNumber' {
41-
$TaskVariable = [Decimal]::Parse((Get-VstsTaskVariable -Name $VariableName))
42-
break
43-
}
41+
'AsNumber' {
42+
$TaskVariable = [Decimal]::Parse($TaskVariable)
43+
break
44+
}
4445

45-
'AsArray' {
46-
$ArrayString = Get-VstsTaskVariable -Name $VariableName
47-
$TaskVariable = @($ArrayString | ConvertFrom-Json)
48-
break
49-
}
46+
'AsArray' {
47+
$TaskVariable = @($TaskVariable | ConvertFrom-Json)
48+
break
49+
}
5050

51-
'AsBool' {
52-
$TaskVariable = Get-VstsTaskVariable -Name $VariableName -AsBool
53-
break
51+
'AsBool' {
52+
$TaskVariable = $TaskVariable.ToLower() -in '1', 'true'
53+
break
54+
}
5455
}
55-
56+
return $TaskVariable
5657
}
5758
}
5859

59-
if (!$TaskVariable -and $null -ne $PropertyObject.Default) {
60-
Write-Verbose -Message "No environment variable found for [ $VariableName ] and a default value is present in the schema"
60+
if ($null -ne $PropertyObject.Default) {
61+
Write-Verbose -Message "No environment variable found but a default value is present in the schema"
6162
$TaskVariable = $PropertyObject.Default.Value
6263
Write-Verbose -Message "Set default value '$TaskVariable'"
64+
return $TaskVariable
6365
}
6466

65-
if ($null -eq $TaskVariable) {
66-
throw "No environment variable found and no default value set in schema"
67-
}
68-
69-
Write-Output $TaskVariable
67+
throw "No environment variable found and no default value set in schema"
7068
}
7169
catch {
7270
Write-Error -Message "Could not get property from object [ $VariableName ] : $_" -ErrorAction Stop

tasks/EnvironmentConfiguration/tests/Get-SchemaProperty.unit.tests.ps1

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ InModuleScope "Handlers" {
1111
Clear-MockEnvironment
1212
}
1313

14-
Mock Get-VstsTaskVariable { return "default-string" }
15-
Mock Get-VstsTaskVariable { return 1 } -ParameterFilter { $AsInt -eq $true }
16-
Mock Get-VstsTaskVariable { return $true } -ParameterFilter { $AsBool -eq $true }
17-
1814
$SchemaDefinitionPath = "$PSScriptRoot/resource/SFA.DAS.Test.schema.json"
1915
$SchemaDefinition = Get-Content -Path $SchemaDefinitionPath -Raw
2016
$SchemaObject = [Newtonsoft.Json.Schema.JSchema, Newtonsoft.Json.Schema, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = 30ad4fe6b2a6aeed]::Parse($SchemaDefinition)
@@ -23,52 +19,48 @@ InModuleScope "Handlers" {
2319
Context "When given a valid properties object" {
2420

2521
It "Should return a string when no parameters are passed" {
26-
22+
Mock Get-VstsTaskVariable { return "default-string" }
2723
$Property = Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsString"]
2824
$Property | Should BeOfType [string]
2925
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 1
3026
}
3127

3228
It "Should return an int when -AsInt is passed" {
33-
29+
Mock Get-VstsTaskVariable { return "1" }
3430
$Property = Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsInt"] -AsInt
3531
$Property | Should BeOfType [int]
36-
Assert-MockCalled -CommandName Get-VstsTaskVariable -ParameterFilter { $AsInt -eq $true } -Times 1
32+
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 1
3733
}
3834

3935
It "Should return a decimal when -AsNumber is passed" {
40-
4136
Mock Get-VstsTaskVariable { return "1.0" }
4237
$Property = Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsNumber"] -AsNumber
4338
$Property | Should BeOfType [decimal]
4439
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 1
4540
}
4641

4742
It "Should return an array when -AsArray is passed" {
48-
4943
Mock Get-VstsTaskVariable { return "['one','two','three']" }
5044
$Property = Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsArray"] -AsArray
5145
$Property.GetType().BaseType.Name | Should Be 'Array'
5246
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 1
5347
}
5448

5549
It "Should return an bool when -AsBool is passed" {
56-
50+
Mock Get-VstsTaskVariable { return "True" }
5751
$Property = Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsBool"] -AsBool
5852
$Property | Should BeOfType [bool]
59-
Assert-MockCalled -CommandName Get-VstsTaskVariable -ParameterFilter { $AsBool -eq $true } -Times 1
53+
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 1
6054
}
6155

6256
It "Should return the default value if no environment variable can be found and the default property is populated" {
63-
6457
Mock Get-VstsTaskVariable { return $null }
6558
$Property = Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsDefaultValue"]
6659
$Property | Should Be "default-value"
6760
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 5
6861
}
6962

7063
It "Should throw an exception if no value can be found" {
71-
7264
Mock Get-VstsTaskVariable { return $null }
7365
{ Get-SchemaProperty -PropertyObject $SchemaObject.Properties["PaymentsString"] } | Should Throw
7466
Assert-MockCalled -CommandName Get-VstsTaskVariable -Times 5

0 commit comments

Comments
 (0)