|
| 1 | +require 'spec_helper' |
| 2 | +require 'ddtrace/tasks/exec' |
| 3 | + |
| 4 | +RSpec.describe Datadog::Tasks::Exec do |
| 5 | + subject(:task) { described_class.new(args) } |
| 6 | + let(:args) { ['ruby', '-e', '"RUBY_VERSION"'] } |
| 7 | + |
| 8 | + describe '::new' do |
| 9 | + it { is_expected.to have_attributes(args: args) } |
| 10 | + end |
| 11 | + |
| 12 | + describe '#run' do |
| 13 | + subject(:run) { task.run } |
| 14 | + let(:result) { double('result') } |
| 15 | + |
| 16 | + around do |example| |
| 17 | + # Make sure RUBYOPT is returned to its original state. |
| 18 | + original_opts = ENV['RUBYOPT'] |
| 19 | + example.run |
| 20 | + ENV['RUBYOPT'] = original_opts |
| 21 | + end |
| 22 | + |
| 23 | + before do |
| 24 | + # Must stub the call out or test will prematurely terminate. |
| 25 | + expect(Kernel).to receive(:exec) |
| 26 | + .with(*args) |
| 27 | + .and_return(result) |
| 28 | + end |
| 29 | + |
| 30 | + context 'when RUBOPT is not defined' do |
| 31 | + it 'runs the task with preloads' do |
| 32 | + is_expected.to be(result) |
| 33 | + |
| 34 | + # Expect preloading to have been attached |
| 35 | + task.rubyopts.each do |opt| |
| 36 | + expect(ENV['RUBYOPT']).to include(opt) |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'when RUBYOPT is defined' do |
| 42 | + before { ENV['RUBYOPT'] = start_opts } |
| 43 | + let(:start_opts) { 'start_opts' } |
| 44 | + |
| 45 | + it 'runs the task with additional preloads' do |
| 46 | + is_expected.to be(result) |
| 47 | + |
| 48 | + # Expect original RUBYOPT to have been preserved |
| 49 | + expect(ENV['RUBYOPT']).to include(start_opts) |
| 50 | + |
| 51 | + # Expect preloading to have been attached |
| 52 | + task.rubyopts.each do |opt| |
| 53 | + expect(ENV['RUBYOPT']).to include(opt) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + describe '#rubyopts' do |
| 60 | + subject(:rubyopts) { task.rubyopts } |
| 61 | + it { is_expected.to eq(['-rddtrace/profiling/preload']) } |
| 62 | + end |
| 63 | +end |
0 commit comments