kitchen-pester 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d308bdccbb4f9795482dcdac1abbe373935c4cb164e0bb1bca3d16c8d7185fe
4
- data.tar.gz: 798da4d69f83306722efd7311104e8797611cec975af00d88168c535a2056cc7
3
+ metadata.gz: 5f323e411287a34d2008dc58aba57ced9995e39511e77207031606c01a5b258d
4
+ data.tar.gz: 45031fdce5ecef091be46aa44e3934afe21e4233d3d1ba469ba2c026105be145
5
5
  SHA512:
6
- metadata.gz: 5bebf6a71183b7c02b3dcce3c8f1769b039dd30266a4da75da95f5c9fefd7da00f42421950e543fa529cfdf3cdbf2eb41756569d13e0a91bce68af881aed3cc3
7
- data.tar.gz: 408ac94d07a5b4a544892f325b7c70b85dbf4e9dcc9193d4505ca3307fcbda5b5e0f5a49c1a3f80cda8b8f084490cf52d7ac5169b372419c6a2075abbb21467a
6
+ metadata.gz: f1bfc59bc045bd3fe2c3313d6f0fb202f7a8f5887017251610546df15b0b1a644fbd75b4222bc7b9fdad80cd9a97faf4c0abd65eee36c8b54c3aeccc14ed43fd
7
+ data.tar.gz: 64f70c980a546c5d930f26bbea48e449f4da064feac617bffddd3fbef77649324e7204754af8fdacb41d79594e03bfcbcc779cf90f12b0766f519758070f36f1
data/Gemfile CHANGED
@@ -11,7 +11,7 @@ group :integration do
11
11
  end
12
12
 
13
13
  group :changelog do
14
- gem "github_changelog_generator", "1.15.2"
14
+ gem "github_changelog_generator", "1.16.4"
15
15
  end
16
16
 
17
17
  group :debug do
data/Rakefile CHANGED
@@ -23,7 +23,7 @@ desc "Run all quality tasks"
23
23
  task quality: :style
24
24
 
25
25
  begin
26
- require "yard"
26
+ require "yard" unless defined?(YARD)
27
27
  YARD::Rake::YardocTask.new
28
28
  rescue LoadError
29
29
  puts "yard is not available. (sudo) gem install yard to generate yard documentation."
@@ -1,4 +1,4 @@
1
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  require "kitchen/verifier/pester_version"
@@ -14,12 +14,12 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "fileutils"
18
- require "pathname"
17
+ require "fileutils" unless defined?(FileUtils)
18
+ require "pathname" unless defined?(Pathname)
19
19
  require "kitchen/util"
20
20
  require "kitchen/verifier/base"
21
21
  require "kitchen/version"
22
- require "base64"
22
+ require "base64" unless defined?(Base64)
23
23
  require_relative "pester_version"
24
24
 
25
25
  module Kitchen
@@ -47,10 +47,25 @@ module Kitchen
47
47
  Force: true,
48
48
  ErrorAction: "Stop",
49
49
  }
50
+ default_config :pester_configuration, {
51
+ run: {
52
+ path: ".",
53
+ PassThru: true,
54
+ },
55
+ TestResult: {
56
+ Enabled: true,
57
+ OutputPath: "PesterTestResults.xml",
58
+ TestSuiteName: "",
59
+ },
60
+ Output: {
61
+ Verbosity: "Detailed",
62
+ },
63
+ }
50
64
  default_config :install_modules, []
51
- default_config :downloads, ["./PesterTestResults.xml"] => "./testresults"
65
+ default_config :downloads, { "./PesterTestResults.xml" => "./testresults/" }
52
66
  default_config :copy_folders, []
53
67
  default_config :sudo, false
68
+ default_config :shell, nil
54
69
 
55
70
  # Creates a new Verifier object using the provided configuration data
56
71
  # which will be merged with any default configuration.
@@ -115,7 +130,7 @@ module Kitchen
115
130
  )
116
131
 
117
132
  if ($modulesToRemove.ModuleBase.Count -eq 0) {
118
- # for PS7 on linux
133
+ # for PS7 on linux
119
134
  return
120
135
  }
121
136
 
