Adding 'yadr' command for managing plugins (wip)

This commit is contained in:
yan
2011-12-07 20:34:25 -08:00
parent 0741bb0a71
commit acb81bb874
36 changed files with 3874 additions and 15 deletions

View File

@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + "/../../lib")
VERSION="0.0.2" # just to test it
require 'git-style-binary/command'

View File

@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + "/../../lib")
require 'git-style-binary/command'
GitStyleBinary.command do
short_desc "download a flickr image"
banner <<-EOS
SYNOPSIS
#{command.full_name} #{all_options_string} url
Downloads an image from flickr
EOS
run do |command|
puts "would download: #{command.argv.inspect}"
end
end

View File

@@ -0,0 +1,42 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + "/../../lib")
require 'git-style-binary/command'
GitStyleBinary.primary do
version "0.0.1 (c) 2009 Nate Murray - local"
opt :test_primary, "test an option on the primary", :type => String
action do
@categories = ["sports", "news"]
end
before_run do |cmd|
puts "before_run command #{cmd}"
end
after_run do |cmd|
puts "after_run command #{cmd}"
end
run do |command|
puts "Primary Options: #{command.opts.inspect}"
end
end
# OR
# require 'git-style-binary/primary'
# command = GitStyleBinary::primary("wordpress") do
# version "#{$0} 0.0.1 (c) 2009 Nate Murray"
# banner <<-EOS
# usage: #{$0} #{all_options.collect(:&to_s).join(" ")} COMMAND [ARGS]
#
# The wordpress subcommands commands are:
# {subcommand_names.pretty_print}
#
# See 'wordpress help COMMAND' for more information on a specific command.
# EOS
# opt :verbose, "verbose", :default => false
# opt :dry, "dry run", :default => false
# opt :test_global, "a basic global string option", :type => String
# end

View File

@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + "/../../lib")
require 'git-style-binary/command'
GitStyleBinary.command do
short_desc "do something with categories"
banner <<-EOS
SYNOPSIS
#{command.full_name} #{all_options_string}
Does something with categories
EOS
run do |command|
puts "does something with categories"
puts @categories.join(" ")
end
end

View File

@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + "/../../lib")
require 'git-style-binary/command'
GitStyleBinary.command do
short_desc "list blog postings"
banner <<-EOS
SYNOPSIS
#{command.full_name} #{all_options_string}
Lists the posts on the blog
EOS
run do |command|
puts "listing blog posts"
end
end

View File

@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
$:.unshift(File.dirname(__FILE__) + "/../../lib")
require 'git-style-binary/command'
GitStyleBinary.command do
short_desc "create a blog post"
banner <<-EOS
SYNOPSIS
#{command.full_name} #{all_options_string} {content|STDIN}
EOS
opt :blog, "short name of the blog to use", :default => 'default'
opt :category, "tag/category. specify multiple times for multiple categories", :type => String, :multi => true
opt :title, "title for the post", :required => true, :type => String
opt :type, "type of the content [html|xhtml|text]", :default => 'html', :type => String
run do |command|
command.die :type, "type must be one of [html|xhtml|text]" unless command.opts[:type] =~ /^(x?html|text)$/i
puts "Subcommand name: #{command.name.inspect}"
puts "Options: #{command.opts.inspect}"
puts "Remaining arguments: #{command.argv.inspect}"
end
end

View File

@@ -0,0 +1,17 @@
require File.dirname(__FILE__) + "/../test_helper.rb"
require 'git-style-binary/command'
class CommandTest < Test::Unit::TestCase
context "cmd" do
setup do
@c = GitStyleBinary::Command.new
end
should "be able to easily work with constraints" do
assert_equal @c.constraints, []
@c.constraints << "foo"
assert_equal @c.constraints, ["foo"]
end
end
end

View File

@@ -0,0 +1,21 @@
require File.dirname(__FILE__) + "/test_helper.rb"
class GitStyleBinariesTest < Test::Unit::TestCase
context "parsing basenames" do
should "accurately parse basenames" do
assert_equal "wordpress", GitStyleBinary.basename("bin/wordpress")
assert_equal "wordpress", GitStyleBinary.basename("bin/wordpress-post")
assert_equal "wordpress", GitStyleBinary.basename("wordpress-post")
end
should "get the current command name" do
# doesn't really apply any more b/c it calls 'current' which is never the
# current when your running rake_test_loader.rb
#
# assert_equal "wordpress", GitStyleBinary.current_command_name("bin/wordpress", ["--help"])
# assert_equal "post", GitStyleBinary.current_command_name("bin/wordpress-post", ["--help"])
# assert_equal "post", GitStyleBinary.current_command_name("bin/wordpress post", ["--help"])
#assert_equal "post", GitStyleBinary.current_command_name("bin/wordpress post", [])
end
end
end

View File

