Skip to content

Commit

Permalink
Merge pull request #188 from DataDog/anmarchenko/test_code_coverage_w…
Browse files Browse the repository at this point in the history
…ith_symlinks

[NO TICKET] test code coverage with symlinked dependency required via entrypoint
  • Loading branch information
anmarchenko authored Jun 4, 2024
2 parents 7869360 + 2c33006 commit 4278885
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 13 deletions.
25 changes: 12 additions & 13 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace :test do
"bundle exec appraisal #{ruby_runtime}-#{appraisal_group} rake #{spec_task}"
end

command += "'[#{spec_arguments}]'" if spec_arguments
command = "#{command} '[#{spec_arguments}]'" if spec_arguments

total_executors = ENV.key?("CIRCLE_NODE_TOTAL") ? ENV["CIRCLE_NODE_TOTAL"].to_i : nil
current_executor = ENV.key?("CIRCLE_NODE_INDEX") ? ENV["CIRCLE_NODE_INDEX"].to_i : nil
Expand Down Expand Up @@ -133,18 +133,17 @@ namespace :spec do
end

# Datadog CI integrations
[
:cucumber,
:rspec,
:minitest,
:minitest_shoulda_context,
:activesupport,
:ci_queue_minitest,
:ci_queue_rspec,
:knapsack_rspec,
:knapsack_rspec_go,
:selenium,
:timecop
%i[
cucumber
rspec
minitest
minitest_shoulda_context
activesupport
ci_queue_minitest
ci_queue_rspec
knapsack_rspec
knapsack_rspec_go
selenium timecop
].each do |contrib|
desc "" # "Explicitly hiding from `rake -T`"
RSpec::Core::RakeTask.new(contrib) do |t, args|
Expand Down
28 changes: 28 additions & 0 deletions spec/ddcov/calculator_with_symlinks/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require_relative "vendor/gems/operations/operations"

class Calculator
def initialize
@adder = Add.new
@subtractor = Subtract.new
@multiplier = Multiply.new
@divider = Divide.new
end

def add(a, b)
@adder.call(a, b)
end

def subtract(a, b)
@subtractor.call(a, b)
end

def multiply(a, b)
@multiplier.call(a, b)
end

def divide(a, b)
@divider.call(a, b)
end
end
7 changes: 7 additions & 0 deletions spec/ddcov/calculator_with_symlinks/operations/add.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Add
def call(a, b)
a + b
end
end
11 changes: 11 additions & 0 deletions spec/ddcov/calculator_with_symlinks/operations/divide.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require_relative "helpers/calculator_logger"

class Divide
prepend CalculatorLogger

def call(a, b)
a / b
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module CalculatorLogger
def call(a, b)
res = super

@log ||= []
@log << "operation performed"

res
end
end
7 changes: 7 additions & 0 deletions spec/ddcov/calculator_with_symlinks/operations/multiply.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Multiply
def call(a, b)
a * b
end
end
4 changes: 4 additions & 0 deletions spec/ddcov/calculator_with_symlinks/operations/operations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative "add"
require_relative "divide"
require_relative "multiply"
require_relative "subtract"
7 changes: 7 additions & 0 deletions spec/ddcov/calculator_with_symlinks/operations/subtract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Subtract
def call(a, b)
a - b
end
end
Empty file.
55 changes: 55 additions & 0 deletions spec/ddcov/ddcov_with_symlinks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "fileutils"

require "datadog_cov.#{RUBY_VERSION}_#{RUBY_PLATFORM}"

RSpec.describe Datadog::CI::ITR::Coverage::DDCov do
def absolute_path(path)
File.expand_path(File.join(__dir__, path))
end

before do
# create a symlink to the calculator_with_symlinks/operations folder in vendor/gems
FileUtils.ln_s(
absolute_path("calculator_with_symlinks/operations"),
absolute_path("calculator_with_symlinks/vendor/gems/operations"),
force: true
)

require_relative "calculator_with_symlinks/calculator"
end

after do
# delete symlink
FileUtils.rm_f(
absolute_path("calculator_with_symlinks/vendor/gems/operations")
)
end

subject { described_class.new(root: root) }

describe "code coverage collection" do
let!(:calculator) { Calculator.new }

context "when root is the calculator project dir" do
let(:root) { absolute_path("calculator_with_symlinks") }

it "collects code coverage including Calculator and operations" do
subject.start

expect(calculator.add(1, 2)).to eq(3)
expect(calculator.subtract(1, 2)).to eq(-1)

coverage = subject.stop

expect(coverage.size).to eq(3)
expect(coverage.keys).to include(
absolute_path("calculator_with_symlinks/calculator.rb"),
absolute_path("calculator_with_symlinks/operations/add.rb"),
absolute_path("calculator_with_symlinks/operations/subtract.rb")
)
end
end
end
end

0 comments on commit 4278885

Please sign in to comment.