Geminstaller C0 Coverage Information - RCov

spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/config_file.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/config_file.rb 346 194
76.88%
63.40%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 #--
2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3 # All rights reserved.
4 # See LICENSE.txt for permissions.
5 #++
6 
7 ##
8 # Gem::ConfigFile RubyGems options and gem command options from ~/.gemrc.
9 #
10 # ~/.gemrc is a YAML file that uses strings to match gem command arguments and
11 # symbols to match RubyGems options.
12 #
13 # Gem command arguments use a String key that matches the command name and
14 # allow you to specify default arguments:
15 #
16 #   install: --no-rdoc --no-ri
17 #   update: --no-rdoc --no-ri
18 #
19 # You can use <tt>gem:</tt> to set default arguments for all commands.
20 #
21 # RubyGems options use symbol keys.  Valid options are:
22 #
23 # +:backtrace+:: See #backtrace
24 # +:benchmark+:: See #benchmark
25 # +:sources+:: Sets Gem::sources
26 # +:verbose+:: See #verbose
27 
28 class Gem::ConfigFile
29 
30   DEFAULT_BACKTRACE = false
31   DEFAULT_BENCHMARK = false
32   DEFAULT_BULK_THRESHOLD = 1000
33   DEFAULT_VERBOSITY = true
34   DEFAULT_UPDATE_SOURCES = true
35 
36   ##
37   # For Ruby packagers to set configuration defaults.  Set in
38   # rubygems/defaults/operating_system.rb
39 
40   OPERATING_SYSTEM_DEFAULTS = {}
41 
42   ##
43   # For Ruby implementers to set configuration defaults.  Set in
44   # rubygems/defaults/#{RUBY_ENGINE}.rb
45 
46   PLATFORM_DEFAULTS = {}
47 
48   system_config_path =
49     begin
50       require 'Win32API'
51 
52       CSIDL_COMMON_APPDATA = 0x0023
53       path = 0.chr * 260
54       if RUBY_VERSION > '1.9' then
55         SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'PLPLP',
56                                        'L', :stdcall
57         SHGetFolderPath.call nil, CSIDL_COMMON_APPDATA, nil, 1, path
58       else
59         SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'LLLLP',
60                                        'L'
61         SHGetFolderPath.call 0, CSIDL_COMMON_APPDATA, 0, 1, path
62       end
63 
64       path.strip
65     rescue LoadError
66       '/etc'
67     end
68 
69   SYSTEM_WIDE_CONFIG_FILE = File.join system_config_path, 'gemrc'
70 
71   ##
72   # List of arguments supplied to the config file object.
73 
74   attr_reader :args
75 
76   ##
77   # Where to look for gems (deprecated)
78 
79   attr_accessor :path
80 
81   ##
82   # Where to install gems (deprecated)
83 
84   attr_accessor :home
85 
86   ##
87   # True if we print backtraces on errors.
88 
89   attr_writer :backtrace
90 
91   ##
92   # True if we are benchmarking this run.
93 
94   attr_accessor :benchmark
95 
96   ##
97   # Bulk threshold value.  If the number of missing gems are above this
98   # threshold value, then a bulk download technique is used.  (deprecated)
99 
100   attr_accessor :bulk_threshold
101 
102   ##
103   # Verbose level of output:
104   # * false -- No output
105   # * true -- Normal output
106   # * :loud -- Extra output
107 
108   attr_accessor :verbose
109 
110   ##
111   # True if we want to update the SourceInfoCache every time, false otherwise
112 
113   attr_accessor :update_sources
114 
115   ##
116   # API key for RubyGems.org
117 
118   attr_reader :rubygems_api_key
119 
120   ##
121   # Create the config file object.  +args+ is the list of arguments
122   # from the command line.
123   #
124   # The following command line options are handled early here rather
125   # than later at the time most command options are processed.
126   #
127   # <tt>--config-file</tt>, <tt>--config-file==NAME</tt>::
128   #   Obviously these need to be handled by the ConfigFile object to ensure we
129   #   get the right config file.
130   #
131   # <tt>--backtrace</tt>::
132   #   Backtrace needs to be turned on early so that errors before normal
133   #   option parsing can be properly handled.
134   #
135   # <tt>--debug</tt>::
136   #   Enable Ruby level debug messages.  Handled early for the same reason as
137   #   --backtrace.
138 
139   def initialize(arg_list)
140     @config_file_name = nil
141     need_config_file_name = false
142 
143     arg_list = arg_list.map do |arg|
144       if need_config_file_name then
145         @config_file_name = arg
146         need_config_file_name = false
147         nil
148       elsif arg =~ /^--config-file=(.*)/ then
149         @config_file_name = $1
150         nil
151       elsif arg =~ /^--config-file$/ then
152         need_config_file_name = true
153         nil
154       else
155         arg
156       end
157     end.compact
158 
159     @backtrace = DEFAULT_BACKTRACE
160     @benchmark = DEFAULT_BENCHMARK
161     @bulk_threshold = DEFAULT_BULK_THRESHOLD
162     @verbose = DEFAULT_VERBOSITY
163     @update_sources = DEFAULT_UPDATE_SOURCES
164 
165     operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
166     platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
167     system_config = load_file SYSTEM_WIDE_CONFIG_FILE
168     user_config = load_file config_file_name.dup.untaint
169 
170     @hash = operating_system_config.merge platform_config
171     @hash = @hash.merge system_config
172     @hash = @hash.merge user_config
173 
174     # HACK these override command-line args, which is bad
175     @backtrace        = @hash[:backtrace]        if @hash.key? :backtrace
176     @benchmark        = @hash[:benchmark]        if @hash.key? :benchmark
177     @bulk_threshold   = @hash[:bulk_threshold]   if @hash.key? :bulk_threshold
178     @home             = @hash[:gemhome]          if @hash.key? :gemhome
179     @path             = @hash[:gempath]          if @hash.key? :gempath
180     @update_sources   = @hash[:update_sources]   if @hash.key? :update_sources
181     @verbose          = @hash[:verbose]          if @hash.key? :verbose
182 
183     load_rubygems_api_key
184 
185     Gem.sources = @hash[:sources] if @hash.key? :sources
186     handle_arguments arg_list
187   end
188 
189   ##
190   # Location of RubyGems.org credentials
191 
192   def credentials_path
193     File.join(Gem.user_home, '.gem', 'credentials')
194   end
195 
196   def load_rubygems_api_key
197     api_key_hash = File.exists?(credentials_path) ? load_file(credentials_path) : @hash
198 
199     @rubygems_api_key = api_key_hash[:rubygems_api_key] if api_key_hash.key? :rubygems_api_key
200   end
201 
202   def rubygems_api_key=(api_key)
203     config = load_file(credentials_path).merge(:rubygems_api_key => api_key)
204 
205     dirname = File.dirname(credentials_path)
206     Dir.mkdir(dirname) unless File.exists?(dirname)
207 
208     require 'yaml'
209 
210     File.open(credentials_path, 'w') do |f|
211       f.write config.to_yaml
212     end
213 
214     @rubygems_api_key = api_key
215   end
216 
217   def load_file(filename)
218     return {} unless filename and File.exists?(filename)
219     begin
220       require 'yaml'
221       YAML.load(File.read(filename))
222     rescue ArgumentError
223       warn "Failed to load #{config_file_name}"
224     rescue Errno::EACCES
225       warn "Failed to load #{config_file_name} due to permissions problem."
226     end or {}
227   end
228 
229   # True if the backtrace option has been specified, or debug is on.
230   def backtrace
231     @backtrace or $DEBUG
232   end
233 
234   # The name of the configuration file.
235   def config_file_name
236     @config_file_name || Gem.config_file
237   end
238 
239   # Delegates to @hash
240   def each(&block)
241     hash = @hash.dup
242     hash.delete :update_sources
243     hash.delete :verbose
244     hash.delete :benchmark
245     hash.delete :backtrace
246     hash.delete :bulk_threshold
247 
248     yield :update_sources, @update_sources
249     yield :verbose, @verbose
250     yield :benchmark, @benchmark
251     yield :backtrace, @backtrace
252     yield :bulk_threshold, @bulk_threshold
253 
254     yield 'config_file_name', @config_file_name if @config_file_name
255 
256     hash.each(&block)
257   end
258 
259   # Handle the command arguments.
260   def handle_arguments(arg_list)
261     @args = []
262 
263     arg_list.each do |arg|
264       case arg
265       when /^--(backtrace|traceback)$/ then
266         @backtrace = true
267       when /^--bench(mark)?$/ then
268         @benchmark = true
269       when /^--debug$/ then
270         $DEBUG = true
271       else
272         @args << arg
273       end
274     end
275   end
276 
277   # Really verbose mode gives you extra output.
278   def really_verbose
279     case verbose
280     when true, false, nil then false
281     else true
282     end
283   end
284 
285   # to_yaml only overwrites things you can't override on the command line.
286   def to_yaml # :nodoc:
287     yaml_hash = {}
288     yaml_hash[:backtrace] = @hash.key?(:backtrace) ? @hash[:backtrace] :
289       DEFAULT_BACKTRACE
290     yaml_hash[:benchmark] = @hash.key?(:benchmark) ? @hash[:benchmark] :
291       DEFAULT_BENCHMARK
292     yaml_hash[:bulk_threshold] = @hash.key?(:bulk_threshold) ?
293       @hash[:bulk_threshold] : DEFAULT_BULK_THRESHOLD
294     yaml_hash[:sources] = Gem.sources
295     yaml_hash[:update_sources] = @hash.key?(:update_sources) ?
296       @hash[:update_sources] : DEFAULT_UPDATE_SOURCES
297     yaml_hash[:verbose] = @hash.key?(:verbose) ? @hash[:verbose] :
298       DEFAULT_VERBOSITY
299 
300     keys = yaml_hash.keys.map { |key| key.to_s }
301     keys << 'debug'
302     re = Regexp.union(*keys)
303 
304     @hash.each do |key, value|
305       key = key.to_s
306       next if key =~ re
307       yaml_hash[key.to_s] = value
308     end
309 
310     yaml_hash.to_yaml
311   end
312 
313   # Writes out this config file, replacing its source.
314   def write
315     require 'yaml'
316     open config_file_name, 'w' do |io|
317       io.write to_yaml
318     end
319   end
320 
321   # Return the configuration information for +key+.
322   def [](key)
323     @hash[key.to_s]
324   end
325 
326   # Set configuration option +key+ to +value+.
327   def []=(key, value)
328     @hash[key.to_s] = value
329   end
330 
331   def ==(other) # :nodoc:
332     self.class === other and
333     @backtrace == other.backtrace and
334     @benchmark == other.benchmark and
335     @bulk_threshold == other.bulk_threshold and
336     @verbose == other.verbose and
337     @update_sources == other.update_sources and
338     @hash == other.hash
339   end
340 
341   protected
342 
343   attr_reader :hash
344 
345 end
346 

Generated on Mon May 10 23:40:28 -0700 2010 with rcov 0.9.8