Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO-TICKET] Update README section on how to add new dependency group #4307

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 86 additions & 28 deletions docs/DevelopmentGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,6 @@ New tests should be written as RSpec tests in the `spec/datadog` folder. Test fi

All changes should be covered by a corresponding RSpec tests. Unit tests are preferred, and integration tests are accepted where appropriate (e.g. acceptance tests, verifying compatibility with datastores, etc) but should be kept to a minimum.

**Considerations for CI**

All tests should run in CI. When adding new `_spec.rb` files, you may need to add rake task to ensure your test file is run in CI.

- Ensure that there is a corresponding Rake task defined in `Rakefile` under the `spec` namespace, whose pattern matches your test file. For example

```ruby
namespace :spec do
RSpec::Core::RakeTask.new(:foo) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/foo/**/*_spec.rb"
t.rspec_opts = args.to_a.join(' ')
end
end
```

- Ensure the Rake task is configured to run for the appropriate Ruby runtimes, by adding it to our `Matrixfile`. You should find the task with `bundle exec rake -T test:foo` after adding it.

```ruby
{
'foo' => {
# With default dependencies for each Ruby runtime
'' => '✅ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby',
# or with dependency group definition `foo-on-rails`, that includes additional gems
'foo-on-rails' => '✅ 2.1 / ✅ 2.2 / ✅ 2.3 / ✅ 2.4 / ✅ 2.5 / ✅ 2.6 / ✅ 2.7 / ✅ 3.0 / ✅ 3.1 / ✅ 3.2 / ✅ 3.3 / ✅ 3.4 / ✅ jruby'
},
}
```

### Running tests

`bundle exec rake ci` will run the entire test suite with any given Ruby runtime, just as CI does. However, this is not recommended because it is going take a long time.
Expand Down Expand Up @@ -149,6 +121,92 @@ bundle exec rake dependency:lock['/app/gemfiles/ruby_3.3_stripe_*.gemfile']
bundle exec rake dependency:lock['/app/gemfiles/ruby_3.3_stripe_latest.gemfile']
```

**How to add new dependency group**
Strech marked this conversation as resolved.
Show resolved Hide resolved

> [!IMPORTANT]
> Add a new group only if the existing groups do not meet your requirements, or if adding a new dependency to an existing group is impractical.
> Remember, each new group increases maintenance and CI costs.

1. Choose the Ruby runtime and group name for your tests. When defining a new group, follow the format `scope:group`.
For example, if you want tests to run only on Ruby 3.3 for tracing, you can define this in the [`Matrixfile`](../Matrixfile).

```ruby
{
'tracing:ruby_on_rails' => {
# With default dependencies for each Ruby runtime
'' => '❌ 2.5 / ❌ 2.6 / ❌ 2.7 / ❌ 3.0 / ❌ 3.1 / ❌ 3.2 / ✅ 3.3 / ❌ 3.4 / ❌ jruby'
# or with dependency group definition `ruby-on-rails`, that includes additional gems or specific versions
'rails-1' => '❌ 2.5 / ❌ 2.6 / ❌ 2.7 / ❌ 3.0 / ❌ 3.1 / ❌ 3.2 / ✅ 3.3 / ❌ 3.4 / ❌ jruby'
# ...
'rails-edge' => '❌ 2.5 / ❌ 2.6 / ❌ 2.7 / ❌ 3.0 / ❌ 3.1 / ❌ 3.2 / ✅ 3.3 / ❌ 3.4 / ❌ jruby'
}
}
```

2. Define the required gems in the corresponding Appraisal file. For this example, we are going to use [`Appraisal/ruby-3.3.rb`](../Appraisal/ruby-3.3.rb). Let's define what `rails-edge` group needs.

```ruby
appraise 'rails-edge' do
gem 'rails', '>= 8'
end
```

3. Now let's generate that dependency Gemfile with `rake`, simply run

> [!IMPORTANT]
> Ensure you are using Ruby 3.3 as the current Ruby version (`ruby -v`) or run commands within a Docker container.

```console
$ bundle exec rake dependency:generate
...
ruby-3.3_rails-edge
```

Verify that the new dependency appears in the list.

```console
$ bundle exec rake dependency:list
Ahoy! Here is a list of gemfiles you are looking for:

========================================
...
/Users/DataDog/dd-trace-rb/gemfiles/ruby_3.3_rails_edge.gemfile
```

4. Use the following command to lock the gem versions.

```console
$ bundle exec rake dependency:lock[]
BUNDLE_GEMFILE=/Users/DataDog/dd-trace-rb/gemfiles/ruby_3.3_rails_edge.gemfile bundle lock --add-platform x86_64-linux aarch64-linux
Fetching gem metadata from https://rubygems.org/...........
Resolving dependencies...
Writing lockfile to /Users/DataDog/dd-trace-rb/gemfiles/ruby_3.3_rails_edge.gemfile.lock
```

5. The last step is to associate the newly generated group with some tests. It can be done in the [`Rakefile`](../Rakefile).

> [!IMPORTANT]
> Ensure the `scope:group` format matches the rake task name.
> In our case, we should define it as `tracing:ruby_on_rails` under `spec` namespace.

```ruby
namespace :spec do
namespace :tracing do
RSpec::Core::RakeTask.new(:ruby_on_rails) do |t, args|
t.pattern = "spec/datadog/tracing/contrib/ruby_on_rails/**/*_spec.rb"
t.rspec_opts = args.to_a.join(' ')
end
end
end
```

and now you should be able to find it by running

```console
$ bundle exec rake -T test:tracing
rake test:tracing:ruby_on_rails[task_args] # Run spec:tracing:ruby_on_rails tests
```

**Passing arguments to tests**

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