-
Notifications
You must be signed in to change notification settings - Fork 4
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
[SDTEST-477] add command line tool to compute a percentage of skippable tests for RSpec #194
Merged
anmarchenko
merged 4 commits into
main
from
anmarchenko/rake_skippable_tests_percentage
Oct 17, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a4872e7
add command line interface to calculate percentage of skipped tests f…
anmarchenko 4d1700f
write in the docs that CLI tools only support RSpec now
anmarchenko f01a939
remove CLI specs for now
anmarchenko b367801
fix failing tests because of changed source start lines
anmarchenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Command line interface | ||
|
||
This library provides experimental command line interface `ddcirb` to get the percentage of the tests | ||
that will be skipped for the current test run (only when using RSpec). | ||
|
||
## Usage | ||
|
||
This tool must be used on the same runner as your tests are running on with the same ENV variables. | ||
Run the command in the same folder where you usually run your tests. Gem datadog-ci must be installed. | ||
|
||
Available commands: | ||
|
||
- `bundle exec ddcirb skipped-tests` - outputs the percentage of skipped tests to stdout. Note that it runs your specs | ||
in dry run mode, don't forget to set RAILS_ENV=test environment variable. | ||
- `bundle exec ddcirb skipped-tests-estimate` - estimates the percentage of skipped tests and outputs to stdout without loading | ||
your test suite and running it in dry run mode. ATTENTION: this is considerably faster but could be very inaccurate. | ||
|
||
Example usage: | ||
|
||
```bash | ||
$ RAILS_ENV=test bundle exec ddcirb skipped-tests | ||
0.45 | ||
``` | ||
|
||
Available arguments: | ||
|
||
- `-f, --file` - output to a file (example: `bundle exec ddcirb skipped-tests -f out`) | ||
- `--verbose` - enable verbose output for debugging purposes (example: `bundle exec ddcirb skipped-tests --verbose`) | ||
- `--spec-path` - path to the folder with RSpec tests (default: `spec`, example: `bundle exec ddcirb skipped-tests --spec-path="myapp/spec"`) | ||
- `--rspec-opts` - additional options to pass to the RSpec when running it in dry run mode (example: `bundle exec ddcirb skipped-tests --rspec-opts="--require rails_helper"`) | ||
|
||
## Example usage in Circle CI | ||
|
||
This tool could be used to determine [Circle CI parallelism](https://support.circleci.com/hc/en-us/articles/14928385117851-How-to-dynamically-set-job-parallelism) dynamically: | ||
|
||
```yaml | ||
version: 2.1 | ||
|
||
setup: true | ||
|
||
orbs: | ||
continuation: circleci/continuation@0.2.0 | ||
|
||
jobs: | ||
determine-parallelism: | ||
docker: | ||
- image: cimg/base:edge | ||
resource_class: medium | ||
steps: | ||
- checkout | ||
- run: | ||
name: Determine parallelism | ||
command: | | ||
PARALLELISM=$(RAILS_ENV=test bundle exec ddcirb skipped-tests) | ||
echo "{\"parallelism\": $PARALLELISM}" > pipeline-parameters.json | ||
- continuation/continue: | ||
configuration_path: .circleci/continue_config.yml | ||
parameters: pipeline-parameters.json | ||
|
||
workflows: | ||
build-setup: | ||
jobs: | ||
- determine-parallelism | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "datadog/ci/cli/cli" | ||
|
||
Datadog::CI::CLI.exec(ARGV.first) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "datadog" | ||
require "datadog/ci" | ||
|
||
require_relative "command/skippable_tests_percentage" | ||
require_relative "command/skippable_tests_percentage_estimate" | ||
|
||
module Datadog | ||
module CI | ||
module CLI | ||
def self.exec(action) | ||
case action | ||
when "skipped-tests", "skippable-tests" | ||
Command::SkippableTestsPercentage.new.exec | ||
when "skipped-tests-estimate", "skippable-tests-estimate" | ||
Command::SkippableTestsPercentageEstimate.new.exec | ||
else | ||
puts("Usage: bundle exec ddcirb [command] [options]. Available commands:") | ||
puts(" skippable-tests - calculates the exact percentage of skipped tests and prints it to stdout or file") | ||
puts(" skippable-tests-estimate - estimates the percentage of skipped tests and prints it to stdout or file") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "optparse" | ||
|
||
module Datadog | ||
module CI | ||
module CLI | ||
module Command | ||
class Base | ||
def exec | ||
action = build_action | ||
result = action&.call | ||
|
||
validate!(action) | ||
output(result) | ||
end | ||
|
||
private | ||
|
||
def build_action | ||
end | ||
|
||
def options | ||
return @options if defined?(@options) | ||
|
||
ddcirb_options = {} | ||
OptionParser.new do |opts| | ||
opts.banner = "Usage: bundle exec ddcirb [command] [options]\n Available commands: skippable-tests, skippable-tests-estimate" | ||
|
||
opts.on("-f", "--file FILENAME", "Output result to file FILENAME") | ||
opts.on("--verbose", "Verbose output to stdout") | ||
|
||
command_options(opts) | ||
end.parse!(into: ddcirb_options) | ||
|
||
@options = ddcirb_options | ||
end | ||
|
||
def command_options(opts) | ||
end | ||
|
||
def validate!(action) | ||
if action.nil? || action.failed | ||
Datadog.logger.error("ddcirb failed, exiting") | ||
Kernel.exit(1) | ||
end | ||
end | ||
|
||
def output(result) | ||
if options[:file] | ||
File.write(options[:file], result) | ||
else | ||
print(result) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require_relative "base" | ||
require_relative "../../test_optimisation/skippable_percentage/calculator" | ||
|
||
module Datadog | ||
module CI | ||
module CLI | ||
module Command | ||
class SkippableTestsPercentage < Base | ||
private | ||
|
||
def build_action | ||
::Datadog::CI::TestOptimisation::SkippablePercentage::Calculator.new( | ||
rspec_cli_options: (options[:"rspec-opts"] || "").split, | ||
verbose: !options[:verbose].nil?, | ||
spec_path: options[:"spec-path"] || "spec" | ||
) | ||
end | ||
|
||
def command_options(opts) | ||
opts.on("--rspec-opts=[OPTIONS]", "Command line options to pass to RSpec") | ||
opts.on("--spec-path=[SPEC_PATH]", "Relative path to the spec directory, example: spec") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
lib/datadog/ci/cli/command/skippable_tests_percentage_estimate.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require_relative "base" | ||
require_relative "../../test_optimisation/skippable_percentage/estimator" | ||
|
||
module Datadog | ||
module CI | ||
module CLI | ||
module Command | ||
class SkippableTestsPercentageEstimate < Base | ||
private | ||
|
||
def build_action | ||
::Datadog::CI::TestOptimisation::SkippablePercentage::Estimator.new( | ||
verbose: !options[:verbose].nil?, | ||
spec_path: options[:"spec-path"] || "spec" | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we show an example of the output? Or is that somewhere else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the output just one line below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.45 is the output