Skip to content

Commit 1bff58e

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
Change utcNow() tests to use regex for validation
1 parent 18fd542 commit 1bff58e

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

dsc/tests/dsc_functions.tests.ps1

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,20 @@ Describe 'tests for function expressions' {
246246
}
247247

248248
It 'utcNow function works for: utcNow(<format>)' -TestCases @(
249-
@{ format = $null}
250-
@{ format = "yyyy-MM-dd"}
251-
@{ format = "yyyy-MM-ddTHH"}
252-
@{ format = "yyyy-MM-ddTHHZ"}
253-
@{ format = "MMM dd, yyyy HH"}
254-
@{ format = "yy-MMMM-dddd tt H" }
255-
@{ format = "MMM ddd zzz" }
256-
@{ format = "YY YYYY MM MMM MMMM" }
249+
@{ format = $null; regex = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z$' }
250+
@{ format = "yyyy-MM-dd"; regex = '^\d{4}-\d{2}-\d{2}$' }
251+
@{ format = "yyyy-MM-ddTHH"; regex = '^\d{4}-\d{2}-\d{2}T\d{2}$' }
252+
@{ format = "yyyy-MM-ddTHHZ"; regex = '^\d{4}-\d{2}-\d{2}T\d{2}Z$' }
253+
@{ format = "MMM dd, yyyy HH"; regex = '^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{2}, \d{4} \d{2}$' }
254+
@{ format = "yy-MMMM-dddd tt H"; regex = '^\d{2}-(January|February|March|April|May|June|July|August|September|October|November|December)-(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday) (AM|PM) \d+$' }
255+
@{ format = "MMM ddd zzz"; regex = '^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (Sun|Mon|Tue|Wed|Thu|Fri|Sat) \+00:00$' }
256+
@{ format = "yy yyyy MM MMM MMMM"; regex = '^\d{2} \d{4} \d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (January|February|March|April|May|June|July|August|September|October|November|December)$' }
257+
@{ format = "yyyy-MM-ddTHH:mm:ss"; regex = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$' }
257258
) {
258-
param($format)
259+
param($format, $regex)
259260

260-
if ($null -eq $format) {
261-
$expected = (Get-Date -AsUTC).ToString("o")
262-
} else {
263-
$expected = (Get-Date -AsUTC).ToString($format)
264-
$format = "'$format'"
261+
if ($null -ne $format) {
262+
$format = "'$format'"
265263
}
266264

267265
$config_yaml = @"
@@ -281,10 +279,8 @@ Describe 'tests for function expressions' {
281279
# ConvertFrom-Json will convert the date to a DateTime object, so we use regex to capture the string
282280
$out -match '"output":"(?<date>.*?)"' | Should -BeTrue -Because "Output should contain a date"
283281
$actual = $matches['date']
284-
# since the datetimes might slightly differ, we remove the seconds and milliseconds
285-
$expected = $expected -replace ':\d+\.\d+Z$', 'Z'
286-
$actual = $actual -replace ':\d+\.\d+Z$', 'Z'
287-
$actual | Should -BeExactly $expected -Because "Expected: '$expected', Actual: '$actual'"
282+
# comapre against the regex
283+
$actual | Should -Match $regex -Because "Output date '$actual' should match regex '$regex'"
288284
}
289285

290286
It 'utcNow errors if used not as a parameter default' {
@@ -298,6 +294,7 @@ Describe 'tests for function expressions' {
298294
"@
299295
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
300296
$LASTEXITCODE | Should -Be 2 -Because (Get-Content $TestDrive/error.log -Raw)
297+
$out | Should -BeNullOrEmpty -Because "Output should be null or empty"
301298
(Get-Content $TestDrive/error.log -Raw) | Should -Match 'utcNow function can only be used as a parameter default'
302299
}
303300

0 commit comments

Comments
 (0)