From 07c797385b810f1f7e286b7086459dd907725e42 Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Tue, 5 Sep 2023 15:51:48 +0200 Subject: [PATCH] hide appraisal from running test tasks --- Rakefile | 120 +++++++++++++++++++++------------------ docs/DevelopmentGuide.md | 25 +++----- tasks/rbs.rake | 4 ++ 3 files changed, 78 insertions(+), 71 deletions(-) diff --git a/Rakefile b/Rakefile index 3c0ab4ac..570f0ebe 100644 --- a/Rakefile +++ b/Rakefile @@ -1,8 +1,9 @@ # frozen_string_literal: true +require_relative "lib/datadog/ci/version" + require "bundler/gem_tasks" require "rspec/core/rake_task" -require "datadog/ci/version" require "yard" RSpec::Core::RakeTask.new(:spec) @@ -20,11 +21,70 @@ YARD::Rake::YardocTask.new(:docs) do |t| t.options += ["--title", "datadog-ci #{Datadog::CI::VERSION} documentation"] end +TEST_METADATA = { + "main" => { + "" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" + }, + "cucumber" => { + "cucumber-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby", + "cucumber-4" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby", + "cucumber-5" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby", + "cucumber-6" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby", + "cucumber-7" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby", + "cucumber-8" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" + }, + "rspec" => { + "rspec-3" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" + }, + "minitest" => { + "minitest-5" => "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" + } +} + +namespace :test do + task all: TEST_METADATA.map { |k, _| "test:#{k}" } + + ruby_version = RUBY_VERSION[0..2] + ruby_runtime = "#{RUBY_ENGINE}-#{RUBY_ENGINE_VERSION}" + + TEST_METADATA.each do |key, spec_metadata| + spec_task = "spec:#{key}" + + desc "Run #{spec_task} tests" + task key, [:task_args] do |_, args| + spec_arguments = args.task_args + + appraisals = spec_metadata.select do |_, rubies| + (RUBY_PLATFORM == "java" && rubies.include?("✅ jruby")) || rubies.include?("✅ #{ruby_version}") + end + + appraisals.each do |appraisal_group, _| + command = if appraisal_group.empty? + "bundle exec rake #{spec_task}" + else + "bundle exec appraisal #{ruby_runtime}-#{appraisal_group} rake #{spec_task}" + end + + command += "'[#{spec_arguments}]'" if spec_arguments + + total_executors = ENV.key?("CIRCLE_NODE_TOTAL") ? ENV["CIRCLE_NODE_TOTAL"].to_i : nil + current_executor = ENV.key?("CIRCLE_NODE_INDEX") ? ENV["CIRCLE_NODE_INDEX"].to_i : nil + + if total_executors && current_executor && total_executors > 1 + @execution_count ||= 0 + @execution_count += 1 + sh(command) if @execution_count % total_executors == current_executor + else + sh(command) + end + end + end + end +end + desc "Run RSpec" -# rubocop:disable Metrics/BlockLength namespace :spec do - task all: [:main] - + desc "" # "Explicitly hiding from `rake -T`" RSpec::Core::RakeTask.new(:main) do |t, args| t.pattern = "spec/**/*_spec.rb" t.exclude_pattern = "spec/**/{contrib}/**/*_spec.rb," @@ -37,6 +97,7 @@ namespace :spec do :rspec, :minitest ].each do |contrib| + desc "" # "Explicitly hiding from `rake -T`" RSpec::Core::RakeTask.new(contrib) do |t, args| t.pattern = "spec/datadog/ci/contrib/#{contrib}/**/*_spec.rb" t.rspec_opts = args.to_a.join(" ") @@ -44,54 +105,5 @@ namespace :spec do end end -# Declare a command for execution. -# Jobs are parallelized if running in CI. -def declare(rubies_to_command) - rubies, command = rubies_to_command.first - - return unless rubies.include?("✅ #{RUBY_VERSION[0..2]}") - return if RUBY_PLATFORM == "java" && rubies.include?("❌ jruby") - - total_executors = ENV.key?("CIRCLE_NODE_TOTAL") ? ENV["CIRCLE_NODE_TOTAL"].to_i : nil - current_executor = ENV.key?("CIRCLE_NODE_INDEX") ? ENV["CIRCLE_NODE_INDEX"].to_i : nil - - ruby_runtime = "#{RUBY_ENGINE}-#{RUBY_ENGINE_VERSION}" - - command = command.sub(/^bundle exec appraisal /, "bundle exec appraisal #{ruby_runtime}-") - - if total_executors && current_executor && total_executors > 1 - @execution_count ||= 0 - @execution_count += 1 - sh(command) if @execution_count % total_executors == current_executor - else - sh(command) - end -end - -# | Cucumber | Ruby required | -# |----------|---------------| -# | 3.x | 2.2+ | -# | 4.x | 2.3+ | -# | 5.x | 2.5+ | -# | 6.x | 2.5+ | -# | 7.x | 2.5+ | -# | 8.x | 2.6+ | - desc "CI task; it runs all tests for current version of Ruby" -task :ci do - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec rake spec:main" - - # RSpec - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal rspec-3 rake spec:rspec" - - # Cucumber - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal cucumber-3 rake spec:cucumber" - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal cucumber-4 rake spec:cucumber" - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal cucumber-5 rake spec:cucumber" - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal cucumber-6 rake spec:cucumber" - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal cucumber-7 rake spec:cucumber" - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal cucumber-8 rake spec:cucumber" - - # Minitest - declare "✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ jruby" => "bundle exec appraisal minitest-5 rake spec:minitest" -end +task ci: "test:all" diff --git a/docs/DevelopmentGuide.md b/docs/DevelopmentGuide.md index 5b4d57a4..3f1e2bb3 100644 --- a/docs/DevelopmentGuide.md +++ b/docs/DevelopmentGuide.md @@ -52,8 +52,8 @@ All changes should be covered by a corresponding RSpec tests. Unit tests are pre All tests should run in CI. When adding new `spec.rb` files, you may need to add a test task to ensure your test file is run in CI. -- Ensure that there is a corresponding Rake task defined in `Rakefile` under the the `spec` namespace, whose pattern matches your test file. -- Verify the Rake task is configured to run for the appropriate Ruby runtimes in the `ci` Rake task. +- Ensure that there is a corresponding Rake task defined in `Rakefile` under the `spec` namespace, whose pattern matches your test file. +- Verify that this task is in the `TEST_METADATA` hash in `Rakefile`. ### Running tests @@ -64,27 +64,18 @@ Simplest way to run tests is to run `bundle exec rake ci`, which will run the en Run the tests for the core library with: ```bash -bundle exec rake spec:main +bundle exec rake test:main ``` #### For integrations Integrations which interact with dependencies not listed in the `datadog-ci` gemspec will need to load these dependencies to run their tests. -To do so, load the dependencies using [Appraisal](https://github.com/thoughtbot/appraisal). You can see a list of available appraisals with `bundle exec appraisal list`, or examine the `Appraisals` file. +To get a list of the spec tasks run `bundle exec rake -T 'test:'` -Then to run tests, prefix the test commain with the appraisal. +To run any of the specs above run `bundle exec rake 'test:'`. -`bundle exec appraisal rake ` - -For example: - -```bash -# Runs tests for rspec-3 -$ bundle exec appraisal ruby-3.2.0-rspec-3 rake spec:rspec -# Runs tests for minitest-5 -$ bundle exec appraisal ruby-3.2.0-minitest-5 rake spec:minitest -``` +For example: `bundle exec rake test:minitest` #### Passing arguments to tests @@ -92,7 +83,7 @@ When running tests, you may pass additional args as parameters to the Rake task. ```bash # Runs minitest integration tests with seed 1234 -$ bundle exec appraisal ruby-3.2.0-minitest-5 rake spec:minitest'[--seed,1234]' +$ bundle exec rake test:minitest'[--seed 1234]' ``` This can be useful for replicating conditions from CI or isolating certain tests. @@ -103,7 +94,7 @@ You can check test code coverage by creating a report *after* running a test sui ```bash # Run the desired test suite -$ bundle exec appraisal ruby-3.2.0-rspec-3 rake spec:rspec +$ bundle exec rake test:rspec # Generate report for the suite executed $ bundle exec rake coverage:report ``` diff --git a/tasks/rbs.rake b/tasks/rbs.rake index 35bcc27b..360ef5ad 100644 --- a/tasks/rbs.rake +++ b/tasks/rbs.rake @@ -1,6 +1,7 @@ require "fileutils" namespace :rbs do # rubocop:disable Metrics/BlockLength + desc "Checks if there are any stale RBS files" task :stale do |_task, args| glob = args.to_a.map { |g| /\.rbs$/.match?(g) ? g : "#{g}/**/*.rbs" } glob = ["sig/**/*.rbs"] if glob.empty? @@ -41,6 +42,7 @@ namespace :rbs do # rubocop:disable Metrics/BlockLength end end + desc "Checks if there are any missing RBS files" task :missing do |_task, args| glob = args.to_a.map { |g| /\.rb$/.match?(g) ? g : "#{g}/**/*.rb" } glob = ["lib/**/*.rb"] if glob.empty? @@ -80,6 +82,7 @@ namespace :rbs do # rubocop:disable Metrics/BlockLength end end + desc "Deletes stale RBS files and empty directories in sig/*" task :clean do |_task, args| glob = args.to_a.map { |g| /\.rbs$/.match?(g) ? g : "#{g}/**/*.rbs" } glob = ["sig/**/*.rbs"] if glob.empty? @@ -106,6 +109,7 @@ namespace :rbs do # rubocop:disable Metrics/BlockLength end end + desc "Creates RBS prototypes for any ruby files missing types" task :prototype do |_task, args| a = args.to_a