Skip to content

Instantly share code, notes, and snippets.

@technicalpickles
Created August 2, 2009 19:08

Revisions

  1. technicalpickles revised this gist Aug 2, 2009. 1 changed file with 51 additions and 0 deletions.
    51 changes: 51 additions & 0 deletions test_install_gem.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    rubyforge_command_context "running" do
    setup do
    stub(@gemspec_helper).gem_path { 'pkg/zomg-1.1.1.gem' }
    stub(@command).sudo_wrapper { 'sudo gem install pkg/zomg-1.1.1.gem' }
    stub(@command).sh

    @command.run
    end

    should "call sudo wrapper with gem install" do
    assert_received(@command) {|command| command.sudo_wrapper('gem install pkg/zomg-1.1.1.gem') }
    end

    should "call sh with output of sudo wrapper" do
    assert_received(@command) {|command| command.sh 'sudo gem install pkg/zomg-1.1.1.gem' }
    end
    end

    rubyforge_command_context "use_sudo?" do
    should "be false on mswin" do
    stub(@command).host_os { "i386-mswin32" }
    assert ! @command.use_sudo?
    end

    should "be false on windows" do
    stub(@command).host_os { "windows" }
    assert ! @command.use_sudo?
    end

    should "be false on cygwin" do
    stub(@command).host_os { "cygwin" }
    assert ! @command.use_sudo?
    end

    should "be true on basically anything else" do
    stub(@command).host_os { "darwin9" }
    assert @command.use_sudo?
    end
    end

    rubyforge_command_context "sudo_wrapper" do
    should "prefix sudo if needed" do
    stub(@command).use_sudo? { true }
    assert_equal "sudo blarg", @command.sudo_wrapper("blarg")
    end

    should "not prefix with sudo if unneeded" do
    stub(@command).use_sudo? { false }
    assert_equal "blarg", @command.sudo_wrapper("blarg")
    end
    end
  2. technicalpickles created this gist Aug 2, 2009.
    27 changes: 27 additions & 0 deletions install_gem.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    def run
    command = "gem install #{gemspec_helper.gem_path}"
    output.puts "Executing #{command.inspect}:"

    sh sudo_wrapper(command) # TODO where does sh actually come from!? - rake, apparently
    end

    def sudo_wrapper(command)
    if use_sudo?
    "sudo #{command}"
    else
    command
    end
    end

    def use_sudo?
    if host_os =~ /mswin|windows|cygwin/i
    false
    else
    true
    end

    end

    def host_os
    Config::CONFIG['host_os']
    end