<commit_before>dir = File.dirname(__FILE__)
require File.expand_path("#{dir}/../spec_helper")
require 'rubygems'
require 'yaml'
require 'rubygems/remote_installer'
require 'rubygems/installer'

module Gem
  class StreamUI
    class << self
      attr_accessor :outs
    end
    def outs
      self.class.outs
    end
  end
end


context "rubygems api" do
  setup do
  end

  specify "should work as we expect it to" do
    # NOTE: this test is dependent upon RubyGems being installed, and write permissions (or sudo) to gem install dir
    sample_gem = "ruby-doom"
    # setup to make sure gem is not installed before test
    # TODO: how to prevent error from being written to console?

    Gem.manage_gems

    Gem::GemRunner.new.run(['install',"#{sample_gem}"])

    # check gem lst to make sure gem is installed
    buff = ""

    def buff.write(str)
      self << str
    end

    outs = Gem::StreamUI.outs
    original_outs = outs

    outs = buff

    # gem list only writes to stdout, so we must capture it
    Gem::GemRunner.new.run(['list'])

    # redirect stdout back to original stdout
    Gem::StreamUI.outs = original_outs

    # write contents of buff to stdout
    gem_list_output = buff


    p "gem_list_output = #{gem_list_output.inspect}"
    #gem_list_output.should_include("#{sample_gem}")

    # uninstall it again after we are done
    gem_list = Gem::GemRunner.new.run(['uninstall',"#{sample_gem}"])
  end

  def execute(cmd)
    exec = open("|" + cmd)
    retbuf = exec.read
    exec.close
    return retbuf
  end

end<commit_msg>working proof-of-concept for using Gem api<commit_after>dir = File.dirname(__FILE__)
require File.expand_path("#{dir}/../spec_helper")
require 'rubygems'
require 'yaml'
require 'rubygems/doc_manager'
require 'rubygems/config_file'
require 'rubygems/cmd_manager'
require 'rubygems/gem_runner'
require 'rubygems/remote_installer'
require 'rubygems/installer'

context "rubygems api" do
  setup do
  end

  specify "should work as we expect it to" do
    # NOTE: this test is dependent upon RubyGems being installed, and write permissions (or sudo) to gem install dir
    sample_gem = "ruby-doom"
    # setup to make sure gem is not installed before test
    # TODO: how to prevent error from being written to console?
    
    if (is_gem_installed(sample_gem)) then
      uninstall_gem(sample_gem)
    end
    
    is_gem_installed(sample_gem).should_equal(false)

    Gem.manage_gems

    #Gem::GemRunner.new.run(['install',"#{sample_gem}"])
    install_gem(sample_gem)

    #gem_list_output.should_include("#{sample_gem}")
    is_gem_installed(sample_gem).should_equal(true)

    # uninstall it again after we are done
    #gem_list = Gem::GemRunner.new.run(['uninstall',"#{sample_gem}"])
    uninstall_gem(sample_gem)
    is_gem_installed(sample_gem).should_equal(false)
  end
  
  def is_gem_installed(gem_name)
    gems = Gem::cache.refresh!
    gems = Gem::cache.search(/.*#{gem_name}$/)
    gems.each do |gem|
      return true if gem.name == gem_name
    end
    return false
  end

  def uninstall_gem(gem_name)
    run_gem_command('uninstall',gem_name)
  end

  def install_gem(gem_name)
    run_gem_command('install',gem_name)
  end

  def run_gem_command(gem_command,gem_name)
    Gem::GemRunner.new.run([gem_command,"#{gem_name}"])
  end
end
{'fraction_non_alphanumeric': 0.10184345723783621, 'fraction_numerical': 0.0, 'mean_word_length': 3.622905027932961, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7240404', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1088, 'n_words': 294}