Skip to content

Commit

Permalink
add some comments and additional test case
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Oct 1, 2024
1 parent 9a24041 commit 98f03e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/datadog/ci/git/local_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,26 @@ 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?

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

# impossible case
# 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
Expand Down
35 changes: 35 additions & 0 deletions spec/datadog/ci/test_optimisation/coverage/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,41 @@
end
end

context "when file paths are relative" do
before do
Datadog::CI::Git::LocalRepository.remove_instance_variable(:@prefix_to_root)

allow(Datadog::CI::Git::LocalRepository).to receive(:root).and_return(
Dir.pwd.gsub("/datadog-ci-rb", "")
)
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" => "datadog-ci-rb/project/file.rb"},
{"filename" => "datadog-ci-rb/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 98f03e5

Please sign in to comment.