Skip to content

Commit

Permalink
(maint) Change winrm specs to write to stdout
Browse files Browse the repository at this point in the history
 - The current WinRM specs use the Write-Output cmdlet. The
   Write-Output cmdlet is designed for writing objects to the
   PowerShell pipeline, *not* for writing stdout to a host.

   PowerShell has a few different methods of writing more directly to
   stdout, the most popular of which is Write-Host. Write-Host is
   typically frowned upon by the PowerShell community, but because it
   is not appropriate for use where pipelines may be involved:

   http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/

   When the goal is to emit text to be consumed by other tooling that
   doesn't integrate with the PowerShell pipeline, Write-Host becomes
   an important tool, and likely the case the tests are interested in
   (independent ordered text-based writes to stdout).

   Such counterpoints are provided at:

   https://psitem.com/2015/01/16/is-write-host-really-harmful/

   The other alternatives to calling Write-Host are Out-Host,
   [Console]::WriteLine and the lesser known $Host.UI.WriteLine

 - While Write-Output can generate output for both stdout *and* for
   the PowerShell pipeline, there is also a problem with using it if
   output ordering is important.

   In PowerShell 5, changes were introduced to its behavior, so that
   implicit table formatting receives a 300ms delay. This often
   results in out-of-order output when Write-Output is used, and may
   especially be the case when pipeline writes with Write-Output are
   mixed with direct writes to stdout with Write-Host

   PowerShell/PowerShell#4594
   https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/14925213-bug-console-output-appears-out-of-order
  • Loading branch information
Iristyle committed Nov 6, 2017
1 parent 4491dd0 commit 359a84d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions spec/bolt/node/winrm_spec.rb
Expand Up @@ -18,7 +18,7 @@
let(:echo_script) { <<PS }
foreach ($i in $args)
{
Write-Output $i
Write-Host $i
}
PS

Expand Down Expand Up @@ -123,7 +123,7 @@ def stub_winrm_to_raise(klass, message)
end

it "can run a script remotely", winrm: true do
contents = "Write-Output \"hellote\""
contents = "Write-Host \"hellote\""
with_tempfile_containing('script-test-winrm', contents) do |file|
expect(
winrm._run_script(file.path, []).value
Expand Down Expand Up @@ -203,7 +203,7 @@ def stub_winrm_to_raise(klass, message)
end

it "can run a task remotely", winrm: true do
contents = 'Write-Output "$env:PT_message_one" ${env:PT_message two}'
contents = 'Write-Host "$env:PT_message_one" ${env:PT_message two}'
arguments = { :message_one => 'task is running',
:"message two" => 'task has run' }
with_tempfile_containing('task-test-winrm', contents) do |file|
Expand All @@ -215,7 +215,7 @@ def stub_winrm_to_raise(klass, message)
it "can run a task passing input on stdin", winrm: true do
contents = <<PS
$line = [Console]::In.ReadLine()
Write-Output $line
Write-Host $line
PS
arguments = { message_one: 'Hello from task', message_two: 'Goodbye' }
with_tempfile_containing('tasks-test-stdin-winrm', contents) do |file|
Expand All @@ -226,9 +226,9 @@ def stub_winrm_to_raise(klass, message)

it "can run a task passing input on stdin and environment", winrm: true do
contents = <<PS
Write-Output "$env:PT_message_one" "$env:PT_message_two"
Write-Host "$env:PT_message_one" "$env:PT_message_two"
$line = [Console]::In.ReadLine()
Write-Output $line
Write-Host $line
PS
arguments = { message_one: 'Hello from task', message_two: 'Goodbye' }
with_tempfile_containing('tasks-test-both-winrm', contents) do |file|
Expand All @@ -245,7 +245,7 @@ def stub_winrm_to_raise(klass, message)
describe "when resolving file extensions" do
it "can apply a powershell-based task", winrm: true do
contents = <<PS
Write-Output "42"
Write-Host "42"
PS
allow(winrm)
.to receive(:execute_process)
Expand Down

0 comments on commit 359a84d

Please sign in to comment.