diff --git a/lib/hub/commands.rb b/lib/hub/commands.rb index 52a4ec04ff50a57776b85dfd1389b2c99353d877..3eacedc017fac7c98920df8df6a5f51053ecffad 100644 --- a/lib/hub/commands.rb +++ b/lib/hub/commands.rb @@ -133,6 +133,15 @@ module Hub end alias_method "--version", :version + # $ hub install standalone ~/bin + def install(args) + command, subcommand, target = args + if subcommand.to_s == 'standalone' + Standalone.save('hub', target.empty? ? '.' : target) + exit + end + end + # $ hub help # (print improved help text) def help(args) diff --git a/lib/hub/standalone.rb b/lib/hub/standalone.rb index 118b48164efe4e69bf6e0fd96cdaa32984aae173..e6ae2cf5e7c06a918b509c908231a07f6e01d69e 100644 --- a/lib/hub/standalone.rb +++ b/lib/hub/standalone.rb @@ -18,6 +18,15 @@ premable POSTAMBLE = "Hub::Runner.execute(*ARGV)" + def save(filename, path) + target = File.join(File.expand_path(path), filename) + File.open(target, 'w') do |f| + f.puts build + end + rescue Errno::EACCES, Errno::ENOENT + puts "** can't write to #{target}" + end + def build root = File.dirname(__FILE__) diff --git a/test/standalone_test.rb b/test/standalone_test.rb index e46c81cf9c978a05f8a6c4ad5b6bdcdc9f3e32bd..7e7f509177249433d5171573d81b8e3a57950fa3 100644 --- a/test/standalone_test.rb +++ b/test/standalone_test.rb @@ -1,7 +1,22 @@ $LOAD_PATH.unshift File.dirname(__FILE__) require 'helper' +require 'fileutils' class StandaloneTest < Test::Unit::TestCase + include FileUtils + + def setup + rm "hub" if File.exists? 'hub' + rm_rf "/tmp/_hub_private" if File.exists? '/tmp/_hub_private' + mkdir "/tmp/_hub_private" + chmod 0400, "/tmp/_hub_private" + end + + def teardown + rm "hub" if File.exists? 'hub' + rm_rf "/tmp/_hub_private" if File.exists? "/tmp/_hub_private" + end + def test_standalone standalone = Hub::Standalone.build assert_includes "This file, hub, is generated code", standalone @@ -11,4 +26,19 @@ class StandaloneTest < Test::Unit::TestCase assert_includes ".execute(*ARGV)", standalone assert_not_includes "module Standalone", standalone end + + def test_standalone_save + hub("install standalone .") + assert_equal Hub::Standalone.build + "\n", File.read('./hub') + end + + def test_standalone_save_permission_denied + out = hub("install standalone /tmp/_hub_private") + assert_equal "** can't write to /tmp/_hub_private/hub\n", out + end + + def test_standalone_save_doesnt_exist + out = hub("install standalone /tmp/something/not/real") + assert_equal "** can't write to /tmp/something/not/real/hub\n", out + end end