From 1330bc9edaf7e35839097ace6b6e2ba4f7e7a0c6 Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Fri, 13 Sep 2024 11:22:44 -0700 Subject: [PATCH] Add filter and list tests options --- anyt.gemspec | 2 +- lib/anyt/cli.rb | 15 +++++++++++++++ lib/anyt/config.rb | 10 ++++++++++ lib/anyt/dummy/application.rb | 6 ++++-- lib/anyt/ext/minitest.rb | 3 +++ lib/anyt/tests.rb | 20 ++++++++++++++++++++ 6 files changed, 53 insertions(+), 3 deletions(-) diff --git a/anyt.gemspec b/anyt.gemspec index 768cf06..84a259f 100644 --- a/anyt.gemspec +++ b/anyt.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |spec| spec.add_dependency "minitest", "~> 5.10" spec.add_dependency "minitest-reporters", "~> 1.1.0" spec.add_dependency "anycable-rails", "> 1.0.99", "< 2.0" - spec.add_dependency "redis", "~> 4.0" + spec.add_dependency "redis", ">= 4.0" spec.add_dependency "childprocess", "~> 3.0" spec.add_development_dependency "bundler", "~> 2" diff --git a/lib/anyt/cli.rb b/lib/anyt/cli.rb index 8e0c49f..86a1718 100644 --- a/lib/anyt/cli.rb +++ b/lib/anyt/cli.rb @@ -25,6 +25,12 @@ class << self def run(args = ARGV) parse_options!(args) + if Anyt.config.list_tests + Tests.load_tests + Tests.list + return 0 + end + ActionCable.server.config.logger = Rails.logger = AnyCable.logger result = 1 @@ -128,6 +134,11 @@ def parse_options!(args) configure_rails_command! end + cli.on("-l", "--list", TrueClass, "List test scenarios") do + Anyt.config.list_tests = true + @skip_rpc = true + end + cli.on("--self-check", "Run tests again Action Cable itself") do @skip_rpc = true @@ -142,6 +153,10 @@ def parse_options!(args) Anyt.config.except_tests = except_tests end + cli.on("-e filter", "Run only tests matching the descripton") do |filter_tests| + Anyt.config.filter_tests = filter_tests + end + cli.on("--wait-command=TIMEOUT", Integer, "Number of seconds to wait for WS server initialization") do |timeout| Anyt.config.wait_command = timeout diff --git a/lib/anyt/config.rb b/lib/anyt/config.rb index b7d094a..961c0d6 100644 --- a/lib/anyt/config.rb +++ b/lib/anyt/config.rb @@ -2,12 +2,16 @@ require "anyway" +Anyway::Rails.disable_postponed_load_warning = true + module Anyt # Anyt configuration class Config < Anyway::Config attr_config :command, :only_tests, :except_tests, + :filter_tests, + :list_tests, :tests_relative_path, remote_control_port: 8919, use_action_cable: false, @@ -37,5 +41,11 @@ def tests_filter (except_rxp.nil? || !except_rxp.match?(path)) end end + + def example_filter + return unless filter_tests + + /#{filter_tests}/i + end end end diff --git a/lib/anyt/dummy/application.rb b/lib/anyt/dummy/application.rb index b31f5a3..746b4a8 100644 --- a/lib/anyt/dummy/application.rb +++ b/lib/anyt/dummy/application.rb @@ -55,8 +55,10 @@ def subscribed end def echo(data) - if ECHO_DELAY > 0 - sleep ECHO_DELAY + puts "ECHO: #{data.inspect}" if data["verbose"] + delay = data.fetch("delay", ECHO_DELAY).to_f + if delay > 0 + sleep delay end transmit data end diff --git a/lib/anyt/ext/minitest.rb b/lib/anyt/ext/minitest.rb index 23ae42b..eb4b1d8 100644 --- a/lib/anyt/ext/minitest.rb +++ b/lib/anyt/ext/minitest.rb @@ -96,6 +96,9 @@ module Minitest::Spec::DSL # Simplified version of `it` which doesn't care # about unique method names def scenario(desc, &block) + full_desc = "#{name.gsub(/\n/, " ").strip} #{desc.gsub(/\n/, " ").strip}" + return if Anyt.config.example_filter && !Anyt.config.example_filter.match?(full_desc) + block ||= proc { skip "(no tests defined)" } define_method "test_ #{desc}", &block diff --git a/lib/anyt/tests.rb b/lib/anyt/tests.rb index 235f1ce..ebf5021 100644 --- a/lib/anyt/tests.rb +++ b/lib/anyt/tests.rb @@ -17,9 +17,29 @@ def run Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new AnyCable.logger.debug "Run tests against: #{Anyt.config.target_url}" + Minitest.run end + def list + printer = Object.new + printer.extend include ANSI::Code + + Minitest::Runnable.runnables.each do |runnable| + def runnable.test_order + :sorted + end + next unless runnable.runnable_methods.any? + + $stdout.puts(runnable.name) + + runnable.runnable_methods.each do |method| + test_name = method.gsub(/^test_/, "").strip + $stdout.puts(printer.magenta { " #{test_name}" }) + end + end + end + # Load tests code (filtered if present) # # NOTE: We should run this before launching RPC server