-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from DataDog/anmarchenko/rake_skippable_tests…
…_percentage [SDTEST-477] add command line tool to compute a percentage of skippable tests for RSpec
- Loading branch information
Showing
48 changed files
with
814 additions
and
103 deletions.
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.