mirror of
https://github.com/Evolix/chexpire.git
synced 2024-05-25 11:48:48 +02:00
Test: Disable Open4 calls, add mock_system_command helper
This commit is contained in:
parent
83df2a2ce3
commit
66de2e146a
1
lib/errors.rb
Normal file
1
lib/errors.rb
Normal file
|
@ -0,0 +1 @@
|
|||
class SystemCommandNotAllowedError < StandardError; end
|
|
@ -3,6 +3,7 @@ require_relative "../config/environment"
|
|||
require "rails/test_help"
|
||||
|
||||
require "minitest/mock"
|
||||
require_relative "test_mocks_helper"
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
|
||||
|
@ -12,9 +13,12 @@ class ActiveSupport::TestCase
|
|||
Warden.test_mode!
|
||||
|
||||
# Add more helper methods to be used by all tests here...
|
||||
include ActiveJob::TestHelper
|
||||
include FactoryBot::Syntax::Methods
|
||||
include TestMocksHelper
|
||||
end
|
||||
|
||||
# Capybara configuration
|
||||
Capybara.register_driver :headless_chrome do |app|
|
||||
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
|
||||
"chromeOptions" => { args: %w[headless disable-gpu] + ["window-size=1280,800"] },
|
||||
|
@ -23,3 +27,21 @@ Capybara.register_driver :headless_chrome do |app|
|
|||
end
|
||||
Capybara.save_path = Rails.root.join("tmp/capybara")
|
||||
Capybara.javascript_driver = :headless_chrome
|
||||
|
||||
# Disable Open4 real system calls
|
||||
require "open4"
|
||||
require "errors"
|
||||
module Open4
|
||||
def popen4(*)
|
||||
fail SystemCommandNotAllowedError,
|
||||
"Real Open4 calls are disabled in test env. Use mock_system_command helper instead."
|
||||
end
|
||||
alias open4 popen4
|
||||
alias pfork4 popen4
|
||||
alias popen4ext popen4
|
||||
|
||||
module_function :open4
|
||||
module_function :popen4
|
||||
module_function :pfork4
|
||||
module_function :popen4ext
|
||||
end
|
||||
|
|
29
test/test_mocks_helper.rb
Normal file
29
test/test_mocks_helper.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require "system_command"
|
||||
|
||||
module TestMocksHelper
|
||||
# rubocop:disable Metrics/MethodLength
|
||||
def mock_system_command(program, args, exit_status: 0, stdout: "", stderr: "")
|
||||
syscmd = "#{program} #{Array.wrap(args).join(' ')}"
|
||||
result = SystemCommandResult.new(syscmd, exit_status, stdout, stderr)
|
||||
|
||||
mock = Minitest::Mock.new
|
||||
mock.expect :execute, result
|
||||
|
||||
fussy = lambda { |actual_program, actual_args, _logger|
|
||||
assert_equal program, actual_program,
|
||||
"SystemCommand was not initialized with the expected program name"
|
||||
|
||||
assert_equal args, actual_args,
|
||||
"SystemCommand was not initialized with the expected arguments"
|
||||
|
||||
mock
|
||||
}
|
||||
|
||||
SystemCommand.stub :new, fussy do
|
||||
yield
|
||||
end
|
||||
|
||||
mock.verify
|
||||
end
|
||||
# rubocop:enable Metrics/MethodLength
|
||||
end
|
Loading…
Reference in a new issue