Guest User

Untitled

a guest
Oct 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #Invoke-Pester -Path "C:\GIT\GITHUB\dbatools.Workspace\Test-Examples.ps1" -OutputFile 'C:\Temp\dbatools\pester.xml' -OutputFormat NUnitXml
  2. #$testRes = [XML](Get-Content -Path "C:\Temp\dbatools\pester.xml")
  3. #$testRes.SelectNodes("//test-suite/results/test-case/failure/..").OuterXml | Out-File 'C:\Temp\dbatools\Analysis\Res.xml'
  4.  
  5. Import-Module "C:\GIT\GITHUB\dbatools.Workspace\dbatools" -Force
  6.  
  7. $excludeCommands = @(
  8. "Import-ModuleFile"
  9. , "Get-DeepClone"
  10. )
  11.  
  12. $commandsRaw = Get-Command -Module dbatools
  13.  
  14. if ($excludeCommands.Count -gt 0) {
  15. $commands = $commandsRaw | Select-String -Pattern $excludeCommands -SimpleMatch -NotMatch
  16.  
  17. } else {
  18. $commands = $commandsRaw
  19. }
  20.  
  21. foreach ( $commandName in $commands) {
  22. # command to be tested
  23. #$commandName = 'New-D365Bacpac'
  24. #$commandName = 'Get-D365PackageLabelFile'
  25. # get all examples from the help
  26. $examples = Get-Help $commandName -Examples
  27.  
  28. # make a describe block that will contain tests for this
  29. Describe "Examples from $commandName" {
  30. $examples.Examples.Example | foreach {
  31. # examples have different format,
  32. # at least the ones I used that MS provided
  33. # so you need to either standardize them,
  34. # or provide some hints about what to do
  35. # such as putting the code first
  36. # followed by
  37. # #output: the desired output
  38.  
  39. # here I am simply taking the first line and removing 'PS C:\>'
  40. # which makes some of the tests fail
  41. $example = $_.Code -replace "`n.*" -replace "PS C:\\>"
  42.  
  43. if ( ($example -like "*|*" ) -or (-not ($example -match $commandName)) ) {
  44. It "Example - $example" -Skip { $true }
  45. } else {
  46. # for every example we want a single It block
  47. It "Example - $example" {
  48. # mock the tested command so we don't actually do anything
  49. # because it can be unsafe and we don't have the environment setup
  50. # (so the only thing we are testing is that the code is semantically
  51. # correct and provides all the needed params)
  52. Mock $commandName {
  53. # I am returning true here,
  54. # but some of the examples drill down to the returned object
  55. # so in strict mode we would fail
  56. $true
  57. }
  58.  
  59. # here simply invoke the example
  60. $result = Invoke-Expression $example
  61. # and check that we got result from the mock
  62. $result | Should -BeTrue
  63. }
  64. }
  65. }
  66. }
  67. }
Add Comment
Please, Sign In to add comment