Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. # this snippet describes how to automatically clean up
  2. # changes in environment varables
  3. # it can clean up re-writing, adding and removing
  4. # scroll down to see how to use it in real test
  5.  
  6. Get-Module Pester | Remove-Module
  7. Import-Module Pester -MaximumVersion 4.9.9
  8.  
  9. function Get-Environment {
  10. $vars = Get-Item Env:\
  11. $hash = @{}
  12. foreach ($var in $vars) {
  13. $hash.Add($var.Name, $var.Value)
  14. }
  15. $hash
  16. }
  17.  
  18. function Set-Environment {
  19. param (
  20. [Parameter(Mandatory=$true)]
  21. [Hashtable] $Environment
  22. )
  23.  
  24. # set all items to their previous state
  25. foreach ($env in $Environment.GetEnumerator()) {
  26. Set-Item "Env:\$($env.Key)" -Value $env.Value
  27. }
  28.  
  29. $vars = Get-Item "Env:\"
  30. # remove all items that were not present before
  31. foreach ($var in $vars) {
  32. if (-not $Environment.ContainsKey($var.Name)) {
  33. Remove-Item "Env:\$($var.Name)"
  34. }
  35. }
  36. }
  37.  
  38. Describe "Restore environment" {
  39. It "Restores the values to the state before the test" {
  40. try {
  41. # -- Arrange
  42. $computername = $env:COMPUTERNAME
  43. $environment = Get-Environment
  44. $env:COMPUTERNAME = "abc"
  45.  
  46. # -- Act
  47. Set-Environment -Environment $environment
  48.  
  49. # -- Assert
  50. $env:COMPUTERNAME | Should -Be $computername
  51. }
  52. finally {
  53. # in case the code did not fix it we fix it ourselves
  54. # to avoid changing the computername
  55. $env:COMPUTERNAME = $computername
  56. }
  57. }
  58.  
  59. It "Removes all variables that did not exist before the test" {
  60. try {
  61. # -- Arrange
  62. "Env:\abc" | Should -Not -Exist -Because "we are testing that adding the variable will remove it at the end"
  63. $environment = Get-Environment
  64.  
  65. $env:abc = "abc"
  66.  
  67. # -- Act
  68. Set-Environment -Environment $environment
  69.  
  70. # -- Assert
  71. "Env:\abc" | Should -Not -Exist
  72. }
  73. finally {
  74. # in case the code did not fix it we fix it ourselves
  75. # to avoid leaking the test variable
  76. if (Test-Path "env:\abc") {
  77. Remove-Item "env:\abc"
  78. }
  79. }
  80. }
  81.  
  82. It "Adds all variables that were removed during the test" {
  83. try {
  84. # -- Arrange
  85. $env:abc = "abc"
  86. "Env:\abc" | Should -Exist -Because "we are testing that removing the variable will add it at the end"
  87. $environment = Get-Environment
  88.  
  89. Remove-Item "Env:\abc"
  90.  
  91. # -- Act
  92. Set-Environment -Environment $environment
  93.  
  94. # -- Assert
  95. "Env:\abc" | Should -Exist
  96. }
  97. finally {
  98. # in case the code did not fix it we fix it ourselves
  99. # to avoid leaking the test variable
  100. if (Test-Path "env:\abc") {
  101. Remove-Item "env:\abc"
  102. }
  103. }
  104. }
  105. }
  106.  
  107. Describe "Testing usage in real test" {
  108. # this is how the code would be used in real life
  109. Describe "Do test" {
  110. BeforeEach {
  111. $environment = Get-Environment
  112. }
  113.  
  114. It "Adds environment variable" {
  115. $env:gef = "gef"
  116. }
  117.  
  118. AfterEach {
  119. Set-Environment $environment
  120. }
  121. }
  122.  
  123. Describe "Environment variable did not leak" {
  124. It "env:gef does not exist" {
  125. "Env:\gef" | Should -Not -Exist
  126. }
  127.  
  128. AfterAll {
  129. # in case the code did not fix it we fix it ourselves
  130. # to avoid leaking the test variable
  131. if (Test-Path "env:\gef") {
  132. Remove-Item "env:\gef"
  133. }
  134. }
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement