Skip to content

Commit

Permalink
introduce FileHelpers.absolute_path to reduce duplication in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jun 18, 2024
1 parent f6943fb commit 93d83ca
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion sig/datadog/ci/worker.rbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Datadog
module CI
class Worker < Datadog::Core::Worker
include Datadog::Core::Workers::Async::Thread::PrependedMethods
include Datadog::Core::Workers::Async::Thread
include Datadog::Core::Workers::Async::Thread::PrependedMethods

DEFAULT_SHUTDOWN_TIMEOUT: 60

Expand Down
6 changes: 2 additions & 4 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,7 @@ def test_pass_other
# expect that background thread is covered
test_span = test_spans.find { |span| span.get_tag("test.name") == "test_pass_other" }
cov_event = find_coverage_for_test(test_span)
expect(cov_event.coverage.keys).to include(
File.expand_path(File.join(__dir__, "helpers/addition_helper.rb"))
)
expect(cov_event.coverage.keys).to include(absolute_path("helpers/addition_helper.rb"))
end

context "when ITR skips tests" do
Expand Down Expand Up @@ -935,7 +933,7 @@ def test_with_background_thread
# expect that background thread is not covered
cov_event = find_coverage_for_test(first_test_span)
expect(cov_event.coverage.keys).not_to include(
File.expand_path(File.join(__dir__, "helpers/addition_helper.rb"))
absolute_path("helpers/addition_helper.rb")
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/datadog/ci/itr/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
{
File.expand_path(File.join(__dir__, "./project/file.rb")) => true
absolute_path("./project/file.rb") => true
}
end

Expand Down
4 changes: 0 additions & 4 deletions spec/ddcov/ddcov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
require_relative "calculator/code_with_❤️"

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

let(:ignored_path) { nil }
let(:threading_mode) { :multi }
subject { described_class.new(root: root, ignored_path: ignored_path, threading_mode: threading_mode) }
Expand Down
4 changes: 0 additions & 4 deletions spec/ddcov/ddcov_with_symlinks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
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(
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require_relative "support/span_helpers"
require_relative "support/platform_helpers"
require_relative "support/synchronization_helpers"
require_relative "support/file_helpers"

# shared contexts
require_relative "support/contexts/ci_mode"
Expand Down Expand Up @@ -63,6 +64,7 @@ def self.load_plugins
config.include TracerHelpers
config.include SpanHelpers
config.include SynchronizationHelpers
config.include FileHelpers

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand Down
11 changes: 11 additions & 0 deletions spec/support/file_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "datadog/ci"

module FileHelpers
# this helper returns the absolute path of a file when given
# path relative to the current __dir__
def absolute_path(path)
callstack_top = caller_locations(1, 1)[0]
caller_dir = File.dirname(callstack_top.absolute_path)
File.join(caller_dir, path)
end
end

0 comments on commit 93d83ca

Please sign in to comment.