Skip to content

Commit

Permalink
Merge pull request #244 from DataDog/anmarchenko/code_coverage_perfor…
Browse files Browse the repository at this point in the history
…mance_optimisation

[SDTEST-878] Optimise LocalRepository.relative_to_root helper to make test impact analysis faster
  • Loading branch information
anmarchenko authored Oct 1, 2024
2 parents 7d381d5 + 777af47 commit c12ad86
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
34 changes: 31 additions & 3 deletions lib/datadog/ci/git/local_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,44 @@ def self.root
@root = git_root || Dir.pwd
end

# ATTENTION: this function is running in a hot path
# and should be optimized for performance
def self.relative_to_root(path)
return "" if path.nil?

root_path = root
return path if root_path.nil?

path = Pathname.new(File.expand_path(path))
root_path = Pathname.new(root_path)
if File.absolute_path?(path)
# prefix_index is where the root path ends in the given path
prefix_index = root_path.size

# impossible case - absolute paths are returned from code coverage tool that always checks
# that root is a prefix of the path
return "" if path.size < prefix_index

prefix_index += 1 if path[prefix_index] == File::SEPARATOR
res = path[prefix_index..]
else
# prefix_to_root is a difference between the root path and the given path
if @prefix_to_root == ""
return path
elsif @prefix_to_root
return File.join(@prefix_to_root, path)
end

pathname = Pathname.new(File.expand_path(path))
root_path = Pathname.new(root_path)

# relative_path_from is an expensive function
res = pathname.relative_path_from(root_path).to_s

unless defined?(@prefix_to_root)
@prefix_to_root = res&.gsub(path, "") if res.end_with?(path)
end
end

path.relative_path_from(root_path).to_s
res || ""
end

def self.repository_name
Expand Down
4 changes: 4 additions & 0 deletions lib/datadog/ci/transport/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def code
nil
end

def response_size
0
end

def inspect
"ErrorResponse error:#{error}"
end
Expand Down
41 changes: 40 additions & 1 deletion spec/datadog/ci/test_optimisation/coverage/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
context "when file paths are absolute" do
let(:coverage) do
{
absolute_path("./project/file.rb") => true
absolute_path("project/file.rb") => true
}
end

Expand All @@ -104,6 +104,45 @@
end
end

context "when file paths are relative" do
let(:current_folder) { File.basename(Dir.pwd) }

before do
if Datadog::CI::Git::LocalRepository.instance_variable_defined?(:@prefix_to_root)
Datadog::CI::Git::LocalRepository.remove_instance_variable(:@prefix_to_root)
end

new_root = Dir.pwd.gsub("/#{current_folder}", "")
new_root = "/" if new_root.empty?
allow(Datadog::CI::Git::LocalRepository).to receive(:root).and_return(new_root)
end

after do
Datadog::CI::Git::LocalRepository.remove_instance_variable(:@prefix_to_root)
end

let(:coverage) do
{
"project/file.rb" => true,
"project/file2.rb" => true
}
end

it "converts all file paths to relative to the git root" do
expect(msgpack_json).to eq(
{
"test_session_id" => 3,
"test_suite_id" => 2,
"span_id" => 1,
"files" => [
{"filename" => "#{current_folder}/project/file.rb"},
{"filename" => "#{current_folder}/project/file2.rb"}
]
}
)
end
end

context "coverage in lines format" do
let(:coverage) { {"file.rb" => {1 => true, 2 => true, 3 => true}} }

Expand Down

0 comments on commit c12ad86

Please sign in to comment.