2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
9 require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
10 require 'rubygems/specification'
12 class TestGemSpecification < RubyGemTestCase
14 LEGACY_YAML_SPEC = <<-EOF
15 --- !ruby/object:Gem::Specification
16 rubygems_version: "1.0"
18 version: !ruby/object:Gem::Version
20 date: 2004-03-28 15:37:49.828000 +02:00
22 summary: A Hash which automatically computes keys.
27 autorequire: keyedlist
33 LEGACY_RUBY_SPEC = <<-EOF
34 Gem::Specification.new do |s|
35 s.name = %q{keyedlist}
38 s.summary = %q{A Hash which automatically computes keys.}
39 s.files = ["lib/keyedlist.rb"]
40 s.require_paths = ["lib"]
41 s.autorequire = %q{keyedlist}
42 s.author = %q{Florian Gross}
43 s.email = %q{flgr@ccan.de}
50 @a1 = quick_gem 'a', '1' do |s|
52 s.extensions << 'ext/a/extconf.rb'
54 s.test_file = 'test/suite.rb'
55 s.requirements << 'A working computer'
56 s.rubyforge_project = 'example'
58 s.add_dependency 'rake', '> 0.4'
59 s.add_dependency 'jabber4r', '> 0.0.0'
60 s.add_dependency 'pqa', ['> 0.4', '<= 0.6']
63 s.files = %w[lib/code.rb]
66 @a2 = quick_gem 'a', '2' do |s|
67 s.files = %w[lib/code.rb]
70 FileUtils.mkdir_p File.join(@tempdir, 'bin')
71 File.open File.join(@tempdir, 'bin', 'exec'), 'w' do |fp|
72 fp.puts "#!#{Gem.ruby}"
76 def test_self_attribute_names
99 required_rubygems_version
104 specification_version
110 actual_value = Gem::Specification.attribute_names.map { |a| a.to_s }.sort
112 assert_equal expected_value, actual_value
116 spec = File.join @gemhome, 'specifications', "#{@a2.full_name}.gemspec"
117 gs = Gem::Specification.load spec
122 def test_self_load_legacy_ruby
123 spec = eval LEGACY_RUBY_SPEC
124 assert_equal 'keyedlist', spec.name
125 assert_equal '0.4.0', spec.version.to_s
126 assert_equal true, spec.has_rdoc?
127 assert_equal Gem::Specification::TODAY, spec.date
128 assert spec.required_ruby_version.satisfied_by?(Gem::Version.new('1'))
129 assert_equal false, spec.has_unit_tests?
132 def test_self_load_legacy_yaml
133 s = YAML.load StringIO.new(LEGACY_YAML_SPEC)
134 assert_equal 'keyedlist', s.name
135 assert_equal '0.4.0', s.version.to_s
136 assert_equal true, s.has_rdoc?
137 #assert_equal Date.today, s.date
138 #assert s.required_ruby_version.satisfied_by?(Gem::Version.new('1'))
139 assert_equal false, s.has_unit_tests?
142 def test_self_normalize_yaml_input_with_183_yaml
143 input = "!ruby/object:Gem::Specification "
144 assert_equal "--- #{input}", Gem::Specification.normalize_yaml_input(input)
147 def test_self_normalize_yaml_input_with_non_183_yaml
148 input = "--- !ruby/object:Gem::Specification "
149 assert_equal input, Gem::Specification.normalize_yaml_input(input)
152 def test_self_normalize_yaml_input_with_183_io
153 input = "!ruby/object:Gem::Specification "
154 assert_equal "--- #{input}",
155 Gem::Specification.normalize_yaml_input(StringIO.new(input))
158 def test_self_normalize_yaml_input_with_non_183_io
159 input = "--- !ruby/object:Gem::Specification "
161 Gem::Specification.normalize_yaml_input(StringIO.new(input))
165 spec = Gem::Specification.new do |s|
170 assert_equal "blah", spec.name
171 assert_equal "1.3.5", spec.version.to_s
172 assert_equal Gem::Platform::RUBY, spec.platform
173 assert_equal nil, spec.summary
174 assert_equal [], spec.files
176 assert_equal [], spec.test_files
177 assert_equal [], spec.rdoc_options
178 assert_equal [], spec.extra_rdoc_files
179 assert_equal [], spec.executables
180 assert_equal [], spec.extensions
181 assert_equal [], spec.requirements
182 assert_equal [], spec.dependencies
183 assert_equal 'bin', spec.bindir
184 assert_equal false, spec.has_rdoc
185 assert_equal false, spec.has_rdoc?
186 assert_equal '>= 0', spec.required_ruby_version.to_s
187 assert_equal '>= 0', spec.required_rubygems_version.to_s
190 def test_initialize_future
191 version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + 1
192 spec = Gem::Specification.new do |s|
196 s.specification_version = version
198 s.new_unknown_attribute = "a value"
201 assert_equal "blah", spec.name
202 assert_equal "1.3.5", spec.version.to_s
206 @a2.platform = Gem::Platform.local
207 @a2.instance_variable_set :@original_platform, 'old_platform'
209 data = Marshal.dump @a2
211 same_spec = Marshal.load data
213 assert_equal 'old_platform', same_spec.original_platform
217 assert_equal 'A User', @a1.author
221 assert_equal ['A User'], @a1.authors
224 def test_bindir_equals
227 assert_equal 'apps', @a1.bindir
230 def test_bindir_equals_nil
232 @a2.executable = 'app'
234 assert_equal nil, @a2.bindir
235 assert_equal %w[lib/code.rb app], @a2.files
239 assert_equal Gem::Specification::TODAY, @a1.date
242 def test_date_equals_date
243 @a1.date = Date.new(2003, 9, 17)
244 assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
247 def test_date_equals_string
248 @a1.date = '2003-09-17'
249 assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
252 def test_date_equals_time
253 @a1.date = Time.local(2003, 9, 17, 0,0,0)
254 assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
257 def test_date_equals_time_local
259 @a1.date = Time.local(2003, 9, 17, 19,50,0)
260 assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
263 def test_date_equals_time_utc
265 @a1.date = Time.local(2003, 9, 17, 19,50,0)
266 assert_equal Time.local(2003, 9, 17, 0,0,0), @a1.date
269 def test_default_executable
270 assert_equal 'exec', @a1.default_executable
272 @a1.default_executable = nil
273 @a1.instance_variable_set :@executables, nil
274 assert_equal nil, @a1.default_executable
277 def test_dependencies
278 rake = Gem::Dependency.new 'rake', '> 0.4'
279 jabber = Gem::Dependency.new 'jabber4r', '> 0.0.0'
280 pqa = Gem::Dependency.new 'pqa', ['> 0.4', '<= 0.6']
282 assert_equal [rake, jabber, pqa], @a1.dependencies
286 assert_equal 'This is a test description', @a1.description
294 assert_equal g1.hash, g2.hash
295 assert_equal true, g1.eql?(g2)
299 assert_equal @a1, @a1
300 assert_equal @a1, @a1.dup
301 assert_not_equal @a1, @a2
302 assert_not_equal @a1, Object.new
305 # The cgikit specification was reported to be causing trouble in at least
306 # one version of RubyGems, so we test explicitly for it.
307 def test_equals2_cgikit
308 cgikit = Gem::Specification.new do |s|
311 s.date = %q{2004-03-13}
312 s.summary = %q{CGIKit is a componented-oriented web application } +
313 %q{framework like Apple Computers WebObjects. } +
314 %{This framework services Model-View-Controller architecture } +
315 %q{programming by components based on a HTML file, a definition } +
316 %q{file and a Ruby source. }
317 s.email = %q{info@spice-of-life.net}
318 s.homepage = %q{http://www.spice-of-life.net/download/cgikit/}
319 s.autorequire = %q{cgikit}
322 s.required_ruby_version = nil
324 s.files = ["lib/cgikit", "lib/cgikit.rb", "lib/cgikit/components", "..."]
327 assert_equal cgikit, cgikit
330 def test_equals2_default_executable
332 spec.default_executable = 'xx'
334 assert_not_equal @a1, spec
335 assert_not_equal spec, @a1
338 def test_equals2_extensions
340 spec.extensions = 'xx'
342 assert_not_equal @a1, spec
343 assert_not_equal spec, @a1
347 @a1.executable = 'app'
348 assert_equal %w[app], @a1.executables
351 def test_executable_equals
352 @a2.executable = 'app'
353 assert_equal 'app', @a2.executable
354 assert_equal %w[lib/code.rb bin/app], @a2.files
358 assert_equal ['ext/a/extconf.rb'], @a1.extensions
362 @a1.files = %w(files bin/common)
363 @a1.test_files = %w(test_files bin/common)
364 @a1.executables = %w(executables common)
365 @a1.extra_rdoc_files = %w(extra_rdoc_files bin/common)
366 @a1.extensions = %w(extensions bin/common)
376 assert_equal expected, @a1.files.sort
379 def test_files_duplicate
380 @a2.files = %w[a b c d b]
381 @a2.extra_rdoc_files = %w[x y z x]
384 assert_equal %w[a b c d x y z], @a2.files
385 assert_equal %w[x y z], @a2.extra_rdoc_files
388 def test_files_extra_rdoc_files
389 @a2.files = %w[a b c d]
390 @a2.extra_rdoc_files = %w[x y z]
392 assert_equal %w[a b c d x y z], @a2.files
395 def test_files_non_array
397 @a1.test_files = "TF"
398 @a1.executables = "X"
399 @a1.extra_rdoc_files = "ERF"
402 assert_equal %w[E ERF F TF bin/X], @a1.files.sort
405 def test_files_non_array_pathological
406 @a1.instance_variable_set :@files, "F"
407 @a1.instance_variable_set :@test_files, "TF"
408 @a1.instance_variable_set :@extra_rdoc_files, "ERF"
409 @a1.instance_variable_set :@extensions, "E"
410 @a1.instance_variable_set :@executables, "X"
412 assert_equal %w[E ERF F TF bin/X], @a1.files.sort
413 assert_kind_of Integer, @a1.hash
416 def test_full_gem_path
417 assert_equal File.join(@gemhome, 'gems', @a1.full_name),
420 @a1.original_platform = 'mswin32'
422 assert_equal File.join(@gemhome, 'gems', @a1.original_name),
427 assert_equal 'a-1', @a1.full_name
429 @a1.platform = Gem::Platform.new ['universal', 'darwin', nil]
430 assert_equal 'a-1-universal-darwin', @a1.full_name
432 @a1.instance_variable_set :@new_platform, 'mswin32'
433 assert_equal 'a-1-mswin32', @a1.full_name, 'legacy'
435 return if win_platform?
437 @a1.platform = 'current'
438 assert_equal 'a-1-x86-darwin-8', @a1.full_name
441 def test_full_name_windows
443 'i386-mswin32' => 'a-1-x86-mswin32-60',
444 'i386-mswin32_80' => 'a-1-x86-mswin32-80',
445 'i386-mingw32' => 'a-1-x86-mingw32'
448 test_cases.each do |arch, expected|
450 @a1.platform = 'current'
451 assert_equal expected, @a1.full_name
456 assert_equal true, @a1.has_rdoc?
460 assert_equal @a1.hash, @a1.hash
461 assert_equal @a1.hash, @a1.dup.hash
462 assert_not_equal @a1.hash, @a2.hash
466 @a1.files = %w[lib/foo.rb Rakefile]
468 assert_equal %w[lib/foo.rb], @a1.lib_files
472 assert_equal 'a', @a1.name
475 def test_original_name
476 assert_equal 'a-1', @a1.full_name
478 @a1.platform = 'i386-linux'
479 @a1.instance_variable_set :@original_platform, 'i386-linux'
480 assert_equal 'a-1-i386-linux', @a1.original_name
484 assert_equal Gem::Platform::RUBY, @a1.platform
487 def test_platform_equals
489 assert_equal Gem::Platform::RUBY, @a1.platform
491 @a1.platform = Gem::Platform::RUBY
492 assert_equal Gem::Platform::RUBY, @a1.platform
495 'i386-mswin32' => ['x86', 'mswin32', '60'],
496 'i386-mswin32_80' => ['x86', 'mswin32', '80'],
497 'i386-mingw32' => ['x86', 'mingw32', nil ],
498 'x86-darwin8' => ['x86', 'darwin', '8' ],
501 test_cases.each do |arch, expected|
503 @a1.platform = Gem::Platform::CURRENT
504 assert_equal Gem::Platform.new(expected), @a1.platform
508 def test_platform_equals_current
509 @a1.platform = Gem::Platform::CURRENT
510 assert_equal Gem::Platform.local, @a1.platform
511 assert_equal Gem::Platform.local.to_s, @a1.original_platform
514 def test_platform_equals_legacy
515 @a1.platform = 'mswin32'
516 assert_equal Gem::Platform.new('x86-mswin32'), @a1.platform
518 @a1.platform = 'i586-linux'
519 assert_equal Gem::Platform.new('x86-linux'), @a1.platform
521 @a1.platform = 'powerpc-darwin'
522 assert_equal Gem::Platform.new('ppc-darwin'), @a1.platform
525 def test_require_paths
526 @a1.require_path = 'lib'
527 assert_equal %w[lib], @a1.require_paths
530 def test_requirements
531 assert_equal ['A working computer'], @a1.requirements
534 def test_spaceship_name
535 s1 = quick_gem 'a', '1'
536 s2 = quick_gem 'b', '1'
538 assert_equal(-1, (s1 <=> s2))
539 assert_equal( 0, (s1 <=> s1))
540 assert_equal( 1, (s2 <=> s1))
543 def test_spaceship_platform
544 s1 = quick_gem 'a', '1'
545 s2 = quick_gem 'a', '1' do |s|
546 s.platform = Gem::Platform.new 'x86-my_platform1'
549 assert_equal( -1, (s1 <=> s2))
550 assert_equal( 0, (s1 <=> s1))
551 assert_equal( 1, (s2 <=> s1))
554 def test_spaceship_version
555 s1 = quick_gem 'a', '1'
556 s2 = quick_gem 'a', '2'
558 assert_equal( -1, (s1 <=> s2))
559 assert_equal( 0, (s1 <=> s1))
560 assert_equal( 1, (s2 <=> s1))
564 assert_equal 'this is a summary', @a1.summary
568 @a1.test_file = 'test/suite.rb'
569 assert_equal ['test/suite.rb'], @a1.test_files
573 @a2.required_rubygems_version = Gem::Requirement.new '> 0'
575 ruby_code = @a2.to_ruby
577 expected = "Gem::Specification.new do |s|
581 s.specification_version = #{Gem::Specification::CURRENT_SPECIFICATION_VERSION} if s.respond_to? :specification_version=
583 s.required_rubygems_version = Gem::Requirement.new(\"> 0\") if s.respond_to? :required_rubygems_version=
584 s.authors = [\"A User\"]
585 s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
586 s.description = %q{This is a test description}
587 s.email = %q{example@example.com}
588 s.files = [\"lib/code.rb\"]
590 s.homepage = %q{http://example.com}
591 s.require_paths = [\"lib\"]
592 s.rubygems_version = %q{#{Gem::RubyGemsVersion}}
593 s.summary = %q{this is a summary}
597 assert_equal expected, ruby_code
599 same_spec = eval ruby_code
601 assert_equal @a2, same_spec
604 def test_to_ruby_fancy
605 @a1.platform = Gem::Platform.local
606 ruby_code = @a1.to_ruby
608 local = Gem::Platform.local
609 expected_platform = "[#{local.cpu.inspect}, #{local.os.inspect}, #{local.version.inspect}]"
611 expected = "Gem::Specification.new do |s|
614 s.platform = Gem::Platform.new(#{expected_platform})
616 s.specification_version = 2 if s.respond_to? :specification_version=
618 s.required_rubygems_version = Gem::Requirement.new(\">= 0\") if s.respond_to? :required_rubygems_version=
619 s.authors = [\"A User\"]
620 s.date = %q{#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}}
621 s.default_executable = %q{exec}
622 s.description = %q{This is a test description}
623 s.email = %q{example@example.com}
624 s.executables = [\"exec\"]
625 s.extensions = [\"ext/a/extconf.rb\"]
626 s.files = [\"lib/code.rb\", \"test/suite.rb\", \"bin/exec\", \"ext/a/extconf.rb\"]
627 s.has_rdoc = %q{true}
628 s.homepage = %q{http://example.com}
629 s.require_paths = [\"lib\"]
630 s.requirements = [\"A working computer\"]
631 s.rubyforge_project = %q{example}
632 s.rubygems_version = %q{#{Gem::RubyGemsVersion}}
633 s.summary = %q{this is a summary}
634 s.test_files = [\"test/suite.rb\"]
636 s.add_dependency(%q<rake>, [\"> 0.4\"])
637 s.add_dependency(%q<jabber4r>, [\"> 0.0.0\"])
638 s.add_dependency(%q<pqa>, [\"> 0.4\", \"<= 0.6\"])
642 assert_equal expected, ruby_code
644 same_spec = eval ruby_code
646 assert_equal @a1, same_spec
649 def test_to_ruby_legacy
650 gemspec1 = eval LEGACY_RUBY_SPEC
651 ruby_code = gemspec1.to_ruby
652 gemspec2 = eval ruby_code
654 assert_equal gemspec1, gemspec2
657 def test_to_ruby_platform
658 @a2.platform = Gem::Platform.local
659 @a2.instance_variable_set :@original_platform, 'old_platform'
661 ruby_code = @a2.to_ruby
663 same_spec = eval ruby_code
665 assert_equal 'old_platform', same_spec.original_platform
669 yaml_str = @a1.to_yaml
670 same_spec = YAML.load(yaml_str)
672 assert_equal @a1, same_spec
675 def test_to_yaml_fancy
676 @a1.platform = Gem::Platform.local
677 yaml_str = @a1.to_yaml
679 same_spec = YAML.load(yaml_str)
681 assert_equal Gem::Platform.local, same_spec.platform
683 assert_equal @a1, same_spec
686 def test_to_yaml_platform_empty_string
687 @a1.instance_variable_set :@original_platform, ''
689 assert_match %r|^platform: ruby$|, @a1.to_yaml
692 def test_to_yaml_platform_legacy
693 @a1.platform = 'powerpc-darwin7.9.0'
694 @a1.instance_variable_set :@original_platform, 'powerpc-darwin7.9.0'
696 yaml_str = @a1.to_yaml
698 same_spec = YAML.load(yaml_str)
700 assert_equal Gem::Platform.new('powerpc-darwin7'), same_spec.platform
701 assert_equal 'powerpc-darwin7.9.0', same_spec.original_platform
704 def test_to_yaml_platform_nil
705 @a1.instance_variable_set :@original_platform, nil
707 assert_match %r|^platform: ruby$|, @a1.to_yaml
711 Dir.chdir @tempdir do
716 def test_validate_authors
717 Dir.chdir @tempdir do
724 assert_equal "WARNING: no author specified\n", @ui.error, 'error'
726 @a1.authors = [Object.new]
728 e = assert_raise Gem::InvalidSpecificationException do
732 assert_equal 'authors must be Array of Strings', e.message
736 def test_validate_autorequire
737 Dir.chdir @tempdir do
738 @a1.autorequire = 'code'
744 assert_equal "WARNING: deprecated autorequire specified\n",
749 def test_validate_email
750 Dir.chdir @tempdir do
757 assert_equal "WARNING: no email specified\n", @ui.error, 'error'
761 def test_validate_empty
762 e = assert_raise Gem::InvalidSpecificationException do
763 Gem::Specification.new.validate
766 assert_equal 'missing value for attribute name', e.message
769 def test_validate_executables
770 FileUtils.mkdir_p File.join(@tempdir, 'bin')
771 File.open File.join(@tempdir, 'bin', 'exec'), 'w' do end
774 Dir.chdir @tempdir do
779 assert_equal '', @ui.output, 'output'
780 assert_equal "WARNING: bin/exec is missing #! line\n", @ui.error, 'error'
783 def test_validate_empty_require_paths
784 @a1.require_paths = []
785 e = assert_raise Gem::InvalidSpecificationException do
789 assert_equal 'specification must have at least one require_path', e.message
792 def test_validate_homepage
793 Dir.chdir @tempdir do
800 assert_equal "WARNING: no homepage specified\n", @ui.error, 'error'
804 def test_validate_has_rdoc
805 Dir.chdir @tempdir do
812 assert_equal "WARNING: RDoc will not be generated (has_rdoc == false)\n",
817 def test_validate_platform_legacy
818 Dir.chdir @tempdir do
819 @a1.platform = 'mswin32'
822 @a1.platform = 'i586-linux'
825 @a1.platform = 'powerpc-darwin'
830 def test_validate_rubyforge_project
831 Dir.chdir @tempdir do
832 @a1.rubyforge_project = ''
838 assert_equal "WARNING: no rubyforge_project specified\n",
843 def test_validate_rubygems_version
844 @a1.rubygems_version = "3"
845 e = assert_raise Gem::InvalidSpecificationException do
849 assert_equal "expected RubyGems version #{Gem::RubyGemsVersion}, was 3",
853 def test_validate_summary
854 Dir.chdir @tempdir do
861 assert_equal "WARNING: no summary specified\n", @ui.error, 'error'
866 assert_equal Gem::Version.new('1'), @a1.version