@@ -0,0 +1,224 @@
require File.dirname(__FILE__) + "/test_helper.rb"
THIS_YEAR=Time.now.year # todo
class RunningBinariesTest < Test::Unit::TestCase
include RunsBinaryFixtures
context "when running primary" do
["wordpress -h", "wordpress help"].each do |format|
context "and getting help as a '#{format}'" do
setup { @stdout, @stderr = bin(format) }
should "have the command name and short description" do
unless format == "wordpress -h" # doesn't apply to wordpress -h
output_matches /NAME\n\s*wordpress\-help \- get help for a specific command/m
end
end
should "have a local (not default) version string" do
output_matches /0\.0\.1 \(c\) 2009 Nate Murray - local/
end
should "get a list of subcommands" do
output_matches /subcommands/mi
end
should "have subcommand short descriptions" do
output_matches /post\s*create a blog post/
output_matches /categories\s*do something with categories/
output_matches /help\s*get help for a specific command/
output_matches /list\s*list blog postings/
end
should "have a usage" do
output_matches /SYNOPSIS/i
output_matches /wordpress(\-help)? \[/
end
should "be able to ask for help about help"
end
end
context "and getting help as subcommand" do
# ["wordpress -h", "wordpress help"].each do |format|
["wordpress help"].each do |format|
context "'#{format}'" do
should "get help on subcommand post"
end
end
end
context "with no options" do
setup { @stdout, @stderr = bin("wordpress") }
should "output the options" do
output_matches /Primary Options:/
end
should "have the test_primary option" do
output_matches /test_primary=>nil/
end
end
should "be able to require 'primary' and run just fine"
end
context "when running with an action" do
# should be the same for both formats
["wordpress-categories", "wordpress categories"].each do |bin_format|
context "#{bin_format}" do
context "with action block" do
setup { @stdout, @stderr = bin("#{bin_format}") }
should "have the parsed action items in the help output" do
output_matches /sports news/m
end
end
end
end
end
context "callbacks" do
context "on a binary" do
setup { @stdout, @stderr = bin("wordpress") }
%w(before after).each do |time|
should "run the callback #{time}_run}" do
assert @stdout.match(/#{time}_run command/)
end
end
end
context "on help" do
setup { @stdout, @stderr = bin("wordpress -h") }
%w(before after).each do |time|
should "not run the callback #{time}_run" do
assert_nil @stdout.match(/#{time}_run command/)
end
end
end
end
context "when running the subcommand" do
# should be the same for both formats
["wordpress-post", "wordpress post"].each do |bin_format|
context "#{bin_format}" do
context "with no options" do
setup { @stdout, @stderr = bin("#{bin_format}") }
should "fail because title is required" do
output_matches /Error: option 'title' must be specified.\s*Try --help for help/m
end
end
context "with options" do
setup { @stdout, @stderr = bin("#{bin_format} --title='glendale'") }
should "be running the subcommand's run block" do
output_matches /Subcommand name/
end
should "have some default options" do
output_matches /version=>false/
output_matches /help=>false/
end
should "have some primary options" do
output_matches /test_primary=>nil/
end
should "have some local options" do
output_matches /title=>"glendale"/
output_matches /type=>"html"/
end
end
context "testing die statements" do
setup { @stdout, @stderr = bin("#{bin_format} --title='glendale' --type=yaml") }
should "die on invalid options" do
output_matches /argument \-\-type type must be one of \[html\|xhtml\|text\]/
end
end
end # end bin_format
end # end #each
end
["wordpress help post", "wordpress post -h"].each do |format|
context "when calling '#{format}'" do
setup { @stdout, @stderr = bin(format) }
should "have a description" do
output_matches /create a blog post/
end
should "have the proper usage line" do
output_matches /SYNOPSIS\n\s*wordpress\-post/m
output_matches /\[--title\]/
end
should "have option flags" do
output_matches /\-\-title(.*)<s>/
end
should "have primary option flags" do
output_matches /\-\-test-primary(.*)<s>/
end
should "have default option flags" do
output_matches /\-\-verbose/
end
should "have trollop default option flags" do
output_matches /\-e, \-\-version/
end
should "have the correct binary name and short description" do
output_matches /NAME\n\s*wordpress\-post \- create a blog post/m
end
should "have a the primaries version string" do
output_matches /0\.0\.1 \(c\) 2009 Nate Murray - local/
end
should "have options" do
output_matches /Options/i
output_matches /\-b, \-\-blog=<s>/
output_matches /short name of the blog to use/
output_matches /-i, \-\-title=<s>/
output_matches /title for the post/
end
end
end
context "when running a bare primary" do
["flickr -h", "flickr help"].each do |format|
context format do
setup { @stdout, @stderr = bin(format) }
should "have the name and short description" do
unless format == "flickr -h" # hmm
output_matches /NAME\n\s*flickr\-help \- get help for a specific command/m
end
end
should "have a local (not default) version string" do
output_matches /0\.0\.2 \(c\) #{Time.now.year}/
end
end
end
["flickr-download -h", "flickr download -h"].each do |format|
context format do
setup { @stdout, @stderr = bin(format) }
should "match on usage" do
output_matches /SYNOPSIS\n\s*flickr\-download/m
end
end
end
end
end

View File

@@ -0,0 +1,13 @@
class Test::Unit::TestCase
def output_should_match(regexp)
assert_match regexp, @stdout + @stderr
end
alias_method :output_matches, :output_should_match
def stdout_should_match(regexp)
assert_match regexp, @stdout
end
def stderr_should_match(regexp)
assert_match regexp, @stderr
end
end

View File

@@ -0,0 +1,28 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
begin require 'redgreen'; rescue LoadError; end
require 'open3'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
Dir[File.join(File.dirname(__FILE__), "shoulda_macros", "*.rb")].each {|f| require f}
ENV['NO_COLOR'] = "true"
require 'git-style-binary'
GitStyleBinary.run = true
class Test::Unit::TestCase
def fixtures_dir
File.join(File.dirname(__FILE__), "fixtures")
end
end
module RunsBinaryFixtures
# run the specified cmd returning the string values of [stdout,stderr]
def bin(cmd)
stdin, stdout, stderr = Open3.popen3("#{fixtures_dir}/#{cmd}")
[stdout.read, stderr.read]
end
end