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

Detect Cucumber as a development environment #3145

Merged
merged 4 commits into from
Sep 22, 2023
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
1 change: 1 addition & 0 deletions Steepfile
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ target :ddtrace do
library 'digest'

repo_path 'vendor/rbs'
library 'cucumber'
library 'ffi'
library 'jruby'
library 'gem'
Expand Down
7 changes: 6 additions & 1 deletion lib/datadog/core/environment/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def webmock_enabled?

# Is this process running a test?
def test?
rspec? || minitest?
rspec? || minitest? || cucumber?
end

# Is this process running inside on a Read–eval–print loop?
Expand Down Expand Up @@ -66,6 +66,11 @@ def minitest?
::Minitest::Unit.class_variable_get(:@@installed_at_exit))
end

# Check if we are running from `bin/cucumber` or `cucumber/rake/task`.
def cucumber?
defined?(::Cucumber::Cli)
end

# If this is a Rails application, use different heuristics to detect
# whether this is a development environment or not.
def rails_development?
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/core/environment/execution.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Datadog
def self.development?: () -> bool
def self.webmock_enabled?: () -> bool

def self.cucumber?: -> bool

private
def self.test?: () -> bool
def self.repl?: () -> bool
Expand Down
47 changes: 42 additions & 5 deletions spec/datadog/core/environment/execution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
end
end

let(:repl_script) do
let!(:repl_script) do
lib = File.expand_path('lib')
<<-RUBY
# Load the working directory version of `ddtrace`
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
$LOAD_PATH.unshift("#{lib}") unless $LOAD_PATH.include?("#{lib}")
require 'datadog/core/environment/execution'

# Print actual value to STDERR, as STDOUT tends to have more noise in REPL sessions.
STDERR.print Datadog::Core::Environment::Execution.development?
STDERR.print "ACTUAL:\#{Datadog::Core::Environment::Execution.development?}"
RUBY
end

Expand All @@ -71,7 +71,7 @@
f.close

out, = Open3.capture2e('pry', '-f', '--noprompt', f.path)
expect(out).to eq('true')
expect(out).to eq('ACTUAL:true')
end
end
end
Expand Down Expand Up @@ -149,6 +149,43 @@
end
end
end

context 'for Cucumber' do
before do
unless PlatformHelpers.ci? || Gem.loaded_specs['cucumber']
skip('cucumber gem not present. In CI, this test is never skipped.')
end
end

let(:script) do
<<-'RUBY'
require 'bundler/inline'

gemfile(true) do
source 'https://rubygems.org'
gem 'cucumber', '>= 3'
end

load Gem.bin_path('cucumber', 'cucumber')
RUBY
end

it 'returns true' do
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
FileUtils.mkdir_p('features/support')

# Add our script to `env.rb`, which is always run before any feature is executed.
File.write('features/support/env.rb', repl_script)

_, err = Bundler.with_clean_env do
Open3.capture3('ruby', stdin_data: script)
end
expect(err).to include('ACTUAL:true')
end
end
end
end
end

context 'when webmock has enabled net-http adapter' do
Expand Down
4 changes: 4 additions & 0 deletions vendor/rbs/cucumber/0/cucumber.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Cucumber
module Cli
end
end