@@ -150,6 +165,7 @@ module Kitchen
150
165
  # @return [String] a command string
151
166
  def prepare_command
152
167
  info("Preparing the SUT and Pester dependencies...")
168
+ resolve_downloads_paths!
153
169
  really_wrap_shell_code(install_command_script)
154
170
  end
155
171
 
@@ -159,7 +175,38 @@ module Kitchen
159
175
  #
160
176
  # @return [String] a command string
161
177
  def run_command
162
- really_wrap_shell_code(run_command_script)
178
+ really_wrap_shell_code(invoke_pester_scriptblock)
179
+ end
180
+
181
+ # Resolves the remote Downloads path from the verifier root path,
182
+ # unless they're absolute path (starts with / or C:\)
183
+ # This updates the config[:downloads], nothing (nil) is returned.
184
+ #
185
+ # @return [nil] updates config downloads
186
+ def resolve_downloads_paths!
187
+ info("Resolving Downloads path from config.")
188
+ config[:downloads] = config[:downloads]
189
+ .map do |source, destination|
190
+ source = source.to_s
191
+ info(" resolving remote source's absolute path.")
192
+ unless source.match?('^/|^[a-zA-Z]:[\\/]') # is Absolute?
193
+ info(" '#{source}' is a relative path, resolving to: #{File.join(config[:root_path], source)}")
194
+ source = File.join(config[:root_path], source.to_s).to_s
195
+ end
196
+
197
+ if destination.match?('\\$|/$') # is Folder (ends with / or \)
198
+ destination = File.join(destination, File.basename(source)).to_s
199
+ end
200
+ info(" Destination: #{destination}")
201
+ if !File.directory?(File.dirname(destination))
202
+ FileUtils.mkdir_p(File.dirname(destination))
203
+ else
204
+ info(" Directory #{File.dirname(destination)} seem to exist.")
205
+ end
206
+
207
+ [ source, destination ]
208
+ end
209
+ nil # make sure we do not return anything
163
210
  end
164
211
 
165
212
  # Download functionality was added to the base verifier behavior after
@@ -168,7 +215,9 @@ module Kitchen
168
215
  def call(state)
169
216
  super
170
217
  ensure
171
- download_test_files(state) unless config[:download].nil?
218
+ info("Ensure download test files.")
219
+ download_test_files(state) unless config[:downloads].nil?
220
+ info("Download complete.")
172
221
  end
173
222
  else
174
223
  def call(state)
@@ -176,25 +225,87 @@ module Kitchen
176
225
  rescue
177
226
  # If the verifier reports failure, we need to download the files ourselves.
178
227
  # Test Kitchen's base verifier doesn't have the download in an `ensure` block.
179
- download_test_files(state) unless config[:download].nil?
180
-
228
+ info("Rescue to download test files.")
229
+ download_test_files(state) unless config[:downloads].nil?
181
230
  # Rethrow original exception, we still want to register the failure.
182
231
  raise
183
232
  end
184
233
  end
185
234
 
186
235
  # private
187
- def run_command_script
236
+ def invoke_pester_scriptblock
188
237
  <<-PS1
189
- Import-Module -Name Pester -Force -ErrorAction Stop
238
+ $PesterModule = Import-Module -Name Pester -Force -ErrorAction Stop -PassThru
190
239
 
191
240
  $TestPath = Join-Path "#{config[:root_path]}" -ChildPath "suites"
192
241
  $OutputFilePath = Join-Path "#{config[:root_path]}" -ChildPath 'PesterTestResults.xml'
193
242
 
