class Webspicy::Configuration
Constants
- LISTENER_KINDS
Attributes
Public Class Methods
# File lib/webspicy/configuration.rb, line 58 def self.dress(arg, &bl) case arg when Configuration arg when /^https?:\/\// Configuration.new{|c| c.scope_factory = SingleUrl.new(arg) } when ->(f){ Path(f).exists? } arg = Path(arg) if arg.file? && arg.ext == ".rb" Configuration.with_default_folder(arg.parent) do c = Kernel.instance_eval arg.read, arg.to_s yield(c) if block_given? c end elsif arg.file? && arg.ext == '.yml' folder = arg.backfind("[config.rb]") if folder && folder.exists? dress(folder/"config.rb"){|c| c.scope_factory = SingleYmlFile.new(arg) } else Configuration.new{|c| c.scope_factory = SingleYmlFile.new(arg) } end elsif arg.directory? and (arg/'config.rb').file? dress(arg/'config.rb', &bl) else raise ArgumentError, "Missing config.rb file" end else raise ArgumentError, "Unable to turn `#{arg}` to a configuration" end end
# File lib/webspicy/configuration.rb, line 95 def self.inherits(*args, &bl) dress(*args, &bl) end
# File lib/webspicy/configuration.rb, line 18 def initialize(folder = Configuration.default_folder || Path.pwd, parent = nil) @folder = folder.expand_path @parent = parent @children = [] @preconditions = [] @postconditions = [] @errconditions = [] @insecure = default_insecure @listeners = Hash.new{|h,k| h[k] = [] } @rspec_options = default_rspec_options @failfast = default_failfast @run_examples = default_run_examples @run_counterexamples = default_run_counterexamples @run_generated_counterexamples = default_run_generated_counterexamples @file_filter = default_file_filter @service_filter = default_service_filter @test_case_filter = default_test_case_filter @colors = { :section => :magenta, :highlight => :cyan, :error => :red, :success => :green } @colorize = true @factory = Webspicy::Web @scope_factory = ->(config){ Scope.new(config) } @client = Web::HttpClient @reporter = default_reporter Path.require_tree(@folder/'support') if (@folder/'support').exists? @world = Support::World.new(folder/'world', self) yield(self) if block_given? end
# File lib/webspicy/configuration.rb, line 9 def with_default_folder(f) old = default_folder @default_folder = f yield.tap{ @default_folder = old } end
Public Instance Methods
Installs a listener that will be called after all tests
The `listener` must respond to `call`.
# File lib/webspicy/configuration.rb, line 370 def after_all(l = nil, &listener) register_listener(:after_all, l || listener) end
Installs a listener that will be called after each web service invocation.
The `listener` must respond to `call`.
# File lib/webspicy/configuration.rb, line 377 def after_each(l = nil, &listener) register_listener(:after_each, l || listener) end
Installs a listener that will be called around each web service invocation.
The `listener` must respond to `call`.
# File lib/webspicy/configuration.rb, line 384 def around_each(l = nil, &listener) register_listener(:around_each, l || listener) end
Installs a listener that will be called before all tests
The `listener` must respond to `call`.
# File lib/webspicy/configuration.rb, line 356 def before_all(l = nil, &listener) register_listener(:before_all, l || listener) end
Installs a listener that will be called before each web service invocation.
The `listener` must respond to `call`.
# File lib/webspicy/configuration.rb, line 363 def before_each(l = nil, &listener) register_listener(:before_each, l || listener) end
Installs a client class to use to invoke web services for real.
This configuration allows defining a subclass of Client to be used for actually invoking web services. Options are:
Note that this configuration variable expected a client class, not an instance
# File lib/webspicy/configuration.rb, line 331 def client=(client) @client = client end
Returns the Data system to use for parsing schemas
The data system associated with a configuration is build when the configuration folder contains a `schema.fio` finitio file. When no such file can be found, the parent config is checked (if any). When no `schema.fio` file can be found, the method ends up returning the default Finition system.
# File lib/webspicy/configuration.rb, line 461 def data_system schema = self.folder/"schema.fio" if schema.file? Finitio.system(schema) elsif not(self.parent.nil?) self.parent.data_system else Webspicy::DEFAULT_SYSTEM end end
Returns the default value for failfast
The following environment variables <-> option are supported:
-
FAILFAST=yes <-> true
# File lib/webspicy/configuration.rb, line 411 def default_failfast ENV['FAILFAST'] == 'yes' || ENV['FAILFAST'] == "1" end
Returns the default value to use for insecure.
The following environment variables <-> option are supported:
-
INSECURE=yes <-> true
# File lib/webspicy/configuration.rb, line 422 def default_insecure ENV['INSECURE'] == 'yes' || ENV['INSECURE'] == "1" end
Returns the default reporter to use.
# File lib/webspicy/configuration.rb, line 445 def default_reporter @reporter = Tester::Reporter::Composite.new @reporter << Tester::Reporter::Documentation.new @reporter << Tester::Reporter::Exceptions.new @reporter << Tester::Reporter::Summary.new @reporter << Tester::Reporter::SuccessOrNot.new end
Returns the default test case filter.
By default no filter is set unless a TAG environment variable is set. In that case, a test case filter is returned that filters the test cases whose tags map the specified value.
# File lib/webspicy/configuration.rb, line 311 def default_test_case_filter return nil unless tags = ENV['TAG'] no, yes = tags.split(/\s*,\s*/).partition{|t| t =~ /^!/ } no, yes = no.map{|t| t[1..-1 ]}, yes ->(tc){ (yes.empty? || !(yes & tc.tags).empty?) \ && \ (no.empty? || (no & tc.tags).empty?) } end
Duplicates this configuration and yields the block with the new one, if a block is given.
The cloned configuration has all same values as the original but shares nothing with it. Therefore, affecting the new one has no effect on the original.
# File lib/webspicy/configuration.rb, line 485 def dup(&bl) super.tap do |d| d.children = [] d.preconditions = self.preconditions.dup d.postconditions = self.postconditions.dup d.errconditions = self.errconditions.dup d.rspec_options = self.rspec_options.dup d.listeners = LISTENER_KINDS.inject({}){|ls,kind| ls.merge(kind => self.listeners(kind).dup) } yield d if block_given? end end
# File lib/webspicy/configuration.rb, line 102 def each_scope(&bl) return enum_for(:each_scope) unless block_given? if has_children? children.each do |config| config.each_scope(&bl) end else yield(factor_scope) end end
Registers an errcondition matcher
# File lib/webspicy/configuration.rb, line 156 def errcondition(clazz) errconditions << clazz end
# File lib/webspicy/configuration.rb, line 116 def factor_scope @scope_factory.call(self) end
Installs a file filter.
A file filter can be added to restrict the scope attention only to the files that match the filter installed. Supported values are:
-
Proc: each file (a Path instance) is passed in turn. Only files for which a truthy value is returned will be considered by the scope.
-
Regexp: the path of each file is matched against the regexp. Only files that match are considered by the scope.
-
: any instance responding to `===` can be used as a matcher, following¶ ↑
Ruby conventions. The match is done on a Path instance.
# File lib/webspicy/configuration.rb, line 249 def file_filter=(file_filter) @file_filter = file_filter end
Returns the data generator to use, for generating random data when needed.
# File lib/webspicy/configuration.rb, line 474 def generator @generator end
Returns whether this configuration has children configurations or not
# File lib/webspicy/configuration.rb, line 163 def has_children? !children.empty? end
Installs a host (resolver).
The host resolver is responsible from transforming URLs found in .yml test files to an absolute URL invoked by the client. Supported values are:
-
String: taken as a prefix for all relative URLs. Using this option lets specify all webservices through relative URLs and having the host itself as global configuration variable.
-
Proc: all URLs are passed to the proc, relative and absolute ones. The result of the proc is used as URL to use in practice.
When no host provider is provided, all URLs are expected to be absolute URLs, otherwise an error will be thrown at runtime.
# File lib/webspicy/configuration.rb, line 232 def host=(host) @host = host end
Installs a listener that will be called right after all precondition instrumentations.
# File lib/webspicy/configuration.rb, line 390 def instrument(&instrumentor) register_listener(:instrument, instrumentor) end
Returns the listeners of a specific kind.
Recognized kinds are `before_each`, `after_each`, `before_all`, `after_all` and `instrument`.
# File lib/webspicy/configuration.rb, line 347 def listeners(kind) @listeners[kind] || [] end
Registers a postcondition matcher
# File lib/webspicy/configuration.rb, line 149 def postcondition(clazz) postconditions << clazz end
Registers a precondition matcher
# File lib/webspicy/configuration.rb, line 142 def precondition(clazz) preconditions << clazz end
Sets whether counter examples have to be ran or not.
# File lib/webspicy/configuration.rb, line 185 def run_counterexamples=(run_counterexamples) @run_counterexamples = run_counterexamples end
Whether counter examples must be ran or not.
# File lib/webspicy/configuration.rb, line 191 def run_counterexamples? @run_counterexamples end
Sets whether examples have to be ran or not.
# File lib/webspicy/configuration.rb, line 168 def run_examples=(run_examples) @run_examples = run_examples end
Whether counter examples must be ran or not.
# File lib/webspicy/configuration.rb, line 174 def run_examples? @run_examples end
Sets whether generated counter examples have to be ran or not.
# File lib/webspicy/configuration.rb, line 202 def run_generated_counterexamples=(run_generated_counterexamples) @run_generated_counterexamples = run_generated_counterexamples end
Whether generated counter examples must be ran or not.
# File lib/webspicy/configuration.rb, line 208 def run_generated_counterexamples? @run_generated_counterexamples end
Installs a service filter.
A service filter can be added to restrict the scope attention only to the services that match the filter installed. Supported values are:
-
Proc: each service is passed in turn. Only services for which a truthy value is returned will be considered by the scope.
-
: any instance responding to `===` can be used as a matcher, following¶ ↑
Ruby conventions. The match is done on a Service instance.
# File lib/webspicy/configuration.rb, line 277 def service_filter=(service_filter) @service_filter = service_filter end
Installs a test case filter.
A test case filter can be added to restrict the scope attention only to the test cases that match the service installed. Supported values are:
-
Proc: each test case is passed in turn. Only test cases for which a
truthy value is returned will be considered by the scope.
-
: any instance responding to `===` can be used as a matcher, following¶ ↑
Ruby conventions. The match is done on a Service instance.
# File lib/webspicy/configuration.rb, line 301 def test_case_filter=(tag_filter) @test_case_filter = test_case_filter end
Protected Instance Methods
Allows setting the options passed at RSpec, which is used by both the runner and checker classes.
`options` is supposed to be valid RSpec options, to be passed at `RSpec::Core::Runner.run`
# File lib/webspicy/configuration.rb, line 399 def rspec_options=(options) @rspec_options = options end
Private Instance Methods
Returns the default file filter to use.
By default no file filter is set, unless a RESOURCE environment variable is set. In that case, a file filter is set that matches the file name to the variable value, through a regular expression.
# File lib/webspicy/configuration.rb, line 259 def default_file_filter return nil unless res = ENV['RESOURCE'] negated = res =~ /^!/ rx = Regexp.compile(negated ? "#{res[1..-1]}" : res) negated ? ->(f){ !(f.to_s =~ rx) } : rx end
Returns the default rspec options.
By default rspec colors are enabled and the format set to 'documentation'. The following environment variables <-> rspec options are supported:
-
FAILFAST <-> –fail-fast
# File lib/webspicy/configuration.rb, line 434 def default_rspec_options dest_folder = (self.folder/'rspec.xml').to_s options = %w{--color --format=documentation --format RspecJunitFormatter --out} << dest_folder if ENV['FAILFAST'] options << (ENV['FAILFAST'] == 'no' ? "--no-fail-fast" : "--fail-fast=#{ENV['FAILFAST']}") end options end
Returns the defaut value for run_counterexamples
# File lib/webspicy/configuration.rb, line 196 def default_run_counterexamples ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'no' && ENV['ROBUST'] != 'generated') end
Returns the defaut value for run_examples
# File lib/webspicy/configuration.rb, line 179 def default_run_examples ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'only' && ENV['ROBUST'] != 'generated') end
Returns the defaut value for run_generated_counterexamples
# File lib/webspicy/configuration.rb, line 213 def default_run_generated_counterexamples ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'no') end
Returns the default service filter.
By default no filter is set unless a METHOD environment variable is set. In that case, a service filter is returned that filters the services whose HTTP
method match the variable value.
# File lib/webspicy/configuration.rb, line 287 def default_service_filter ENV['METHOD'] ? ->(s){ s.method.to_s.downcase == ENV['METHOD'].downcase } : nil end
Registers a listener under a given kind.
# File lib/webspicy/configuration.rb, line 337 def register_listener(kind, listener) raise "Must respond to call" unless listener.respond_to?(:call) @listeners[kind] << listener end