2011-01-28 18:46:47 -05:00
|
|
|
require 'rubygems/test_case'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/config_file'
|
|
|
|
|
2011-01-28 18:46:47 -05:00
|
|
|
class TestGemConfigFile < Gem::TestCase
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
|
|
|
@temp_conf = File.join @tempdir, '.gemrc'
|
|
|
|
|
|
|
|
@cfg_args = %W[--config-file #{@temp_conf}]
|
2008-06-17 18:04:18 -04:00
|
|
|
|
|
|
|
@orig_SYSTEM_WIDE_CONFIG_FILE = Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE
|
|
|
|
Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
|
|
|
|
Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
|
|
|
|
File.join(@tempdir, 'system-gemrc')
|
2008-06-25 22:06:00 -04:00
|
|
|
Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS.clear
|
|
|
|
Gem::ConfigFile::PLATFORM_DEFAULTS.clear
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
@env_gemrc = ENV['GEMRC']
|
|
|
|
ENV['GEMRC'] = ''
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
util_config_file
|
|
|
|
end
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
def teardown
|
2008-06-25 22:06:00 -04:00
|
|
|
Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS.clear
|
|
|
|
Gem::ConfigFile::PLATFORM_DEFAULTS.clear
|
2008-06-17 18:04:18 -04:00
|
|
|
Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
|
|
|
|
Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
|
|
|
|
@orig_SYSTEM_WIDE_CONFIG_FILE
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
ENV['GEMRC'] = @env_gemrc
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_initialize
|
|
|
|
assert_equal @temp_conf, @cfg.config_file_name
|
|
|
|
|
|
|
|
assert_equal false, @cfg.backtrace
|
|
|
|
assert_equal true, @cfg.update_sources
|
|
|
|
assert_equal Gem::ConfigFile::DEFAULT_BULK_THRESHOLD, @cfg.bulk_threshold
|
|
|
|
assert_equal true, @cfg.verbose
|
2008-06-17 18:04:18 -04:00
|
|
|
assert_equal [@gem_repo], Gem.sources
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":backtrace: true"
|
|
|
|
fp.puts ":update_sources: false"
|
|
|
|
fp.puts ":bulk_threshold: 10"
|
|
|
|
fp.puts ":verbose: false"
|
|
|
|
fp.puts ":sources:"
|
|
|
|
fp.puts " - http://more-gems.example.com"
|
|
|
|
fp.puts "install: --wrappers"
|
2008-09-25 06:13:50 -04:00
|
|
|
fp.puts ":gempath:"
|
|
|
|
fp.puts "- /usr/ruby/1.8/lib/ruby/gems/1.8"
|
|
|
|
fp.puts "- /var/ruby/1.8/gem_home"
|
2012-04-19 19:07:48 -04:00
|
|
|
fp.puts ":ssl_verify_mode: 0"
|
|
|
|
fp.puts ":ssl_ca_cert: /etc/ssl/certs"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal true, @cfg.backtrace
|
|
|
|
assert_equal 10, @cfg.bulk_threshold
|
|
|
|
assert_equal false, @cfg.verbose
|
|
|
|
assert_equal false, @cfg.update_sources
|
|
|
|
assert_equal %w[http://more-gems.example.com], Gem.sources
|
|
|
|
assert_equal '--wrappers', @cfg[:install]
|
2008-09-25 06:13:50 -04:00
|
|
|
assert_equal(['/usr/ruby/1.8/lib/ruby/gems/1.8', '/var/ruby/1.8/gem_home'],
|
|
|
|
@cfg.path)
|
2012-04-19 19:07:48 -04:00
|
|
|
assert_equal 0, @cfg.ssl_verify_mode
|
|
|
|
assert_equal '/etc/ssl/certs', @cfg.ssl_ca_cert
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_handle_arguments_config_file
|
|
|
|
util_config_file %W[--config-file #{@temp_conf}]
|
|
|
|
|
2008-06-19 14:15:26 -04:00
|
|
|
assert_equal @temp_conf, @cfg.config_file_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_handle_arguments_config_file_with_other_params
|
|
|
|
util_config_file %W[--config-file #{@temp_conf} --backtrace]
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
assert_equal @temp_conf, @cfg.config_file_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_handle_arguments_config_file_equals
|
|
|
|
util_config_file %W[--config-file=#{@temp_conf}]
|
|
|
|
|
|
|
|
assert_equal @temp_conf, @cfg.config_file_name
|
|
|
|
end
|
|
|
|
|
2008-06-25 22:06:00 -04:00
|
|
|
def test_initialize_operating_system_override
|
|
|
|
Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS[:bulk_threshold] = 1
|
|
|
|
Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS['install'] = '--no-env-shebang'
|
|
|
|
|
|
|
|
Gem::ConfigFile::PLATFORM_DEFAULTS[:bulk_threshold] = 2
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal 2, @cfg.bulk_threshold
|
|
|
|
assert_equal '--no-env-shebang', @cfg[:install]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_platform_override
|
|
|
|
Gem::ConfigFile::PLATFORM_DEFAULTS[:bulk_threshold] = 2
|
|
|
|
Gem::ConfigFile::PLATFORM_DEFAULTS['install'] = '--no-env-shebang'
|
|
|
|
|
|
|
|
File.open Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE, 'w' do |fp|
|
|
|
|
fp.puts ":bulk_threshold: 3"
|
|
|
|
end
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal 3, @cfg.bulk_threshold
|
|
|
|
assert_equal '--no-env-shebang', @cfg[:install]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize_system_wide_override
|
|
|
|
File.open Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE, 'w' do |fp|
|
|
|
|
fp.puts ":backtrace: false"
|
|
|
|
fp.puts ":bulk_threshold: 2048"
|
|
|
|
end
|
|
|
|
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":backtrace: true"
|
|
|
|
end
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal 2048, @cfg.bulk_threshold
|
|
|
|
assert_equal true, @cfg.backtrace
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_initialize_environment_variable_override
|
|
|
|
File.open Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE, 'w' do |fp|
|
|
|
|
fp.puts ':backtrace: false'
|
|
|
|
fp.puts ':verbose: false'
|
|
|
|
fp.puts ':bulk_threshold: 2048'
|
|
|
|
end
|
|
|
|
|
|
|
|
conf1 = File.join @tempdir, 'gemrc1'
|
|
|
|
File.open conf1, 'w' do |fp|
|
|
|
|
fp.puts ':backtrace: true'
|
|
|
|
end
|
|
|
|
|
|
|
|
conf2 = File.join @tempdir, 'gemrc2'
|
|
|
|
File.open conf2, 'w' do |fp|
|
|
|
|
fp.puts ':verbose: true'
|
|
|
|
end
|
|
|
|
|
|
|
|
conf3 = File.join @tempdir, 'gemrc3'
|
|
|
|
File.open conf3, 'w' do |fp|
|
|
|
|
fp.puts ':verbose: :loud'
|
|
|
|
end
|
|
|
|
|
|
|
|
ENV['GEMRC'] = conf1 + ':' + conf2 + ';' + conf3
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal true, @cfg.backtrace
|
|
|
|
assert_equal :loud, @cfg.verbose
|
|
|
|
assert_equal 2048, @cfg.bulk_threshold
|
|
|
|
end
|
|
|
|
|
2013-02-25 02:30:41 -05:00
|
|
|
def test_api_keys
|
|
|
|
assert_nil @cfg.instance_variable_get :@api_keys
|
|
|
|
|
|
|
|
temp_cred = File.join Gem.user_home, '.gem', 'credentials'
|
|
|
|
FileUtils.mkdir File.dirname(temp_cred)
|
|
|
|
File.open temp_cred, 'w', 0600 do |fp|
|
|
|
|
fp.puts ':rubygems_api_key: 701229f217cdf23b1344c7b4b54ca97'
|
|
|
|
end
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal({:rubygems => '701229f217cdf23b1344c7b4b54ca97'},
|
|
|
|
@cfg.api_keys)
|
|
|
|
end
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
def test_check_credentials_permissions
|
2013-02-07 07:11:19 -05:00
|
|
|
skip 'chmod not supported' if win_platform?
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
@cfg.rubygems_api_key = 'x'
|
|
|
|
|
|
|
|
File.chmod 0644, @cfg.credentials_path
|
|
|
|
|
|
|
|
use_ui @ui do
|
|
|
|
assert_raises Gem::MockGemUi::TermError do
|
|
|
|
@cfg.load_api_keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_empty @ui.output
|
|
|
|
|
|
|
|
expected = <<-EXPECTED
|
|
|
|
ERROR: Your gem push credentials file located at:
|
|
|
|
|
|
|
|
\t#{@cfg.credentials_path}
|
|
|
|
|
|
|
|
has file permissions of 0644 but 0600 is required.
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
To fix this error run:
|
|
|
|
|
|
|
|
\tchmod 0600 #{@cfg.credentials_path}
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
You should reset your credentials at:
|
|
|
|
|
|
|
|
\thttps://rubygems.org/profile/edit
|
|
|
|
|
|
|
|
if you believe they were disclosed to a third party.
|
|
|
|
EXPECTED
|
|
|
|
|
|
|
|
assert_equal expected, @ui.error
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_handle_arguments
|
|
|
|
args = %w[--backtrace --bunch --of --args here]
|
|
|
|
|
|
|
|
@cfg.handle_arguments args
|
|
|
|
|
|
|
|
assert_equal %w[--bunch --of --args here], @cfg.args
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_arguments_backtrace
|
|
|
|
assert_equal false, @cfg.backtrace
|
|
|
|
|
|
|
|
args = %w[--backtrace]
|
|
|
|
|
|
|
|
@cfg.handle_arguments args
|
|
|
|
|
|
|
|
assert_equal true, @cfg.backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_arguments_debug
|
|
|
|
old_dollar_DEBUG = $DEBUG
|
|
|
|
assert_equal false, $DEBUG
|
|
|
|
|
|
|
|
args = %w[--debug]
|
|
|
|
|
|
|
|
@cfg.handle_arguments args
|
|
|
|
|
|
|
|
assert_equal true, $DEBUG
|
|
|
|
ensure
|
|
|
|
$DEBUG = old_dollar_DEBUG
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_arguments_override
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
2012-11-29 01:52:18 -05:00
|
|
|
fp.puts ":backtrace: false"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
util_config_file %W[--backtrace --config-file=#{@temp_conf}]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
assert_equal true, @cfg.backtrace
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_handle_arguments_traceback
|
|
|
|
assert_equal false, @cfg.backtrace
|
|
|
|
|
|
|
|
args = %w[--traceback]
|
|
|
|
|
|
|
|
@cfg.handle_arguments args
|
|
|
|
|
|
|
|
assert_equal true, @cfg.backtrace
|
|
|
|
end
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
def test_load_api_keys
|
|
|
|
temp_cred = File.join Gem.user_home, '.gem', 'credentials'
|
|
|
|
FileUtils.mkdir File.dirname(temp_cred)
|
|
|
|
File.open temp_cred, 'w', 0600 do |fp|
|
|
|
|
fp.puts ":rubygems_api_key: 701229f217cdf23b1344c7b4b54ca97"
|
|
|
|
fp.puts ":other: a5fdbb6ba150cbb83aad2bb2fede64c"
|
|
|
|
end
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
assert_equal({:rubygems => '701229f217cdf23b1344c7b4b54ca97',
|
|
|
|
:other => 'a5fdbb6ba150cbb83aad2bb2fede64c'}, @cfg.api_keys)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_api_keys_bad_permission
|
|
|
|
skip 'chmod not supported' if win_platform?
|
|
|
|
|
|
|
|
@cfg.rubygems_api_key = 'x'
|
|
|
|
|
|
|
|
File.chmod 0644, @cfg.credentials_path
|
|
|
|
|
|
|
|
assert_raises Gem::MockGemUi::TermError do
|
|
|
|
@cfg.load_api_keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_really_verbose
|
|
|
|
assert_equal false, @cfg.really_verbose
|
|
|
|
|
|
|
|
@cfg.verbose = true
|
|
|
|
|
|
|
|
assert_equal false, @cfg.really_verbose
|
|
|
|
|
|
|
|
@cfg.verbose = 1
|
|
|
|
|
|
|
|
assert_equal true, @cfg.really_verbose
|
|
|
|
end
|
|
|
|
|
2013-02-04 21:37:35 -05:00
|
|
|
def test_rubygems_api_key_equals
|
|
|
|
@cfg.rubygems_api_key = 'x'
|
|
|
|
|
|
|
|
assert_equal 'x', @cfg.rubygems_api_key
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
:rubygems_api_key => 'x',
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, YAML.load_file(@cfg.credentials_path)
|
|
|
|
|
|
|
|
unless win_platform? then
|
|
|
|
stat = File.stat @cfg.credentials_path
|
|
|
|
|
|
|
|
assert_equal 0600, stat.mode & 0600
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rubygems_api_key_equals_bad_permission
|
|
|
|
skip 'chmod not supported' if win_platform?
|
|
|
|
|
|
|
|
@cfg.rubygems_api_key = 'x'
|
|
|
|
|
|
|
|
File.chmod 0644, @cfg.credentials_path
|
|
|
|
|
|
|
|
assert_raises Gem::MockGemUi::TermError do
|
|
|
|
@cfg.rubygems_api_key = 'y'
|
|
|
|
end
|
|
|
|
|
|
|
|
expected = {
|
|
|
|
:rubygems_api_key => 'x',
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, YAML.load_file(@cfg.credentials_path)
|
|
|
|
|
|
|
|
stat = File.stat @cfg.credentials_path
|
|
|
|
|
|
|
|
assert_equal 0644, stat.mode & 0644
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def test_write
|
|
|
|
@cfg.backtrace = true
|
|
|
|
@cfg.update_sources = false
|
|
|
|
@cfg.bulk_threshold = 10
|
|
|
|
@cfg.verbose = false
|
|
|
|
Gem.sources.replace %w[http://more-gems.example.com]
|
|
|
|
@cfg[:install] = '--wrappers'
|
|
|
|
|
|
|
|
@cfg.write
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
# These should not be written out to the config file.
|
|
|
|
assert_equal false, @cfg.backtrace, 'backtrace'
|
|
|
|
assert_equal Gem::ConfigFile::DEFAULT_BULK_THRESHOLD, @cfg.bulk_threshold,
|
|
|
|
'bulk_threshold'
|
|
|
|
assert_equal true, @cfg.update_sources, 'update_sources'
|
|
|
|
assert_equal true, @cfg.verbose, 'verbose'
|
|
|
|
|
|
|
|
assert_equal '--wrappers', @cfg[:install], 'install'
|
|
|
|
|
|
|
|
# this should be written out to the config file.
|
|
|
|
assert_equal %w[http://more-gems.example.com], Gem.sources
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_from_hash
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":backtrace: true"
|
|
|
|
fp.puts ":bulk_threshold: 10"
|
|
|
|
fp.puts ":update_sources: false"
|
|
|
|
fp.puts ":verbose: false"
|
|
|
|
fp.puts ":sources:"
|
|
|
|
fp.puts " - http://more-gems.example.com"
|
|
|
|
fp.puts "install: --wrappers"
|
|
|
|
end
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
@cfg.backtrace = :junk
|
|
|
|
@cfg.update_sources = :junk
|
|
|
|
@cfg.bulk_threshold = 20
|
|
|
|
@cfg.verbose = :junk
|
|
|
|
Gem.sources.replace %w[http://even-more-gems.example.com]
|
|
|
|
@cfg[:install] = '--wrappers --no-rdoc'
|
|
|
|
|
|
|
|
@cfg.write
|
|
|
|
|
|
|
|
util_config_file
|
|
|
|
|
|
|
|
# These should not be written out to the config file
|
|
|
|
assert_equal true, @cfg.backtrace, 'backtrace'
|
|
|
|
assert_equal 10, @cfg.bulk_threshold, 'bulk_threshold'
|
|
|
|
assert_equal false, @cfg.update_sources, 'update_sources'
|
|
|
|
assert_equal false, @cfg.verbose, 'verbose'
|
|
|
|
|
|
|
|
assert_equal '--wrappers --no-rdoc', @cfg[:install], 'install'
|
|
|
|
|
|
|
|
assert_equal %w[http://even-more-gems.example.com], Gem.sources
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
def test_ignore_invalid_config_file
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts "some-non-yaml-hash-string"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Avoid writing stuff to output when running tests
|
2012-11-29 03:10:23 -05:00
|
|
|
Gem::ConfigFile.class_eval { def warn(args); end }
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
# This should not raise exception
|
|
|
|
util_config_file
|
|
|
|
end
|
|
|
|
|
2012-04-19 19:07:48 -04:00
|
|
|
def test_load_ssl_verify_mode_from_config
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":ssl_verify_mode: 1"
|
|
|
|
end
|
|
|
|
util_config_file
|
|
|
|
assert_equal(1, @cfg.ssl_verify_mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load_ssl_ca_cert_from_config
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":ssl_ca_cert: /home/me/certs"
|
|
|
|
end
|
|
|
|
util_config_file
|
|
|
|
assert_equal('/home/me/certs', @cfg.ssl_ca_cert)
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def test_load_ssl_client_cert_from_config
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":ssl_client_cert: /home/me/mine.pem"
|
|
|
|
end
|
|
|
|
util_config_file
|
|
|
|
assert_equal('/home/me/mine.pem', @cfg.ssl_client_cert)
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def util_config_file(args = @cfg_args)
|
|
|
|
@cfg = Gem::ConfigFile.new args
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def test_disable_default_gem_server
|
|
|
|
File.open @temp_conf, 'w' do |fp|
|
|
|
|
fp.puts ":disable_default_gem_server: true"
|
|
|
|
end
|
|
|
|
util_config_file
|
|
|
|
assert_equal(true, @cfg.disable_default_gem_server)
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|