194
- $options = New-PesterOption -TestSuiteName "Pester - #{instance.to_str}"
243
+ if ($PesterModule.Version.Major -le 4)
244
+ {
245
+ Write-Host -Object "Invoke Pester with v$($PesterModule.Version) Options"
246
+ $options = New-PesterOption -TestSuiteName "Pester - #{instance.to_str}"
247
+ $defaultPesterParameters = @{
248
+ Script = $TestPath
249
+ OutputFile = $OutputFilePath
250
+ OutputFormat = 'NUnitXml'
251
+ PassThru = $true
252
+ PesterOption = $options
253
+ }
254
+
255
+ $pesterCmd = Get-Command -Name 'Invoke-Pester'
256
+ $pesterConfig = #{ps_hash(config[:pester_configuration])}
257
+ $invokePesterParams = @{}
258
+
259
+ foreach ($paramName in $pesterCmd.Parameters.Keys)
260
+ {
261
+ $paramValue = $pesterConfig.($paramName)
262
+
263
+ if ($paramValue) {
264
+ Write-Host -Object "Using $paramName from Yaml config."
265
+ $invokePesterParams[$paramName] = $paramValue
266
+ }
267
+ elseif ($defaultPesterParameters.ContainsKey($paramName))
268
+ {
269
+ Write-Host -Object "Using $paramName from Defaults: $($defaultPesterParameters[$paramName])."
270
+ $invokePesterParams[$paramName] = $defaultPesterParameters[$paramName]
271
+ }
272
+ }
195
273
 
196
- $result = Invoke-Pester -Script $TestPath -OutputFile $OutputFilePath -OutputFormat NUnitXml -PesterOption $options -PassThru
197
- $result | Export-CliXml -Path (Join-Path -Path $TestPath -ChildPath 'result.xml')
274
+ $result = Invoke-Pester @invokePesterParams
275
+ }
276
+ else
277
+ {
278
+ Write-Host -Object "Invoke Pester with v$($PesterModule.Version) Configuration."
279
+ $pesterConfigHash = #{ps_hash(config[:pester_configuration])}
280
+
281
+ if (-not $pesterConfigHash.ContainsKey('run')) {
282
+ $pesterConfigHash['run'] = @{}
283
+ }
284
+
285
+ if (-not $pesterConfigHash.ContainsKey('TestResult')) {
286
+ $pesterConfigHash['TestResult'] = @{}
287
+ }
288
+
289
+ if (-not $pesterConfigHash.run.path) {
290
+ $pesterConfigHash['run']['path'] = $TestPath
291
+ }
292
+
293
+ if (-not $pesterConfigHash.TestResult.TestSuiteName) {
294
+ $pesterConfigHash['TestResult']['TestSuiteName'] = 'Pester - #{instance.to_str}'
295
+ }
296
+
297
+ if (-not $pesterConfigHash.TestResult.OutputPath) {
298
+ $pesterConfigHash['TestResult']['OutputPath'] = $OutputFilePath
299
+ }
300
+
301
+ $PesterConfig = New-PesterConfiguration -Hashtable $pesterConfigHash
302
+ $result = Invoke-Pester -Configuration $PesterConfig
303
+ }
304
+
305
+ $resultXmlPath = (Join-Path -Path $TestPath -ChildPath 'result.xml')
306
+ if (Test-Path -Path $resultXmlPath) {
307
+ $result | Export-CliXml -Path
308
+ }
198
309
 
199
310
  $LASTEXITCODE = $result.FailedCount
200
311
  $host.SetShouldExit($LASTEXITCODE)
@@ -216,6 +327,7 @@ module Kitchen
216
327
  if powershell_module.is_a? Hash
217
328
  <<-PS1
