Skip to content

Commit

Permalink
hide appraisal from running test tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 5, 2023
1 parent 8fa37f8 commit 07c7973
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 71 deletions.
120 changes: 66 additions & 54 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,"
Expand All @@ -37,61 +97,13 @@ 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(" ")
end
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"
25 changes: 8 additions & 17 deletions docs/DevelopmentGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -64,35 +64,26 @@ 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:<spec_name>'`.

`bundle exec appraisal <appraisal_name> rake <test_comand>`

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

When running tests, you may pass additional args as parameters to the Rake task. For example:

```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.
Expand All @@ -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
```
Expand Down
4 changes: 4 additions & 0 deletions tasks/rbs.rake
Original file line number Diff line number Diff line change
@@ -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?
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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?
Expand All @@ -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

Expand Down

0 comments on commit 07c7973

Please sign in to comment.