PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/test/functional/bio/test_command.rb

https://github.com/nmb/bioruby
Ruby | 354 lines | 299 code | 36 blank | 19 comment | 11 complexity | c127201e0c45d4e418dc100351cfb2ca MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. #
  2. # test/unit/bio/test_command.rb - Functional test for external command execution methods in Bio::Command
  3. #
  4. # Copyright:: Copyright (C) 2008
  5. # Naohisa Goto <ng@bioruby.org>
  6. # License:: The Ruby License
  7. #
  8. # $Id:$
  9. #
  10. # loading helper routine for testing bioruby
  11. require 'pathname'
  12. load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 2,
  13. 'bioruby_test_helper.rb')).cleanpath.to_s
  14. # libraries needed for the tests
  15. require 'test/unit'
  16. require 'tempfile'
  17. require 'bio/command'
  18. module Bio
  19. class FuncTestCommandCall < Test::Unit::TestCase
  20. def setup
  21. case RUBY_PLATFORM
  22. when /mswin32|bccwin32/
  23. cmd = File.expand_path(File.join(BioRubyTestDataPath, 'command', 'echoarg2.bat'))
  24. @arg = [ cmd, 'test "argument 1"', '"test" argument 2', 'arg3' ]
  25. @expected = '"""test"" argument 2"'
  26. else
  27. cmd = "/bin/echo"
  28. @arg = [ cmd, "test (echo) command" ]
  29. @expected = "test (echo) command"
  30. unless FileTest.executable?(cmd) then
  31. raise "Unsupported environment: /bin/echo not found"
  32. end
  33. end
  34. end
  35. def test_call_command
  36. ret = Bio::Command.call_command(@arg) do |io|
  37. io.close_write
  38. io.read
  39. end
  40. assert_equal(@expected, ret.to_s.strip)
  41. end
  42. def test_call_command_popen
  43. ret = Bio::Command.call_command_popen(@arg) do |io|
  44. io.close_write
  45. io.read
  46. end
  47. assert_equal(@expected, ret.to_s.strip)
  48. end
  49. def test_call_command_fork
  50. return unless Thread.respond_to?(:critical)
  51. begin
  52. ret = Bio::Command.call_command_fork(@arg) do |io|
  53. io.close_write
  54. io.read
  55. end
  56. rescue Errno::ENOENT, NotImplementedError
  57. # fork() not supported
  58. return
  59. end
  60. assert_equal(@expected, ret.to_s.strip)
  61. end
  62. def test_call_command_open3
  63. begin
  64. ret = Bio::Command.call_command_open3(@arg) do |pin, pout, perr|
  65. t = Thread.start { perr.read }
  66. begin
  67. pin.close
  68. output = pout.read
  69. ensure
  70. t.join
  71. end
  72. output
  73. end
  74. rescue NotImplementedError
  75. # fork() not supported
  76. return
  77. end
  78. assert_equal(@expected, ret.to_s.strip)
  79. end
  80. end #class FuncTestCommandCall
  81. class FuncTestCommandQuery < Test::Unit::TestCase
  82. def setup
  83. @data = [ "987", "123", "567", "456", "345" ]
  84. @sorted = @data.sort
  85. case RUBY_PLATFORM
  86. when /mswin32|bccwin32/
  87. @sort = "sort"
  88. @data = @data.join("\r\n") + "\r\n"
  89. else
  90. @sort = "/usr/bin/sort"
  91. unless FileTest.executable?(@sort) then
  92. raise "Unsupported environment: /usr/bin/sort not found"
  93. end
  94. @data = @data.join("\n") + "\n"
  95. end
  96. end
  97. def test_query_command
  98. ary = [ @sort ]
  99. assert_equal('', Bio::Command.query_command(ary).to_s.strip)
  100. str = Bio::Command.query_command(ary, @data).to_s
  101. assert_equal(@sorted, str.strip.split(/\s+/))
  102. end
  103. def test_query_command_popen
  104. ary = [ @sort ]
  105. assert_equal('', Bio::Command.query_command_popen(ary).to_s.strip)
  106. str = Bio::Command.query_command_popen(ary, @data).to_s
  107. assert_equal(@sorted, str.strip.split(/\s+/))
  108. end
  109. def test_query_command_fork
  110. return unless Thread.respond_to?(:critical)
  111. ary = [ @sort ]
  112. begin
  113. str = Bio::Command.query_command_fork(ary).to_s
  114. rescue Errno::ENOENT, NotImplementedError
  115. # fork() not supported
  116. return
  117. end
  118. assert_equal('', str.strip)
  119. str = Bio::Command.query_command_fork(ary, @data).to_s
  120. assert_equal(@sorted, str.strip.split(/\s+/))
  121. end
  122. def test_query_command_open3
  123. ary = [ @sort ]
  124. begin
  125. str, err = Bio::Command.query_command_open3(ary)
  126. rescue NotImplementedError
  127. # fork() not supported
  128. return
  129. end
  130. assert_equal('', str.to_s.strip)
  131. str, err = Bio::Command.query_command_open3(ary, @data)
  132. assert_equal(@sorted, str.to_s.strip.split(/\s+/))
  133. end
  134. end #class FuncTestCommandQuery
  135. class FuncTestCommandChdir < Test::Unit::TestCase
  136. def setup
  137. case RUBY_PLATFORM
  138. when /mswin32|bccwin32/
  139. @arg = [ 'dir', '/B', '/-P' ]
  140. else
  141. cmd = '/bin/ls'
  142. @arg = [ cmd ]
  143. unless FileTest.executable?(cmd) then
  144. raise "Unsupported environment: #{cmd} not found"
  145. end
  146. end
  147. @tempfile = Tempfile.new('chdir')
  148. @tempfile.close(false)
  149. @filename = File.basename(@tempfile.path)
  150. @dirname = File.dirname(@tempfile.path)
  151. end
  152. def teardown
  153. @tempfile.close(true)
  154. end
  155. def test_call_command_chdir
  156. str = nil
  157. Bio::Command.call_command(@arg, { :chdir => @dirname }) do |io|
  158. io.close_write
  159. str = io.read
  160. end
  161. assert(str.index(@filename))
  162. end
  163. def test_call_command_popen_chdir
  164. str = nil
  165. Bio::Command.call_command_popen(@arg,
  166. { :chdir => @dirname }) do |io|
  167. io.close_write
  168. str = io.read
  169. end
  170. assert(str.index(@filename))
  171. end
  172. def test_call_command_fork_chdir
  173. return unless Thread.respond_to?(:critical)
  174. str = nil
  175. begin
  176. Bio::Command.call_command_fork(@arg,
  177. { :chdir => @dirname }) do |io|
  178. io.close_write
  179. str = io.read
  180. end
  181. rescue Errno::ENOENT, NotImplementedError
  182. # fork() not supported
  183. return
  184. end
  185. assert(str.index(@filename))
  186. end
  187. def test_query_command_chdir
  188. str = Bio::Command.query_command(@arg, nil,
  189. { :chdir => @dirname }).to_s
  190. assert(str.index(@filename))
  191. end
  192. def test_query_command_popen_chdir
  193. str = Bio::Command.query_command_popen(@arg, nil,
  194. { :chdir => @dirname }).to_s
  195. assert(str.index(@filename))
  196. end
  197. def test_query_command_fork_chdir
  198. return unless Thread.respond_to?(:critical)
  199. begin
  200. str = Bio::Command.query_command_fork(@arg, nil,
  201. { :chdir => @dirname }).to_s
  202. rescue Errno::ENOENT, NotImplementedError
  203. # fork() not supported
  204. return
  205. end
  206. assert(str.index(@filename))
  207. end
  208. end #class FuncTestCommandChdir
  209. class FuncTestCommandBackports < Test::Unit::TestCase
  210. def setup
  211. if RUBY_VERSION < "1.8.3"
  212. @notest = true
  213. else
  214. @notest = false
  215. end
  216. end
  217. def test_remove_entry_secure
  218. return if @notest
  219. begin
  220. tempfile = Tempfile.new('removed')
  221. tempfile.close(false)
  222. assert(File.exist?(tempfile.path))
  223. Bio::Command.remove_entry_secure(tempfile.path)
  224. assert_equal(false, File.exist?(tempfile.path))
  225. ensure
  226. tempfile.close(true) if tempfile
  227. end
  228. end
  229. def test_mktmpdir_with_block
  230. return if @notest
  231. tmpdirpath = nil
  232. Bio::Command.mktmpdir('bioruby') do |path|
  233. tmpdirpath = path
  234. assert(File.directory?(path))
  235. assert_nothing_raised {
  236. File.open(File.join(path, 'test'), 'w') do |w|
  237. w.print "This is test."
  238. end
  239. }
  240. end
  241. assert_equal(false, File.directory?(tmpdirpath))
  242. end
  243. def test_mktmpdir_without_block
  244. return if @notest
  245. path = nil
  246. begin
  247. assert_nothing_raised {
  248. path = Bio::Command.mktmpdir('bioruby')
  249. }
  250. assert(File.directory?(path))
  251. assert_nothing_raised {
  252. File.open(File.join(path, 'test'), 'w') do |w|
  253. w.print "This is test."
  254. end
  255. }
  256. ensure
  257. Bio::Command.remove_entry_secure(path) if path
  258. end
  259. end
  260. end #class FuncTestCommandBackports
  261. class FuncTestCommandTmpdir < Test::Unit::TestCase
  262. def setup
  263. if RUBY_VERSION < "1.8.3"
  264. @notest = true
  265. else
  266. @notest = false
  267. end
  268. end
  269. def test_initialize
  270. return if @notest
  271. tmpdir = Bio::Command::Tmpdir.new('bioruby')
  272. assert_instance_of(Bio::Command::Tmpdir, tmpdir)
  273. assert(File.directory?(tmpdir.path))
  274. assert_nothing_raised {
  275. # creates a dummy file
  276. File.open(File.join(tmpdir.path, 'test'), 'w') do |w|
  277. w.print "This is test."
  278. end
  279. }
  280. end
  281. def test_path
  282. return if @notest
  283. tmpdir = Bio::Command::Tmpdir.new('bioruby')
  284. assert_kind_of(String, tmpdir.path)
  285. assert(File.directory?(tmpdir.path))
  286. end
  287. def test_close!
  288. return if @notest
  289. tmpdir = Bio::Command::Tmpdir.new('bioruby')
  290. path = tmpdir.path
  291. # creates a dummy file
  292. File.open(File.join(tmpdir.path, 'test'), 'w') do |w|
  293. w.print "This is test."
  294. end
  295. assert_nothing_raised { tmpdir.close! }
  296. assert_equal(false, File.directory?(path))
  297. end
  298. def test_path_after_close
  299. return if @notest
  300. tmpdir = Bio::Command::Tmpdir.new('bioruby')
  301. tmpdir.close!
  302. assert_raise(IOError) { tmpdir.path }
  303. end
  304. end #class FuncTestCommandTmpdir
  305. class FuncTestCommandNet < Test::Unit::TestCase
  306. def test_read_uri
  307. assert_nothing_raised {
  308. Bio::Command.read_uri("http://bioruby.open-bio.org/")
  309. }
  310. end
  311. def test_start_http
  312. end
  313. def test_new_http
  314. end
  315. def test_post_form
  316. end
  317. end #class FuncTestCommandNet
  318. end #module Bio