218
329
  ${#{powershell_module[:Name]}} = #{ps_hash(powershell_module)}
330
+
219
331
  Install-ModuleFromNuget -Module ${#{powershell_module[:Name]}} #{gallery_url_param}
220
332
  PS1
221
333
  else
@@ -231,7 +343,7 @@ module Kitchen
231
343
  #
232
344
  # @return [Array<String>] array of suite files
233
345
  # @api private
234
- def register_psrepository
346
+ def register_psrepository_scriptblock
235
347
  return if config[:register_repository].nil?
236
348
 
237
349
  info("Registering a new PowerShellGet Repository")
@@ -302,30 +414,58 @@ module Kitchen
302
414
  windows_os? ? really_wrap_windows_shell_code(code) : really_wrap_posix_shell_code(code)
303
415
  end
304
416
 
417
+ # Get the defined shell or fall back to pwsh, unless we're on windows where we use powershell
418
+ # call via sudo if sudo is true.
419
+ # This allows to use pwsh-preview instead of pwsh, or a full path to a specific binary.
420
+ def shell_cmd
421
+ if !config[:shell].nil?
422
+ config[:sudo] ? "sudo #{config[:shell]}" : "#{config[:shell]}"
423
+ elsif windows_os?
424
+ "powershell"
425
+ else
426
+ config[:sudo] ? "sudo pwsh" : "pwsh"
427
+ end
428
+ end
429
+
305
430
  def really_wrap_windows_shell_code(code)
306
- wrap_shell_code(Util.outdent!(use_local_powershell_modules(code)))
431
+ my_command = <<-PWSH
432
+ echo "Running as '$(whoami)'..."
433
+ New-Item -ItemType Directory -Path '#{config[:root_path]}/modules' -Force -ErrorAction SilentlyContinue
434
+ Set-Location -Path "#{config[:root_path]}"
435
+ # Send the pwsh here string to the file kitchen_cmd.ps1
436
+ @'
437
+ try {
438
+ Set-ExecutionPolicy Unrestricted -force
439
+ }
440
+ catch {
441
+ $_ | Out-String | Write-Warning
442
+ }
443
+ #{Util.outdent!(use_local_powershell_modules(code))}
444
+ '@ | Set-Content -Path kitchen_cmd.ps1 -Encoding utf8 -Force -ErrorAction 'Stop'
445
+ # create the modules folder, making sure it's done as current user (not root)
446
+ #
447
+ # Invoke the created kitchen_cmd.ps1 file using pwsh
448
+ #{shell_cmd} ./kitchen_cmd.ps1
449
+ PWSH
450
+ wrap_shell_code(Util.outdent!(my_command))
307
451
  end
308
452
 
309
453
  # Writing the command to a ps1 file, adding the pwsh shebang
310
454
  # invoke the file
311
455
  def really_wrap_posix_shell_code(code)
312
- if config[:sudo]
313
- pwsh_cmd = "sudo pwsh"
314
- else
315
- pwsh_cmd = "pwsh"
316
- end
317
-
318
456
  my_command = <<-BASH
319
457
  echo "Running as '$(whoami)'"
320
- # Send the bash heredoc 'EOF' to the file current.ps1 using the tool cat
321
- cat << 'EOF' > current.ps1
458
+ # create the modules folder, making sure it's done as current user (not root)
459
+ mkdir -p #{config[:root_path]}/modules
460
+ cd #{config[:root_path]}
461
+ # Send the bash heredoc 'EOF' to the file kitchen_cmd.ps1 using the tool cat
462
+ cat << 'EOF' > kitchen_cmd.ps1
322
463
  #!/usr/bin/env pwsh
323
464
  #{Util.outdent!(use_local_powershell_modules(code))}
324
465
  EOF
325
- # create the modules folder, making sure it's done as current user (not root)
326
- mkdir -p foo #{config[:root_path]}/modules
327
- # Invoke the created current.ps1 file using pwsh
328
- #{pwsh_cmd} -f current.ps1
466
+ chmod +x kitchen_cmd.ps1
467
+ # Invoke the created kitchen_cmd.ps1 file using pwsh
468
+ #{shell_cmd} ./kitchen_cmd.ps1
329
469
  BASH
330
470
 
331
471
  debug(Util.outdent!(my_command))
@@ -334,23 +474,15 @@ module Kitchen
334
474
 
335
475
  def use_local_powershell_modules(script)
336
476
  <<-PS1
337
- try {
338
- if (!$IsLinux -and !$IsMacOs) {
339
- Set-ExecutionPolicy Unrestricted -force
340
- }
341
- }
342
- catch {
343
- $_ | Out-String | Write-Warning
344
- }
345
-
477
+ Write-Host -Object ("{0} - PowerShell {1}" -f $PSVersionTable.OS,$PSVersionTable.PSVersion)
346
478
  $global:ProgressPreference = 'SilentlyContinue'
347
479
  $PSModPathToPrepend = Join-Path "#{config[:root_path]}" -ChildPath 'modules'
348
480
  Write-Verbose "Adding '$PSModPathToPrepend' to `$Env:PSModulePath."
349
481
  if (!$isLinux -and -not (Test-Path -Path $PSModPathToPrepend)) {
350
- # if you create this folder now un Linux, it will run as root (via sudo).
482
+ # if you create this folder now in Linux, it may run as root (via sudo).
351
483
  $null = New-Item -Path $PSModPathToPrepend -Force -ItemType Directory
352
484
  }
353
-
485
+
354
486
  if ($Env:PSModulePath.Split([io.path]::PathSeparator) -notcontains $PSModPathToPrepend) {
355
487
  $env:PSModulePath = @($PSModPathToPrepend, $env:PSModulePath) -Join [io.path]::PathSeparator
356
488
  }
@@ -367,7 +499,7 @@ module Kitchen
367
499
 
368
500
  #{get_powershell_modules_from_nugetapi.join("\n") unless config.dig(:bootstrap, :modules).nil?}
369
501
 
370
- #{register_psrepository.join("\n") unless config[:register_repository].nil?}
502
+ #{register_psrepository_scriptblock.join("\n") unless config[:register_repository].nil?}
371
503
 
372
504
  #{install_pester}
373
505
 
@@ -389,12 +521,15 @@ module Kitchen
389
521
  end
390
522
 
391
523
  def download_test_files(state)
392
- return if config[:downloads].nil?
524
+ if config[:downloads].nil?
525
+ info("Skipped downloading test result file from #{instance.to_str}; 'downloads' hash is empty.")
526
+ return
527
+ end
393
528
 
394
529
  info("Downloading test result files from #{instance.to_str}")
395
530
  instance.transport.connection(state) do |conn|
396
- config[:downloads].to_h.each do |remotes, local|
397
- debug("Downloading #{Array(remotes).join(", ")} to #{local}")
531
+ config[:downloads].each do |remotes, local|
532
+ debug("downloading #{Array(remotes).join(", ")} to #{local}")
398
533
  conn.download(remotes, local)
399
534
  end
400
535
  end
@@ -527,7 +662,7 @@ module Kitchen
527
662
  end
528
663
 
529
664
  def prepare_supporting_psmodules
530
- debug("Preparing to copy files from '#{support_psmodule_folder}' to the SUT.")
665
+ info("Preparing to copy files from '#{support_psmodule_folder}' to the SUT.")
531
666
  sandbox_module_path = File.join(sandbox_path, "modules")
532
667
  copy_if_src_exists(support_psmodule_folder, sandbox_module_path)
533
668
  end
@@ -542,7 +677,7 @@ module Kitchen
542
677
  return
543
678
  end
544
679
 
545
- debug("Moving #{src_to_validate} to #{destination}")
680
+ info("Moving #{src_to_validate} to #{destination}")
546
681
  unless Dir.exist?(destination)
547
682
  FileUtils.mkdir_p(destination)
548
683
  debug("Folder '#{destination}' created.")
@@ -1,5 +1,5 @@
1
1
  module Kitchen
2
2
  module Verifier
3
- PESTER_VERSION = "1.0.0".freeze
3
+ PESTER_VERSION = "1.1.0".freeze
4
4
  end
5
5
  end
@@ -106,8 +106,9 @@ function Set-PSRepo {
106
106
  [Parameter(Mandatory)]
107
107
  $Repository
108
108
  )
109
+
109
110
  if (-not (Get-Command Get-PSRepository) -and (Get-Command Get-PackageSource)) {
110
- # Old version of PSGet do not have a *-PSrepository but have *-PackageSource instead.
111
+ # Old versions of PSGet do not have a *-PSrepository but have *-PackageSource instead.
111
112
  if (Get-PackageSource -Name $Repository.Name) {
112
113
  Set-PackageSource @Repository
113
114
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen-pester
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Murawski
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-19 00:00:00.000000000 Z
11
+ date: 2021-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -110,7 +110,7 @@ homepage: https://github.com/test-kitchen/kitchen-pester
110
110
  licenses:
111
111
  - Apache-2.0
112
112
  metadata: {}
113
- post_install_message:
113
+ post_install_message:
114
114
  rdoc_options: []
115
115
  require_paths:
116
116
  - lib
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
126
  version: '0'
127
127
  requirements: []
128
128
  rubygems_version: 3.0.6
129
- signing_key:
129
+ signing_key:
130
130
  specification_version: 4
131
131
  summary: Test-Kitchen verifier for Pester.
132
132
  test_files: []