/usr/lib/ruby/vendor_ruby/rspec/mocks/mutate_const.rb is in ruby-rspec-mocks 3.4.0c3e0m1s1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | RSpec::Support.require_rspec_support 'recursive_const_methods'
module RSpec
module Mocks
# Provides information about constants that may (or may not)
# have been mutated by rspec-mocks.
class Constant
extend Support::RecursiveConstMethods
# @api private
def initialize(name)
@name = name
@previously_defined = false
@stubbed = false
@hidden = false
@valid_name = true
yield self if block_given?
end
# @return [String] The fully qualified name of the constant.
attr_reader :name
# @return [Object, nil] The original value (e.g. before it
# was mutated by rspec-mocks) of the constant, or
# nil if the constant was not previously defined.
attr_accessor :original_value
# @private
attr_writer :previously_defined, :stubbed, :hidden, :valid_name
# @return [Boolean] Whether or not the constant was defined
# before the current example.
def previously_defined?
@previously_defined
end
# @return [Boolean] Whether or not rspec-mocks has mutated
# (stubbed or hidden) this constant.
def mutated?
@stubbed || @hidden
end
# @return [Boolean] Whether or not rspec-mocks has stubbed
# this constant.
def stubbed?
@stubbed
end
# @return [Boolean] Whether or not rspec-mocks has hidden
# this constant.
def hidden?
@hidden
end
# @return [Boolean] Whether or not the provided constant name
# is a valid Ruby constant name.
def valid_name?
@valid_name
end
# The default `to_s` isn't very useful, so a custom version is provided.
def to_s
"#<#{self.class.name} #{name}>"
end
alias inspect to_s
# @private
def self.unmutated(name)
previously_defined = recursive_const_defined?(name)
rescue NameError
new(name) do |c|
c.valid_name = false
end
else
new(name) do |const|
const.previously_defined = previously_defined
const.original_value = recursive_const_get(name) if previously_defined
end
end
# Queries rspec-mocks to find out information about the named constant.
#
# @param [String] name the name of the constant
# @return [Constant] an object contaning information about the named
# constant.
def self.original(name)
mutator = ::RSpec::Mocks.space.constant_mutator_for(name)
mutator ? mutator.to_constant : unmutated(name)
end
end
# Provides a means to stub constants.
class ConstantMutator
extend Support::RecursiveConstMethods
# Stubs a constant.
#
# @param (see ExampleMethods#stub_const)
# @option (see ExampleMethods#stub_const)
# @return (see ExampleMethods#stub_const)
#
# @see ExampleMethods#stub_const
# @note It's recommended that you use `stub_const` in your
# examples. This is an alternate public API that is provided
# so you can stub constants in other contexts (e.g. helper
# classes).
def self.stub(constant_name, value, options={})
mutator = if recursive_const_defined?(constant_name, &raise_on_invalid_const)
DefinedConstantReplacer
else
UndefinedConstantSetter
end
mutate(mutator.new(constant_name, value, options[:transfer_nested_constants]))
value
end
# Hides a constant.
#
# @param (see ExampleMethods#hide_const)
#
# @see ExampleMethods#hide_const
# @note It's recommended that you use `hide_const` in your
# examples. This is an alternate public API that is provided
# so you can hide constants in other contexts (e.g. helper
# classes).
def self.hide(constant_name)
mutate(ConstantHider.new(constant_name, nil, {}))
nil
end
# Contains common functionality used by all of the constant mutators.
#
# @private
class BaseMutator
include Support::RecursiveConstMethods
attr_reader :original_value, :full_constant_name
def initialize(full_constant_name, mutated_value, transfer_nested_constants)
@full_constant_name = normalize_const_name(full_constant_name)
@mutated_value = mutated_value
@transfer_nested_constants = transfer_nested_constants
@context_parts = @full_constant_name.split('::')
@const_name = @context_parts.pop
@reset_performed = false
end
def to_constant
const = Constant.new(full_constant_name)
const.original_value = original_value
const
end
def idempotently_reset
reset unless @reset_performed
@reset_performed = true
end
end
# Hides a defined constant for the duration of an example.
#
# @private
class ConstantHider < BaseMutator
def mutate
return unless (@defined = recursive_const_defined?(full_constant_name))
@context = recursive_const_get(@context_parts.join('::'))
@original_value = get_const_defined_on(@context, @const_name)
@context.__send__(:remove_const, @const_name)
end
def to_constant
return Constant.unmutated(full_constant_name) unless @defined
const = super
const.hidden = true
const.previously_defined = true
const
end
def reset
return unless @defined
@context.const_set(@const_name, @original_value)
end
end
# Replaces a defined constant for the duration of an example.
#
# @private
class DefinedConstantReplacer < BaseMutator
def initialize(*args)
super
@constants_to_transfer = []
end
def mutate
@context = recursive_const_get(@context_parts.join('::'))
@original_value = get_const_defined_on(@context, @const_name)
@constants_to_transfer = verify_constants_to_transfer!
@context.__send__(:remove_const, @const_name)
@context.const_set(@const_name, @mutated_value)
transfer_nested_constants
end
def to_constant
const = super
const.stubbed = true
const.previously_defined = true
const
end
def reset
@constants_to_transfer.each do |const|
@mutated_value.__send__(:remove_const, const)
end
@context.__send__(:remove_const, @const_name)
@context.const_set(@const_name, @original_value)
end
def transfer_nested_constants
@constants_to_transfer.each do |const|
@mutated_value.const_set(const, get_const_defined_on(original_value, const))
end
end
def verify_constants_to_transfer!
return [] unless should_transfer_nested_constants?
{ @original_value => "the original value", @mutated_value => "the stubbed value" }.each do |value, description|
next if value.respond_to?(:constants)
raise ArgumentError,
"Cannot transfer nested constants for #{@full_constant_name} " \
"since #{description} is not a class or module and only classes " \
"and modules support nested constants."
end
if Array === @transfer_nested_constants
@transfer_nested_constants = @transfer_nested_constants.map(&:to_s) if RUBY_VERSION == '1.8.7'
undefined_constants = @transfer_nested_constants - constants_defined_on(@original_value)
if undefined_constants.any?
available_constants = constants_defined_on(@original_value) - @transfer_nested_constants
raise ArgumentError,
"Cannot transfer nested constant(s) #{undefined_constants.join(' and ')} " \
"for #{@full_constant_name} since they are not defined. Did you mean " \
"#{available_constants.join(' or ')}?"
end
@transfer_nested_constants
else
constants_defined_on(@original_value)
end
end
def should_transfer_nested_constants?
return true if @transfer_nested_constants
return false unless RSpec::Mocks.configuration.transfer_nested_constants?
@original_value.respond_to?(:constants) && @mutated_value.respond_to?(:constants)
end
end
# Sets an undefined constant for the duration of an example.
#
# @private
class UndefinedConstantSetter < BaseMutator
def mutate
@parent = @context_parts.inject(Object) do |klass, name|
if const_defined_on?(klass, name)
get_const_defined_on(klass, name)
else
ConstantMutator.stub(name_for(klass, name), Module.new)
end
end
@parent.const_set(@const_name, @mutated_value)
end
def to_constant
const = super
const.stubbed = true
const.previously_defined = false
const
end
def reset
@parent.__send__(:remove_const, @const_name)
end
private
def name_for(parent, name)
root = if parent == Object
''
else
parent.name
end
root + '::' + name
end
end
# Uses the mutator to mutate (stub or hide) a constant. Ensures that
# the mutator is correctly registered so it can be backed out at the end
# of the test.
#
# @private
def self.mutate(mutator)
::RSpec::Mocks.space.register_constant_mutator(mutator)
mutator.mutate
end
# Used internally by the constant stubbing to raise a helpful
# error when a constant like "A::B::C" is stubbed and A::B is
# not a module (and thus, it's impossible to define "A::B::C"
# since only modules can have nested constants).
#
# @api private
def self.raise_on_invalid_const
lambda do |const_name, failed_name|
raise "Cannot stub constant #{failed_name} on #{const_name} " \
"since #{const_name} is not a module."
end
end
end
end
end
|