Best Rr_ruby code snippet using Gem.add_spec
source_index.rb
Source:source_index.rb
...18 # Undef old methods19 alias old_initialize initialize20 undef old_initialize21 %w(all_gems prerelease_gems load_gems_in latest_specs prerelease_specs22 released_specs add_spec add_specs remove_spec each specification23 index_signature gem_signature size length find_name search released_gems24 refresh! outdated == dump gems spec_dirs spec_dirs=).each do |meth|25 undef_method(meth) if method_defined?(meth)26 end27 include Enumerable28 attr_reader :gems # :nodoc:29 ##30 # Directories to use to refresh this SourceIndex when calling refresh!31 attr_accessor :spec_dirs32 class << self33 # Undef old methods34 %w(from_installed_gems installed_spec_directories35 from_gems_in load_specification).each do |meth|36 if instance_methods(true).find {|m| m.to_s == meth }37 undef_method(meth)38 end39 end40 ##41 # Factory method to construct a source index instance for a given42 # path.43 #44 # deprecated::45 # If supplied, from_installed_gems will act just like46 # +from_gems_in+. This argument is deprecated and is provided47 # just for backwards compatibility, and should not generally48 # be used.49 #50 # return::51 # SourceIndex instance52 def from_installed_gems(*deprecated)53 if deprecated.empty?54 from_gems_in(*installed_spec_directories)55 else56 from_gems_in(*deprecated) # HACK warn57 end58 end59 ##60 # Returns a list of directories from Gem.path that contain specifications.61 def installed_spec_directories62 Gem.path.collect { |dir| File.join(dir, "specifications") }63 end64 ##65 # Creates a new SourceIndex from the ruby format gem specifications in66 # +spec_dirs+.67 def from_gems_in(*spec_dirs)68 source_index = new69 source_index.spec_dirs = spec_dirs70 source_index.refresh!71 end72 ##73 # Loads a ruby-format specification from +file_name+ and returns the74 # loaded spec.75 def load_specification(file_name)76 Gem::Specification.load file_name77 end78 end79 ##80 # Constructs a source index instance from the provided specifications, which81 # is a Hash of gem full names and Gem::Specifications.82 #--83 # TODO merge @gems and @prerelease_gems and provide a separate method84 # #prerelease_gems85 def initialize(specifications={})86 @gems = {}87 specifications.each{ |full_name, spec| add_spec spec }88 @spec_dirs = nil89 end90 # TODO: remove method91 def all_gems92 @gems93 end94 def prerelease_gems95 @gems.reject{ |name, gem| !gem.version.prerelease? }96 end97 def released_gems98 @gems.reject{ |name, gem| gem.version.prerelease? }99 end100 ##101 # Reconstruct the source index from the specifications in +spec_dirs+.102 def load_gems_in(*spec_dirs)103 @gems.clear104 spec_dirs.reverse_each do |spec_dir|105 spec_files = Dir.glob File.join(spec_dir, '*.gemspec')106 spec_files.each do |spec_file|107 gemspec = Gem::Specification.load spec_file108 add_spec gemspec if gemspec109 end110 end111 self112 end113 ##114 # Returns an Array specifications for the latest released versions115 # of each gem in this index.116 def latest_specs(include_prerelease=false)117 result = Hash.new { |h,k| h[k] = [] }118 latest = {}119 sort.each do |_, spec|120 name = spec.name121 curr_ver = spec.version122 prev_ver = latest.key?(name) ? latest[name].version : nil123 next if !include_prerelease && curr_ver.prerelease?124 next unless prev_ver.nil? or curr_ver >= prev_ver or125 latest[name].platform != Gem::Platform::RUBY126 if prev_ver.nil? or127 (curr_ver > prev_ver and spec.platform == Gem::Platform::RUBY) then128 result[name].clear129 latest[name] = spec130 end131 if spec.platform != Gem::Platform::RUBY then132 result[name].delete_if do |result_spec|133 result_spec.platform == spec.platform134 end135 end136 result[name] << spec137 end138 # TODO: why is this a hash while @gems is an array? Seems like139 # structural similarity would be good.140 result.values.flatten141 end142 ##143 # An array including only the prerelease gemspecs144 def prerelease_specs145 prerelease_gems.values146 end147 ##148 # An array including only the released gemspecs149 def released_specs150 released_gems.values151 end152 ##153 # Add a gem specification to the source index.154 def add_spec(gem_spec, name = gem_spec.full_name)155 # No idea why, but the Indexer wants to insert them using original_name156 # instead of full_name. So we make it an optional arg.157 @gems[name] = gem_spec158 end159 ##160 # Add gem specifications to the source index.161 def add_specs(*gem_specs)162 gem_specs.each do |spec|163 add_spec spec164 end165 end166 ##167 # Remove a gem specification named +full_name+.168 def remove_spec(full_name)169 @gems.delete full_name170 end171 ##172 # Iterate over the specifications in the source index.173 def each(&block) # :yields: gem.full_name, gem174 @gems.each(&block)175 end176 ##177 # The gem specification given a full gem spec name....
test_gem_source_index.rb
Source:test_gem_source_index.rb
...14 assert_equal [@a2], @source_index.find_name('a', '= 2')15 assert_equal [], @source_index.find_name('bogusstring')16 assert_equal [], @source_index.find_name('a', '= 3')17 source_index = Gem::SourceIndex.new18 source_index.add_spec @a119 source_index.add_spec @a220 assert_equal [@a1], source_index.find_name(@a1.name, '= 1')21 r1 = Gem::Requirement.create '= 1'22 assert_equal [@a1], source_index.find_name(@a1.name, r1)23 end24 end25 def test_find_name_empty_cache26 Deprecate.skip_during do27 empty_source_index = Gem::SourceIndex.new28 assert_equal [], empty_source_index.find_name("foo")29 end30 end31 # HACK: deprecated impl is failing tests, but I may want to port it over32 def test_latest_specs33 Deprecate.skip_during do34 p1_ruby = quick_spec 'p', '1'35 p1_platform = quick_spec 'p', '1' do |spec|36 spec.platform = Gem::Platform::CURRENT37 end38 a1_platform = quick_spec @a1.name, (@a1.version) do |s|39 s.platform = Gem::Platform.new 'x86-my_platform1'40 end41 a2_platform = quick_spec @a2.name, (@a2.version) do |s|42 s.platform = Gem::Platform.new 'x86-my_platform1'43 end44 a2_platform_other = quick_spec @a2.name, (@a2.version) do |s|45 s.platform = Gem::Platform.new 'x86-other_platform1'46 end47 a3_platform_other = quick_spec @a2.name, (@a2.version.bump) do |s|48 s.platform = Gem::Platform.new 'x86-other_platform1'49 end50 @source_index.add_spec p1_ruby51 @source_index.add_spec p1_platform52 @source_index.add_spec a1_platform53 @source_index.add_spec a2_platform54 @source_index.add_spec a2_platform_other55 @source_index.add_spec a3_platform_other56 expected = [57 @a2.full_name,58 a2_platform.full_name,59 a3_platform_other.full_name,60 @b2.full_name,61 @c1_2.full_name,62 @a_evil9.full_name,63 p1_ruby.full_name,64 p1_platform.full_name,65 @pl1.full_name66 ].sort67 latest_specs = @source_index.latest_specs.map { |s| s.full_name }.sort68 assert_equal expected, latest_specs69 end70 end71 def test_load_gems_in72 Deprecate.skip_during do73 spec_dir1 = File.join @gemhome, 'specifications'74 spec_dir2 = File.join @tempdir, 'gemhome2', 'specifications'75 FileUtils.rm_r spec_dir176 FileUtils.mkdir_p spec_dir177 FileUtils.mkdir_p spec_dir278 a1 = quick_spec 'a', '1' do |spec| spec.author = 'author 1' end79 a2 = quick_spec 'a', '1' do |spec| spec.author = 'author 2' end80 path1 = File.join(spec_dir1, a1.spec_name)81 path2 = File.join(spec_dir2, a2.spec_name)82 File.open path1, 'w' do |fp|83 fp.write a1.to_ruby84 end85 File.open path2, 'w' do |fp|86 fp.write a2.to_ruby87 end88 @source_index.load_gems_in File.dirname(path1), File.dirname(path2)89 assert_equal a1.author, @source_index.specification(a1.full_name).author90 end91 end92 # REFACTOR: move to test_gem_commands_outdated_command.rb93 def test_outdated94 Deprecate.skip_during do95 util_setup_spec_fetcher96 assert_equal [], @source_index.outdated97 updated = quick_spec @a2.name, (@a2.version.bump)98 util_setup_spec_fetcher updated99 assert_equal [updated.name], @source_index.outdated100 updated_platform = quick_spec @a2.name, (updated.version.bump) do |s|101 s.platform = Gem::Platform.new 'x86-other_platform1'102 end103 util_setup_spec_fetcher updated, updated_platform104 assert_equal [updated_platform.name], @source_index.outdated105 end106 end107 def test_prerelease_specs_kept_in_right_place108 Deprecate.skip_during do109 gem_a1_alpha = quick_spec 'abba', '1.a'110 @source_index.add_spec gem_a1_alpha111 refute_includes @source_index.latest_specs, gem_a1_alpha112 assert_includes @source_index.latest_specs(true), gem_a1_alpha113 assert_empty @source_index.find_name gem_a1_alpha.full_name114 assert_includes @source_index.prerelease_specs, gem_a1_alpha115 end116 end117 def test_refresh_bang118 Deprecate.skip_during do119 a1_spec = File.join @gemhome, "specifications", @a1.spec_name120 FileUtils.mv a1_spec, @tempdir121 Gem::Specification.reset122 Gem.send :class_variable_set, :@@source_index, nil123 source_index = Gem.source_index124 refute_includes source_index.gems.keys.sort, @a1.full_name125 FileUtils.mv File.join(@tempdir, @a1.spec_name), a1_spec126 source_index.refresh!127 assert source_index.gems.include?(@a1.full_name)128 end129 end130 def test_remove_spec131 Deprecate.skip_during do132 si = Gem.source_index133 expected = si.gems.keys.sort134 expected.delete "a-1"135 @source_index.remove_spec 'a-1'136 assert_equal expected, si.gems.keys.sort137 expected.delete "a-3.a"138 @source_index.remove_spec 'a-3.a'139 assert_equal expected, si.gems.keys.sort140 end141 end142 def test_search143 Deprecate.skip_during do144 requirement = Gem::Requirement.create '= 9'145 with_version = Gem::Dependency.new(/^a/, requirement)146 assert_equal [@a_evil9], @source_index.search(with_version)147 with_default = Gem::Dependency.new(/^a/, Gem::Requirement.default)148 assert_equal [@a1, @a2, @a3a, @a_evil9], @source_index.search(with_default)149 c1_1_dep = Gem::Dependency.new 'c', '~> 1.1'150 assert_equal [@c1_2], @source_index.search(c1_1_dep)151 end152 end153 def test_search_platform154 Deprecate.skip_during do155 util_set_arch 'x86-my_platform1'156 a1 = quick_spec 'a', '1'157 a1_mine = quick_spec 'a', '1' do |s|158 s.platform = Gem::Platform.new 'x86-my_platform1'159 end160 a1_other = quick_spec 'a', '1' do |s|161 s.platform = Gem::Platform.new 'x86-other_platform1'162 end163 si = Gem::SourceIndex.new164 si.add_specs a1, a1_mine, a1_other165 dep = Gem::Dependency.new 'a', Gem::Requirement.new('1')166 gems = si.search dep, true167 assert_equal [a1, a1_mine], gems.sort168 end169 end170 def test_signature171 Deprecate.skip_during do172 sig = @source_index.gem_signature('foo-1.2.3')173 assert_equal 64, sig.length174 assert_match(/^[a-f0-9]{64}$/, sig)175 end176 end177 def test_specification178 Deprecate.skip_during do...
add_spec
Using AI Code Generation
1 f.puts Gem::Specification.map { |s| s.name }2 f.puts Gem::Specification.map { |s| s.name }3 f.puts Gem::Specification.map { |s| s.name }4 f.puts Gem::Specification.map { |s| s.name }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!