Skip to content

Commit

Permalink
Add Gem::ConfigFile constants for packagers and implementors to overr…
Browse files Browse the repository at this point in the history
…ide.

git-svn-id: http://rubygems.rubyforge.org/svn/trunk@1822 3d4018f9-ac1a-0410-99e9-8a154d859a19
  • Loading branch information
drbrain committed Jun 25, 2008
1 parent df3bb81 commit 9d9b598
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,4 +1,9 @@
# -*- coding: utf-8 -*-
2008-06-25 Eric Hodel <drbrain@segment7.net>

* lib/rubygems/config_file.rb: Add Gem::ConfigFile constants for
packagers and implementors to override defaults.

2008-06-24 Eric Hodel <drbrain@segment7.net>

* lib/rubygems/remote_fetcher.rb: Cleanup to support
Expand Down
22 changes: 20 additions & 2 deletions lib/rubygems/config_file.rb
Expand Up @@ -18,6 +18,18 @@ class Gem::ConfigFile
DEFAULT_VERBOSITY = true
DEFAULT_UPDATE_SOURCES = true

##
# For Ruby packagers to set configuration defaults. Set in
# rubygems/defaults/operating_system.rb

OPERATING_SYSTEM_DEFAULTS = {}

##
# For Ruby implementers to set configuration defaults. Set in
# rubygems/defaults/#{RUBY_ENGINE}.rb

PLATFORM_DEFAULTS = {}

system_config_path =
begin
require 'Win32API'
Expand Down Expand Up @@ -98,8 +110,14 @@ def initialize(arg_list)
@verbose = DEFAULT_VERBOSITY
@update_sources = DEFAULT_UPDATE_SOURCES

@hash = load_file(SYSTEM_WIDE_CONFIG_FILE)
@hash.merge!(load_file(config_file_name.dup.untaint))
operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
system_config = load_file SYSTEM_WIDE_CONFIG_FILE
user_config = load_file config_file_name.dup.untaint

@hash = operating_system_config.merge platform_config
@hash = @hash.merge system_config
@hash = @hash.merge user_config

# HACK these override command-line args, which is bad
@backtrace = @hash[:backtrace] if @hash.key? :backtrace
Expand Down
63 changes: 46 additions & 17 deletions test/test_gem_config_file.rb
Expand Up @@ -22,11 +22,15 @@ def setup
Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
File.join(@tempdir, 'system-gemrc')
Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS.clear
Gem::ConfigFile::PLATFORM_DEFAULTS.clear

util_config_file
end

def teardown
Gem::ConfigFile::OPERATING_SYSTEM_DEFAULTS.clear
Gem::ConfigFile::PLATFORM_DEFAULTS.clear
Gem::ConfigFile.send :remove_const, :SYSTEM_WIDE_CONFIG_FILE
Gem::ConfigFile.send :const_set, :SYSTEM_WIDE_CONFIG_FILE,
@orig_SYSTEM_WIDE_CONFIG_FILE
Expand Down Expand Up @@ -84,6 +88,48 @@ def test_initialize_handle_arguments_config_file_equals
assert_equal @temp_conf, @cfg.config_file_name
end

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

def test_handle_arguments
args = %w[--backtrace --bunch --of --args here]

Expand Down Expand Up @@ -222,23 +268,6 @@ def test_write_from_hash
assert_equal %w[http://even-more-gems.example.com], Gem.sources
end

def test_global_config_file
File.open(@temp_conf, 'w') do |fp|
fp.puts ":backtrace: true"
end

File.open(File.join(Gem::ConfigFile::SYSTEM_WIDE_CONFIG_FILE),
'w') do |fp|
fp.puts ":backtrace: false"
fp.puts ":bulk_threshold: 2048"
end

util_config_file

assert_equal 2048, @cfg.bulk_threshold
assert @cfg.backtrace
end

def util_config_file(args = @cfg_args)
@cfg = Gem::ConfigFile.new args
end
Expand Down

0 comments on commit 9d9b598

Please sign in to comment.