Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit d613c48

Browse files
author
Seth Vargo
committed
Merge pull request #40 from customink/no_pty_windows
Don't use PTY when running on Windows
2 parents 5a55248 + 948435d commit d613c48

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

lib/strainer/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18+
require 'buff/platform'
1819
require 'strainer'
1920
require_relative 'strainerfile'
2021

@@ -65,7 +66,7 @@ def initialize(*args)
6566
class_option :strainer_file, :type => :string, :aliases => '-s', :desc => 'The path to the Strainer file to run against', :banner => 'FILE', :default => Strainer::Strainerfile::DEFAULT_FILENAME
6667
class_option :sandbox, :type => :string, :aliases => '-s', :desc => 'The sandbox path (defaults to a temporary directory)', :default => Dir.mktmpdir
6768
class_option :debug, :type => :boolean, :aliases => '-d', :desc => 'Show debugging log output', :default => false
68-
class_option :color, :type => :boolean, :aliases => '-co', :desc => 'Enable color in Strainer output', :default => true
69+
class_option :color, :type => :boolean, :aliases => '-co', :desc => 'Enable color in Strainer output', :default => !Buff::Platform.windows?
6970

7071
# strainer test *COOKBOOKS
7172
method_option :except, :type => :array, :aliases => '-e', :desc => 'Strainerfile labels to ignore'

lib/strainer/command.rb

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
# limitations under the License.
1616
#
1717

18-
require 'pty'
18+
begin
19+
# PTY is not a thing on Windows :(
20+
require 'pty'
21+
rescue LoadError
22+
end
1923

2024
module Strainer
2125
# The Command class is responsible for a command (test) against a cookbook.
@@ -58,18 +62,8 @@ def run!
5862

5963
inside do
6064
Strainer.ui.debug "Running '#{command}'"
61-
speak command
62-
PTY.spawn(command) do |r, _, pid|
63-
begin
64-
r.sync
65-
r.each_line { |line| speak line }
66-
rescue Errno::EIO => e
67-
# Ignore this. Otherwise errors will be thrown whenever
68-
# the process is closed
69-
ensure
70-
::Process.wait pid
71-
end
72-
end
65+
speak(command)
66+
defined?(PTY) ? run_as_pty(command) : run_as_system(command)
7367

7468
unless $?.success?
7569
Strainer.ui.error label_with_padding + Strainer.ui.set_color('Terminated with a non-zero exit status. Strainer assumes this is a failure.', :red)
@@ -177,5 +171,33 @@ def inside_cookbook(&block)
177171
Strainer.ui.debug "Restoring working directory to '#{original_pwd}'"
178172
success
179173
end
174+
175+
# Run a command using PTY
176+
#
177+
# @param [String] command
178+
# the command to run
179+
def run_as_pty(command)
180+
Strainer.ui.debug 'Using PTY'
181+
PTY.spawn(command) do |r, _, pid|
182+
begin
183+
r.sync
184+
r.each_line { |line| speak line }
185+
rescue Errno::EIO => e
186+
# Ignore this. Otherwise errors will be thrown whenever
187+
# the process is closed
188+
ensure
189+
::Process.wait pid
190+
end
191+
end
192+
end
193+
194+
# Run a command using Ruby's shell out (backticks/%x).
195+
#
196+
# @param [String] command
197+
# the command to run
198+
def run_as_system(command)
199+
Strainer.ui.debug 'Using %x'
200+
%x{#{command}}
201+
end
180202
end
181203
end

strainer.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Gem::Specification.new do |gem|
1616
gem.name = 'strainer'
1717
gem.require_paths = ['lib']
1818

19-
gem.add_runtime_dependency 'berkshelf', '~> 2.0'
19+
gem.add_runtime_dependency 'berkshelf', '~> 2.0'
20+
gem.add_runtime_dependency 'buff-platform', '~> 0.1'
2021

2122
gem.add_development_dependency 'redcarpet'
2223
gem.add_development_dependency 'yard'

0 commit comments

Comments
 (0)