Write Jekyll::Utils::Exec.run for running shell commands.

This commit is contained in:
Parker Moore 2016-12-07 10:13:14 -08:00
parent 4382b2467b
commit 6e2449b482
No known key found for this signature in database
GPG Key ID: 193CDEBA72063C58
6 changed files with 48 additions and 29 deletions

View File

@ -1,6 +1,5 @@
require "fileutils"
require "jekyll"
require "open3"
require "time"
require "safe_yaml/load"
@ -106,21 +105,16 @@ end
# rubocop:disable Metrics/AbcSize
def run_in_shell(*args)
i, o, e, p = Open3.popen3(*args)
out = o.read.strip
err = e.read.strip
p, output = Jekyll::Utils::Exec.run(*args)
[i, o, e].each(&:close)
File.write(Paths.status_file, p.value.exitstatus)
File.write(Paths.status_file, p.exitstatus)
File.open(Paths.output_file, "wb") do |f|
f.puts "$ " << args.join(" ")
f.puts out
f.puts err
f.puts "EXIT STATUS: #{p.value.exitstatus}"
f.puts output
f.puts "EXIT STATUS: #{p.exitstatus}"
end
p.value
p
end
# rubocop:enable Metrics/AbcSize

View File

@ -133,10 +133,9 @@ RUBY
Jekyll::External.require_with_graceful_fail "bundler"
Jekyll.logger.info "Running bundle install in #{path.cyan}..."
Dir.chdir(path) do
if ENV["CI"]
system("bundle", "install", "--quiet")
else
system("bundle", "install")
_, output = Jekyll::Utils::Exec.run("bundle", "install", "--quiet")
output.to_s.each_line do |line|
Jekyll.logger.info(line)
end
end
end

View File

@ -2,8 +2,9 @@
module Jekyll
module Utils
extend self
autoload :Platforms, "jekyll/utils/platforms"
autoload :Ansi, "jekyll/utils/ansi"
autoload :Exec, "jekyll/utils/exec"
autoload :Platforms, "jekyll/utils/platforms"
# Constants for use in #slugify
SLUGIFY_MODES = %w(raw default pretty ascii).freeze

25
lib/jekyll/utils/exec.rb Normal file
View File

@ -0,0 +1,25 @@
require 'open3'
module Jekyll
module Utils
module Exec
extend self
# Runs a program in a sub-shell.
#
# *args - a list of strings containing the program name and arguments
#
# Returns a Process::Status and a String of output in an array in
# that order.
def run(*args)
stdin, stdout, stderr, process = Open3.popen3(*args)
out = stdout.read.strip
err = stderr.read.strip
[stdin, stdout, stderr].each(&:close)
[process.value, out + err]
end
end
end
end

View File

@ -159,11 +159,11 @@ class JekyllUnitTest < Minitest::Test
end
def capture_output
stderr = StringIO.new
Jekyll.logger = Logger.new stderr
buffer = StringIO.new
Jekyll.logger = Logger.new(buffer)
yield
stderr.rewind
return stderr.string.to_s
buffer.rewind
buffer.string.to_s
end
alias_method :capture_stdout, :capture_output
alias_method :capture_stderr, :capture_output

View File

@ -25,14 +25,14 @@ class TestNewCommand < JekyllUnitTest
should "create a new directory" do
refute_exist @full_path
Jekyll::Commands::New.process(@args)
capture_output { Jekyll::Commands::New.process(@args) }
assert_exist @full_path
end
should "create a Gemfile" do
gemfile = File.join(@full_path, "Gemfile")
refute_exist @full_path
capture_stdout { Jekyll::Commands::New.process(@args) }
capture_output { Jekyll::Commands::New.process(@args) }
assert_exist gemfile
assert_match(%r!gem "jekyll", "#{Jekyll::VERSION}"!, File.read(gemfile))
assert_match(%r!gem "github-pages"!, File.read(gemfile))
@ -54,7 +54,7 @@ class TestNewCommand < JekyllUnitTest
end
static_template_files << "/Gemfile"
capture_stdout { Jekyll::Commands::New.process(@args) }
capture_output { Jekyll::Commands::New.process(@args) }
new_site_files = dir_contents(@full_path).reject do |f|
File.extname(f) == ".markdown"
@ -76,7 +76,7 @@ class TestNewCommand < JekyllUnitTest
f.gsub! "0000-00-00", stubbed_date
end
capture_stdout { Jekyll::Commands::New.process(@args) }
capture_output { Jekyll::Commands::New.process(@args) }
new_site_files = dir_contents(@full_path).select do |f|
erb_template_files.include? f
@ -87,7 +87,7 @@ class TestNewCommand < JekyllUnitTest
should "create blank project" do
blank_contents = %w(/_drafts /_layouts /_posts /index.html)
capture_stdout { Jekyll::Commands::New.process(@args, "--blank") }
capture_output { Jekyll::Commands::New.process(@args, "--blank") }
output = Jekyll.logger.messages.last
bundle_message = "Running bundle install in #{@full_path.cyan}..."
assert_same_elements blank_contents, dir_contents(@full_path)
@ -95,13 +95,13 @@ class TestNewCommand < JekyllUnitTest
end
should "force created folder" do
capture_stdout { Jekyll::Commands::New.process(@args) }
output = capture_stdout { Jekyll::Commands::New.process(@args, "--force") }
capture_output { Jekyll::Commands::New.process(@args) }
output = capture_output { Jekyll::Commands::New.process(@args, "--force") }
assert_match(%r!New jekyll site installed in!, output)
end
should "skip bundle install when opted to" do
capture_stdout { Jekyll::Commands::New.process(@args, "--skip-bundle") }
capture_output { Jekyll::Commands::New.process(@args, "--skip-bundle") }
output = Jekyll.logger.messages.last
bundle_message = "Bundle install skipped."
assert_includes output, bundle_message
@ -120,7 +120,7 @@ class TestNewCommand < JekyllUnitTest
should "create a new directory" do
refute_exist @site_name_with_spaces
capture_stdout { Jekyll::Commands::New.process(@multiple_args) }
capture_output { Jekyll::Commands::New.process(@multiple_args) }
assert_exist @site_name_with_spaces
end
end