You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_case.rb 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../../test_helper', __FILE__)
  18. module RedminePmTest
  19. class TestCase < ActiveSupport::TestCase
  20. attr_reader :command, :response, :status, :username, :password
  21. # Cannot use transactional fixtures here: database
  22. # will be accessed from Redmine.pm with its own connection
  23. self.use_transactional_tests = false
  24. def test_dummy
  25. end
  26. protected
  27. def assert_response(expected, msg=nil)
  28. case expected
  29. when :success
  30. assert_equal 0, status,
  31. (msg || "The command failed (exit: #{status}):\n #{command}\nOutput was:\n#{formatted_response}")
  32. when :failure
  33. assert_not_equal 0, status,
  34. (msg || "The command succeed (exit: #{status}):\n #{command}\nOutput was:\n#{formatted_response}")
  35. else
  36. assert_equal expected, status, msg
  37. end
  38. end
  39. def assert_success(*args)
  40. execute *args
  41. assert_response :success
  42. end
  43. def assert_failure(*args)
  44. execute *args
  45. assert_response :failure
  46. end
  47. def with_credentials(username, password)
  48. old_username, old_password = @username, @password
  49. @username, @password = username, password
  50. yield if block_given?
  51. ensure
  52. @username, @password = old_username, old_password
  53. end
  54. def execute(*args)
  55. @command = args.join(' ')
  56. @status = nil
  57. IO.popen("#{command} 2>&1") do |io|
  58. io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
  59. @response = io.read
  60. end
  61. @status = $?.exitstatus
  62. end
  63. def formatted_response
  64. "#{'='*40}\n#{response}#{'='*40}"
  65. end
  66. def random_filename
  67. Redmine::Utils.random_hex(16)
  68. end
  69. end
  70. end