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

[SDTEST-878] Optimise LocalRepository.relative_to_root helper to make test impact analysis faster #244

Merged
Merged
Show file tree
Hide file tree
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
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change, I noticed that library started to fail if certain backend API is not available because for ErrorResponse there is no http_response

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
Loading