-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add skippable-tests-estimate cli command, add verbose mode
- Loading branch information
1 parent
0602e3d
commit 33a78f1
Showing
7 changed files
with
112 additions
and
9 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
54 changes: 54 additions & 0 deletions
54
lib/datadog/ci/test_optimisation/estimate_skippable_percentage.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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module CI | ||
module TestOptimisation | ||
# This class estimates the percentage of tests that are going to be skipped in the next run | ||
# without actually running the tests. This estimate is very rough: | ||
# | ||
# - it counts the number of lines that start with "it" or "scenario" in the spec files, which could be inaccurate | ||
# if you defiuse shared examples | ||
# - it only counts the number of tests that could be skipped, this does not mean that they will be actually skipped: | ||
# if in this commit you replaced all the tests in your test suite with new ones, all the tests would be run (but | ||
# this is highly unlikely) | ||
# | ||
# It is useful to determine the number of parallel jobs that are required for the CI pipeline. | ||
# | ||
# NOTE: Only RSpec is supported at the moment. | ||
class EstimateSkippablePercentage | ||
def initialize(verbose: false) | ||
@verbose = verbose | ||
end | ||
|
||
def calculate | ||
require "datadog/ci" | ||
|
||
Datadog.configure do |c| | ||
c.ci.enabled = true | ||
c.ci.itr_enabled = true | ||
c.ci.discard_traces = true | ||
c.tracing.enabled = true | ||
end | ||
|
||
spec_files = Dir["spec/**/*_spec.rb"] | ||
estimated_tests_count = spec_files.sum do |file| | ||
content = File.read(file) | ||
content.scan(/(^\s*it\s+)|(^\s*scenario\s+)/).size | ||
end | ||
puts "Estimated tests count: #{estimated_tests_count}" if @verbose | ||
|
||
# starting and finishing a test session is required to get the skippable tests response | ||
Datadog::CI.start_test_session(total_tests_count: estimated_tests_count)&.finish | ||
|
||
test_optimisation = Datadog.send(:components).test_optimisation | ||
|
||
skippable_tests_count = test_optimisation.skippable_tests.count | ||
|
||
puts "Skippable tests count: #{skippable_tests_count}" if @verbose | ||
|
||
[(skippable_tests_count.to_f / estimated_tests_count).floor(2), 0.99].min | ||
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
13 changes: 13 additions & 0 deletions
13
sig/datadog/ci/test_optimisation/estimate_skippable_percentage.rbs
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,13 @@ | ||
module Datadog | ||
module CI | ||
module TestOptimisation | ||
class EstimateSkippablePercentage | ||
@verbose: bool | ||
|
||
def initialize: (?verbose: bool) -> void | ||
|
||
def calculate: () -> Float | ||
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