Skip to content

Commit

Permalink
coverage using TracePoint class
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Feb 27, 2024
1 parent ee647e5 commit 011f09e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
28 changes: 22 additions & 6 deletions lib/datadog/ci/itr/coverage/collector.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
# frozen_string_literal: true

require "coverage"
require "set"

require_relative "../../utils/git"
require_relative "../../../../ddcov/ddcov"

module Datadog
module CI
module Itr
module Coverage
class Collector
def initialize(mode: :files, enabled: true)
# TODO: make this thread local
# modes available: :files, :lines
@mode = mode
@enabled = enabled
@regex = /\A#{Regexp.escape(Utils::Git.root + File::SEPARATOR)}/i.freeze

@coverage = {}

if @enabled
@ddcov = DDCov.new(root: Utils::Git.root, mode: mode)
@tracepoint = TracePoint.new(:line) do |tp|
next unless tp.path =~ @regex

if @mode == :files
@coverage[tp.path] = true
elsif @mode == :lines
@coverage[tp.path] ||= Set.new
@coverage[tp.path] << tp.lineno
end
end
end
end

Expand All @@ -30,11 +40,17 @@ def setup
end

def start
@ddcov.start if @enabled
return unless @enabled

@tracepoint.enable
end

def stop
@ddcov.stop if @enabled
return unless @enabled
@tracepoint.disable
res = @coverage
@coverage = {}
res
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/datadog/ci/test_visibility/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,15 @@ def on_test_end(test)

coverage.each do |filename, lines|
f.write("#{filename}\n")
sorted_lines = lines.uniq.sort
sorted_lines = lines.to_a.sort
f.write("#{sorted_lines}\n")
end
f.write("---------------------------------------------------\n")
end
end
# p "FILTERED"
# p coverage.count
# p coverage
p "FILTERED"
p coverage.count
p coverage
# move this to the code coverage transport
# files_covered = coverage.keys.map { |filename| Utils::Git.relative_to_root(filename) }
test.set_tag("_test.coverage", coverage)
Expand Down

0 comments on commit 011f09e

Please sign in to comment.