PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rubygems-0.8.11/test/test_specification.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 386 lines | 315 code | 56 blank | 15 comment | 1 complexity | be1dc56d4706845143fbf27029012887 MD5 | raw file
  1. #---
  2. # Excerpted from "Everyday Scripting in Ruby"
  3. # We make no guarantees that this code is fit for any purpose.
  4. # Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
  5. #---
  6. require 'test/unit'
  7. require 'test/gemutilities'
  8. require 'rubygems'
  9. Gem::manage_gems
  10. LEGACY_GEM_SPEC_FILE = 'test/data/legacy/keyedlist-0.4.0.ruby'
  11. LEGACY_GEM_YAML_FILE = 'test/data/legacy/keyedlist-0.4.0.yaml'
  12. class TestDefaultSpecification < Test::Unit::TestCase
  13. def test_defaults
  14. spec = Gem::Specification.new do |s|
  15. s.name = "blah"
  16. s.version = "1.3.5"
  17. end
  18. assert_equal "blah", spec.name
  19. assert_equal "1.3.5", spec.version.to_s
  20. assert_equal Gem::Platform::RUBY, spec.platform
  21. assert_equal nil, spec.summary
  22. assert_equal [], spec.files
  23. end
  24. end
  25. class TestSimpleSpecification < Test::Unit::TestCase
  26. def setup
  27. @spec = Gem::Specification.new do |s|
  28. s.version = "1.0.0"
  29. s.name = "boo"
  30. s.platform = Gem::Platform::RUBY
  31. s.date = Time.now
  32. s.summary = "Hello"
  33. s.require_paths = ["."]
  34. end
  35. @spec.mark_version
  36. end
  37. def test_empty_specification_is_invalid
  38. spec = Gem::Specification.new
  39. assert_raises(Gem::InvalidSpecificationException) {
  40. spec.validate
  41. }
  42. end
  43. def test_empty_non_nil_require_paths_is_invalid
  44. @spec.require_paths = []
  45. assert_raises(Gem::InvalidSpecificationException) {
  46. @spec.validate
  47. }
  48. end
  49. def test_spec_with_all_required_attributes_validates
  50. assert_nothing_raised {
  51. @spec.validate
  52. }
  53. end
  54. def test_rdoc_files_included
  55. @spec.files = %w(a b c d)
  56. @spec.extra_rdoc_files = %w(x y z)
  57. @spec.normalize
  58. assert @spec.files.include?('x')
  59. end
  60. def test_duplicate_files_removed
  61. @spec.files = %w(a b c d b)
  62. @spec.extra_rdoc_files = %w(x y z x)
  63. @spec.normalize
  64. assert_equal 1, @spec.files.select{|n| n=='b'}.size
  65. assert_equal 1, @spec.extra_rdoc_files.select{|n| n=='x'}.size
  66. end
  67. def test_invalid_version_in_gem_spec_makes_spec_invalid
  68. @spec.rubygems_version = "3"
  69. assert_raises(Gem::InvalidSpecificationException) { @spec.validate }
  70. end
  71. def test_singular_attributes
  72. @spec.require_path = 'mylib'
  73. @spec.test_file = 'test/suite.rb'
  74. @spec.executable = 'bin/app'
  75. assert_equal ['mylib'], @spec.require_paths
  76. assert_equal ['test/suite.rb'], @spec.test_files
  77. assert_equal ['bin/app'], @spec.executables
  78. end
  79. def test_add_bindir_to_list_of_files
  80. @spec.bindir = 'apps'
  81. @spec.executable = 'app'
  82. assert_equal ['apps/app'], @spec.files
  83. end
  84. def test_no_bindir_in_list_of_files
  85. @spec.bindir = nil
  86. @spec.executable = 'bin/app'
  87. assert_equal ['bin/app'], @spec.files
  88. end
  89. def test_deprecated_attributes
  90. @spec.test_suite_file = 'test/suite.rb'
  91. assert_equal ['test/suite.rb'], @spec.test_files
  92. # XXX: what about the warning?
  93. end
  94. def test_attribute_names
  95. expected_value = %w{
  96. rubygems_version specification_version name version date summary
  97. require_paths authors email homepage rubyforge_project description
  98. autorequire default_executable bindir has_rdoc required_ruby_version
  99. platform files test_files rdoc_options extra_rdoc_files
  100. executables extensions requirements dependencies signing_key cert_chain
  101. }.sort
  102. actual_value = Gem::Specification.attribute_names.map { |a| a.to_s }.sort
  103. assert_equal expected_value, actual_value
  104. end
  105. # TODO: test all the methods in the "convenience class methods" section of specification.rb
  106. def test_defaults
  107. # @spec is pretty plain, so we'll test some of the default values.
  108. assert_equal [], @spec.test_files
  109. assert_equal [], @spec.rdoc_options
  110. assert_equal [], @spec.extra_rdoc_files
  111. assert_equal [], @spec.executables
  112. assert_equal [], @spec.extensions
  113. assert_equal [], @spec.requirements
  114. assert_equal [], @spec.dependencies
  115. assert_equal 'bin', @spec.bindir
  116. assert_equal false, @spec.has_rdoc
  117. assert_equal false, @spec.has_rdoc?
  118. assert_equal '> 0.0.0', @spec.required_ruby_version.to_s
  119. end
  120. def test_directly_setting_dependencies_doesnt_work
  121. assert_raises(NoMethodError) do
  122. @spec.dependencies = [1,2,3]
  123. end
  124. end
  125. def test_array_attributes
  126. @spec.files = (1..10)
  127. assert_equal Array, @spec.files.class
  128. end
  129. def test_equality
  130. same_spec = @spec.dup
  131. assert_equal @spec, same_spec
  132. end
  133. def test_to_yaml_and_back
  134. yaml_str = @spec.to_yaml
  135. same_spec = YAML.load(yaml_str)
  136. assert_equal @spec, same_spec
  137. end
  138. def test_to_ruby_and_back
  139. ruby_code = @spec.to_ruby
  140. same_spec = eval ruby_code
  141. assert_equal @spec, same_spec
  142. end
  143. end # class TestSimpleSpecification
  144. class TestSpecification < RubyGemTestCase
  145. def setup
  146. super
  147. @spec = quick_gem "TestSpecification"
  148. end
  149. def test_autorequire_array
  150. name = "AutorequireArray"
  151. files = %w(a.rb b.rb)
  152. gem = quick_gem(name) do |s|
  153. s.files = files.map { |f| File.join("lib", f) }
  154. s.autorequire = files
  155. end
  156. fullname = gem.full_name
  157. write_file("gems/#{fullname}/lib/a.rb") do |io|
  158. io.puts "$LOADED_A = true"
  159. end
  160. write_file("gems/#{fullname}/lib/b.rb") do |io|
  161. io.puts "$LOADED_B = true"
  162. end
  163. old_loaded = $".dup
  164. require_gem name
  165. new_loaded = $".dup
  166. assert_equal(files, (new_loaded - old_loaded))
  167. assert(defined? $LOADED_A)
  168. assert(defined? $LOADED_B)
  169. end
  170. def test_autorequire_string
  171. name = "AutorequireString"
  172. file = "c.rb"
  173. gem = quick_gem(name) do |s|
  174. s.files = File.join("lib", file)
  175. s.autorequire = file
  176. end
  177. fullname = gem.full_name
  178. write_file("gems/#{fullname}/lib/c.rb") do |io|
  179. io.puts "$LOADED_C = true"
  180. end
  181. old_loaded = $".dup
  182. require_gem name
  183. new_loaded = $".dup
  184. assert_equal(Array(file), (new_loaded - old_loaded))
  185. assert(defined? $LOADED_C)
  186. end
  187. def test_date_equals_date
  188. @spec.date = Date.new(2003, 9, 17)
  189. assert_equal Time.local(2003, 9, 17, 0,0,0), @spec.date
  190. end
  191. def test_date_equals_string
  192. @spec.date = '2003-09-17'
  193. assert_equal Time.local(2003, 9, 17, 0,0,0), @spec.date
  194. end
  195. def test_date_equals_time
  196. @spec.date = Time.local(2003, 9, 17, 0,0,0)
  197. assert_equal Time.local(2003, 9, 17, 0,0,0), @spec.date
  198. end
  199. def test_date_equals_time_local
  200. # HACK PDT
  201. @spec.date = Time.local(2003, 9, 17, 19,50,0)
  202. assert_equal Time.local(2003, 9, 17, 0,0,0), @spec.date
  203. end
  204. def test_date_equals_time_utc
  205. # HACK PDT
  206. @spec.date = Time.local(2003, 9, 17, 19,50,0)
  207. assert_equal Time.local(2003, 9, 17, 0,0,0), @spec.date
  208. end
  209. def test_to_ruby
  210. today = Time.now.strftime("%Y-%m-%d")
  211. ruby = "Gem::Specification.new do |s|
  212. s.name = %q{TestSpecification}
  213. s.version = \"0.0.2\"
  214. s.date = %q{#{today}}
  215. s.summary = %q{this is a summary}
  216. s.email = %q{example@example.com}
  217. s.homepage = %q{http://example.com}
  218. s.description = %q{This is a test description}
  219. s.has_rdoc = true
  220. s.authors = [\"A User\"]
  221. end
  222. "
  223. assert_equal ruby, @spec.to_ruby
  224. end
  225. end
  226. class TestComplexSpecification < Test::Unit::TestCase
  227. def setup
  228. @spec = Gem::Specification.new do |s|
  229. s.name = "rfoo"
  230. s.version = "0.1"
  231. # Omit 'platform' and test for default.
  232. # Omit 'date' and test for default.
  233. s.summary = <<-EOF
  234. Ruby/Foo is an example RubyGem used for
  235. unit testing.
  236. EOF
  237. # Omit 'require_paths' and test for default.
  238. s.author = "The RubyGems Team"
  239. s.description = s.summary
  240. s.executable = 'foo1' # We'll test default_executable.
  241. s.has_rdoc = 'true' # We'll test has_rdoc?
  242. s.test_file = 'test/suite.rb' # We'll test has_unit_tests?
  243. s.extensions << 'ext/rfoo/extconf.rb'
  244. s.requirements << 'A working computer'
  245. s.add_dependency('rake', '> 0.4')
  246. s.add_dependency('jabber4r')
  247. s.add_dependency('pqa', '> 0.4', '<= 0.6')
  248. end
  249. @spec.mark_version
  250. end
  251. def test_basics
  252. @spec.normalize
  253. summary_value = "Ruby/Foo is an example RubyGem used for unit testing."
  254. assert_equal 'rfoo', @spec.name
  255. assert_equal '0.1', @spec.version.to_s
  256. assert_equal Gem::Platform::RUBY, @spec.platform
  257. assert_equal Time.today, @spec.date
  258. assert_equal summary_value, @spec.summary
  259. assert_equal summary_value, @spec.description
  260. assert_equal "The RubyGems Team", @spec.author
  261. assert_equal ['foo1'], @spec.executables
  262. assert_equal 'foo1', @spec.default_executable
  263. assert_equal true, @spec.has_rdoc?
  264. assert_equal ['test/suite.rb'], @spec.test_files
  265. assert_equal ['ext/rfoo/extconf.rb'], @spec.extensions
  266. assert_equal ['A working computer'], @spec.requirements
  267. end
  268. def test_dependencies
  269. deps = @spec.dependencies.map { |d| d.to_s }
  270. assert_equal 3, deps.size
  271. assert deps.include?('rake (> 0.4)')
  272. assert deps.include?('jabber4r (> 0.0.0)')
  273. assert deps.include?('pqa (> 0.4, <= 0.6)')
  274. end
  275. def test_equality
  276. same_spec = @spec.dup
  277. assert_equal @spec, same_spec
  278. end
  279. def xtest_to_yaml_and_back
  280. yaml_str = @spec.to_yaml
  281. same_spec = YAML.load(yaml_str)
  282. assert_equal @spec, same_spec
  283. end
  284. def test_to_ruby_and_back
  285. ruby_code = @spec.to_ruby
  286. same_spec = eval ruby_code
  287. assert_equal @spec, same_spec
  288. end
  289. end # class TestComplexSpecification
  290. class TestLegacyRubySpecification < Test::Unit::TestCase
  291. def setup
  292. @ruby_spec = File.read(LEGACY_GEM_SPEC_FILE)
  293. end
  294. def test_load_legacy
  295. s = gemspec = eval(@ruby_spec)
  296. assert_equal 'keyedlist', s.name
  297. assert_equal '0.4.0', s.version.to_s
  298. assert_equal true, s.has_rdoc?
  299. assert_equal Time.today, s.date
  300. assert s.required_ruby_version.satisfied_by?(Gem::Version.new('0.0.1'))
  301. assert_equal false, s.has_unit_tests?
  302. end
  303. def test_to_ruby_and_back
  304. gemspec1 = eval(@ruby_spec)
  305. ruby_code = gemspec1.to_ruby
  306. gemspec2 = eval(ruby_code)
  307. assert_equal gemspec1, gemspec2
  308. end
  309. end # class TestLegacyRubySpecification
  310. class TestLegacyYamlSpecification < Test::Unit::TestCase
  311. def setup
  312. @yaml_spec = File.read(LEGACY_GEM_YAML_FILE)
  313. end
  314. def test_load
  315. s = gemspec = YAML.load(@yaml_spec)
  316. assert_equal 'keyedlist', s.name
  317. assert_equal '0.4.0', s.version.to_s
  318. assert_equal true, s.has_rdoc?
  319. #assert_equal Date.today, s.date
  320. #assert s.required_ruby_version.satisfied_by?(Gem::Version.new('0.0.1'))
  321. assert_equal false, s.has_unit_tests?
  322. end
  323. end # class TestLegacyYamlSpecification
  324. class TestSpecificationClassMethods < Test::Unit::TestCase
  325. def test_load
  326. gs = Gem::Specification.load("test/data/one/one.gemspec")
  327. assert_equal "one", gs.name
  328. assert_equal "one-0.0.1", gs.full_name
  329. end
  330. end