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

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