Module: RSpec::FlakeClassifier::RuntimeControls

Defined in:
lib/rspec/flake/classifier/runtime_controls.rb

Class Method Summary collapse

Class Method Details

.activate!Object



8
9
10
11
# File 'lib/rspec/flake/classifier/runtime_controls.rb', line 8

def activate!
  freeze_time if ENV["RSPEC_FLAKE_TIME_NOW"]
  block_network if ENV["RSPEC_FLAKE_BLOCK_NETWORK"] == "1"
end

.block_networkObject



28
29
30
31
32
33
34
# File 'lib/rspec/flake/classifier/runtime_controls.rb', line 28

def block_network
  block_webmock
  require "socket"
  patch_tcp_socket
rescue LoadError
  nil
end

.block_webmockObject



36
37
38
39
40
41
42
43
# File 'lib/rspec/flake/classifier/runtime_controls.rb', line 36

def block_webmock
  require "webmock"
  return unless defined?(::WebMock) && ::WebMock.respond_to?(:disable_net_connect!)

  ::WebMock.disable_net_connect!
rescue LoadError
  nil
end

.freeze_timeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/flake/classifier/runtime_controls.rb', line 13

def freeze_time
  timestamp = Integer(ENV.fetch("RSPEC_FLAKE_TIME_NOW"))
  return if Time.singleton_class.method_defined?(:flake_classifier_original_now)

  Time.singleton_class.class_eval do
    alias_method :flake_classifier_original_now, :now

    define_method(:now) do
      at(timestamp).getlocal
    end
  end
rescue ArgumentError
  nil
end

.patch_tcp_socketObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rspec/flake/classifier/runtime_controls.rb', line 45

def patch_tcp_socket
  return if TCPSocket.singleton_class.method_defined?(:flake_classifier_original_open)

  TCPSocket.singleton_class.class_eval do
    alias_method :flake_classifier_original_open, :open
    alias_method :flake_classifier_original_new, :new

    define_method(:open) do |*|
      raise IOError, "network disabled by rspec-flake-classifier"
    end

    define_method(:new) do |*|
      raise IOError, "network disabled by rspec-flake-classifier"
    end
  end
end