Skip to content
Browse files

Implement the "update" execution argument.

  • Loading branch information...
1 parent 202b48b commit a84614ec3bcb662e2f1d994d780ebbca59d385d3 @rsutphin committed Jun 13, 2011
Showing with 95 additions and 1 deletion.
  1. +1 −1 README.md
  2. +15 −0 hrirb
  3. +37 −0 lib/handbrake/cli.rb
  4. +22 −0 spec/handbrake/cli_spec.rb
  5. +20 −0 spec/spec_helper.rb
View
2 README.md
@@ -63,7 +63,7 @@ configuration options:
is `'HandBrakeCLI'`; i.e., by default it will be searched for on the
normal executable path.
* `:trace`: if true, all output from `HandBrakeCLI` will be echoed to
- the project's output stream.
+ the project's error stream.
### Set options
View
15 hrirb
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+
+######
+# Run irb for interactive fiddling with this library from the source
+
+require 'irb'
+
+$LOAD_PATH << File.expand_path('../lib', __FILE__)
+require 'handbrake'
+
+$cli = HandBrake::CLI.new(:trace => true)
+
+puts "`$cli` is a HandBrake::CLI instance with tracing on"
+
+IRB.start(__FILE__)
View
37 lib/handbrake/cli.rb
@@ -8,6 +8,7 @@ class CLI
def initialize(options={})
@bin_path = options[:bin_path] || 'HandBrakeCLI'
@trace = options[:trace].nil? ? false : options[:trace]
+ @runner = options[:runner] || PopenRunner.new(self)
@args = []
end
@@ -25,6 +26,20 @@ def trace?
end
##
+ # Checks to see if the `HandBrakeCLI` instance designated by
+ # {#bin_path} is the current version.
+ #
+ # Note that `HandBrakeCLI` will always report that it is up to
+ # date if it can't connect to the update server, so this is not
+ # terribly reliable.
+ #
+ # @return [Boolean]
+ def update
+ result = @runner.run(arguments.push('--update'))
+ result.output =~ /Your version of HandBrake is up to date./i
+ end
+
+ ##
# @private
def arguments
@args.collect { |req, *rest| ["--#{req.to_s.gsub('_', '-')}", *rest] }.flatten
@@ -35,5 +50,27 @@ def method_missing(name, *args)
copy.instance_eval { @args << [name, *(args.collect { |a| a.to_s })] }
copy
end
+
+ class PopenRunner
+ def initialize(cli_instance)
+ @cli = cli_instance
+ end
+
+ def run(arguments)
+ output = ''
+ cmd = arguments.unshift(@cli.bin_path).push(:err => [:child, :out])
+
+ $stderr.puts "Spawning HandBrakeCLI using #{cmd.inspect}" if @cli.trace?
+ IO.popen(cmd) do |io|
+ while line = io.gets
+ output << line
+ $stderr.puts(line.chomp) if @cli.trace?
+ end
+ end
+ RunnerResult.new(output, $?)
+ end
+ end
+
+ RunnerResult = Struct.new(:output, :status)
end
end
View
22 spec/handbrake/cli_spec.rb
@@ -55,5 +55,27 @@ module HandBrake
%w(--input /foo/bar --title 6 --ipod-atom)
end
end
+
+ describe 'execution' do
+ let(:cli) { HandBrake::CLI.new(:runner => runner) }
+ let(:runner) { HandBrake::Spec::StaticRunner.new }
+
+ describe '#update' do
+ it 'uses the update argument' do
+ cli.update
+ runner.actual_arguments.should == %w(--update)
+ end
+
+ it 'returns true if the thing is up to date' do
+ runner.output = 'Your version of HandBrake is up to date.'
+ cli.update.should be_true
+ end
+
+ it 'returns false if the executable is out of date' do
+ runner.output = 'You are using an old version of HandBrake.'
+ cli.update.should be_false
+ end
+ end
+ end
end
end
View
20 spec/spec_helper.rb
@@ -35,3 +35,23 @@ def tmpdir(sub=nil)
end
end
end
+
+module HandBrake
+ module Spec
+ ##
+ # A stub implementation of the runner for HandBrake::CLI.
+ class StaticRunner
+ attr_accessor :output, :status
+ attr_reader :actual_arguments
+
+ def run(args)
+ @actual_arguments = args
+ HandBrake::CLI::RunnerResult.new(output, status)
+ end
+
+ def status
+ @status ||= 0
+ end
+ end
+ end
+end

0 comments on commit a84614e

Please sign in to comment.
Something went wrong with that request. Please try again.